source: doc/examples/muc_client.tac

Last change on this file was 162:8bffd58d7b6d, checked in by Ralph Meijer <ralphm@…>, 6 years ago

Expose locked state of a room on join response with room created status.

When the presence in response to a join includes the room created status code
(201), the client should configure the room before it can be used. To enable
this, the locked state of the room is now exposed through the Room.locked
attribute.

Also, update example MUC client to deal with newly created, locked rooms.

File size: 3.1 KB
Line 
1"""
2An XMPP MUC client.
3
4This XMPP Client logs in as C{user@example.org}, joins the room
5C{'room@muc.example.org'} using the nick C{'greeter'} and responds to
6greetings addressed to it. If another occupant writes C{'greeter: hello'}, it
7will return the favor.
8
9This example uses L{MUCClient} instead of the protocol-only
10L{MUCClientProtocol<wokkel.muc.MUCClientProtocol>} so that it can hook into
11its C{receivedGroupChat}. L{MUCClient} implements C{groupChatReceived} and
12makes a distinction between messages setting the subject, messages that a
13part of the room's conversation history, and 'live' messages. In this case,
14we only want to inspect and respond to the 'live' messages.
15"""
16
17from twisted.application import service
18from twisted.python import log
19from twisted.words.protocols.jabber.jid import JID
20from wokkel.client import XMPPClient
21from wokkel.muc import MUCClient
22
23# Configuration parameters
24
25THIS_JID = JID('user@example.org')
26ROOM_JID = JID('room@muc.example.org')
27NICK = u'greeter'
28SECRET = 'secret'
29LOG_TRAFFIC = True
30
31class MUCGreeter(MUCClient):
32    """
33    I join a room and respond to greetings.
34    """
35
36    def __init__(self, roomJID, nick):
37        MUCClient.__init__(self)
38        self.roomJID = roomJID
39        self.nick = nick
40
41
42    def connectionInitialized(self):
43        """
44        Once authorized, join the room.
45
46        If the join action causes a new room to be created, the room will be
47        locked until configured. Here we will just accept the default
48        configuration by submitting an empty form using L{configure}, which
49        usually results in a public non-persistent room.
50
51        Alternatively, you would use L{getConfiguration} to retrieve the
52        configuration form, and then submit the filled in form with the
53        required settings using L{configure}, possibly after presenting it to
54        an end-user.
55        """
56        def joinedRoom(room):
57            if room.locked:
58                # Just accept the default configuration.
59                return self.configure(room.roomJID, {})
60
61        MUCClient.connectionInitialized(self)
62
63        d = self.join(self.roomJID, self.nick)
64        d.addCallback(joinedRoom)
65        d.addCallback(lambda _: log.msg("Joined room"))
66        d.addErrback(log.err, "Join failed")
67
68
69    def receivedGroupChat(self, room, user, message):
70        """
71        Called when a groupchat message was received.
72
73        Check if the message was addressed to my nick and if it said
74        C{'hello'}. Respond by sending a message to the room addressed to
75        the sender.
76        """
77        if message.body.startswith(self.nick + u":"):
78            nick, text = message.body.split(':', 1)
79            text = text.strip().lower()
80            if text == u'hello':
81                body = u"%s: Hi!" % (user.nick)
82                self.groupChat(self.roomJID, body)
83
84
85# Set up the Twisted application
86
87application = service.Application("MUC Client")
88
89client = XMPPClient(THIS_JID, SECRET)
90client.logTraffic = LOG_TRAFFIC
91client.setServiceParent(application)
92
93mucHandler = MUCGreeter(ROOM_JID, NICK)
94mucHandler.setHandlerParent(client)
Note: See TracBrowser for help on using the repository browser.