Changeset 112:be0b126081f2
- Timestamp:
- Oct 6, 2008, 10:56:52 PM (14 years ago)
- Branch:
- wokkel-muc-client-support-24
- Convert:
- svn:b33ecbfc-034c-dc11-8662-000475d9059e/branches/wokkel-muc-client-support-24@76
- Location:
- wokkel
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
wokkel/muc.py
r111 r112 121 121 Configure MUC room request. 122 122 123 @ivar namespace: Request namespace.124 @type namespace: C{str}125 123 @ivar method: Type attribute of the IQ request. Either C{'set'} or C{'get'} 126 124 @type method: C{str} … … 144 142 Register room request. 145 143 146 @ivar namespace: Request namespace.147 @type namespace: C{str}148 144 @ivar method: Type attribute of the IQ request. Either C{'set'} or C{'get'} 149 145 @type method: C{str} 146 150 147 """ 151 148 … … 162 159 # create a field 163 160 form.addField(f) 161 162 163 class AffiliationRequest(xmlstream.IQ): 164 """ 165 Register room request. 166 167 @ivar method: Type attribute of the IQ request. Either C{'set'} or C{'get'} 168 @type method: C{str} 169 170 @ivar affiliation: The affiliation type to send to room. 171 @type affiliation: C{str} 172 173 """ 174 175 def __init__(self, xs, method='get', affiliation='none', a_jid=None, reason=None): 176 xmlstream.IQ.__init__(self, xs, method) 177 178 q = self.addElement((NS_ADMIN, 'query')) 179 i = q.addElement('item') 180 181 i['affiliation'] = affiliation 182 if a_jid: 183 i['jid'] = a_jid.full() 184 185 if reason: 186 i.addElement('reason', None, reason) 187 188 189 164 190 165 191 class GroupChat(domish.Element): … … 178 204 if subject: 179 205 self.addElement('subject',None, subject) 180 181 206 207 208 class PrivateChat(domish.Element): 209 """ 210 """ 211 def __init__(self, to, body=None, frm=None): 212 """To needs to be a string 213 """ 214 domish.Element.__init__(self, (None, 'message')) 215 self['type'] = 'chat' 216 self['to'] = to 217 if frm: 218 self['from'] = frm 219 if body: 220 self.addElement('body',None, body) 221 222 class InviteMessage(PrivateChat): 223 def __init__(self, to, reason=None, full_jid=None, body=None, frm=None, password=None): 224 PrivateChat.__init__(self, to, body=body, frm=frm) 225 del self['type'] # remove type 226 x = self.addElement('x', NS_USER) 227 invite = x.addElement('invite') 228 if full_jid: 229 invite['to'] = full_jid 230 if reason: 231 invite.addElement('reason', None, reason) 232 if password: 233 invite.addElement('password', None, password) 234 235 class HistoryMessage(GroupChat): 236 """ 237 """ 238 def __init__(self, to, stamp, body=None, subject=None, frm=None, h_frm=None): 239 GroupChat.__init__(self, to, body=body, subject=subject, frm=frm) 240 d = self.addElement('delay', NS_DELAY) 241 d['stamp'] = stamp 242 if h_frm: 243 d['from'] = h_frm 182 244 183 245 class Room(object): … … 304 366 """ 305 367 """ 306 self.receivedGroupChat(msg) 307 368 delay = getattr(msg, 'delay', None) 369 if delay is None: 370 self.receivedGroupChat(msg) 371 else: 372 self.receivedHistory(msg) 308 373 309 374 … … 372 437 pass 373 438 439 440 def receivedHistory(self, msg): 441 """ 442 """ 443 pass 444 445 374 446 def _cbDisco(self, iq): 375 447 # grab query … … 425 497 d = defer.Deferred() 426 498 427 self._getRoom(room_jid)499 r = self._getRoom(room_jid) 428 500 429 501 p = xmppim.UnavailablePresence(to=r.entity_id) … … 432 504 433 505 # add observer for joining the room 434 self.xmlstream.addOnetimeObserver(PRESENCE+"[@from='%s' and type='unavailable']" % (r.entity_id.full()),506 self.xmlstream.addOnetimeObserver(PRESENCE+"[@from='%s' and @type='unavailable']" % (r.entity_id.full()), 435 507 self._leftRoom, 1, d) 436 508 … … 440 512 441 513 442 def groupChat(self, to, message, children=None): 443 """Send a groupchat message 444 """ 445 msg = GroupChat(to, body=message) 514 def _sendMessage(self, msg, children=None): 446 515 447 516 if children: … … 451 520 self.xmlstream.send(msg) 452 521 522 def groupChat(self, to, message, children=None): 523 """Send a groupchat message 524 """ 525 msg = GroupChat(to, body=message) 526 527 self._sendMessage(msg, children=children) 528 529 def chat(self, to, message, children=None): 530 msg = PrivateChat(to, body=message) 531 532 self._sendMessage(msg, children=children) 533 534 def invite(self, to, reason=None, full_jid=None): 535 msg = InviteMessage(to, reason=reason, full_jid=full_jid) 536 self._sendMessage(msg) 537 538 539 def password(self, to, password): 540 p = PasswordPresence(to, password) 541 542 self.xmlstream.send(p) 453 543 454 544 def register(self, to, fields=[]): … … 491 581 self.xmlstream.send(m) 492 582 583 def ban(self, to, ban_jid, frm, reason=None): 584 585 iq = AffiliationRequest(self.xmlstream, 586 method='set', 587 affiliation='outcast', 588 a_jid=ban_jid, 589 reason=reason) 590 iq['to'] = to.userhost() # this is a room jid, only send to room 591 iq['from'] = frm.full() 592 return iq.send() 593 594 595 def kick(self, to, kick_jid, frm, reason=None): 596 597 iq = AffiliationRequest(self.xmlstream, 598 method='set', 599 a_jid=kick_jid, 600 reason=reason) 601 iq['to'] = to.userhost() # this is a room jid, only send to room 602 iq['from'] = frm.full() 603 return iq.send() -
wokkel/test/test_muc.py
r111 r112 55 55 self.room_jid = JID(self.test_room+'@'+self.test_srv+'/'+self.test_nick) 56 56 57 self.user_jid = JID('test@jabber.org/Testing') 58 57 59 def test_interface(self): 58 60 """ … … 167 169 168 170 def test_partRoom(self): 169 self.fail('Not Implemented') 171 172 def cb(left): 173 self.failUnless(left, 'did not leave room') 174 175 176 d = self.protocol.leave(self.room_jid) 177 d.addCallback(cb) 178 179 prs = self.stub.output[-1] 180 181 self.failUnless(prs['type']=='unavailable', 'Unavailable is not being sent') 182 183 response = prs 184 response['from'] = response['to'] 185 response['to'] = 'test@jabber.org' 186 187 self.stub.send(response) 188 return d 170 189 171 190 172 191 def test_ban(self): 173 192 174 self.fail('Not Implemented') 193 banned = JID('ban@jabber.org/TroubleMakger') 194 def cb(banned): 195 self.failUnless(banned, 'Did not ban user') 196 197 198 d = self.protocol.ban(self.room_jid, banned, self.user_jid, reason='Spam') 199 d.addCallback(cb) 200 201 iq = self.stub.output[-1] 202 203 self.failUnless(xpath.matches("/iq[@type='set' and @to='%s']/query/item[@affiliation='outcast']" % (self.room_jid.userhost(),), iq), 'Wrong ban stanza') 204 205 response = toResponse(iq, 'result') 206 207 self.stub.send(response) 208 209 return d 210 175 211 176 212 def test_kick(self): 213 214 kicked = JID('kick@jabber.org/TroubleMakger') 215 def cb(kicked): 216 self.failUnless(kicked, 'Did not kick user') 217 218 219 d = self.protocol.kick(self.room_jid, kicked, self.user_jid, reason='Spam') 220 d.addCallback(cb) 221 222 iq = self.stub.output[-1] 223 224 self.failUnless(xpath.matches("/iq[@type='set' and @to='%s']/query/item[@affiliation='none']" % (self.room_jid.userhost(),), iq), 'Wrong kick stanza') 225 226 response = toResponse(iq, 'result') 227 228 self.stub.send(response) 229 230 return d 231 177 232 self.fail('Not Implemented') 178 233 … … 182 237 """ 183 238 184 185 self.fail('Not Implemented') 239 self.protocol.password(self.room_jid, 'secret') 240 241 prs = self.stub.output[-1] 242 243 self.failUnless(xpath.matches("/presence[@to='%s']/x/password[text()='secret']" % (self.room_jid.full(),), prs), 'Wrong presence stanza') 244 186 245 187 246 def test_history(self): 188 189 self.fail('Not Implemented') 247 """Test receiving history on room join. 248 """ 249 m = muc.HistoryMessage(self.room_jid.userhost(), self.protocol._makeTimeStamp(), body='test') 250 251 def roomHistory(h): 252 self.failUnless(getattr(h,'delay',None), 'No delay element') 253 254 255 d, self.protocol.receivedHistory = calledAsync(roomHistory) 256 self.stub.send(m) 257 return d 190 258 191 259 … … 223 291 224 292 def test_invite(self): 225 self.fail('Not Implemented') 293 other_jid = 'test@jabber.org' 294 295 self.protocol.invite(other_jid, 'This is a test') 296 297 msg = self.stub.output[-1] 298 299 self.failUnless(xpath.matches("/message[@to='%s']/x/invite/reason" % (other_jid,), msg), 'Wrong message type') 300 226 301 227 302 228 303 def test_privateMessage(self): 229 230 self.fail('Not Implemented') 304 """Test sending private messages to muc entities. 305 """ 306 other_nick = self.room_jid.userhost()+'/OtherNick' 307 308 self.protocol.chat(other_nick, 'This is a test') 309 310 msg = self.stub.output[-1] 311 312 self.failUnless(xpath.matches("/message[@type='chat' and @to='%s']/body" % (other_nick,), msg), 'Wrong message type') 313 231 314 232 315 def test_register(self):
Note: See TracChangeset
for help on using the changeset viewer.