summaryrefslogtreecommitdiff
path: root/src/multiuserchat.py
blob: be4d2277718aeedd2d47877f5b62eb439f2e5149 (plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# -*- coding: utf-8 -*-

# Copyright 2009, 2010 Erwan Briand
# Copyright 2010, Florent Le Coz <louizatakk@fedoraproject.org>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation version 3 of the License.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Implementation of the XEP-0045: Multi-User Chat.

from xmpp import NS_MUC_ADMIN, NS_MUC
from xmpp.protocol import Presence, Iq, Message, JID
import xmpp
import common
import threading

from time import (altzone, daylight, gmtime, localtime, mktime, strftime,
        time as time_time, timezone, tzname)

from handler import Handler
from config import config

def get_stripped_jid(jid):
    """Return the stripped JID (bare representation)"""
    if isinstance(jid, basestring):
        jid = JID(jid)
    return jid.getStripped()

def is_jid(jid):
    """Return True if this is a valid JID"""
    if JID(jid).getNode() != '':
        return True

class VcardSender(threading.Thread):
    """
    avatar sending is really slow (don't know why...)
    use a thread to send it...
    """
    def __init__(self, connection):
        threading.Thread.__init__(self)
        self.connection = connection
        self.handler = Handler()

    def run(self):
        self.send_vcard()

    def send_vcard(self):
        """
        Method stolen from Gajim (thanks)
        ## Copyright (C) 2006 Dimitur Kirov <dkirov AT gmail.com>
        ##                    Junglecow J <junglecow AT gmail.com>
        ## Copyright (C) 2006-2007 Tomasz Melcer <liori AT exroot.org>
        ##                         Travis Shirk <travis AT pobox.com>
        ##                         Nikos Kouremenos <kourem AT gmail.com>
        ## Copyright (C) 2006-2008 Yann Leboulanger <asterix AT lagaule.org>
        ## Copyright (C) 2007 Julien Pivotto <roidelapluie AT gmail.com>
        ## Copyright (C) 2007-2008 Brendan Taylor <whateley AT gmail.com>
        ##                         Jean-Marie Traissard <jim AT lapin.org>
        ##                         Stephan Erb <steve-e AT h3c.de>
        ## Copyright (C) 2008 Jonathan Schleifer <js-gajim AT webkeks.org>
        (one of these people coded this method, probably)
        """
        if not self.connection:
            return
        vcard = {
            "FN":config.get('full_name', ''),
            "URL":config.get('website', ''),
            "EMAIL":{
                "USERID":config.get('email', '')
                },
            "DESC":config.get('comment', 'A proud Poezio user')
            }
        photo_file_path = config.get('photo', '../data/poezio_80.png')
        (image, mime_type, sha1) = common.get_base64_from_file(photo_file_path)
        if image:
            vcard['PHOTO'] = {"TYPE":mime_type,"BINVAL":image}
        iq = xmpp.Iq(typ = 'set')
        iq2 = iq.setTag(xmpp.NS_VCARD + ' vCard')
        for i in vcard:
            if i == 'jid':
                continue
            if isinstance(vcard[i], dict):
                iq3 = iq2.addChild(i)
                for j in vcard[i]:
                    iq3.addChild(j).setData(vcard[i][j])
            elif isinstance(vcard[i], list):
                for j in vcard[i]:
                    iq3 = iq2.addChild(i)
                    for k in j:
                        iq3.addChild(k).setData(j[k])
            else:
                iq2.addChild(i).setData(vcard[i])
        # id_ = self.connect.getAnId()
        # iq.setID(id_)
        self.connection.send(iq)
        iq = xmpp.Iq(typ = 'set')
        iq2 = iq.setTag(xmpp.NS_VCARD_UPDATE)
        iq2.addChild('PHOTO').setData(sha1)
        self.connection.send(iq)

class MultiUserChat(object):
    def __init__(self, connection):
        self.connection = connection
        self.vcard_sender = VcardSender(self.connection)

        self.rooms = []
        self.rn = {}

        self.own_jid = None

        self.handler = Handler()
        self.handler.connect('join-room', self.join_room)
        self.handler.connect('on-connected', self.on_connected)
        self.handler.connect('send-version', self.send_version)
        self.handler.connect('send-time', self.send_time)

    def on_connected(self, jid):
        self.own_jid = jid
        rooms = config.get('rooms', '')
        if rooms == '' or type(rooms) != str:
            return
        else:
            rooms = rooms.split(':')
        for room in rooms:
            args = room.split('/')
            if args[0] == '':
                return
            roomname = args[0]
            if len(args) == 2:
                nick = args[1]
            else:
                nick = config.get('default_nick', 'poezio')
            self.handler.emit('join-room', room=roomname, nick=nick)
        if config.get('jid', '') == '': # Don't send the vcard if we're not anonymous
            self.vcard_sender.start()   # because the user ALREADY has one on the server

    def send_message(self, room, message):
        mes = Message(to=room)
        mes.setBody(message)
        mes.setType('groupchat')
        self.connection.send(mes)

    def send_private_message(self, user_jid, message):
        from common import debug
        mes = Message(to=user_jid)
        mes.setBody(message)
        mes.setType('chat')
        self.connection.send(mes)

    def join_room(self, room, nick, password=None):
        """Join a new room"""
        pres = Presence(to='%s/%s' % (room, nick))
        pres.setFrom('%s'%self.own_jid)
        if not password:
            pres.addChild(name='x', namespace=NS_MUC)
        else:
            item = pres.addChild(name='x', namespace=NS_MUC)
            passwd = item.addChild(name='password')
            passwd.setData(password)
        self.connection.send(pres)

    def quit_room(self, room, nick, msg=None):
        """Quit a room"""
        if room is None and nick is None:
            self.on_disconnect()
            return

        pres = Presence(to='%s/%s' % (room, nick), typ='unavailable')
        if msg:
            pres.setStatus(msg)
        self.connection.send(pres)

    def disconnect(self, rooms, msg):
        """
        """
        for room in rooms:
            if room.jid is None and room.joined:
                pres = Presence(to='%s' % room.name,
                                typ='unavailable')
                pres.setStatus(msg)
                self.connection.send(pres)

    def on_disconnect(self):
        """Called at disconnection"""
        for room in self.rooms:
            pres = Presence(to='%s/%s' % (room, self.rn[room]),
                            typ='unavailable')
            self.connection.send(pres)

    def on_iq(self, iq):
        """Receive a MUC iq notification"""
        from_ = iq.getFrom().__str__()

        if get_stripped_jid(from_) in self.rooms:
            children = iq.getChildren()
            for child in children:
                if child.getName() == 'error':
                    code = int(child.getAttr('code'))
                    msg = None

                    echildren = child.getChildren()
                    for echild in echildren:
                        if echild.getName() == 'text':
                            msg = echild.getData()

                    self.handler.emit('on-muc-error',
                                      room=from_,
                                      code=code,
                                      msg=msg)



    def on_presence(self, presence):
        """Receive a MUC presence notification"""
        from_ = presence.getFrom().__str__()

        if get_stripped_jid(from_) in self.rooms:
            self.handler.emit('on-muc-presence-changed',
                               jid=from_.encode('utf-8'),
                               priority=presence.getPriority(),
                               show=presence.getShow(),
                               status=presence.getStatus(),
                               stanza=presence
                              )

    def on_message(self, message):
        """Receive a MUC message notification"""
        from_ = message.getFrom().__str__().encode('utf-8')

        if get_stripped_jid(from_) in self.rooms:
            body_ = message.getBody()
            type_ = message.getType()
            subj_ = message.getSubject()
            self.handler.emit('on-muc-message-received',
                              jid=from_, msg=body_, subject=subj_,
                              typ=type_, stanza=message)

    def eject_user(self, room, action, nick, reason):
        """Eject an user from a room"""
        iq = Iq(typ='set', to=room)
        query = iq.addChild('query', namespace=NS_MUC_ADMIN)
        item = query.addChild('item')

        if action == 'kick':
            item.setAttr('role', 'none')
            if is_jid(nick):
                item.setAttr('jid', nick)
            else:
                item.setAttr('nick', nick)
        elif action == 'ban':
            item.setAttr('affiliation', 'outcast')
            item.setAttr('jid', nick)

        if reason is not None:
            rson = item.addChild('reason')
            rson.setData(reason)

        self.connection.send(iq)

    def change_role(self, room, nick, role):
        """Change the role of an user"""
        iq = Iq(typ='set', to=room)
        query = iq.addChild('query', namespace=NS_MUC_ADMIN)
        item = query.addChild('item')
        item.setAttr('nick', nick)
        item.setAttr('role', role)

        self.connection.send(iq)

    def change_aff(self, room, jid, aff):
        """Change the affiliation of an user"""
        iq = Iq(typ='set', to=room)
        query = iq.addChild('query', namespace=NS_MUC_ADMIN)
        item = query.addChild('item')
        item.setAttr('jid', jid)
        item.setAttr('affiliation', aff)

        self.connection.send(iq)

    def change_subject(self, room, subject):
        """Change the subject of a room"""
        message = Message(typ='groupchat', to=room)
        subj = message.addChild('subject')
        subj.setData(subject)

        self.connection.send(message)

    def change_nick(self, room, nick):
        """Change the nickname"""
        pres = Presence(to='%s/%s' % (room, nick))
        self.connection.send(pres)

    def change_show(self, room, nick, show, status):
        pres = Presence(to='%s/%s' % (room, nick))
        pres.setShow(show)
        if status:
            pres.setStatus(status)
        self.connection.send(pres)

    def send_version(self, iq_obj):
        """
        from gajim and modified
        """
        iq_obj = iq_obj.buildReply('result')
        qp = iq_obj.getTag('query')
        if config.get('send_poezio_info', 'true') == 'true':
            qp.setTagData('name', 'Poezio')
            qp.setTagData('version', '0.6 trunk')
        else:
            qp.setTagData('name', 'Unknown')
            qp.setTagData('version', 'Unknown')
        if config.get('send_os_info', 'true') == 'true':
            qp.setTagData('os', common.get_os_info())
        else:
            qp.setTagData('os', 'Unknown')
        self.connection.send(iq_obj)
        raise xmpp.protocol.NodeProcessed

    def send_time(self, iq_obj):
        """
        from gajim
        """
        iq_obj = iq_obj.buildReply('result')
        qp = iq_obj.setTag('time',
                           namespace="urn:xmpp:time")
        if config.get('send_time', 'true') == 'true':
            qp.setTagData('utc', strftime('%Y-%m-%dT%H:%M:%SZ', gmtime()))
            isdst = localtime().tm_isdst
            zone = -(timezone, altzone)[isdst] / 60
            tzo = (zone / 60, abs(zone % 60))
            qp.setTagData('tzo', '%+03d:%02d' % (tzo))
            self.connection.send(iq_obj)
            raise xmpp.protocol.NodeProcessed