source: doc/examples/echo_server.tac @ 251:554782284e08

Last change on this file since 251:554782284e08 was 207:3e9d00891fc4, checked in by Ralph Meijer <ralphm@…>, 4 years ago

imported patch py3-examples.patch

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