1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
L{twisted.words} support for Instance Messenger.
"""
from zope.interface import implementer
from twisted.internet import defer, error
from twisted.python import log
from twisted.python.failure import Failure
from twisted.spread import pb
from twisted.words.im import basesupport, interfaces
from twisted.words.im.locals import AWAY, OFFLINE, ONLINE
class TwistedWordsPerson(basesupport.AbstractPerson):
"""I a facade for a person you can talk to through a twisted.words service."""
def __init__(self, name, wordsAccount):
basesupport.AbstractPerson.__init__(self, name, wordsAccount)
self.status = OFFLINE
def isOnline(self):
return (self.status == ONLINE) or (self.status == AWAY)
def getStatus(self):
return self.status
def sendMessage(self, text, metadata):
"""Return a deferred..."""
if metadata:
d = self.account.client.perspective.directMessage(self.name, text, metadata)
d.addErrback(self.metadataFailed, "* " + text)
return d
else:
return self.account.client.perspective.callRemote(
"directMessage", self.name, text
)
def metadataFailed(self, result, text):
print("result:", result, "text:", text)
return self.account.client.perspective.directMessage(self.name, text)
def setStatus(self, status):
self.status = status
self.chat.getContactsList().setContactStatus(self)
@implementer(interfaces.IGroup)
class TwistedWordsGroup(basesupport.AbstractGroup):
def __init__(self, name, wordsClient):
basesupport.AbstractGroup.__init__(self, name, wordsClient)
self.joined = 0
def sendGroupMessage(self, text, metadata=None):
"""Return a deferred."""
# for backwards compatibility with older twisted.words servers.
if metadata:
d = self.account.client.perspective.callRemote(
"groupMessage", self.name, text, metadata
)
d.addErrback(self.metadataFailed, "* " + text)
return d
else:
return self.account.client.perspective.callRemote(
"groupMessage", self.name, text
)
def setTopic(self, text):
self.account.client.perspective.callRemote(
"setGroupMetadata",
{"topic": text, "topic_author": self.client.name},
self.name,
)
def metadataFailed(self, result, text):
print("result:", result, "text:", text)
return self.account.client.perspective.callRemote(
"groupMessage", self.name, text
)
def joining(self):
self.joined = 1
def leaving(self):
self.joined = 0
def leave(self):
return self.account.client.perspective.callRemote("leaveGroup", self.name)
class TwistedWordsClient(pb.Referenceable, basesupport.AbstractClientMixin):
"""In some cases, this acts as an Account, since it a source of text
messages (multiple Words instances may be on a single PB connection)
"""
def __init__(self, acct, serviceName, perspectiveName, chatui, _logonDeferred=None):
self.accountName = "{} ({}:{})".format(
acct.accountName,
serviceName,
perspectiveName,
)
self.name = perspectiveName
print("HELLO I AM A PB SERVICE", serviceName, perspectiveName)
self.chat = chatui
self.account = acct
self._logonDeferred = _logonDeferred
def getPerson(self, name):
return self.chat.getPerson(name, self)
def getGroup(self, name):
return self.chat.getGroup(name, self)
def getGroupConversation(self, name):
return self.chat.getGroupConversation(self.getGroup(name))
def addContact(self, name):
self.perspective.callRemote("addContact", name)
def remote_receiveGroupMembers(self, names, group):
print("received group members:", names, group)
self.getGroupConversation(group).setGroupMembers(names)
def remote_receiveGroupMessage(self, sender, group, message, metadata=None):
print("received a group message", sender, group, message, metadata)
self.getGroupConversation(group).showGroupMessage(sender, message, metadata)
def remote_memberJoined(self, member, group):
print("member joined", member, group)
self.getGroupConversation(group).memberJoined(member)
def remote_memberLeft(self, member, group):
print("member left")
self.getGroupConversation(group).memberLeft(member)
def remote_notifyStatusChanged(self, name, status):
self.chat.getPerson(name, self).setStatus(status)
def remote_receiveDirectMessage(self, name, message, metadata=None):
self.chat.getConversation(self.chat.getPerson(name, self)).showMessage(
message, metadata
)
def remote_receiveContactList(self, clist):
for name, status in clist:
self.chat.getPerson(name, self).setStatus(status)
def remote_setGroupMetadata(self, dict_, groupName):
if "topic" in dict_:
self.getGroupConversation(groupName).setTopic(
dict_["topic"], dict_.get("topic_author", None)
)
def joinGroup(self, name):
self.getGroup(name).joining()
return self.perspective.callRemote("joinGroup", name).addCallback(
self._cbGroupJoined, name
)
def leaveGroup(self, name):
self.getGroup(name).leaving()
return self.perspective.callRemote("leaveGroup", name).addCallback(
self._cbGroupLeft, name
)
def _cbGroupJoined(self, result, name):
groupConv = self.chat.getGroupConversation(self.getGroup(name))
groupConv.showGroupMessage("sys", "you joined")
self.perspective.callRemote("getGroupMembers", name)
def _cbGroupLeft(self, result, name):
print("left", name)
groupConv = self.chat.getGroupConversation(self.getGroup(name), 1)
groupConv.showGroupMessage("sys", "you left")
def connected(self, perspective):
print("Connected Words Client!", perspective)
if self._logonDeferred is not None:
self._logonDeferred.callback(self)
self.perspective = perspective
self.chat.getContactsList()
pbFrontEnds = {"twisted.words": TwistedWordsClient, "twisted.reality": None}
@implementer(interfaces.IAccount)
class PBAccount(basesupport.AbstractAccount):
gatewayType = "PB"
_groupFactory = TwistedWordsGroup
_personFactory = TwistedWordsPerson
def __init__(
self, accountName, autoLogin, username, password, host, port, services=None
):
"""
@param username: The name of your PB Identity.
@type username: string
"""
basesupport.AbstractAccount.__init__(
self, accountName, autoLogin, username, password, host, port
)
self.services = []
if not services:
services = [("twisted.words", "twisted.words", username)]
for serviceType, serviceName, perspectiveName in services:
self.services.append(
[pbFrontEnds[serviceType], serviceName, perspectiveName]
)
def logOn(self, chatui):
"""
@returns: this breaks with L{interfaces.IAccount}
@returntype: DeferredList of L{interfaces.IClient}s
"""
# Overriding basesupport's implementation on account of the
# fact that _startLogOn tends to return a deferredList rather
# than a simple Deferred, and we need to do registerAccountClient.
if (not self._isConnecting) and (not self._isOnline):
self._isConnecting = 1
d = self._startLogOn(chatui)
d.addErrback(self._loginFailed)
def registerMany(results):
for success, result in results:
if success:
chatui.registerAccountClient(result)
self._cb_logOn(result)
else:
log.err(result)
d.addCallback(registerMany)
return d
else:
raise error.ConnectionError("Connection in progress")
def logOff(self):
# IAccount.logOff
pass
def _startLogOn(self, chatui):
print("Connecting...", end=" ")
d = pb.getObjectAt(self.host, self.port)
d.addCallbacks(self._cbConnected, self._ebConnected, callbackArgs=(chatui,))
return d
def _cbConnected(self, root, chatui):
print("Connected!")
print("Identifying...", end=" ")
d = pb.authIdentity(root, self.username, self.password)
d.addCallbacks(self._cbIdent, self._ebConnected, callbackArgs=(chatui,))
return d
def _cbIdent(self, ident, chatui):
if not ident:
print("falsely identified.")
return self._ebConnected(
Failure(Exception("username or password incorrect"))
)
print("Identified!")
dl = []
for handlerClass, sname, pname in self.services:
d = defer.Deferred()
dl.append(d)
handler = handlerClass(self, sname, pname, chatui, d)
ident.callRemote("attach", sname, pname, handler).addCallback(
handler.connected
)
return defer.DeferredList(dl)
def _ebConnected(self, error):
print("Not connected.")
return error
|