Ignore:
Timestamp:
Jul 17, 2009, 3:19:40 PM (13 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Convert:
svn:b33ecbfc-034c-dc11-8662-000475d9059e/trunk@186
Message:

Implement a better presence protocol handler PresenceProtocol?.

This handler is not client-only, like PresenceClientProtocol?, and accepts
sender arguments to all methods for outgoing presence. The *Received
methods now expect a single argument that is a parsed representation of the
presence stanza.

Also included is a simple XMPP server example that connects via the
server-to-server protocol, accepting presence requests and echoing incoming
messages for all potential addresses at a domain.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wokkel/generic.py

    r57 r68  
    11# -*- test-case-name: wokkel.test.test_generic -*-
    22#
    3 # Copyright (c) 2003-2008 Ralph Meijer
     3# Copyright (c) 2003-2009 Ralph Meijer
    44# See LICENSE for details.
    55
     
    1111
    1212from twisted.internet import defer, protocol
     13from twisted.python import reflect
    1314from twisted.words.protocols.jabber import error, jid, xmlstream
    1415from twisted.words.protocols.jabber.xmlstream import toResponse
     
    174175    """
    175176
    176     sender = None
    177     recipient = None
     177    stanzaKind = None
     178    stanzaID = None
    178179    stanzaType = None
     180
     181    def __init__(self, recipient=None, sender=None):
     182        self.recipient = recipient
     183        self.sender = sender
     184
    179185
    180186    @classmethod
     
    186192
    187193    def parseElement(self, element):
    188         self.sender = jid.internJID(element['from'])
    189194        if element.hasAttribute('from'):
    190195            self.sender = jid.internJID(element['from'])
     
    192197            self.recipient = jid.internJID(element['to'])
    193198        self.stanzaType = element.getAttribute('type')
     199        self.stanzaID = element.getAttribute('id')
     200
     201        # Save element
     202        stripNamespace(element)
     203        self.element = element
     204
     205        # accumulate all childHandlers in the class hierarchy of Class
     206        handlers = {}
     207        reflect.accumulateClassDict(self.__class__, 'childParsers', handlers)
     208
     209        for child in element.elements():
     210            try:
     211                handler = handlers[child.uri, child.name]
     212            except KeyError:
     213                pass
     214            else:
     215                getattr(self, handler)(child)
     216
     217
     218    def toElement(self):
     219        element = domish.Element((None, self.stanzaKind))
     220        if self.sender is not None:
     221            element['from'] = self.sender.full()
     222        if self.recipient is not None:
     223            element['to'] = self.recipient.full()
     224        if self.stanzaType:
     225            element['type'] = self.stanzaType
     226        if self.stanzaID:
     227            element['id'] = self.stanzaID
     228        return element
     229
     230
     231
     232class ErrorStanza(Stanza):
     233
     234    def parseElement(self, element):
     235        Stanza.parseElement(self, element)
     236        self.exception = error.exceptionFromStanza(element)
    194237
    195238
Note: See TracChangeset for help on using the changeset viewer.