~ntt-pf-lab/nova/monkey_patch_notification

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/doc/words/examples/minchat.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
# Copyright (c) 2001-2009 Twisted Matrix Laboratories.
 
4
# See LICENSE for details.
 
5
 
 
6
 
 
7
"""
 
8
A very simple twisted.im-based logbot.
 
9
"""
 
10
 
 
11
from twisted.im import basechat, baseaccount
 
12
 
 
13
# A list of account objects. We might as well create them at runtime, this is
 
14
# supposed to be a Minimalist Implementation, after all.
 
15
from twisted.im import ircsupport 
 
16
accounts = [
 
17
    ircsupport.IRCAccount("IRC", 1,
 
18
        "Tooty",            # nickname
 
19
        "",                 # passwd
 
20
        "irc.freenode.net", # irc server
 
21
        6667,               # port
 
22
        "#twisted",         # comma-seperated list of channels
 
23
    )
 
24
]
 
25
 
 
26
 
 
27
class AccountManager (baseaccount.AccountManager):
 
28
    """This class is a minimal implementation of the Acccount Manager.
 
29
 
 
30
    Most implementations will show some screen that lets the user add and
 
31
    remove accounts, but we're not quite that sophisticated.
 
32
    """
 
33
 
 
34
    def __init__(self):
 
35
 
 
36
        self.chatui = MinChat()
 
37
 
 
38
        if len(accounts) == 0:
 
39
            print "You have defined no accounts."
 
40
        for acct in accounts:
 
41
            acct.logOn(self.chatui)
 
42
 
 
43
 
 
44
class MinConversation(basechat.Conversation):
 
45
    """This class is a minimal implementation of the abstract Conversation class.
 
46
 
 
47
    This is all you need to override to receive one-on-one messages.
 
48
    """
 
49
    def show(self):
 
50
        """If you don't have a GUI, this is a no-op.
 
51
        """
 
52
        pass
 
53
    
 
54
    def hide(self):
 
55
        """If you don't have a GUI, this is a no-op.
 
56
        """
 
57
        pass
 
58
    
 
59
    def showMessage(self, text, metadata=None):
 
60
        print "<%s> %s" % (self.person.name, text)
 
61
        
 
62
    def contactChangedNick(self, person, newnick):
 
63
        basechat.Conversation.contactChangedNick(self, person, newnick)
 
64
        print "-!- %s is now known as %s" % (person.name, newnick)
 
65
 
 
66
 
 
67
class MinGroupConversation(basechat.GroupConversation):
 
68
    """This class is a minimal implementation of the abstract GroupConversation class.
 
69
 
 
70
    This is all you need to override to listen in on a group conversaion.
 
71
    """
 
72
    def show(self):
 
73
        """If you don't have a GUI, this is a no-op.
 
74
        """
 
75
        pass
 
76
 
 
77
    def hide(self):
 
78
        """If you don't have a GUI, this is a no-op.
 
79
        """
 
80
        pass
 
81
 
 
82
    def showGroupMessage(self, sender, text, metadata=None):
 
83
        print "<%s/%s> %s" % (sender, self.group.name, text)
 
84
 
 
85
    def setTopic(self, topic, author):
 
86
        print "-!- %s set the topic of %s to: %s" % (author, 
 
87
            self.group.name, topic)
 
88
 
 
89
    def memberJoined(self, member):
 
90
        basechat.GroupConversation.memberJoined(self, member)
 
91
        print "-!- %s joined %s" % (member, self.group.name)
 
92
 
 
93
    def memberChangedNick(self, oldnick, newnick):
 
94
        basechat.GroupConversation.memberChangedNick(self, oldnick, newnick)
 
95
        print "-!- %s is now known as %s in %s" % (oldnick, newnick,
 
96
            self.group.name)
 
97
 
 
98
    def memberLeft(self, member):
 
99
        basechat.GroupConversation.memberLeft(self, member)
 
100
        print "-!- %s left %s" % (member, self.group.name)
 
101
    
 
102
class MinChat(basechat.ChatUI):
 
103
    """This class is a minimal implementation of the abstract ChatUI class.
 
104
 
 
105
    There are only two methods that need overriding - and of those two, 
 
106
    the only change that needs to be made is the default value of the Class
 
107
    parameter.
 
108
    """
 
109
 
 
110
    def getGroupConversation(self, group, Class=MinGroupConversation, 
 
111
        stayHidden=0):
 
112
 
 
113
        return basechat.ChatUI.getGroupConversation(self, group, Class, 
 
114
            stayHidden)
 
115
 
 
116
    def getConversation(self, person, Class=MinConversation, 
 
117
        stayHidden=0):
 
118
 
 
119
        return basechat.ChatUI.getConversation(self, person, Class, stayHidden)
 
120
 
 
121
if __name__ == "__main__":
 
122
    from twisted.internet import reactor
 
123
 
 
124
    AccountManager()
 
125
 
 
126
    reactor.run()