handlers.py 43.8 KB
Newer Older
mathieui's avatar
mathieui committed
1
2
3
4
5
6
7
8
9
10
"""
XMPP-related handlers for the Core class
"""

import logging
log = logging.getLogger(__name__)

import curses
import ssl
import time
11
from hashlib import sha1, sha512
mathieui's avatar
mathieui committed
12
13
14
from gettext import gettext as _

from sleekxmpp import InvalidJID
15
from sleekxmpp.stanza import Message
mathieui's avatar
mathieui committed
16
17
from sleekxmpp.xmlstream.stanzabase import StanzaBase

18
import bookmark
mathieui's avatar
mathieui committed
19
import common
20
import fixes
mathieui's avatar
mathieui committed
21
22
23
import pep
import tabs
import windows
24
import xhtml
mathieui's avatar
mathieui committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import multiuserchat as muc
from common import safeJID
from config import config
from contact import Resource
from logger import logger
from roster import roster
from text_buffer import CorrectionError
from theming import dump_tuple, get_theme

from . commands import dumb_callback

def on_session_start_features(self, _):
    """
    Enable carbons & blocking on session start if wanted and possible
    """
    def callback(iq):
        if not iq:
            return
        features = iq['disco_info']['features']
44
        rostertab = self.get_tab_by_name('Roster', tabs.RosterInfoTab)
mathieui's avatar
mathieui committed
45
46
47
48
49
50
51
52
53
        rostertab.check_blocking(features)
        if (config.get('enable_carbons', True) and
                'urn:xmpp:carbons:2' in features):
            self.xmpp.plugin['xep_0280'].enable()
            self.xmpp.add_event_handler('carbon_received', self.on_carbon_received)
            self.xmpp.add_event_handler('carbon_sent', self.on_carbon_sent)
    features = self.xmpp.plugin['xep_0030'].get_info(jid=self.xmpp.boundjid.domain, callback=callback, block=False)

def on_carbon_received(self, message):
54
55
56
    """
    Carbon <received/> received
    """
mathieui's avatar
mathieui committed
57
    recv = message['carbon_received']
58
59
    if (recv['from'].bare not in roster or
            roster[recv['from'].bare].subscription == 'none'):
mathieui's avatar
mathieui committed
60
        try:
61
62
63
64
            if fixes.has_identity(self.xmpp, recv['from'].server,
                                  identity='conference'):
                log.debug('%s has category conference, ignoring carbon',
                          recv['from'].server)
mathieui's avatar
mathieui committed
65
66
                return
        except:
67
68
            log.debug('Traceback when getting the identity of a server:',
                      exc_info=True)
mathieui's avatar
mathieui committed
69
    recv['to'] = self.xmpp.boundjid.full
70
71
    if recv['receipt']:
        return self.on_receipt(recv)
mathieui's avatar
mathieui committed
72
73
74
    self.on_normal_message(recv)

def on_carbon_sent(self, message):
75
76
77
    """
    Carbon <sent/> received
    """
mathieui's avatar
mathieui committed
78
    sent = message['carbon_sent']
79
80
    if (sent['to'].bare not in roster or
            roster[sent['to'].bare].subscription == 'none'):
mathieui's avatar
mathieui committed
81
        try:
82
83
84
85
            if fixes.has_identity(self.xmpp, sent['to'].server,
                                  identity='conference'):
                log.debug('%s has category conference, ignoring carbon',
                          sent['to'].server)
mathieui's avatar
mathieui committed
86
87
                return
        except:
88
89
            log.debug('Traceback when getting the identity of a server:',
                      exc_info=True)
mathieui's avatar
mathieui committed
90
91
92
93
94
    sent['from'] = self.xmpp.boundjid.full
    self.on_normal_message(sent)

### Invites ###

95
96
97
98
def on_groupchat_invitation(self, message):
    """
    Mediated invitation received
    """
mathieui's avatar
mathieui committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
    jid = message['from']
    if jid.bare in self.pending_invites:
        return
    # there are 2 'x' tags in the messages, making message['x'] useless
    invite = StanzaBase(self.xmpp, xml=message.find('{http://jabber.org/protocol/muc#user}x/{http://jabber.org/protocol/muc#user}invite'))
    inviter = invite['from']
    reason = invite['reason']
    password = invite['password']
    msg = "You are invited to the room %s by %s" % (jid.full, inviter.full)
    if reason:
        msg += "because: %s" % reason
    if password:
        msg += ". The password is \"%s\"." % password
    self.information(msg, 'Info')
    if 'invite' in config.get('beep_on', 'invite').split():
        curses.beep()
    logger.log_roster_change(inviter.full, 'invited you to %s' % jid.full)
    self.pending_invites[jid.bare] = inviter.full

def on_groupchat_decline(self, decline):
119
    "Mediated invitation declined; skip for now"
mathieui's avatar
mathieui committed
120
121
    pass

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
def on_groupchat_direct_invitation(self, message):
    """
    Direct invitation received
    """
    room = safeJID(message['groupchat_invite']['jid'])
    if room.bare in self.pending_invites:
        return

    inviter = message['from']
    reason = message['groupchat_invite']['reason']
    password = message['groupchat_invite']['password']
    continue_ = message['groupchat_invite']['continue']
    msg = "You are invited to the room %s by %s" % (room, inviter.full)

    if password:
        msg += ' (password: "%s")' % password
    if continue_:
        msg += '\nto continue the discussion'
    if reason:
        msg += "\nreason: %s" % reason

    self.information(msg, 'Info')
    if 'invite' in config.get('beep_on', 'invite').split():
        curses.beep()

    self.pending_invites[room.bare] = inviter.full
    logger.log_roster_change(inviter.full, 'invited you to %s' % room.bare)

mathieui's avatar
mathieui committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
### "classic" messages ###

def on_message(self, message):
    """
    When receiving private message from a muc OR a normal message
    (from one of our contacts)
    """
    if message.find('{http://jabber.org/protocol/muc#user}x/{http://jabber.org/protocol/muc#user}invite') != None:
        return
    if message['type'] == 'groupchat':
        return
    # Differentiate both type of messages, and call the appropriate handler.
    jid_from = message['from']
    for tab in self.get_tabs(tabs.MucTab):
164
        if tab.name == jid_from.bare:
mathieui's avatar
mathieui committed
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
            if message['type'] == 'error':
                return self.room_error(message, jid_from)
            else:
                return self.on_groupchat_private_message(message)
    return self.on_normal_message(message)

def on_normal_message(self, message):
    """
    When receiving "normal" messages (from someone in our roster)
    """
    if message['type'] == 'error':
        return self.information(self.get_error_message(message, deprecated=True), 'Error')
    elif message['type'] == 'headline' and message['body']:
        return self.information('%s says: %s' % (message['from'], message['body']), 'Headline')

    use_xhtml = config.get('enable_xhtml_im', True)
    body = xhtml.get_body_from_message_stanza(message, use_xhtml=use_xhtml)
    if not body:
        return

    remote_nick = ''
    # normal message, we are the recipient
    if message['to'].bare == self.xmpp.boundjid.bare:
        conv_jid = message['from']
        jid = conv_jid
        color = get_theme().COLOR_REMOTE_USER
        # check for a name
        if conv_jid.bare in roster:
            remote_nick = roster[conv_jid.bare].name
        # check for a received nick
        if not remote_nick and config.get('enable_user_nick', True):
            if message.xml.find('{http://jabber.org/protocol/nick}nick') is not None:
                remote_nick = message['nick']['nick']
198
199
        if not remote_nick:
            remote_nick = conv_jid.user
200
201
            if not remote_nick:
                remote_nick = conv_jid.full
mathieui's avatar
mathieui committed
202
203
204
205
206
207
208
209
210
211
212
213
214
        own = False
    # we wrote the message (happens with carbons)
    elif message['from'].bare == self.xmpp.boundjid.bare:
        conv_jid = message['to']
        jid = self.xmpp.boundjid
        color = get_theme().COLOR_OWN_NICK
        remote_nick = self.own_nick
        own = True
    # we are not part of that message, drop it
    else:
        return

    conversation = self.get_conversation_by_jid(conv_jid, create=True)
215
    if isinstance(conversation, tabs.DynamicConversationTab) and conv_jid.resource:
mathieui's avatar
mathieui committed
216
217
        conversation.lock(conv_jid.resource)

218
219
220
    if not own and not conversation.nick:
        conversation.nick = remote_nick
    elif not own: # keep a fixed nick during the whole conversation
mathieui's avatar
mathieui committed
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
        remote_nick = conversation.nick

    self.events.trigger('conversation_msg', message, conversation)
    if not message['body']:
        return
    body = xhtml.get_body_from_message_stanza(message, use_xhtml=use_xhtml)
    delayed, date = common.find_delayed_tag(message)

    def try_modify():
        replaced_id = message['replace']['id']
        if replaced_id and (config.get_by_tabname('group_corrections',
            True, conv_jid.bare)):
            try:
                conversation.modify_message(body, replaced_id, message['id'], jid=jid,
                        nickname=remote_nick)
                return True
            except CorrectionError:
                log.debug('Unable to correct a message', exc_info=True)
        return False

    if not try_modify():
        conversation.add_message(body, date,
                nickname=remote_nick,
                nick_color=color,
                history=delayed,
                identifier=message['id'],
                jid=jid,
                typ=1)

    if conversation.remote_wants_chatstates is None and not delayed:
        if message['chat_state']:
            conversation.remote_wants_chatstates = True
        else:
            conversation.remote_wants_chatstates = False
    if 'private' in config.get('beep_on', 'highlight private').split():
        if not config.get_by_tabname('disable_beep', False, conv_jid.bare, False):
            curses.beep()
    if self.current_tab() is not conversation:
        conversation.state = 'private'
        self.refresh_tab_win()
    else:
        self.refresh_window()

def on_nick_received(self, message):
    """
    Called when a pep notification for an user nickname
    is received
    """
    contact = roster[message['from'].bare]
    if not contact:
        return
    item = message['pubsub_event']['items']['item']
    if item.xml.find('{http://jabber.org/protocol/nick}nick'):
        contact.name = item['nick']['nick']
    else:
mathieui's avatar
mathieui committed
276
        contact.name = ''
mathieui's avatar
mathieui committed
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396

def on_gaming_event(self, message):
    """
    Called when a pep notification for user gaming
    is received
    """
    contact = roster[message['from'].bare]
    if not contact:
        return
    item = message['pubsub_event']['items']['item']
    old_gaming = contact.gaming
    if item.xml.find('{urn:xmpp:gaming:0}gaming'):
        item = item['gaming']
        # only name and server_address are used for now
        contact.gaming = {
                'character_name': item['character_name'],
                'character_profile': item['character_profile'],
                'name': item['name'],
                'level': item['level'],
                'uri': item['uri'],
                'server_name': item['server_name'],
                'server_address': item['server_address'],
            }
    else:
        contact.gaming = {}

    if contact.gaming:
        logger.log_roster_change(contact.bare_jid, 'is playing %s' % (common.format_gaming_string(contact.gaming)))

    if old_gaming != contact.gaming and config.get_by_tabname('display_gaming_notifications', False, contact.bare_jid):
        if contact.gaming:
            self.information('%s is playing %s' % (contact.bare_jid, common.format_gaming_string(contact.gaming)), 'Gaming')
        else:
            self.information(contact.bare_jid + ' stopped playing.', 'Gaming')

def on_mood_event(self, message):
    """
    Called when a pep notification for an user mood
    is received.
    """
    contact = roster[message['from'].bare]
    if not contact:
        return
    roster.modified()
    item = message['pubsub_event']['items']['item']
    old_mood = contact.mood
    if item.xml.find('{http://jabber.org/protocol/mood}mood'):
        mood = item['mood']['value']
        if mood:
            mood = pep.MOODS.get(mood, mood)
            text = item['mood']['text']
            if text:
                mood = '%s (%s)' % (mood, text)
            contact.mood = mood
        else:
            contact.mood = ''
    else:
        contact.mood = ''

    if contact.mood:
        logger.log_roster_change(contact.bare_jid, 'has now the mood: %s' % contact.mood)

    if old_mood != contact.mood and config.get_by_tabname('display_mood_notifications', False, contact.bare_jid):
        if contact.mood:
            self.information('Mood from '+ contact.bare_jid + ': ' + contact.mood, 'Mood')
        else:
            self.information(contact.bare_jid + ' stopped having his/her mood.', 'Mood')

def on_activity_event(self, message):
    """
    Called when a pep notification for an user activity
    is received.
    """
    contact = roster[message['from'].bare]
    if not contact:
        return
    roster.modified()
    item = message['pubsub_event']['items']['item']
    old_activity = contact.activity
    if item.xml.find('{http://jabber.org/protocol/activity}activity'):
        try:
            activity = item['activity']['value']
        except ValueError:
            return
        if activity[0]:
            general = pep.ACTIVITIES.get(activity[0])
            s = general['category']
            if activity[1]:
                s = s + '/' + general.get(activity[1], 'other')
            text = item['activity']['text']
            if text:
                s = '%s (%s)' % (s, text)
            contact.activity = s
        else:
            contact.activity = ''
    else:
        contact.activity = ''

    if contact.activity:
        logger.log_roster_change(contact.bare_jid, 'has now the activity %s' % contact.activity)

    if old_activity != contact.activity and config.get_by_tabname('display_activity_notifications', False, contact.bare_jid):
        if contact.activity:
            self.information('Activity from '+ contact.bare_jid + ': ' + contact.activity, 'Activity')
        else:
            self.information(contact.bare_jid + ' stopped doing his/her activity.', 'Activity')

def on_tune_event(self, message):
    """
    Called when a pep notification for an user tune
    is received
    """
    contact = roster[message['from'].bare]
    if not contact:
        return
    roster.modified()
    item = message['pubsub_event']['items']['item']
    old_tune = contact.tune
    if item.xml.find('{http://jabber.org/protocol/tune}tune'):
        item = item['tune']
mathieui's avatar
mathieui committed
397
        contact.tune = {
mathieui's avatar
mathieui committed
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
                'artist': item['artist'],
                'length': item['length'],
                'rating': item['rating'],
                'source': item['source'],
                'title': item['title'],
                'track': item['track'],
                'uri': item['uri']
            }
    else:
        contact.tune = {}

    if contact.tune:
        logger.log_roster_change(message['from'].bare, 'is now listening to %s' % common.format_tune_string(contact.tune))

    if old_tune != contact.tune and config.get_by_tabname('display_tune_notifications', False, contact.bare_jid):
        if contact.tune:
            self.information(
                    'Tune from '+ message['from'].bare + ': ' + common.format_tune_string(contact.tune),
                    'Tune')
        else:
            self.information(contact.bare_jid + ' stopped listening to music.', 'Tune')

def on_groupchat_message(self, message):
    """
    Triggered whenever a message is received from a multi-user chat room.
    """
    if message['subject']:
        return
    room_from = message['from'].bare

    if message['type'] == 'error': # Check if it's an error
        return self.room_error(message, room_from)

    tab = self.get_tab_by_name(room_from, tabs.MucTab)
    if not tab:
        self.information(_("message received for a non-existing room: %s") % (room_from))
        muc.leave_groupchat(self.xmpp, room_from, self.own_nick, msg='')
        return

    nick_from = message['mucnick']
    user = tab.get_user_by_name(nick_from)
    if user and user in tab.ignores:
        return

    self.events.trigger('muc_msg', message, tab)
    use_xhtml = config.get('enable_xhtml_im', True)
    body = xhtml.get_body_from_message_stanza(message, use_xhtml=use_xhtml)
    if not body:
        return

    old_state = tab.state
    delayed, date = common.find_delayed_tag(message)
    replaced_id = message['replace']['id']
    replaced = False
    if replaced_id is not '' and (config.get_by_tabname(
        'group_corrections', True, message['from'].bare)):
        try:
            if tab.modify_message(body, replaced_id, message['id'], time=date,
                    nickname=nick_from, user=user):
                self.events.trigger('highlight', message, tab)
            replaced = True
        except CorrectionError:
            log.debug('Unable to correct a message', exc_info=True)
    if not replaced and tab.add_message(body, date, nick_from, history=delayed, identifier=message['id'], jid=message['from'], typ=1):
        self.events.trigger('highlight', message, tab)

    if message['from'].resource == tab.own_nick:
        tab.last_sent_message = message

    if tab is self.current_tab():
        tab.text_win.refresh()
        tab.info_header.refresh(tab, tab.text_win)
        tab.input.refresh()
        self.doupdate()
    elif tab.state != old_state:
        self.refresh_tab_win()
        current = self.current_tab()
        if hasattr(current, 'input') and current.input:
            current.input.refresh()
        self.doupdate()

    if 'message' in config.get('beep_on', 'highlight private').split():
480
481
        if (not config.get_by_tabname('disable_beep', False, room_from, False)
                and self.own_nick != message['from'].resource):
mathieui's avatar
mathieui committed
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
            curses.beep()

def on_muc_own_nickchange(self, muc):
    "We changed our nick in a MUC"
    for tab in self.get_tabs(tabs.PrivateTab):
        if tab.parent_muc == muc:
            tab.own_nick = muc.own_nick

def on_groupchat_private_message(self, message):
    """
    We received a Private Message (from someone in a Muc)
    """
    jid = message['from']
    nick_from = jid.resource
    if not nick_from:
        return self.on_groupchat_message(message)

    room_from = jid.bare
    use_xhtml = config.get('enable_xhtml_im', True)
    body = xhtml.get_body_from_message_stanza(message, use_xhtml=use_xhtml)
    tab = self.get_tab_by_name(jid.full, tabs.PrivateTab) # get the tab with the private conversation
    ignore = config.get_by_tabname('ignore_private', False, room_from)
    if not tab: # It's the first message we receive: create the tab
        if body and not ignore:
            tab = self.open_private_window(room_from, nick_from, False)
    if ignore:
        self.events.trigger('ignored_private', message, tab)
        msg = config.get_by_tabname('private_auto_response', None, room_from)
        if msg and body:
            self.xmpp.send_message(mto=jid.full, mbody=msg, mtype='chat')
        return
    self.events.trigger('private_msg', message, tab)
    body = xhtml.get_body_from_message_stanza(message, use_xhtml=use_xhtml)
    if not body or not tab:
        return
    replaced_id = message['replace']['id']
    replaced = False
    user = tab.parent_muc.get_user_by_name(nick_from)
    if replaced_id is not '' and (config.get_by_tabname(
        'group_corrections', True, room_from)):
        try:
            tab.modify_message(body, replaced_id, message['id'], user=user, jid=message['from'],
                    nickname=nick_from)
            replaced = True
        except CorrectionError:
            log.debug('Unable to correct a message', exc_info=True)
    if not replaced:
        tab.add_message(body, time=None, nickname=nick_from,
                        forced_user=user,
                        identifier=message['id'],
                        jid=message['from'],
                        typ=1)

    if tab.remote_wants_chatstates is None:
        if message['chat_state']:
            tab.remote_wants_chatstates = True
        else:
            tab.remote_wants_chatstates = False
    if 'private' in config.get('beep_on', 'highlight private').split():
        if not config.get_by_tabname('disable_beep', False, jid.full, False):
            curses.beep()
    if tab is self.current_tab():
        self.refresh_window()
    else:
        tab.state = 'private'
        self.refresh_tab_win()

### Chatstates ###

def on_chatstate_active(self, message):
    self.on_chatstate(message, "active")

def on_chatstate_inactive(self, message):
    self.on_chatstate(message, "inactive")

def on_chatstate_composing(self, message):
    self.on_chatstate(message, "composing")

def on_chatstate_paused(self, message):
    self.on_chatstate(message, "paused")

def on_chatstate_gone(self, message):
    self.on_chatstate(message, "gone")

def on_chatstate(self, message, state):
    if message['type'] == 'chat':
        if not self.on_chatstate_normal_conversation(message, state):
            tab = self.get_tab_by_name(message['from'].full, tabs.PrivateTab)
            if not tab:
                return
            self.on_chatstate_private_conversation(message, state)
    elif message['type'] == 'groupchat':
        self.on_chatstate_groupchat_conversation(message, state)

def on_chatstate_normal_conversation(self, message, state):
    tab = self.get_conversation_by_jid(message['from'], False)
    if not tab:
        return False
    tab.remote_wants_chatstates = True
    self.events.trigger('normal_chatstate', message, tab)
    tab.chatstate = state
    if state == 'gone' and isinstance(tab, tabs.DynamicConversationTab):
        tab.unlock()
    if tab == self.current_tab():
        tab.refresh_info_header()
        self.doupdate()
588
    else:
589
        _composing_tab_state(tab, state)
590
        self.refresh_tab_win()
mathieui's avatar
mathieui committed
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
    return True

def on_chatstate_private_conversation(self, message, state):
    """
    Chatstate received in a private conversation from a MUC
    """
    tab = self.get_tab_by_name(message['from'].full, tabs.PrivateTab)
    if not tab:
        return
    tab.remote_wants_chatstates = True
    self.events.trigger('private_chatstate', message, tab)
    tab.chatstate = state
    if tab == self.current_tab():
        tab.refresh_info_header()
        self.doupdate()
606
    else:
607
        _composing_tab_state(tab, state)
608
        self.refresh_tab_win()
mathieui's avatar
mathieui committed
609
610
611
612
613
614
615
616
617
618
619
620
621
    return True

def on_chatstate_groupchat_conversation(self, message, state):
    """
    Chatstate received in a MUC
    """
    nick = message['mucnick']
    room_from = message.getMucroom()
    tab = self.get_tab_by_name(room_from, tabs.MucTab)
    if tab and tab.get_user_by_name(nick):
        self.events.trigger('muc_chatstate', message, tab)
        tab.get_user_by_name(nick).chatstate = state
    if tab == self.current_tab():
622
623
        if not self.size.tab_degrade_x:
            tab.user_win.refresh(tab.users)
mathieui's avatar
mathieui committed
624
625
        tab.input.refresh()
        self.doupdate()
626
    else:
627
        _composing_tab_state(tab, state)
628
        self.refresh_tab_win()
mathieui's avatar
mathieui committed
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817

### subscription-related handlers ###

def on_roster_update(self, iq):
    """
    The roster was received.
    """
    for item in iq['roster']:
        try:
            jid = item['jid']
        except InvalidJID:
            jid = item._get_attr('jid', '')
            log.error('Invalid JID: "%s"', jid, exc_info=True)
        else:
            if item['subscription'] == 'remove':
                del roster[jid]
            else:
                roster.update_contact_groups(jid)
    if isinstance(self.current_tab(), tabs.RosterInfoTab):
        self.refresh_window()

def on_subscription_request(self, presence):
    """subscribe received"""
    jid = presence['from'].bare
    contact = roster[jid]
    if contact and contact.subscription in ('from', 'both'):
        return
    elif contact and contact.subscription == 'to':
        self.xmpp.sendPresence(pto=jid, ptype='subscribed')
        self.xmpp.sendPresence(pto=jid)
    else:
        if not contact:
            contact = roster.get_and_set(jid)
        roster.update_contact_groups(contact)
        contact.pending_in = True
        self.information('%s wants to subscribe to your presence' % jid, 'Roster')
        self.get_tab_by_number(0).state = 'highlight'
        roster.modified()
    if isinstance(self.current_tab(), tabs.RosterInfoTab):
        self.refresh_window()

def on_subscription_authorized(self, presence):
    """subscribed received"""
    jid = presence['from'].bare
    contact = roster[jid]
    if contact.subscription not in ('both', 'from'):
        self.information('%s accepted your contact proposal' % jid, 'Roster')
    if contact.pending_out:
        contact.pending_out = False

    roster.modified()

    if isinstance(self.current_tab(), tabs.RosterInfoTab):
        self.refresh_window()

def on_subscription_remove(self, presence):
    """unsubscribe received"""
    jid = presence['from'].bare
    contact = roster[jid]
    if not contact:
        return
    roster.modified()
    self.information('%s does not want to receive your status anymore.' % jid, 'Roster')
    self.get_tab_by_number(0).state = 'highlight'
    if isinstance(self.current_tab(), tabs.RosterInfoTab):
        self.refresh_window()

def on_subscription_removed(self, presence):
    """unsubscribed received"""
    jid = presence['from'].bare
    contact = roster[jid]
    if not contact:
        return
    roster.modified()
    if contact.pending_out:
        self.information('%s rejected your contact proposal' % jid, 'Roster')
        contact.pending_out = False
    else:
        self.information('%s does not want you to receive his/her/its status anymore.'%jid, 'Roster')
    self.get_tab_by_number(0).state = 'highlight'
    if isinstance(self.current_tab(), tabs.RosterInfoTab):
        self.refresh_window()

### Presence-related handlers ###

def on_presence(self, presence):
    if presence.match('presence/muc') or presence.xml.find('{http://jabber.org/protocol/muc#user}x'):
        return
    jid = presence['from']
    contact = roster[jid.bare]
    tab = self.get_conversation_by_jid(jid, create=False)
    if isinstance(tab, tabs.DynamicConversationTab):
        if tab.get_dest_jid() != jid.full:
            tab.unlock(from_=jid.full)
        elif presence['type'] == 'unavailable':
            tab.unlock()
    if contact is None:
        return
    roster.modified()
    contact.error = None
    self.events.trigger('normal_presence', presence, contact[jid.full])
    tab = self.get_conversation_by_jid(jid, create=False)
    if isinstance(self.current_tab(), tabs.RosterInfoTab):
        self.refresh_window()
    elif self.current_tab() == tab:
        tab.refresh()
        self.doupdate()

def on_presence_error(self, presence):
    jid = presence['from']
    contact = roster[jid.bare]
    if not contact:
        return
    roster.modified()
    contact.error = presence['error']['type'] + ': ' + presence['error']['condition']
    # reset chat states status on presence error
    tab = self.get_tab_by_name(jid.full, tabs.ConversationTab)
    if tab:
        tab.remote_wants_chatstates = None

def on_got_offline(self, presence):
    """
    A JID got offline
    """
    if presence.match('presence/muc') or presence.xml.find('{http://jabber.org/protocol/muc#user}x'):
        return
    jid = presence['from']
    if not logger.log_roster_change(jid.bare, 'got offline'):
        self.information(_('Unable to write in the log file'), 'Error')
    # If a resource got offline, display the message in the conversation with this
    # precise resource.
    if jid.resource:
        self.add_information_message_to_conversation_tab(jid.full, '\x195}%s is \x191}offline' % (jid.full))
    self.add_information_message_to_conversation_tab(jid.bare, '\x195}%s is \x191}offline' % (jid.bare))
    self.information('\x193}%s \x195}is \x191}offline' % (jid.bare), 'Roster')
    roster.modified()
    if isinstance(self.current_tab(), tabs.RosterInfoTab):
        self.refresh_window()

def on_got_online(self, presence):
    """
    A JID got online
    """
    if presence.match('presence/muc') or presence.xml.find('{http://jabber.org/protocol/muc#user}x'):
        return
    jid = presence['from']
    contact = roster[jid.bare]
    if contact is None:
        # Todo, handle presence coming from contacts not in roster
        return
    roster.modified()
    if not logger.log_roster_change(jid.bare, 'got online'):
        self.information(_('Unable to write in the log file'), 'Error')
    resource = Resource(jid.full, {
        'priority': presence.get_priority() or 0,
        'status': presence['status'],
        'show': presence['show'],
        })
    self.events.trigger('normal_presence', presence, resource)
    self.add_information_message_to_conversation_tab(jid.full, '\x195}%s is \x194}online' % (jid.full))
    if time.time() - self.connection_time > 10:
        # We do not display messages if we recently logged in
        if presence['status']:
            self.information("\x193}%s \x195}is \x194}online\x195} (\x19o%s\x195})" % (safeJID(resource.jid).bare, presence['status']), "Roster")
        else:
            self.information("\x193}%s \x195}is \x194}online\x195}" % safeJID(resource.jid).bare, "Roster")
        self.add_information_message_to_conversation_tab(jid.bare, '\x195}%s is \x194}online' % (jid.bare))
    if isinstance(self.current_tab(), tabs.RosterInfoTab):
        self.refresh_window()

def on_groupchat_presence(self, presence):
    """
    Triggered whenever a presence stanza is received from a user in a multi-user chat room.
    Display the presence on the room window and update the
    presence information of the concerned user
    """
    from_room = presence['from'].bare
    tab = self.get_tab_by_name(from_room, tabs.MucTab)
    if tab:
        self.events.trigger('muc_presence', presence, tab)
        tab.handle_presence(presence)


### Connection-related handlers ###

def on_failed_connection(self):
    """
    We cannot contact the remote server
    """
818
    self.information(_("Connection to remote server failed"), _('Error'))
mathieui's avatar
mathieui committed
819
820
821
822
823
824
825
826

def on_disconnected(self, event):
    """
    When we are disconnected from remote server
    """
    roster.modified()
    for tab in self.get_tabs(tabs.MucTab):
        tab.disconnect()
827
    self.information(_("Disconnected from server."), _('Error'))
mathieui's avatar
mathieui committed
828
829
830
831
832

def on_failed_auth(self, event):
    """
    Authentication failed
    """
833
834
    self.information(_("Authentication failed (bad credentials?)."),
                     _('Error'))
mathieui's avatar
mathieui committed
835
836
837
838
839

def on_no_auth(self, event):
    """
    Authentication failed (no mech)
    """
840
841
    self.information(_("Authentication failed, no login method available."),
                     _('Error'))
mathieui's avatar
mathieui committed
842
843
844
845
846

def on_connected(self, event):
    """
    Remote host responded, but we are not yet authenticated
    """
847
    self.information(_("Connected to server."), 'Info')
mathieui's avatar
mathieui committed
848
849
850
851
852
853
854
855

def on_session_start(self, event):
    """
    Called when we are connected and authenticated
    """
    self.connection_time = time.time()
    if not self.plugins_autoloaded: # Do not reload plugins on reconnection
        self.autoload_plugins()
856
857
    self.information(_("Authentication success."), 'Info')
    self.information(_("Your JID is %s") % self.xmpp.boundjid.full, 'Info')
mathieui's avatar
mathieui committed
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
    if not self.xmpp.anon:
        # request the roster
        self.xmpp.get_roster()
        # send initial presence
        if config.get('send_initial_presence', True):
            pres = self.xmpp.make_presence()
            pres['show'] = self.status.show
            pres['status'] = self.status.message
            self.events.trigger('send_normal_presence', pres)
            pres.send()
    bookmark.get_local()
    if not self.xmpp.anon and config.get('use_remote_bookmarks', True):
        bookmark.get_remote(self.xmpp)
    for bm in bookmark.bookmarks:
        tab = self.get_tab_by_name(bm.jid, tabs.MucTab)
        nick = bm.nick if bm.nick else self.own_nick
        if not tab:
            self.open_new_room(bm.jid, nick, False)
        self.initial_joins.append(bm.jid)
        histo_length = config.get('muc_history_length', 20)
        if histo_length == -1:
mathieui's avatar
mathieui committed
879
            histo_length = None
mathieui's avatar
mathieui committed
880
        if histo_length is not None:
mathieui's avatar
mathieui committed
881
            histo_length = str(histo_length)
mathieui's avatar
mathieui committed
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
        # do not join rooms that do not have autojoin
        # but display them anyway
        if bm.autojoin:
            muc.join_groupchat(self, bm.jid, nick,
                    passwd=bm.password,
                    maxhistory=histo_length,
                    status=self.status.message,
                    show=self.status.show)

    if config.get('enable_user_nick', True):
        self.xmpp.plugin['xep_0172'].publish_nick(nick=self.own_nick, callback=dumb_callback, block=False)
    self.xmpp.plugin['xep_0115'].update_caps()

### Other handlers ###

def on_status_codes(self, message):
    """
    Handle groupchat messages with status codes.
    Those are received when a room configuration change occurs.
    """
    room_from = message['from']
    tab = self.get_tab_by_name(room_from, tabs.MucTab)
    status_codes = set([s.attrib['code'] for s in message.findall('{%s}x/{%s}status' % (tabs.NS_MUC_USER, tabs.NS_MUC_USER))])
    if '101' in status_codes:
        self.information('Your affiliation in the room %s changed' % room_from, 'Info')
    elif tab and status_codes:
        show_unavailable = '102' in status_codes
        hide_unavailable = '103' in status_codes
        non_priv = '104' in status_codes
        logging_on = '170' in status_codes
mathieui's avatar
mathieui committed
912
        logging_off = '171' in status_codes
mathieui's avatar
mathieui committed
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
        non_anon = '172' in status_codes
        semi_anon = '173' in status_codes
        full_anon = '174' in status_codes
        modif = False
        if show_unavailable or hide_unavailable or non_priv or logging_off\
                or non_anon or semi_anon or full_anon:
            tab.add_message('\x19%(info_col)s}Info: A configuration change not privacy-related occured.' %
                    {'info_col': dump_tuple(get_theme().COLOR_INFORMATION_TEXT)},
                    typ=2)
            modif = True
        if show_unavailable:
            tab.add_message('\x19%(info_col)s}Info: The unavailable members are now shown.' %
                    {'info_col': dump_tuple(get_theme().COLOR_INFORMATION_TEXT)},
                    typ=2)
        elif hide_unavailable:
            tab.add_message('\x19%(info_col)s}Info: The unavailable members are now hidden.' %
                    {'info_col': dump_tuple(get_theme().COLOR_INFORMATION_TEXT)},
                    typ=2)
        if non_anon:
            tab.add_message('\x191}Warning:\x19%(info_col)s} The room is now not anonymous. (public JID)' %
                    {'info_col': dump_tuple(get_theme().COLOR_INFORMATION_TEXT)},
                    typ=2)
        elif semi_anon:
            tab.add_message('\x19%(info_col)s}Info: The room is now semi-anonymous. (moderators-only JID)' %
                    {'info_col': dump_tuple(get_theme().COLOR_INFORMATION_TEXT)},
                    typ=2)
        elif full_anon:
            tab.add_message('\x19%(info_col)s}Info: The room is now fully anonymous.' %
                    {'info_col': dump_tuple(get_theme().COLOR_INFORMATION_TEXT)},
                    typ=2)
        if logging_on:
            tab.add_message('\x191}Warning: \x19%(info_col)s}This room is publicly logged' %
                    {'info_col': dump_tuple(get_theme().COLOR_INFORMATION_TEXT)},
                    typ=2)
        elif logging_off:
            tab.add_message('\x19%(info_col)s}Info: This room is not logged anymore.' %
                    {'info_col': dump_tuple(get_theme().COLOR_INFORMATION_TEXT)},
                    typ=2)
        if modif:
            self.refresh_window()

def on_groupchat_subject(self, message):
    """
    Triggered when the topic is changed.
    """
    nick_from = message['mucnick']
    room_from = message.getMucroom()
    tab = self.get_tab_by_name(room_from, tabs.MucTab)
    subject = message['subject']
    if not subject or not tab:
        return
    if nick_from:
        tab.add_message(_("\x19%(info_col)s}%(nick)s set the subject to: %(subject)s") %
                {'info_col': dump_tuple(get_theme().COLOR_INFORMATION_TEXT), 'nick':nick_from, 'subject':subject},
                time=None,
                typ=2)
    else:
        tab.add_message(_("\x19%(info_col)s}The subject is: %(subject)s") %
                {'subject':subject, 'info_col': dump_tuple(get_theme().COLOR_INFORMATION_TEXT)},
                time=None,
                typ=2)
    tab.topic = subject
975
    tab.topic_from = nick_from
mathieui's avatar
mathieui committed
976
977
978
    if self.get_tab_by_name(room_from, tabs.MucTab) is self.current_tab():
        self.refresh_window()

979
980
981
982
983
984
985
986
987
def on_receipt(self, message):
    """
    When a delivery receipt is received (XEP-0184)
    """
    jid = message['from']
    msg_id = message['receipt']
    if not msg_id:
        return

988
989
    conversation = self.get_tab_by_name(jid, tabs.ChatTab)
    conversation = conversation or self.get_tab_by_name(jid.bare, tabs.ChatTab)
990
991
992
993
994
    if not conversation:
        return

    conversation.ack_message(msg_id)

mathieui's avatar
mathieui committed
995
996
997
998
999
1000
def on_data_form(self, message):
    """
    When a data form is received
    """
    self.information('%s' % message)

For faster browsing, not all history is shown. View entire blame