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