File: componentservertap.py

package info (click to toggle)
python-tx-xmpp 0.10.1.post1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,468 kB
  • sloc: python: 12,915; makefile: 3
file content (77 lines) | stat: -rw-r--r-- 2,230 bytes parent folder | download
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
# Copyright (c) Ralph Meijer.
# See LICENSE for details.

"""
XMPP Component Service.

This provides an XMPP server that accepts External Components connections
and accepts and initiates server-to-server connections for the specified
domain(s).
"""

from __future__ import division, absolute_import

from twisted.application import service, strports
from twisted.python import usage
from twisted.words.protocols.jabber import component
from . import server


class Options(usage.Options):

    optParameters = [
        (
            "component-port",
            None,
            "tcp:5347:interface=127.0.0.1",
            "Port components connect to",
        ),
        ("component-secret", None, "secret", "Secret components use to connect"),
        ("server-port", None, "tcp:5269", "Port other servers connect to"),
        ("server-secret", None, None, "Shared secret for dialback verification"),
    ]

    optFlags = [
        ("verbose", "v", "Log traffic"),
    ]

    def __init__(self):
        usage.Options.__init__(self)
        self["domains"] = set()

    def opt_domain(self, domain):
        """
        Domain to accept server connections for. Repeat for more domains.
        """
        self["domains"].add(domain)

    def postOptions(self):
        if not self["domains"]:
            raise usage.UsageError("Need at least one domain")


def makeService(config):
    s = service.MultiService()

    router = component.Router()

    # Set up the XMPP server service

    serverService = server.ServerService(router, secret=config["server-secret"])
    serverService.domains = config["domains"]
    serverService.logTraffic = config["verbose"]

    # Hook up XMPP server-to-server service
    s2sFactory = server.XMPPS2SServerFactory(serverService)
    s2sFactory.logTraffic = config["verbose"]
    s2sService = strports.service(config["server-port"], s2sFactory)
    s2sService.setServiceParent(s)

    # Hook up XMPP external server-side component service
    cFactory = component.XMPPComponentServerFactory(router, config["component-secret"])

    cFactory.logTraffic = config["verbose"]
    cServer = strports.service(config["component-port"], cFactory)
    cServer.setServiceParent(s)

    return s