1 | """ |
---|
2 | An XMPP echo server as a standalone server via s2s. |
---|
3 | |
---|
4 | This echo server accepts and initiates server-to-server connections using |
---|
5 | dialback and listens on C{127.0.0.1} with the domain C{localhost}. It will |
---|
6 | accept subscription requests for any potential entity at the domain and |
---|
7 | send back messages sent to it. |
---|
8 | """ |
---|
9 | |
---|
10 | from twisted.application import service, strports |
---|
11 | from twisted.python.compat import unicode |
---|
12 | from twisted.words.protocols.jabber.xmlstream import toResponse |
---|
13 | from wokkel import component, server, xmppim |
---|
14 | |
---|
15 | # Configuration parameters |
---|
16 | |
---|
17 | S2S_PORT = 'tcp:5269:interface=127.0.0.1' |
---|
18 | SECRET = 'secret' |
---|
19 | DOMAIN = 'localhost' |
---|
20 | LOG_TRAFFIC = True |
---|
21 | |
---|
22 | |
---|
23 | # Protocol handlers |
---|
24 | |
---|
25 | class 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 | |
---|
89 | class 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 | |
---|
113 | application = service.Application("Ping Server") |
---|
114 | |
---|
115 | router = component.Router() |
---|
116 | |
---|
117 | serverService = server.ServerService(router, domain=DOMAIN, secret=SECRET) |
---|
118 | serverService.logTraffic = LOG_TRAFFIC |
---|
119 | |
---|
120 | s2sFactory = server.XMPPS2SServerFactory(serverService) |
---|
121 | s2sFactory.logTraffic = LOG_TRAFFIC |
---|
122 | s2sService = strports.service(S2S_PORT, s2sFactory) |
---|
123 | s2sService.setServiceParent(application) |
---|
124 | |
---|
125 | echoComponent = component.InternalComponent(router, DOMAIN) |
---|
126 | echoComponent.logTraffic = LOG_TRAFFIC |
---|
127 | echoComponent.setServiceParent(application) |
---|
128 | |
---|
129 | presenceHandler = PresenceAcceptingHandler() |
---|
130 | presenceHandler.setHandlerParent(echoComponent) |
---|
131 | |
---|
132 | echoHandler = EchoHandler() |
---|
133 | echoHandler.setHandlerParent(echoComponent) |
---|