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