Ignore:
Timestamp:
Aug 4, 2011, 9:38:24 PM (11 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
wokkel-muc-client-support-24
Message:

Use descendants from wokkel.generic.Stanza in messages and presence.

  • Use wokkel.xmppim.AvailabilityPresence as a basis for presence stanzas.
  • Split wokkel.xmppim.PresenceProtocol and Base MUCClient on BasePresenceProtocol
  • Add new wokkel.xmppim.Message as the basis for message stanzas.
  • `receivedGroupChat
  • Change order of parameters for invite, as reason is optional.
  • Use datetimes everywhere where timestamps are needed.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • wokkel/xmppim.py

    r96 r141  
    297297
    298298
     299    def __get_status(self):
     300        if None in self.statuses:
     301            return self.statuses[None]
     302        elif self.statuses:
     303            for status in self.status.itervalues():
     304                return status
     305        else:
     306            return None
     307
     308    status = property(__get_status)
     309
     310
    299311    def _childParser_show(self, element):
    300312        show = unicode(element)
     
    368380
    369381
    370 class PresenceProtocol(XMPPHandler):
    371     """
    372     XMPP Presence protocol.
     382class BasePresenceProtocol(XMPPHandler):
     383    """
     384    XMPP Presence base protocol handler.
     385
     386    This class is the base for protocol handlers that receive presence
     387    stanzas. Listening to all incoming presence stanzas, it extracts the
     388    stanza's type and looks up a matching stanza parser and calls the
     389    associated method. The method's name is the type + C{Received}. E.g.
     390    C{availableReceived}. See L{PresenceProtocol} for a complete example.
    373391
    374392    @cvar presenceTypeParserMap: Maps presence stanza types to their respective
     
    376394    @type presenceTypeParserMap: C{dict}
    377395    """
     396
     397    presenceTypeParserMap = {}
     398
     399    def connectionInitialized(self):
     400        self.xmlstream.addObserver("/presence", self._onPresence)
     401
     402
     403
     404    def _onPresence(self, element):
     405        """
     406        Called when a presence stanza has been received.
     407        """
     408        stanza = Stanza.fromElement(element)
     409
     410        presenceType = stanza.stanzaType or 'available'
     411
     412        try:
     413            parser = self.presenceTypeParserMap[presenceType]
     414        except KeyError:
     415            return
     416
     417        presence = parser.fromElement(element)
     418
     419        try:
     420            handler = getattr(self, '%sReceived' % presenceType)
     421        except AttributeError:
     422            return
     423        else:
     424            handler(presence)
     425
     426
     427
     428class PresenceProtocol(BasePresenceProtocol):
    378429
    379430    presenceTypeParserMap = {
     
    386437                'unsubscribed': SubscriptionPresence,
    387438                'probe': ProbePresence,
    388         }
    389 
    390     def connectionInitialized(self):
    391         self.xmlstream.addObserver("/presence", self._onPresence)
    392 
    393 
    394     def _onPresence(self, element):
    395         stanza = Stanza.fromElement(element)
    396 
    397         presenceType = stanza.stanzaType or 'available'
    398 
    399         try:
    400             parser = self.presenceTypeParserMap[presenceType]
    401         except KeyError:
    402             return
    403 
    404         presence = parser.fromElement(element)
    405 
    406         try:
    407             handler = getattr(self, '%sReceived' % presenceType)
    408         except AttributeError:
    409             return
    410         else:
    411             handler(presence)
     439                }
    412440
    413441
     
    700728        """
    701729
     730
     731
     732class Message(Stanza):
     733    """
     734    A message stanza.
     735    """
     736
     737    stanzaKind = 'message'
     738
     739    childParsers = {
     740            (None, 'body'): '_childParser_body',
     741            (None, 'subject'): '_childParser_subject',
     742            }
     743
     744    def __init__(self, recipient=None, sender=None, body=None, subject=None):
     745        Stanza.__init__(self, recipient, sender)
     746        self.body = body
     747        self.subject = subject
     748
     749
     750    def _childParser_body(self, element):
     751        self.body = unicode(element)
     752
     753
     754    def _childParser_subject(self, element):
     755        self.subject = unicode(element)
     756
     757
     758    def toElement(self):
     759        element = Stanza.toElement(self)
     760
     761        if self.body:
     762            element.addElement('body', content=self.body)
     763        if self.subject:
     764            element.addElement('subject', content=self.subject)
     765
     766        return element
     767
     768
     769
    702770class MessageProtocol(XMPPHandler):
    703771    """
Note: See TracChangeset for help on using the changeset viewer.