source: doc/examples/echo_server.tac @ 68:2c6d1f895583

Last change on this file since 68:2c6d1f895583 was 68:2c6d1f895583, checked in by Ralph Meijer <ralphm@…>, 13 years ago

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 size: 3.5 KB
Line 
1"""
2An XMPP echo server as a standalone server via s2s.
3
4This ping responder accepts and initiates server-to-server connections using
5dialback and listens on C{127.0.0.1} with the domain C{localhost}.
6"""
7
8from twisted.application import service, strports
9from twisted.words.protocols.jabber.xmlstream import toResponse
10from wokkel import component, server, xmppim
11
12# Configuration parameters
13
14S2S_PORT = 'tcp:5269:interface=127.0.0.1'
15SECRET = 'secret'
16DOMAIN = 'localhost'
17LOG_TRAFFIC = True
18
19
20# Protocol handlers
21
22class PresenceAcceptingHandler(xmppim.PresenceProtocol):
23    """
24    Presence accepting XMPP subprotocol handler.
25
26    This handler blindly accepts incoming presence subscription requests,
27    confirms unsubscription requests and responds to presence probes.
28
29    Note that this handler does not remember any contacts, so it will not
30    send presence when starting.
31    """
32    def subscribedReceived(self, presence):
33        """
34        Subscription approval confirmation was received.
35
36        This is just a confirmation. Don't respond.
37        """
38        pass
39
40
41    def unsubscribedReceived(self, presence):
42        """
43        Unsubscription confirmation was received.
44
45        This is just a confirmation. Don't respond.
46        """
47        pass
48
49
50    def subscribeReceived(self, presence):
51        """
52        Subscription request was received.
53
54        Always grant permission to see our presence.
55        """
56        self.subscribed(recipient=presence.sender,
57                        sender=presence.recipient)
58        self.available(recipient=presence.sender,
59                       status=u"I'm here",
60                       sender=presence.recipient)
61
62
63    def unsubscribeReceived(self, presence):
64        """
65        Unsubscription request was received.
66
67        Always confirm unsubscription requests.
68        """
69        self.unsubscribed(recipient=presence.sender,
70                          sender=presence.recipient)
71
72
73
74    def probeReceived(self, presence):
75        """
76        A presence probe was received.
77
78        Always send available presence to whoever is asking.
79        """
80        self.available(recipient=presence.sender,
81                       status=u"I'm here",
82                       sender=presence.recipient)
83
84
85
86class EchoHandler(xmppim.MessageProtocol):
87    """
88    Message echoing XMPP subprotocol handler.
89    """
90
91    def onMessage(self, message):
92        """
93        Called when a message stanza was received.
94        """
95
96        # Ignore error messages
97        if message.getAttribute('type') == 'error':
98            return
99
100        # Echo incoming messages, if they have a body.
101        if message.body and unicode(message.body):
102            response = toResponse(message, message.getAttribute('type'))
103            response.addElement('body', content=unicode(message.body))
104            self.send(response)
105
106
107
108# Set up the Twisted application
109
110application = service.Application("Ping Server")
111
112router = component.Router()
113
114serverService = server.ServerService(router, domain=DOMAIN, secret=SECRET)
115serverService.logTraffic = LOG_TRAFFIC
116
117s2sFactory = server.XMPPS2SServerFactory(serverService)
118s2sFactory.logTraffic = LOG_TRAFFIC
119s2sService = strports.service(S2S_PORT, s2sFactory)
120s2sService.setServiceParent(application)
121
122echoComponent = component.InternalComponent(router, DOMAIN)
123echoComponent.logTraffic = LOG_TRAFFIC
124echoComponent.setServiceParent(application)
125
126presenceHandler = PresenceAcceptingHandler()
127presenceHandler.setHandlerParent(echoComponent)
128
129echoHandler = EchoHandler()
130echoHandler.setHandlerParent(echoComponent)
Note: See TracBrowser for help on using the repository browser.