# HG changeset patch # Parent d0b90e77877631f08248efcc0d40a409e735f713 diff --git a/doc/examples/client_service.tac b/doc/examples/client_service.tac new file mode 100644 --- /dev/null +++ b/doc/examples/client_service.tac @@ -0,0 +1,81 @@ +""" +An XMPP server that accepts client connections with static users/rosters. +""" + +import socket + +from twisted.application import service, strports +from twisted.cred.portal import Portal +from twisted.cred.checkers import InMemoryUsernamePasswordDatabaseDontUse +from twisted.words.protocols.jabber.jid import internJID as JID + +from wokkel import client, xmppim +from wokkel.component import Router +from wokkel.generic import FallbackHandler +from wokkel.ping import PingHandler +from wokkel.xmppim import SessionManager, StaticRealm, User +from wokkel.xmppim import InMemoryRoster, RosterItem + +# Configuration parameters + +C2S_PORT = "tcp:5222:interface=127.0.0.1" +DOMAIN = socket.gethostname() +LOG_TRAFFIC = True + +ALICE = JID('alice@'+DOMAIN) +BOB = JID('bob@'+DOMAIN) +CHARLIE = JID('charlie@'+DOMAIN) + +USERS = { + ALICE: User(ALICE, roster=InMemoryRoster([ + RosterItem(BOB, + subscriptionTo=True, + subscriptionFrom=True, + name='Bob'), + RosterItem(CHARLIE, + subscriptionTo=True, + subscriptionFrom=True, + name='Charlie', + groups=set(['Friends'])), + ])), + BOB: User(BOB, roster=InMemoryRoster([ + RosterItem(ALICE, + subscriptionTo=True, + subscriptionFrom=True, + name='Alice'), + ])), + } + +USER_PASSWORDS = { + 'alice': 'secret', + 'bob': 'secret', + } + + +# Set up the Twisted application + +application = service.Application("Jabber server") + +router = Router() +sessionManager = SessionManager(router, DOMAIN) +sessionManager.setServiceParent(application) + +realm = StaticRealm(DOMAIN, USERS) +realm.server = sessionManager + +checkers = ( + InMemoryUsernamePasswordDatabaseDontUse(**USER_PASSWORDS), + ) +portal = Portal(realm, checkers) +portals = {JID(DOMAIN): portal} + +xmppim.AccountIQHandler(realm).setHandlerParent(sessionManager) +xmppim.AccountMessageHandler(realm).setHandlerParent(sessionManager) +xmppim.AccountPresenceHandler(realm).setHandlerParent(sessionManager) +FallbackHandler().setHandlerParent(sessionManager) +PingHandler().setHandlerParent(sessionManager) + +c2sFactory = client.XMPPC2SServerFactory(portals) +c2sFactory.logTraffic = LOG_TRAFFIC +c2sService = strports.service(C2S_PORT, c2sFactory) +c2sService.setServiceParent(application) diff --git a/doc/examples/client_service_anonymous.tac b/doc/examples/client_service_anonymous.tac new file mode 100644 --- /dev/null +++ b/doc/examples/client_service_anonymous.tac @@ -0,0 +1,51 @@ +""" +An XMPP server that accepts client connections with anonymous authentication. +""" + +import socket + +from twisted.application import service, strports +from twisted.cred.portal import Portal +from twisted.cred.checkers import AllowAnonymousAccess +from twisted.words.protocols.jabber.jid import internJID as JID + +from wokkel import client, xmppim +from wokkel.component import Router +from wokkel.generic import FallbackHandler +from wokkel.ping import PingHandler +from wokkel.xmppim import SessionManager, AnonymousRealm + +# Configuration parameters + +C2S_PORT = "tcp:5222:interface=127.0.0.1" +DOMAIN = socket.gethostname() +LOG_TRAFFIC = True + + +# Set up the Twisted application + +application = service.Application("Jabber server") + +router = Router() +sessionManager = SessionManager(router, DOMAIN) +sessionManager.setServiceParent(application) + +realm = AnonymousRealm(DOMAIN) +realm.server = sessionManager + +checkers = ( + AllowAnonymousAccess(), + ) +portal = Portal(realm, checkers) +portals = {JID(DOMAIN): portal} + +xmppim.AccountIQHandler(realm).setHandlerParent(sessionManager) +xmppim.AccountMessageHandler(realm).setHandlerParent(sessionManager) +xmppim.AccountPresenceHandler(realm).setHandlerParent(sessionManager) +FallbackHandler().setHandlerParent(sessionManager) +PingHandler().setHandlerParent(sessionManager) + +c2sFactory = client.XMPPC2SServerFactory(portals) +c2sFactory.logTraffic = LOG_TRAFFIC +c2sService = strports.service(C2S_PORT, c2sFactory) +c2sService.setServiceParent(application)