1 | # -*- test-case-name: wokkel.test.test_muc -*- |
---|
2 | # |
---|
3 | # Copyright (c) 2003-2008 Ralph Meijer |
---|
4 | # See LICENSE for details. |
---|
5 | |
---|
6 | """ |
---|
7 | XMPP Multi-User Chat protocol. |
---|
8 | |
---|
9 | This protocol is specified in |
---|
10 | U{XEP-0045<http://www.xmpp.org/extensions/xep-0045.html>}. |
---|
11 | """ |
---|
12 | import datetime |
---|
13 | |
---|
14 | from zope.interface import implements |
---|
15 | |
---|
16 | from twisted.internet import defer |
---|
17 | from twisted.words.protocols.jabber import jid, error, xmlstream |
---|
18 | from twisted.words.xish import domish |
---|
19 | |
---|
20 | from wokkel import disco, data_form, shim, xmppim |
---|
21 | from wokkel.subprotocols import IQHandlerMixin, XMPPHandler |
---|
22 | from wokkel.iwokkel import IMUCClient |
---|
23 | |
---|
24 | # Multi User Chat namespaces |
---|
25 | NS = 'http://jabber.org/protocol/muc' |
---|
26 | NS_USER = NS + '#user' |
---|
27 | NS_ADMIN = NS + '#admin' |
---|
28 | NS_OWNER = NS + '#owner' |
---|
29 | NS_ROOMINFO = NS + '#roominfo' |
---|
30 | NS_CONFIG = NS + '#roomconfig' |
---|
31 | NS_REQUEST = NS + '#request' |
---|
32 | NS_REGISTER = NS + '#register' |
---|
33 | |
---|
34 | NS_DELAY = 'urn:xmpp:delay' |
---|
35 | NS_REQUEST = 'jabber:iq:register' |
---|
36 | |
---|
37 | # ad hoc commands |
---|
38 | NS_AD_HOC = "http://jabber.org/protocol/commands" |
---|
39 | |
---|
40 | |
---|
41 | # Iq get and set XPath queries |
---|
42 | IQ = '/iq' |
---|
43 | IQ_GET = IQ+'[@type="get"]' |
---|
44 | IQ_SET = IQ+'[@type="set"]' |
---|
45 | |
---|
46 | IQ_RESULT = IQ+'[@type="result"]' |
---|
47 | IQ_ERROR = IQ+'[@type="error"]' |
---|
48 | |
---|
49 | IQ_QUERY = IQ+'/query' |
---|
50 | IQ_GET_QUERY = IQ_GET + '/query' |
---|
51 | IQ_SET_QUERY = IQ_SET + '/query' |
---|
52 | |
---|
53 | IQ_COMMAND = IQ+'/command' |
---|
54 | |
---|
55 | MUC_ADMIN = IQ_QUERY+'[@xmlns="' + NS_ADMIN + '"]' |
---|
56 | MUC_OWNER = IQ_QUERY+'[@xmlns="' + NS_OWNER + '"]' |
---|
57 | |
---|
58 | MUC_AO = MUC_ADMIN + '|' + MUC_OWNER |
---|
59 | |
---|
60 | |
---|
61 | MESSAGE = '/message' |
---|
62 | PRESENCE = '/presence' |
---|
63 | |
---|
64 | CHAT_BODY = MESSAGE +'[@type="chat"]/body' |
---|
65 | CHAT = MESSAGE +'[@type="chat"]' |
---|
66 | |
---|
67 | GROUPCHAT = MESSAGE +'[@type="groupchat"]/body' |
---|
68 | SUBJECT = MESSAGE +'[@type="groupchat"]/subject' |
---|
69 | MESSAGE_ERROR = MESSAGE +'[@type="error"]' |
---|
70 | |
---|
71 | STATUS_CODES = { # see http://www.xmpp.org/extensions/xep-0045.html#registrar-statuscodes |
---|
72 | 100: |
---|
73 | {'name':'fulljid', |
---|
74 | 'stanza':'presence', |
---|
75 | |
---|
76 | }, |
---|
77 | 201: |
---|
78 | {'name':'created', |
---|
79 | 'stanza': 'presence', |
---|
80 | 'context':'Entering a room', |
---|
81 | 'purpose':'Inform user that a new room has been created' |
---|
82 | }, |
---|
83 | } |
---|
84 | |
---|
85 | STATUS_CODE_CREATED = 201 |
---|
86 | |
---|
87 | |
---|
88 | class MUCError(error.StanzaError): |
---|
89 | """ |
---|
90 | Exception with muc specific condition. |
---|
91 | """ |
---|
92 | def __init__(self, condition, mucCondition, feature=None, text=None): |
---|
93 | appCondition = domish.Element((NS, mucCondition)) |
---|
94 | if feature: |
---|
95 | appCondition['feature'] = feature |
---|
96 | error.StanzaError.__init__(self, condition, |
---|
97 | text=text, |
---|
98 | appCondition=appCondition) |
---|
99 | |
---|
100 | |
---|
101 | class BadRequest(MUCError): |
---|
102 | """ |
---|
103 | Bad request stanza error. |
---|
104 | """ |
---|
105 | def __init__(self, mucCondition=None, text=None): |
---|
106 | MUCError.__init__(self, 'bad-request', mucCondition, text) |
---|
107 | |
---|
108 | |
---|
109 | |
---|
110 | class Unsupported(MUCError): |
---|
111 | def __init__(self, feature, text=None): |
---|
112 | MUCError.__init__(self, 'feature-not-implemented', |
---|
113 | 'unsupported', |
---|
114 | feature, |
---|
115 | text) |
---|
116 | |
---|
117 | |
---|
118 | |
---|
119 | class ConfigureRequest(xmlstream.IQ): |
---|
120 | """ |
---|
121 | Configure MUC room request. |
---|
122 | |
---|
123 | http://xmpp.org/extensions/xep-0045.html#roomconfig |
---|
124 | |
---|
125 | @ivar method: Type attribute of the IQ request. Either C{'set'} or C{'get'} |
---|
126 | @type method: C{str} |
---|
127 | """ |
---|
128 | |
---|
129 | def __init__(self, xs, method='get', fields=[]): |
---|
130 | xmlstream.IQ.__init__(self, xs, method) |
---|
131 | q = self.addElement((NS_OWNER, 'query')) |
---|
132 | if method == 'set': |
---|
133 | # build data form |
---|
134 | form = data_form.Form('submit', formNamespace=NS_CONFIG) |
---|
135 | q.addChild(form.toElement()) |
---|
136 | |
---|
137 | for f in fields: |
---|
138 | # create a field |
---|
139 | form.addField(f) |
---|
140 | |
---|
141 | |
---|
142 | class RegisterRequest(xmlstream.IQ): |
---|
143 | """ |
---|
144 | Register room request. |
---|
145 | |
---|
146 | @ivar method: Type attribute of the IQ request. Either C{'set'} or C{'get'} |
---|
147 | @type method: C{str} |
---|
148 | |
---|
149 | """ |
---|
150 | |
---|
151 | def __init__(self, xs, method='get', fields=[]): |
---|
152 | xmlstream.IQ.__init__(self, xs, method) |
---|
153 | q = self.addElement((NS_REQUEST, 'query')) |
---|
154 | if method == 'set': |
---|
155 | # build data form |
---|
156 | form_type = 'submit' |
---|
157 | form = data_form.Form(form_type, formNamespace=NS_REGISTER) |
---|
158 | q.addChild(form.toElement()) |
---|
159 | |
---|
160 | for f in fields: |
---|
161 | # create a field |
---|
162 | form.addField(f) |
---|
163 | |
---|
164 | |
---|
165 | class AffiliationRequest(xmlstream.IQ): |
---|
166 | """ |
---|
167 | Register room request. |
---|
168 | |
---|
169 | @ivar method: Type attribute of the IQ request. Either C{'set'} or C{'get'} |
---|
170 | @type method: C{str} |
---|
171 | |
---|
172 | @ivar affiliation: The affiliation type to send to room. |
---|
173 | @type affiliation: C{str} |
---|
174 | |
---|
175 | """ |
---|
176 | |
---|
177 | def __init__(self, xs, method='get', affiliation='none', a_jid=None, reason=None): |
---|
178 | xmlstream.IQ.__init__(self, xs, method) |
---|
179 | |
---|
180 | q = self.addElement((NS_ADMIN, 'query')) |
---|
181 | i = q.addElement('item') |
---|
182 | |
---|
183 | i['affiliation'] = affiliation |
---|
184 | if a_jid: |
---|
185 | i['jid'] = a_jid.full() |
---|
186 | |
---|
187 | if reason: |
---|
188 | i.addElement('reason', None, reason) |
---|
189 | |
---|
190 | |
---|
191 | |
---|
192 | |
---|
193 | class GroupChat(domish.Element): |
---|
194 | """ |
---|
195 | """ |
---|
196 | def __init__(self, to, body=None, subject=None, frm=None): |
---|
197 | """To needs to be a string |
---|
198 | """ |
---|
199 | domish.Element.__init__(self, (None, 'message')) |
---|
200 | self['type'] = 'groupchat' |
---|
201 | self['to'] = to |
---|
202 | if frm: |
---|
203 | self['from'] = frm |
---|
204 | if body: |
---|
205 | self.addElement('body',None, body) |
---|
206 | if subject: |
---|
207 | self.addElement('subject',None, subject) |
---|
208 | |
---|
209 | |
---|
210 | class PrivateChat(domish.Element): |
---|
211 | """ |
---|
212 | """ |
---|
213 | def __init__(self, to, body=None, frm=None): |
---|
214 | """To needs to be a string |
---|
215 | """ |
---|
216 | domish.Element.__init__(self, (None, 'message')) |
---|
217 | self['type'] = 'chat' |
---|
218 | self['to'] = to |
---|
219 | if frm: |
---|
220 | self['from'] = frm |
---|
221 | if body: |
---|
222 | self.addElement('body',None, body) |
---|
223 | |
---|
224 | class InviteMessage(PrivateChat): |
---|
225 | def __init__(self, to, reason=None, full_jid=None, body=None, frm=None, password=None): |
---|
226 | PrivateChat.__init__(self, to, body=body, frm=frm) |
---|
227 | del self['type'] # remove type |
---|
228 | x = self.addElement('x', NS_USER) |
---|
229 | invite = x.addElement('invite') |
---|
230 | if full_jid: |
---|
231 | invite['to'] = full_jid |
---|
232 | if reason: |
---|
233 | invite.addElement('reason', None, reason) |
---|
234 | if password: |
---|
235 | invite.addElement('password', None, password) |
---|
236 | |
---|
237 | class HistoryMessage(GroupChat): |
---|
238 | """ |
---|
239 | """ |
---|
240 | def __init__(self, to, stamp, body=None, subject=None, frm=None, h_frm=None): |
---|
241 | GroupChat.__init__(self, to, body=body, subject=subject, frm=frm) |
---|
242 | d = self.addElement('delay', NS_DELAY) |
---|
243 | d['stamp'] = stamp |
---|
244 | if h_frm: |
---|
245 | d['from'] = h_frm |
---|
246 | |
---|
247 | class User(object): |
---|
248 | """ |
---|
249 | A user/entity in a multi-user chat room. |
---|
250 | """ |
---|
251 | |
---|
252 | def __init__(self, nick, user_jid=None): |
---|
253 | self.nick = nick |
---|
254 | self.user_jid = user_jid |
---|
255 | self.affiliation = 'none' |
---|
256 | self.role = 'none' |
---|
257 | |
---|
258 | self.status = None |
---|
259 | self.show = None |
---|
260 | |
---|
261 | |
---|
262 | class Room(object): |
---|
263 | """ |
---|
264 | A Multi User Chat Room |
---|
265 | """ |
---|
266 | |
---|
267 | |
---|
268 | def __init__(self, name, server, nick, state=None): |
---|
269 | """ |
---|
270 | """ |
---|
271 | self.state = state |
---|
272 | self.name = name |
---|
273 | self.server = server |
---|
274 | self.nick = nick |
---|
275 | self.status = None |
---|
276 | |
---|
277 | self.entity_id = jid.internJID(name+'@'+server+'/'+nick) |
---|
278 | |
---|
279 | self.roster = {} |
---|
280 | |
---|
281 | |
---|
282 | |
---|
283 | def addUser(self, user): |
---|
284 | """ |
---|
285 | """ |
---|
286 | self.roster[user.nick.lower()] = user |
---|
287 | |
---|
288 | def inRoster(self, user): |
---|
289 | """ |
---|
290 | """ |
---|
291 | |
---|
292 | return self.roster.has_key(user.nick.lower()) |
---|
293 | |
---|
294 | def getUser(self, nick): |
---|
295 | """ |
---|
296 | """ |
---|
297 | return self.roster.get(nick.lower()) |
---|
298 | |
---|
299 | |
---|
300 | |
---|
301 | class BasicPresence(xmppim.AvailablePresence): |
---|
302 | """ |
---|
303 | This behaves like an object providing L{domish.IElement}. |
---|
304 | |
---|
305 | """ |
---|
306 | |
---|
307 | def __init__(self, to=None, show=None, statuses=None): |
---|
308 | xmppim.AvailablePresence.__init__(self, to=to, show=show, statuses=statuses) |
---|
309 | # add muc elements |
---|
310 | x = self.addElement('x', NS) |
---|
311 | |
---|
312 | |
---|
313 | class UserPresence(xmppim.Presence): |
---|
314 | """ |
---|
315 | This behaves like an object providing L{domish.IElement}. |
---|
316 | |
---|
317 | """ |
---|
318 | |
---|
319 | def __init__(self, to=None, type=None, frm=None, affiliation=None, role=None): |
---|
320 | xmppim.Presence.__init__(self, to, type) |
---|
321 | if frm: |
---|
322 | self['from'] = frm |
---|
323 | # add muc elements |
---|
324 | x = self.addElement('x', NS_USER) |
---|
325 | if affiliation: |
---|
326 | x['affiliation'] = affiliation |
---|
327 | if role: |
---|
328 | x['role'] = role |
---|
329 | |
---|
330 | |
---|
331 | class PasswordPresence(BasicPresence): |
---|
332 | """ |
---|
333 | """ |
---|
334 | def __init__(self, to, password): |
---|
335 | BasicPresence.__init__(self, to) |
---|
336 | |
---|
337 | self.x.addElement('password', None, password) |
---|
338 | |
---|
339 | |
---|
340 | class MessageVoice(GroupChat): |
---|
341 | """ |
---|
342 | """ |
---|
343 | def __init__(self, to=None, frm=None): |
---|
344 | GroupChat.__init__(self, to=to, frm=frm) |
---|
345 | # build data form |
---|
346 | form = data_form.Form('submit', formNamespace=NS_REQUEST) |
---|
347 | form.addField(data_form.Field(var='muc#role', |
---|
348 | value='participant', |
---|
349 | label='Requested role')) |
---|
350 | self.addChild(form.toElement()) |
---|
351 | |
---|
352 | class PresenceError(xmppim.Presence): |
---|
353 | """ |
---|
354 | This behaves like an object providing L{domish.IElement}. |
---|
355 | |
---|
356 | """ |
---|
357 | |
---|
358 | def __init__(self, error, to=None, frm=None): |
---|
359 | xmppim.Presence.__init__(self, to, type='error') |
---|
360 | if frm: |
---|
361 | self['from'] = frm |
---|
362 | # add muc elements |
---|
363 | x = self.addElement('x', NS) |
---|
364 | # add error |
---|
365 | self.addChild(error) |
---|
366 | |
---|
367 | |
---|
368 | class MUCClient(XMPPHandler): |
---|
369 | """ |
---|
370 | Multi-User chat client protocol. |
---|
371 | """ |
---|
372 | |
---|
373 | implements(IMUCClient) |
---|
374 | |
---|
375 | rooms = {} |
---|
376 | |
---|
377 | def connectionInitialized(self): |
---|
378 | self.xmlstream.addObserver(PRESENCE+"[not(@type) or @type='available']/x", self._onXPresence) |
---|
379 | self.xmlstream.addObserver(PRESENCE+"[@type='unavailable']", self._onUnavailablePresence) |
---|
380 | self.xmlstream.addObserver(PRESENCE+"[@type='error']", self._onPresenceError) |
---|
381 | self.xmlstream.addObserver(GROUPCHAT, self._onGroupChat) |
---|
382 | self.xmlstream.addObserver(SUBJECT, self._onSubject) |
---|
383 | # add history |
---|
384 | |
---|
385 | def _setRoom(self, room): |
---|
386 | self.rooms[room.entity_id.full().lower()] = room |
---|
387 | |
---|
388 | def _getRoom(self, room_jid): |
---|
389 | return self.rooms.get(room_jid.full().lower()) |
---|
390 | |
---|
391 | def _removeRoom(self, room_jid): |
---|
392 | if self.rooms.has_key(room_jid.full().lower()): |
---|
393 | del self.rooms[room_jid.full().lower()] |
---|
394 | |
---|
395 | |
---|
396 | def _onUnavailablePresence(self, prs): |
---|
397 | """ |
---|
398 | """ |
---|
399 | if not prs.hasAttribute('from'): |
---|
400 | return |
---|
401 | room_jid = jid.internJID(prs.getAttribute('from', '')) |
---|
402 | |
---|
403 | |
---|
404 | def _onPresenceError(self, prs): |
---|
405 | """ |
---|
406 | """ |
---|
407 | if not prs.hasAttribute('from'): |
---|
408 | return |
---|
409 | room_jid = jid.internJID(prs.getAttribute('from', '')) |
---|
410 | |
---|
411 | |
---|
412 | def _onXPresence(self, prs): |
---|
413 | """ |
---|
414 | """ |
---|
415 | if not prs.hasAttribute('from'): |
---|
416 | return |
---|
417 | room_jid = jid.internJID(prs.getAttribute('from', '')) |
---|
418 | |
---|
419 | status = getattr(prs, 'status', None) |
---|
420 | show = getattr(prs, 'show', None) |
---|
421 | |
---|
422 | # grab room |
---|
423 | room = self._getRoom(room_jid) |
---|
424 | if room is None: |
---|
425 | # not in the room yet |
---|
426 | return |
---|
427 | |
---|
428 | # check if user is in roster |
---|
429 | user = room.getUser(room_jid.resource) |
---|
430 | if user is None: # create a user that does not exist |
---|
431 | user = User(room_jid.resource) |
---|
432 | |
---|
433 | |
---|
434 | if room.inRoster(user): |
---|
435 | # we changed status or nick |
---|
436 | muc_status = getattr(prs.x, 'status', None) |
---|
437 | if muc_status: |
---|
438 | code = muc_status.getAttribute('code', 0) |
---|
439 | else: |
---|
440 | self.userUpdatedStatus(room, user, show, status) |
---|
441 | else: |
---|
442 | room.addUser(user) |
---|
443 | self.userJoinedRoom(room, user) |
---|
444 | |
---|
445 | |
---|
446 | def _onGroupChat(self, msg): |
---|
447 | """ |
---|
448 | """ |
---|
449 | if not msg.hasAttribute('from'): |
---|
450 | # need to return an error here |
---|
451 | return |
---|
452 | room_jid = jid.internJID(msg.getAttribute('from', '')) |
---|
453 | |
---|
454 | room = self._getRoom(room_jid) |
---|
455 | if room is None: |
---|
456 | # not in the room yet |
---|
457 | return |
---|
458 | user = room.getUser(room_jid.resource) |
---|
459 | |
---|
460 | delay = getattr(msg, 'delay', None) |
---|
461 | body = unicode(msg.body) |
---|
462 | # grab room |
---|
463 | if delay is None: |
---|
464 | self.receivedGroupChat(room, user, body) |
---|
465 | else: |
---|
466 | self.receivedHistory(room, user, body, delay['stamp'], frm=delay.getAttribute('from',None)) |
---|
467 | |
---|
468 | |
---|
469 | def _onSubject(self, msg): |
---|
470 | """ |
---|
471 | """ |
---|
472 | if not msg.hasAttribute('from'): |
---|
473 | return |
---|
474 | room_jid = jid.internJID(msg['from']) |
---|
475 | |
---|
476 | # grab room |
---|
477 | room = self._getRoom(room_jid) |
---|
478 | if room is None: |
---|
479 | # not in the room yet |
---|
480 | return |
---|
481 | |
---|
482 | self.receivedSubject(room_jid, unicode(msg.subject)) |
---|
483 | |
---|
484 | |
---|
485 | def _makeTimeStamp(self, stamp=None): |
---|
486 | if stamp is None: |
---|
487 | stamp = datetime.datetime.now() |
---|
488 | |
---|
489 | return stamp.strftime('%Y%m%dT%H:%M:%S') |
---|
490 | |
---|
491 | |
---|
492 | def _joinedRoom(self, d, prs): |
---|
493 | """We have presence that says we joined a room. |
---|
494 | """ |
---|
495 | room_jid = jid.internJID(prs['from']) |
---|
496 | |
---|
497 | # check for errors |
---|
498 | if prs.hasAttribute('type') and prs['type'] == 'error': |
---|
499 | d.errback(prs) |
---|
500 | else: |
---|
501 | # change the state of the room |
---|
502 | r = self._getRoom(room_jid) |
---|
503 | if r is None: |
---|
504 | raise Exception, 'Room Not Found' |
---|
505 | r.state = 'joined' |
---|
506 | |
---|
507 | # grab status |
---|
508 | status = getattr(prs.x,'status',None) |
---|
509 | if status: |
---|
510 | r.status = status.getAttribute('code', None) |
---|
511 | |
---|
512 | d.callback(r) |
---|
513 | |
---|
514 | |
---|
515 | def _leftRoom(self, d, prs): |
---|
516 | """We have presence that says we joined a room. |
---|
517 | """ |
---|
518 | room_jid = jid.internJID(prs['from']) |
---|
519 | |
---|
520 | # check for errors |
---|
521 | if prs.hasAttribute('type') and prs['type'] == 'error': |
---|
522 | d.errback(prs) |
---|
523 | else: |
---|
524 | # change the state of the room |
---|
525 | r = self._getRoom(room_jid) |
---|
526 | if r is None: |
---|
527 | raise Exception, 'Room Not Found' |
---|
528 | self._removeRoom(room_jid) |
---|
529 | |
---|
530 | d.callback(True) |
---|
531 | |
---|
532 | def userJoinedRoom(self, room, user): |
---|
533 | """User has joined a room |
---|
534 | """ |
---|
535 | pass |
---|
536 | |
---|
537 | |
---|
538 | def userUserUpdatedStatus(self, room, user, show, status): |
---|
539 | """User Presence has been received |
---|
540 | """ |
---|
541 | pass |
---|
542 | |
---|
543 | |
---|
544 | def receivedSubject(self, room, subject): |
---|
545 | """ |
---|
546 | """ |
---|
547 | pass |
---|
548 | |
---|
549 | |
---|
550 | def receivedHistory(self, room, user, message, history, frm=None): |
---|
551 | """ |
---|
552 | """ |
---|
553 | pass |
---|
554 | |
---|
555 | |
---|
556 | def _cbDisco(self, iq): |
---|
557 | # grab query |
---|
558 | |
---|
559 | return getattr(iq,'query', None) |
---|
560 | |
---|
561 | def disco(self, entity, type='info'): |
---|
562 | """Send disco queries to a XMPP entity |
---|
563 | """ |
---|
564 | |
---|
565 | iq = disco.DiscoRequest(self.xmlstream, disco.NS_INFO, 'get') |
---|
566 | iq['to'] = entity |
---|
567 | |
---|
568 | return iq.send().addBoth(self._cbDisco) |
---|
569 | |
---|
570 | |
---|
571 | def configure(self, room_jid, fields=[]): |
---|
572 | """Configure a room |
---|
573 | """ |
---|
574 | request = ConfigureRequest(self.xmlstream, method='set', fields=fields) |
---|
575 | request['to'] = room_jid |
---|
576 | |
---|
577 | return request.send() |
---|
578 | |
---|
579 | def getConfigureForm(self, room_jid): |
---|
580 | request = ConfigureRequest(self.xmlstream) |
---|
581 | request['to'] = room_jid |
---|
582 | return request.send() |
---|
583 | |
---|
584 | |
---|
585 | def join(self, server, room, nick): |
---|
586 | """ |
---|
587 | """ |
---|
588 | d = defer.Deferred() |
---|
589 | r = Room(room, server, nick, state='joining') |
---|
590 | self._setRoom(r) |
---|
591 | |
---|
592 | p = BasicPresence(to=r.entity_id) |
---|
593 | # p['from'] = self.jid.full() |
---|
594 | self.xmlstream.send(p) |
---|
595 | |
---|
596 | # add observer for joining the room |
---|
597 | self.xmlstream.addOnetimeObserver(PRESENCE+"[@from='%s']" % (r.entity_id.full()), |
---|
598 | self._joinedRoom, 1, d) |
---|
599 | |
---|
600 | return d |
---|
601 | |
---|
602 | |
---|
603 | |
---|
604 | def leave(self, room_jid): |
---|
605 | """ |
---|
606 | """ |
---|
607 | d = defer.Deferred() |
---|
608 | |
---|
609 | r = self._getRoom(room_jid) |
---|
610 | |
---|
611 | p = xmppim.UnavailablePresence(to=r.entity_id) |
---|
612 | # p['from'] = self.jid.full() |
---|
613 | self.xmlstream.send(p) |
---|
614 | |
---|
615 | # add observer for joining the room |
---|
616 | self.xmlstream.addOnetimeObserver(PRESENCE+"[@from='%s' and @type='unavailable']" % (r.entity_id.full()), |
---|
617 | self._leftRoom, 1, d) |
---|
618 | |
---|
619 | return d |
---|
620 | |
---|
621 | |
---|
622 | |
---|
623 | |
---|
624 | def _sendMessage(self, msg, children=None): |
---|
625 | |
---|
626 | if children: |
---|
627 | for c in children: |
---|
628 | msg.addChild(c) |
---|
629 | |
---|
630 | self.xmlstream.send(msg) |
---|
631 | |
---|
632 | def groupChat(self, to, message, children=None): |
---|
633 | """Send a groupchat message |
---|
634 | """ |
---|
635 | msg = GroupChat(to, body=message) |
---|
636 | |
---|
637 | self._sendMessage(msg, children=children) |
---|
638 | |
---|
639 | def chat(self, to, message, children=None): |
---|
640 | msg = PrivateChat(to, body=message) |
---|
641 | |
---|
642 | self._sendMessage(msg, children=children) |
---|
643 | |
---|
644 | def invite(self, to, reason=None, full_jid=None): |
---|
645 | """ |
---|
646 | """ |
---|
647 | msg = InviteMessage(to, reason=reason, full_jid=full_jid) |
---|
648 | self._sendMessage(msg) |
---|
649 | |
---|
650 | |
---|
651 | def password(self, to, password): |
---|
652 | p = PasswordPresence(to, password) |
---|
653 | |
---|
654 | self.xmlstream.send(p) |
---|
655 | |
---|
656 | def register(self, to, fields=[]): |
---|
657 | iq = RegisterRequest(self.xmlstream, method='set', fields=fields) |
---|
658 | iq['to'] = to |
---|
659 | return iq.send() |
---|
660 | |
---|
661 | def getRegisterForm(self, room): |
---|
662 | """ |
---|
663 | """ |
---|
664 | iq = RegisterRequest(self.xmlstream) |
---|
665 | iq['to'] = room.userhost() |
---|
666 | return iq.send() |
---|
667 | |
---|
668 | def subject(self, to, subject): |
---|
669 | """ |
---|
670 | """ |
---|
671 | msg = GroupChat(to, subject=subject) |
---|
672 | self.xmlstream.send(msg) |
---|
673 | |
---|
674 | def voice(self, to): |
---|
675 | """ |
---|
676 | """ |
---|
677 | msg = MessageVoice(to=to) |
---|
678 | self.xmlstream.send(msg) |
---|
679 | |
---|
680 | |
---|
681 | def history(self, to, message_list): |
---|
682 | """ |
---|
683 | """ |
---|
684 | |
---|
685 | for m in message_list: |
---|
686 | m['type'] = 'groupchat' |
---|
687 | mto = m['to'] |
---|
688 | frm = m.getAttribute('from', None) |
---|
689 | m['to'] = to |
---|
690 | |
---|
691 | d = m.addElement('delay', NS_DELAY) |
---|
692 | d['stamp'] = self._makeTimeStamp() |
---|
693 | d['from'] = mto |
---|
694 | |
---|
695 | self.xmlstream.send(m) |
---|
696 | |
---|
697 | def ban(self, to, ban_jid, frm, reason=None): |
---|
698 | |
---|
699 | iq = AffiliationRequest(self.xmlstream, |
---|
700 | method='set', |
---|
701 | affiliation='outcast', |
---|
702 | a_jid=ban_jid, |
---|
703 | reason=reason) |
---|
704 | iq['to'] = to.userhost() # this is a room jid, only send to room |
---|
705 | iq['from'] = frm.full() |
---|
706 | return iq.send() |
---|
707 | |
---|
708 | |
---|
709 | def kick(self, to, kick_jid, frm, reason=None): |
---|
710 | |
---|
711 | iq = AffiliationRequest(self.xmlstream, |
---|
712 | method='set', |
---|
713 | a_jid=kick_jid, |
---|
714 | reason=reason) |
---|
715 | iq['to'] = to.userhost() # this is a room jid, only send to room |
---|
716 | iq['from'] = frm.full() |
---|
717 | return iq.send() |
---|