Debian Codegraph @

doc/examples/echo_server.tac

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