source:
ralphm-patches/copy_xmppim.patch
@
54:03ec57713c90
Last change on this file since 54:03ec57713c90 was 49:537d1413b661, checked in by Ralph Meijer <ralphm@…>, 11 years ago | |
---|---|
File size: 18.0 KB |
-
.py
# HG changeset patch # Parent 2c8dc93fbef4f2d5b7115c7afdd1b7fb799d47c3 diff --git a/wokkel/xmppim.py b/wokkel/im.py copy from wokkel/xmppim.py copy to wokkel/im.py
old new 22 22 NS_XML = 'http://www.w3.org/XML/1998/namespace' 23 23 NS_ROSTER = 'jabber:iq:roster' 24 24 25 class Presence(domish.Element):26 def __init__(self, to=None, type=None):27 domish.Element.__init__(self, (None, "presence"))28 if type:29 self["type"] = type30 31 if to is not None:32 self["to"] = to.full()33 34 class AvailablePresence(Presence):35 def __init__(self, to=None, show=None, statuses=None, priority=0):36 Presence.__init__(self, to, type=None)37 38 if show in ['away', 'xa', 'chat', 'dnd']:39 self.addElement('show', content=show)40 41 if statuses is not None:42 for lang, status in statuses.iteritems():43 s = self.addElement('status', content=status)44 if lang:45 s[(NS_XML, "lang")] = lang46 47 if priority != 0:48 self.addElement('priority', content=unicode(int(priority)))49 50 class UnavailablePresence(Presence):51 def __init__(self, to=None, statuses=None):52 Presence.__init__(self, to, type='unavailable')53 54 if statuses is not None:55 for lang, status in statuses.iteritems():56 s = self.addElement('status', content=status)57 if lang:58 s[(NS_XML, "lang")] = lang59 60 class PresenceClientProtocol(XMPPHandler):61 62 def connectionInitialized(self):63 self.xmlstream.addObserver('/presence', self._onPresence)64 65 def _getStatuses(self, presence):66 statuses = {}67 for element in presence.elements():68 if element.name == 'status':69 lang = element.getAttribute((NS_XML, 'lang'))70 text = unicode(element)71 statuses[lang] = text72 return statuses73 74 def _onPresence(self, presence):75 type = presence.getAttribute("type", "available")76 try:77 handler = getattr(self, '_onPresence%s' % (type.capitalize()))78 except AttributeError:79 return80 else:81 handler(presence)82 83 def _onPresenceAvailable(self, presence):84 entity = JID(presence["from"])85 86 show = unicode(presence.show or '')87 if show not in ['away', 'xa', 'chat', 'dnd']:88 show = None89 90 statuses = self._getStatuses(presence)91 92 try:93 priority = int(unicode(presence.priority or '')) or 094 except ValueError:95 priority = 096 97 self.availableReceived(entity, show, statuses, priority)98 99 def _onPresenceUnavailable(self, presence):100 entity = JID(presence["from"])101 102 statuses = self._getStatuses(presence)103 104 self.unavailableReceived(entity, statuses)105 106 def _onPresenceSubscribed(self, presence):107 self.subscribedReceived(JID(presence["from"]))108 109 def _onPresenceUnsubscribed(self, presence):110 self.unsubscribedReceived(JID(presence["from"]))111 112 def _onPresenceSubscribe(self, presence):113 self.subscribeReceived(JID(presence["from"]))114 115 def _onPresenceUnsubscribe(self, presence):116 self.unsubscribeReceived(JID(presence["from"]))117 118 119 def availableReceived(self, entity, show=None, statuses=None, priority=0):120 """121 Available presence was received.122 123 @param entity: entity from which the presence was received.124 @type entity: {JID}125 @param show: detailed presence information. One of C{'away'}, C{'xa'},126 C{'chat'}, C{'dnd'} or C{None}.127 @type show: C{str} or C{NoneType}128 @param statuses: dictionary of natural language descriptions of the129 availability status, keyed by the language130 descriptor. A status without a language131 specified, is keyed with C{None}.132 @type statuses: C{dict}133 @param priority: priority level of the resource.134 @type priority: C{int}135 """136 137 def unavailableReceived(self, entity, statuses=None):138 """139 Unavailable presence was received.140 141 @param entity: entity from which the presence was received.142 @type entity: {JID}143 @param statuses: dictionary of natural language descriptions of the144 availability status, keyed by the language145 descriptor. A status without a language146 specified, is keyed with C{None}.147 @type statuses: C{dict}148 """149 150 def subscribedReceived(self, entity):151 """152 Subscription approval confirmation was received.153 154 @param entity: entity from which the confirmation was received.155 @type entity: {JID}156 """157 158 def unsubscribedReceived(self, entity):159 """160 Unsubscription confirmation was received.161 162 @param entity: entity from which the confirmation was received.163 @type entity: {JID}164 """165 166 def subscribeReceived(self, entity):167 """168 Subscription request was received.169 170 @param entity: entity from which the request was received.171 @type entity: {JID}172 """173 174 def unsubscribeReceived(self, entity):175 """176 Unsubscription request was received.177 178 @param entity: entity from which the request was received.179 @type entity: {JID}180 """181 182 def available(self, entity=None, show=None, statuses=None, priority=0):183 """184 Send available presence.185 186 @param entity: optional entity to which the presence should be sent.187 @type entity: {JID}188 @param show: optional detailed presence information. One of C{'away'},189 C{'xa'}, C{'chat'}, C{'dnd'}.190 @type show: C{str}191 @param statuses: dictionary of natural language descriptions of the192 availability status, keyed by the language193 descriptor. A status without a language194 specified, is keyed with C{None}.195 @type statuses: C{dict}196 @param priority: priority level of the resource.197 @type priority: C{int}198 """199 self.send(AvailablePresence(entity, show, statuses, priority))200 201 def unavailable(self, entity=None, statuses=None):202 """203 Send unavailable presence.204 205 @param entity: optional entity to which the presence should be sent.206 @type entity: {JID}207 @param statuses: dictionary of natural language descriptions of the208 availability status, keyed by the language209 descriptor. A status without a language210 specified, is keyed with C{None}.211 @type statuses: C{dict}212 """213 self.send(UnavailablePresence(entity, statuses))214 215 def subscribe(self, entity):216 """217 Send subscription request218 219 @param entity: entity to subscribe to.220 @type entity: {JID}221 """222 self.send(Presence(to=entity, type='subscribe'))223 224 def unsubscribe(self, entity):225 """226 Send unsubscription request227 228 @param entity: entity to unsubscribe from.229 @type entity: {JID}230 """231 self.send(Presence(to=entity, type='unsubscribe'))232 233 def subscribed(self, entity):234 """235 Send subscription confirmation.236 237 @param entity: entity that subscribed.238 @type entity: {JID}239 """240 self.send(Presence(to=entity, type='subscribed'))241 242 def unsubscribed(self, entity):243 """244 Send unsubscription confirmation.245 246 @param entity: entity that unsubscribed.247 @type entity: {JID}248 """249 self.send(Presence(to=entity, type='unsubscribed'))250 251 252 253 25 class BasePresence(Stanza): 254 26 """ 255 27 Stanza of kind presence. … … 605 377 self.groups = set() 606 378 607 379 380 608 381 class RosterClientProtocol(XMPPHandler): 609 382 """ 610 383 Client side XMPP roster protocol. … … 614 387 ROSTER_SET = "/iq[@type='set']/query[@xmlns='%s']" % NS_ROSTER 615 388 self.xmlstream.addObserver(ROSTER_SET, self._onRosterSet) 616 389 390 617 391 def _parseRosterItem(self, element): 618 392 jid = JID(element['jid']) 619 393 item = RosterItem(jid) … … 628 402 629 403 return item 630 404 405 631 406 def getRoster(self): 632 407 """ 633 408 Retrieve contact list. … … 683 458 item = self._parseRosterItem(iq.query.item) 684 459 self.onRosterSet(item) 685 460 461 686 462 def onRosterSet(self, item): 687 463 """ 688 464 Called when a roster push for a new or update item was received. … … 691 467 @type item: L{RosterItem} 692 468 """ 693 469 470 694 471 def onRosterRemove(self, entity): 695 472 """ 696 473 Called when a roster push for the removal of an item was received. … … 698 475 @param entity: The entity for which the roster item has been removed. 699 476 @type entity: L{JID} 700 477 """ 701 702 class MessageProtocol(XMPPHandler):703 """704 Generic XMPP subprotocol handler for incoming message stanzas.705 """706 707 messageTypes = None, 'normal', 'chat', 'headline', 'groupchat'708 709 def connectionInitialized(self):710 self.xmlstream.addObserver("/message", self._onMessage)711 712 def _onMessage(self, message):713 if message.handled:714 return715 716 messageType = message.getAttribute("type")717 718 if messageType == 'error':719 return720 721 if messageType not in self.messageTypes:722 message["type"] = 'normal'723 724 self.onMessage(message)725 726 def onMessage(self, message):727 """728 Called when a message stanza was received.729 """ -
.py
diff --git a/wokkel/test/test_xmppim.py b/wokkel/test/test_im.py copy from wokkel/test/test_xmppim.py copy to wokkel/test/test_im.py
old new 2 2 # See LICENSE for details 3 3 4 4 """ 5 Tests for L{wokkel. xmppim}.5 Tests for L{wokkel.im}. 6 6 """ 7 7 8 8 from twisted.internet import defer … … 11 11 from twisted.words.protocols.jabber.xmlstream import toResponse 12 12 from twisted.words.xish import domish, utility 13 13 14 from wokkel import xmppim14 from wokkel import im 15 15 from wokkel.generic import ErrorStanza, parseXml 16 16 from wokkel.test.helpers import XmlStreamStub 17 17 18 18 NS_XML = 'http://www.w3.org/XML/1998/namespace' 19 19 NS_ROSTER = 'jabber:iq:roster' 20 20 21 class PresenceClientProtocolTest(unittest.TestCase):22 def setUp(self):23 self.output = []24 self.protocol = xmppim.PresenceClientProtocol()25 self.protocol.parent = self26 27 def send(self, obj):28 self.output.append(obj)29 30 def test_unavailableDirected(self):31 """32 Test sending of directed unavailable presence broadcast.33 """34 35 self.protocol.unavailable(JID('user@example.com'))36 presence = self.output[-1]37 self.assertEquals("presence", presence.name)38 self.assertEquals(None, presence.uri)39 self.assertEquals("user@example.com", presence.getAttribute('to'))40 self.assertEquals("unavailable", presence.getAttribute('type'))41 42 def test_unavailableWithStatus(self):43 """44 Test sending of directed unavailable presence broadcast with status.45 """46 47 self.protocol.unavailable(JID('user@example.com'),48 {None: 'Disconnected'})49 presence = self.output[-1]50 self.assertEquals("presence", presence.name)51 self.assertEquals(None, presence.uri)52 self.assertEquals("user@example.com", presence.getAttribute('to'))53 self.assertEquals("unavailable", presence.getAttribute('type'))54 self.assertEquals("Disconnected", unicode(presence.status))55 56 def test_unavailableBroadcast(self):57 """58 Test sending of unavailable presence broadcast.59 """60 61 self.protocol.unavailable(None)62 presence = self.output[-1]63 self.assertEquals("presence", presence.name)64 self.assertEquals(None, presence.uri)65 self.assertEquals(None, presence.getAttribute('to'))66 self.assertEquals("unavailable", presence.getAttribute('type'))67 68 def test_unavailableBroadcastNoEntityParameter(self):69 """70 Test sending of unavailable presence broadcast by not passing entity.71 """72 73 self.protocol.unavailable()74 presence = self.output[-1]75 self.assertEquals("presence", presence.name)76 self.assertEquals(None, presence.uri)77 self.assertEquals(None, presence.getAttribute('to'))78 self.assertEquals("unavailable", presence.getAttribute('type'))79 80 81 82 21 class AvailabilityPresenceTest(unittest.TestCase): 83 22 84 23 def test_fromElement(self): … … 89 28 </presence> 90 29 """ 91 30 92 presence = xmppim.AvailabilityPresence.fromElement(parseXml(xml))31 presence = im.AvailabilityPresence.fromElement(parseXml(xml)) 93 32 self.assertEquals(JID('user@example.org'), presence.sender) 94 33 self.assertEquals(JID('user@example.com'), presence.recipient) 95 34 self.assertTrue(presence.available) … … 98 37 self.assertEquals(50, presence.priority) 99 38 100 39 40 101 41 class PresenceProtocolTest(unittest.TestCase): 102 42 """ 103 Tests for L{ xmppim.PresenceProtocol}43 Tests for L{im.PresenceProtocol} 104 44 """ 105 45 106 46 def setUp(self): 107 47 self.output = [] 108 self.protocol = xmppim.PresenceProtocol()48 self.protocol = im.PresenceProtocol() 109 49 self.protocol.parent = self 110 50 self.protocol.xmlstream = utility.EventDispatcher() 111 51 self.protocol.connectionInitialized() … … 122 62 xml = """<presence type="error"/>""" 123 63 124 64 def errorReceived(error): 125 xmppim.PresenceProtocol.errorReceived(self.protocol, error)65 im.PresenceProtocol.errorReceived(self.protocol, error) 126 66 try: 127 67 self.assertIsInstance(error, ErrorStanza) 128 68 except: … … 143 83 xml = """<presence/>""" 144 84 145 85 def availableReceived(presence): 146 xmppim.PresenceProtocol.availableReceived(self.protocol, presence)86 im.PresenceProtocol.availableReceived(self.protocol, presence) 147 87 try: 148 self.assertIsInstance(presence, xmppim.AvailabilityPresence)88 self.assertIsInstance(presence, im.AvailabilityPresence) 149 89 except: 150 90 d.errback() 151 91 else: … … 164 104 xml = """<presence type='unavailable'/>""" 165 105 166 106 def unavailableReceived(presence): 167 xmppim.PresenceProtocol.unavailableReceived(self.protocol, presence)107 im.PresenceProtocol.unavailableReceived(self.protocol, presence) 168 108 try: 169 self.assertIsInstance(presence, xmppim.AvailabilityPresence)109 self.assertIsInstance(presence, im.AvailabilityPresence) 170 110 except: 171 111 d.errback() 172 112 else: … … 185 125 xml = """<presence type='subscribe'/>""" 186 126 187 127 def subscribeReceived(presence): 188 xmppim.PresenceProtocol.subscribeReceived(self.protocol, presence)128 im.PresenceProtocol.subscribeReceived(self.protocol, presence) 189 129 try: 190 self.assertIsInstance(presence, xmppim.SubscriptionPresence)130 self.assertIsInstance(presence, im.SubscriptionPresence) 191 131 except: 192 132 d.errback() 193 133 else: … … 206 146 xml = """<presence type='unsubscribe'/>""" 207 147 208 148 def unsubscribeReceived(presence): 209 xmppim.PresenceProtocol.unsubscribeReceived(self.protocol, presence)149 im.PresenceProtocol.unsubscribeReceived(self.protocol, presence) 210 150 try: 211 self.assertIsInstance(presence, xmppim.SubscriptionPresence)151 self.assertIsInstance(presence, im.SubscriptionPresence) 212 152 except: 213 153 d.errback() 214 154 else: … … 227 167 xml = """<presence type='subscribed'/>""" 228 168 229 169 def subscribedReceived(presence): 230 xmppim.PresenceProtocol.subscribedReceived(self.protocol, presence)170 im.PresenceProtocol.subscribedReceived(self.protocol, presence) 231 171 try: 232 self.assertIsInstance(presence, xmppim.SubscriptionPresence)172 self.assertIsInstance(presence, im.SubscriptionPresence) 233 173 except: 234 174 d.errback() 235 175 else: … … 248 188 xml = """<presence type='unsubscribed'/>""" 249 189 250 190 def unsubscribedReceived(presence): 251 xmppim.PresenceProtocol.unsubscribedReceived(self.protocol,191 im.PresenceProtocol.unsubscribedReceived(self.protocol, 252 192 presence) 253 193 try: 254 self.assertIsInstance(presence, xmppim.SubscriptionPresence)194 self.assertIsInstance(presence, im.SubscriptionPresence) 255 195 except: 256 196 d.errback() 257 197 else: … … 270 210 xml = """<presence type='probe'/>""" 271 211 272 212 def probeReceived(presence): 273 xmppim.PresenceProtocol.probeReceived(self.protocol, presence)213 im.PresenceProtocol.probeReceived(self.protocol, presence) 274 214 try: 275 self.assertIsInstance(presence, xmppim.ProbePresence)215 self.assertIsInstance(presence, im.ProbePresence) 276 216 except: 277 217 d.errback() 278 218 else: … … 451 391 452 392 class RosterClientProtocolTest(unittest.TestCase): 453 393 """ 454 Tests for L{ xmppim.RosterClientProtocol}.394 Tests for L{im.RosterClientProtocol}. 455 395 """ 456 396 457 397 def setUp(self): 458 398 self.stub = XmlStreamStub() 459 self.protocol = xmppim.RosterClientProtocol()399 self.protocol = im.RosterClientProtocol() 460 400 self.protocol.xmlstream = self.stub.xmlstream 461 401 self.protocol.connectionInitialized() 462 402
Note: See TracBrowser
for help on using the repository browser.