File: echo_server.tac

package info (click to toggle)
wokkel 18.0.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,352 kB
  • sloc: python: 11,753; makefile: 6
file content (133 lines) | stat: -rw-r--r-- 3,745 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""
An XMPP echo server as a standalone server via s2s.

This echo server accepts and initiates server-to-server connections using
dialback and listens on C{127.0.0.1} with the domain C{localhost}. It will
accept subscription requests for any potential entity at the domain and
send back messages sent to it.
"""

from twisted.application import service, strports
from twisted.python.compat import unicode
from twisted.words.protocols.jabber.xmlstream import toResponse
from wokkel import component, server, xmppim

# Configuration parameters

S2S_PORT = 'tcp:5269:interface=127.0.0.1'
SECRET = 'secret'
DOMAIN = 'localhost'
LOG_TRAFFIC = True


# Protocol handlers

class PresenceAcceptingHandler(xmppim.PresenceProtocol):
    """
    Presence accepting XMPP subprotocol handler.

    This handler blindly accepts incoming presence subscription requests,
    confirms unsubscription requests and responds to presence probes.

    Note that this handler does not remember any contacts, so it will not
    send presence when starting.
    """
    def subscribedReceived(self, presence):
        """
        Subscription approval confirmation was received.

        This is just a confirmation. Don't respond.
        """
        pass


    def unsubscribedReceived(self, presence):
        """
        Unsubscription confirmation was received.

        This is just a confirmation. Don't respond.
        """
        pass


    def subscribeReceived(self, presence):
        """
        Subscription request was received.

        Always grant permission to see our presence.
        """
        self.subscribed(recipient=presence.sender,
                        sender=presence.recipient)
        self.available(recipient=presence.sender,
                       status=u"I'm here",
                       sender=presence.recipient)


    def unsubscribeReceived(self, presence):
        """
        Unsubscription request was received.

        Always confirm unsubscription requests.
        """
        self.unsubscribed(recipient=presence.sender,
                          sender=presence.recipient)



    def probeReceived(self, presence):
        """
        A presence probe was received.

        Always send available presence to whoever is asking.
        """
        self.available(recipient=presence.sender,
                       status=u"I'm here",
                       sender=presence.recipient)



class EchoHandler(xmppim.MessageProtocol):
    """
    Message echoing XMPP subprotocol handler.
    """

    def onMessage(self, message):
        """
        Called when a message stanza was received.
        """

        # Ignore error messages
        if message.getAttribute('type') == 'error':
            return

        # Echo incoming messages, if they have a body.
        if message.body and unicode(message.body):
            response = toResponse(message, message.getAttribute('type'))
            response.addElement('body', content=unicode(message.body))
            self.send(response)



# Set up the Twisted application

application = service.Application("Ping Server")

router = component.Router()

serverService = server.ServerService(router, domain=DOMAIN, secret=SECRET)
serverService.logTraffic = LOG_TRAFFIC

s2sFactory = server.XMPPS2SServerFactory(serverService)
s2sFactory.logTraffic = LOG_TRAFFIC
s2sService = strports.service(S2S_PORT, s2sFactory)
s2sService.setServiceParent(application)

echoComponent = component.InternalComponent(router, DOMAIN)
echoComponent.logTraffic = LOG_TRAFFIC
echoComponent.setServiceParent(application)

presenceHandler = PresenceAcceptingHandler()
presenceHandler.setHandlerParent(echoComponent)

echoHandler = EchoHandler()
echoHandler.setHandlerParent(echoComponent)