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