muctab.py 78.5 KB
Newer Older
1 2 3 4 5 6 7 8 9
"""
Module for the MucTab

A MucTab is a tab for multi-user chats as defined in XEP-0045.

It keeps track of many things such as part/joins, maintains an
user list, and updates private tabs when necessary.
"""

10
import bisect
11
import curses
12
import logging
13 14
import os
import random
15
import re
16
import functools
mathieui's avatar
mathieui committed
17
from datetime import datetime
18
from typing import Dict, Callable, List, Optional, Union, Set
19

20
from slixmpp import JID
21
from poezio.tabs import ChatTab, Tab, SHOW_NAME
22

23 24 25 26 27 28 29 30
from poezio import common
from poezio import fixes
from poezio import multiuserchat as muc
from poezio import timed_events
from poezio import windows
from poezio import xhtml
from poezio.common import safeJID
from poezio.config import config
31
from poezio.core.structs import Command
32 33 34 35 36
from poezio.decorators import refresh_wrapper, command_args_parser
from poezio.logger import logger
from poezio.roster import roster
from poezio.theming import get_theme, dump_tuple
from poezio.user import User
37
from poezio.core.structs import Completion, Status
38

39 40
log = logging.getLogger(__name__)

41
NS_MUC_USER = 'http://jabber.org/protocol/muc#user'
42
STATUS_XPATH = '{%s}x/{%s}status' % (NS_MUC_USER, NS_MUC_USER)
43

44 45
COMPARE_USERS_LAST_TALKED = lambda x: x.last_talked

46 47 48 49 50 51 52

class MucTab(ChatTab):
    """
    The tab containing a multi-user-chat room.
    It contains an userlist, an input, a topic, an information and a chat zone
    """
    message_type = 'groupchat'
53 54
    plugin_commands = {}  # type: Dict[str, Command]
    plugin_keys = {}  # type: Dict[str, Callable]
55
    additional_information = {}  # type: Dict[str, Callable[[str], str]]
56
    lagged = False
57

58 59
    def __init__(self, core, jid, nick, password=None):
        ChatTab.__init__(self, core, jid)
60 61 62
        self.joined = False
        self._state = 'disconnected'
        # our nick in the MUC
63
        self.own_nick = nick
64
        # self User object
65
        self.own_user = None  # type: Optional[User]
66
        self.name = jid
67
        self.password = password
68
        # buffered presences
69
        self.presence_buffer = []
70
        # userlist
71
        self.users = []  # type: List[User]
72
        # private conversations
73
        self.privates = []  # type: List[Tab]
74
        self.topic = ''
75
        self.topic_from = ''
76 77 78
        # Self ping event, so we can cancel it when we leave the room
        self.self_ping_event = None
        # UI stuff
79 80 81 82 83 84 85
        self.topic_win = windows.Topic()
        self.text_win = windows.TextWin()
        self._text_buffer.add_window(self.text_win)
        self.v_separator = windows.VerticalSeparator()
        self.user_win = windows.UserList()
        self.info_header = windows.MucInfoWin()
        self.input = windows.MessageInput()
86
        # List of ignored users
87
        self.ignores = []  # type: List[User]
88
        # keys
89 90
        self.register_keys()
        self.update_keys()
91
        # commands
92
        self.register_commands()
93
        self.update_commands()
94
        self.resize()
95 96 97

    @property
    def general_jid(self):
98
        return self.name
99

100
    def check_send_chat_state(self) -> bool:
mathieui's avatar
mathieui committed
101 102 103
        "If we should send a chat state"
        return self.joined

104
    @property
105
    def last_connection(self) -> Optional[datetime]:
106 107 108 109 110
        last_message = self._text_buffer.last_message
        if last_message:
            return last_message.time
        return None

111 112 113 114 115 116 117 118 119 120 121 122 123 124
    @staticmethod
    def add_information_element(plugin_name: str, callback: Callable[[str], str]) -> None:
        """
        Lets a plugin add its own information to the MucInfoWin
        """
        MucTab.additional_information[plugin_name] = callback

    @staticmethod
    def remove_information_element(plugin_name: str) -> None:
        """
        Lets a plugin add its own information to the MucInfoWin
        """
        del MucTab.additional_information[plugin_name]

125 126
    def cancel_config(self, form):
        """
Kim Alvefur's avatar
Kim Alvefur committed
127
        The user do not want to send their config, send an iq cancel
128
        """
129
        muc.cancel_config(self.core.xmpp, self.name)
130 131 132 133
        self.core.close_tab()

    def send_config(self, form):
        """
Kim Alvefur's avatar
Kim Alvefur committed
134
        The user sends their config to the server
135
        """
136
        muc.configure_room(self.core.xmpp, self.name, form)
137 138
        self.core.close_tab()

139 140 141 142 143 144 145 146 147
    def join(self):
        """
        Join the room
        """
        status = self.core.get_status()
        if self.last_connection:
            delta = datetime.now() - self.last_connection
            seconds = delta.seconds + delta.days * 24 * 3600
        else:
148
            seconds = None
mathieui's avatar
mathieui committed
149 150 151 152 153 154 155 156
        muc.join_groupchat(
            self.core,
            self.name,
            self.own_nick,
            self.password,
            status=status.message,
            show=status.show,
            seconds=seconds)
157

158
    def leave_room(self, message: str):
mathieui's avatar
mathieui committed
159
        if self.joined:
160 161 162 163
            theme = get_theme()
            info_col = dump_tuple(theme.COLOR_INFORMATION_TEXT)
            char_quit = theme.CHAR_QUIT
            spec_col = dump_tuple(theme.COLOR_QUIT_CHAR)
164

165 166
            if config.get_by_tabname('display_user_color_in_join_part',
                                     self.general_jid):
167
                color = dump_tuple(theme.COLOR_OWN_NICK)
168
            else:
169
                color = "3"
170

mathieui's avatar
mathieui committed
171
            if message:
172 173
                msg = ('\x19%(color_spec)s}%(spec)s\x19%(info_col)s} '
                       'You (\x19%(color)s}%(nick)s\x19%(info_col)s})'
174
                       ' left the room'
175
                       ' (\x19o%(reason)s\x19%(info_col)s})') % {
mathieui's avatar
mathieui committed
176 177 178 179
                           'info_col': info_col,
                           'reason': message,
                           'spec': char_quit,
                           'color': color,
180 181 182
                           'color_spec': spec_col,
                           'nick': self.own_nick,
                       }
183
            else:
184 185
                msg = ('\x19%(color_spec)s}%(spec)s\x19%(info_col)s} '
                       'You (\x19%(color)s}%(nick)s\x19%(info_col)s})'
186
                       ' left the room') % {
187
                           'info_col': info_col,
mathieui's avatar
mathieui committed
188 189
                           'spec': char_quit,
                           'color': color,
190 191 192
                           'color_spec': spec_col,
                           'nick': self.own_nick,
                       }
193

194
            self.add_message(msg, typ=2)
195
            self.disconnect()
mathieui's avatar
mathieui committed
196 197
            muc.leave_groupchat(self.core.xmpp, self.name, self.own_nick,
                                message)
198
            self.core.disable_private_tabs(self.name, reason=msg)
199
        else:
mathieui's avatar
mathieui committed
200 201
            muc.leave_groupchat(self.core.xmpp, self.name, self.own_nick,
                                message)
mathieui's avatar
mathieui committed
202

203 204 205 206
    def change_affiliation(self,
                           nick_or_jid: Union[str, JID],
                           affiliation: str,
                           reason=''):
207 208 209
        """
        Change the affiliation of a nick or JID
        """
mathieui's avatar
mathieui committed
210

211 212
        def callback(iq):
            if iq['type'] == 'error':
mathieui's avatar
mathieui committed
213 214 215 216
                self.core.information(
                    "Could not set affiliation '%s' for '%s'." %
                    (affiliation, nick_or_jid), "Warning")

217 218 219 220 221
        if not self.joined:
            return

        valid_affiliations = ('outcast', 'none', 'member', 'admin', 'owner')
        if affiliation not in valid_affiliations:
mathieui's avatar
mathieui committed
222 223 224
            return self.core.information(
                'The affiliation must be one of ' +
                ', '.join(valid_affiliations), 'Error')
225
        if nick_or_jid in [user.nick for user in self.users]:
mathieui's avatar
mathieui committed
226
            muc.set_user_affiliation(
mathieui's avatar
mathieui committed
227 228 229 230 231 232
                self.core.xmpp,
                self.name,
                affiliation,
                nick=nick_or_jid,
                callback=callback,
                reason=reason)
233
        else:
mathieui's avatar
mathieui committed
234
            muc.set_user_affiliation(
mathieui's avatar
mathieui committed
235 236 237 238 239 240
                self.core.xmpp,
                self.name,
                affiliation,
                jid=safeJID(nick_or_jid),
                callback=callback,
                reason=reason)
241

242
    def change_role(self, nick: str, role: str, reason=''):
243 244 245
        """
        Change the role of a nick
        """
mathieui's avatar
mathieui committed
246

247 248
        def callback(iq):
            if iq['type'] == 'error':
mathieui's avatar
mathieui committed
249 250 251
                self.core.information(
                    "Could not set role '%s' for '%s'." % (role, nick),
                    "Warning")
mathieui's avatar
mathieui committed
252

253 254 255
        valid_roles = ('none', 'visitor', 'participant', 'moderator')

        if not self.joined or role not in valid_roles:
mathieui's avatar
mathieui committed
256 257
            return self.core.information(
                'The role must be one of ' + ', '.join(valid_roles), 'Error')
258 259 260

        if not safeJID(self.name + '/' + nick):
            return self.core.information('Invalid nick', 'Info')
mathieui's avatar
mathieui committed
261 262
        muc.set_user_role(
            self.core.xmpp, self.name, nick, reason, role, callback=callback)
263

mathieui's avatar
mathieui committed
264
    @refresh_wrapper.conditional
265
    def print_info(self, nick: str) -> bool:
mathieui's avatar
mathieui committed
266 267 268 269 270 271 272 273 274
        """Print information about a user"""
        user = self.get_user_by_name(nick)
        if not user:
            return False

        theme = get_theme()
        inf = '\x19' + dump_tuple(theme.COLOR_INFORMATION_TEXT) + '}'
        if user.jid:
            user_jid = '%s (\x19%s}%s\x19o%s)' % (
mathieui's avatar
mathieui committed
275
                inf, dump_tuple(theme.COLOR_MUC_JID), user.jid, inf)
mathieui's avatar
mathieui committed
276 277 278 279 280 281
        else:
            user_jid = ''
        info = ('\x19%(user_col)s}%(nick)s\x19o%(jid)s%(info)s: show: '
                '\x19%(show_col)s}%(show)s\x19o%(info)s, affiliation: '
                '\x19%(role_col)s}%(affiliation)s\x19o%(info)s, role: '
                '\x19%(role_col)s}%(role)s\x19o%(status)s') % {
mathieui's avatar
mathieui committed
282 283 284 285 286 287 288 289 290 291 292
                    'user_col': dump_tuple(user.color),
                    'nick': nick,
                    'jid': user_jid,
                    'info': inf,
                    'show_col': dump_tuple(theme.color_show(user.show)),
                    'show': user.show or 'Available',
                    'role_col': dump_tuple(theme.color_role(user.role)),
                    'affiliation': user.affiliation or 'None',
                    'role': user.role or 'None',
                    'status': '\n%s' % user.status if user.status else ''
                }
mathieui's avatar
mathieui committed
293 294 295
        self.add_message(info, typ=0)
        return True

296
    def change_topic(self, topic: str):
mathieui's avatar
mathieui committed
297 298 299 300 301 302 303 304
        """Change the current topic"""
        muc.change_subject(self.core.xmpp, self.name, topic)

    @refresh_wrapper.always
    def show_topic(self):
        """
        Print the current topic
        """
305 306 307
        theme = get_theme()
        info_text = dump_tuple(theme.COLOR_INFORMATION_TEXT)
        norm_text = dump_tuple(theme.COLOR_NORMAL_TEXT)
mathieui's avatar
mathieui committed
308 309 310 311
        if self.topic_from:
            user = self.get_user_by_name(self.topic_from)
            if user:
                user_text = dump_tuple(user.color)
mathieui's avatar
mathieui committed
312 313
                user_string = '\x19%s}(set by \x19%s}%s\x19%s})' % (
                    info_text, user_text, user.nick, info_text)
mathieui's avatar
mathieui committed
314 315 316 317 318 319
            else:
                user_string = self.topic_from
        else:
            user_string = ''

        self._text_buffer.add_message(
mathieui's avatar
mathieui committed
320 321
            "\x19%s}The subject of the room is: \x19%s}%s %s" %
            (info_text, norm_text, self.topic, user_string))
mathieui's avatar
mathieui committed
322

mathieui's avatar
mathieui committed
323 324 325
    @refresh_wrapper.always
    def recolor(self, random_colors=False):
        """Recolor the current MUC users"""
mathieui's avatar
mathieui committed
326 327
        deterministic = config.get_by_tabname('deterministic_nick_colors',
                                              self.name)
mathieui's avatar
mathieui committed
328 329 330 331 332 333 334 335 336 337 338
        if deterministic:
            for user in self.users:
                if user is self.own_user:
                    continue
                color = self.search_for_color(user.nick)
                if color != '':
                    continue
                user.set_deterministic_color()
            return
        # Sort the user list by last talked, to avoid color conflicts
        # on active participants
339
        sorted_users = sorted(self.users, key=COMPARE_USERS_LAST_TALKED, reverse=True)
mathieui's avatar
mathieui committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
        full_sorted_users = sorted_users[:]
        # search our own user, to remove it from the list
        # Also remove users whose color is fixed
        for user in full_sorted_users:
            color = self.search_for_color(user.nick)
            if user is self.own_user:
                sorted_users.remove(user)
            elif color != '':
                sorted_users.remove(user)
                user.change_color(color, deterministic)
        colors = list(get_theme().LIST_COLOR_NICKNAMES)
        if random_colors:
            random.shuffle(colors)
        for i, user in enumerate(sorted_users):
            user.color = colors[i % len(colors)]
        self.text_win.rebuild_everything(self._text_buffer)

357
    @refresh_wrapper.conditional
358
    def set_nick_color(self, nick: str, color: str) -> bool:
359 360 361 362 363 364 365 366
        "Set a custom color for a nick, permanently"
        user = self.get_user_by_name(nick)
        if color not in xhtml.colors and color not in ('unset', 'random'):
            return False
        if nick == self.own_nick:
            return False
        if color == 'unset':
            if config.remove_and_save(nick, 'muc_colors'):
mathieui's avatar
mathieui committed
367 368
                self.core.information('Color for nick %s unset' % (nick),
                                      'Info')
369 370 371 372 373 374
        else:
            if color == 'random':
                color = random.choice(list(xhtml.colors))
            if user:
                user.change_color(color)
            config.set_and_save(nick, color, 'muc_colors')
mathieui's avatar
mathieui committed
375 376
            nick_color_aliases = config.get_by_tabname('nick_color_aliases',
                                                       self.name)
377 378 379 380 381 382 383 384 385 386 387
            if nick_color_aliases:
                # if any user in the room has a nick which is an alias of the
                # nick, update its color
                for tab in self.core.get_tabs(MucTab):
                    for u in tab.users:
                        nick_alias = re.sub('^_*', '', u.nick)
                        nick_alias = re.sub('_*$', '', nick_alias)
                        if nick_alias == nick:
                            u.change_color(color)
        self.text_win.rebuild_everything(self._text_buffer)
        return True
mathieui's avatar
mathieui committed
388

mathieui's avatar
mathieui committed
389 390 391 392 393 394
    def on_input(self, key, raw):
        if not raw and key in self.key_func:
            self.key_func[key]()
            return False
        self.input.do_command(key, raw=raw)
        empty_after = self.input.get_text() == ''
mathieui's avatar
mathieui committed
395 396 397
        empty_after = empty_after or (
            self.input.get_text().startswith('/')
            and not self.input.get_text().startswith('//'))
mathieui's avatar
mathieui committed
398 399
        self.send_composing_chat_state(empty_after)
        return False
400

401
    def get_nick(self) -> str:
402 403 404 405 406 407 408
        if config.get('show_muc_jid'):
            return self.name
        bookmark = self.core.bookmarks[self.name]
        if bookmark is not None and bookmark.name:
            return bookmark.name
        # TODO: send the disco#info identity name here, if it exists.
        return safeJID(self.name).user
409

mathieui's avatar
mathieui committed
410 411
    def get_text_window(self):
        return self.text_win
412

mathieui's avatar
mathieui committed
413 414 415 416
    def on_lose_focus(self):
        if self.joined:
            if self.input.text:
                self.state = 'nonempty'
417 418
            elif self.lagged:
                self.state = 'disconnected'
mathieui's avatar
mathieui committed
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
            else:
                self.state = 'normal'
        else:
            self.state = 'disconnected'
        self.text_win.remove_line_separator()
        self.text_win.add_line_separator(self._text_buffer)
        if config.get_by_tabname('send_chat_states', self.general_jid):
            self.send_chat_state('inactive')
        self.check_scrolled()

    def on_gain_focus(self):
        self.state = 'current'
        if (self.text_win.built_lines and self.text_win.built_lines[-1] is None
                and not config.get('show_useless_separator')):
            self.text_win.remove_line_separator()
        curses.curs_set(1)
mathieui's avatar
mathieui committed
435 436
        if self.joined and config.get_by_tabname(
                'send_chat_states',
mathieui's avatar
mathieui committed
437 438 439 440
                self.general_jid) and not self.input.get_text():
            self.send_chat_state('active')

    def handle_presence(self, presence):
441
        """
mathieui's avatar
mathieui committed
442
        Handle MUC presence
443
        """
444
        self.reset_lag()
mathieui's avatar
mathieui committed
445 446 447
        status_codes = set()
        for status_code in presence.xml.findall(STATUS_XPATH):
            status_codes.add(status_code.attrib['code'])
mathieui's avatar
mathieui committed
448 449 450
        if presence['type'] == 'error':
            self.core.room_error(presence, self.name)
        elif not self.joined:
451 452 453
            own = '110' in status_codes or self.own_nick == presence['from'].resource
            if own or len(self.presence_buffer) >= 10:
                self.process_presence_buffer(presence, own)
mathieui's avatar
mathieui committed
454 455 456 457 458 459 460 461
            else:
                self.presence_buffer.append(presence)
                return
        else:
            try:
                self.handle_presence_joined(presence, status_codes)
            except PresenceError:
                self.core.room_error(presence, presence['from'].bare)
462
        if self.core.tabs.current_tab is self:
mathieui's avatar
mathieui committed
463 464
            self.text_win.refresh()
            self.user_win.refresh_if_changed(self.users)
465 466 467
            self.info_header.refresh(
                self, self.text_win, user=self.own_user,
                information=MucTab.additional_information)
mathieui's avatar
mathieui committed
468 469
            self.input.refresh()
            self.core.doupdate()
470

471
    def process_presence_buffer(self, last_presence, own):
472
        """
mathieui's avatar
mathieui committed
473
        Batch-process all the initial presences
474
        """
mathieui's avatar
mathieui committed
475 476
        deterministic = config.get_by_tabname('deterministic_nick_colors',
                                              self.name)
mathieui's avatar
mathieui committed
477

mathieui's avatar
mathieui committed
478 479 480 481 482
        for stanza in self.presence_buffer:
            try:
                self.handle_presence_unjoined(stanza, deterministic)
            except PresenceError:
                self.core.room_error(stanza, stanza['from'].bare)
483 484
        self.presence_buffer = []
        self.handle_presence_unjoined(last_presence, deterministic, own)
mathieui's avatar
mathieui committed
485 486 487
        self.users.sort()
        # Enable the self ping event, to regularly check if we
        # are still in the room.
488 489
        if own:
            self.enable_self_ping_event()
490
        if self.core.tabs.current_tab is not self:
mathieui's avatar
mathieui committed
491
            self.refresh_tab_win()
492
            self.core.tabs.current_tab.refresh_input()
mathieui's avatar
mathieui committed
493
            self.core.doupdate()
494

mathieui's avatar
mathieui committed
495 496 497 498
    def handle_presence_unjoined(self, presence, deterministic, own=False):
        """
        Presence received while we are not in the room (before code=110)
        """
mathieui's avatar
mathieui committed
499
        from_nick, _, affiliation, show, status, role, jid, typ = dissect_presence(
mathieui's avatar
mathieui committed
500
            presence)
501 502
        if typ == 'unavailable':
            return
mathieui's avatar
mathieui committed
503
        user_color = self.search_for_color(from_nick)
mathieui's avatar
mathieui committed
504 505
        new_user = User(from_nick, affiliation, show, status, role, jid,
                        deterministic, user_color)
mathieui's avatar
mathieui committed
506 507 508 509 510 511 512
        self.users.append(new_user)
        self.core.events.trigger('muc_join', presence, self)
        if own:
            status_codes = set()
            for status_code in presence.xml.findall(STATUS_XPATH):
                status_codes.add(status_code.attrib['code'])
            self.own_join(from_nick, new_user, status_codes)
513

514
    def own_join(self, from_nick: str, new_user: User, status_codes: Set[str]):
515
        """
mathieui's avatar
mathieui committed
516
        Handle the last presence we received, entering the room
517
        """
mathieui's avatar
mathieui committed
518 519 520 521 522 523
        self.own_nick = from_nick
        self.own_user = new_user
        self.joined = True
        if self.name in self.core.initial_joins:
            self.core.initial_joins.remove(self.name)
            self._state = 'normal'
524
        elif self != self.core.tabs.current_tab:
mathieui's avatar
mathieui committed
525
            self._state = 'joined'
526
        if (self.core.tabs.current_tab is self
mathieui's avatar
mathieui committed
527 528
                and self.core.status.show not in ('xa', 'away')):
            self.send_chat_state('active')
529 530
        theme = get_theme()
        new_user.color = theme.COLOR_OWN_NICK
531

mathieui's avatar
mathieui committed
532 533 534 535
        if config.get_by_tabname('display_user_color_in_join_part',
                                 self.general_jid):
            color = dump_tuple(new_user.color)
        else:
536
            color = "3"
537

538 539 540
        info_col = dump_tuple(theme.COLOR_INFORMATION_TEXT)
        warn_col = dump_tuple(theme.COLOR_WARNING_TEXT)
        spec_col = dump_tuple(theme.COLOR_JOIN_CHAR)
mathieui's avatar
mathieui committed
541 542 543 544
        enable_message = ('\x19%(color_spec)s}%(spec)s\x19%(info_col)s} You '
                          '(\x19%(nick_col)s}%(nick)s\x19%(info_col)s}) joined'
                          ' the room') % {
                              'nick': from_nick,
545
                              'spec': theme.CHAR_JOIN,
mathieui's avatar
mathieui committed
546 547 548 549
                              'color_spec': spec_col,
                              'nick_col': color,
                              'info_col': info_col,
                          }
mathieui's avatar
mathieui committed
550 551 552 553
        self.add_message(enable_message, typ=2)
        self.core.enable_private_tabs(self.name, enable_message)
        if '201' in status_codes:
            self.add_message(
mathieui's avatar
mathieui committed
554 555
                '\x19%(info_col)s}Info: The room '
                'has been created' % {'info_col': info_col},
mathieui's avatar
mathieui committed
556 557 558
                typ=0)
        if '170' in status_codes:
            self.add_message(
mathieui's avatar
mathieui committed
559
                '\x19%(warn_col)s}Warning:\x19%(info_col)s}'
mathieui's avatar
mathieui committed
560 561 562 563
                ' This room is publicly logged' % {
                    'info_col': info_col,
                    'warn_col': warn_col
                },
mathieui's avatar
mathieui committed
564 565 566
                typ=0)
        if '100' in status_codes:
            self.add_message(
mathieui's avatar
mathieui committed
567
                '\x19%(warn_col)s}Warning:\x19%(info_col)s}'
mathieui's avatar
mathieui committed
568 569 570 571
                ' This room is not anonymous.' % {
                    'info_col': info_col,
                    'warn_col': warn_col
                },
mathieui's avatar
mathieui committed
572
                typ=0)
573

mathieui's avatar
mathieui committed
574
    def handle_presence_joined(self, presence, status_codes):
575
        """
mathieui's avatar
mathieui committed
576
        Handle new presences when we are already in the room
577
        """
mathieui's avatar
mathieui committed
578 579
        from_nick, from_room, affiliation, show, status, role, jid, typ = dissect_presence(
            presence)
mathieui's avatar
mathieui committed
580 581 582 583
        change_nick = '303' in status_codes
        kick = '307' in status_codes and typ == 'unavailable'
        ban = '301' in status_codes and typ == 'unavailable'
        shutdown = '332' in status_codes and typ == 'unavailable'
584
        server_initiated = '333' in status_codes and typ == 'unavailable'
mathieui's avatar
mathieui committed
585 586 587 588 589 590
        non_member = '322' in status_codes and typ == 'unavailable'
        user = self.get_user_by_name(from_nick)
        # New user
        if not user and typ != "unavailable":
            user_color = self.search_for_color(from_nick)
            self.core.events.trigger('muc_join', presence, self)
mathieui's avatar
mathieui committed
591 592
            self.on_user_join(from_nick, affiliation, show, status, role, jid,
                              user_color)
mathieui's avatar
mathieui committed
593 594 595 596 597 598 599 600
        elif user is None:
            log.error('BUG: User %s in %s is None', from_nick, self.name)
            return
        elif change_nick:
            self.core.events.trigger('muc_nickchange', presence, self)
            self.on_user_nick_change(presence, user, from_nick, from_room)
        elif ban:
            self.core.events.trigger('muc_ban', presence, self)
mathieui's avatar
mathieui committed
601 602
            self.core.on_user_left_private_conversation(
                from_room, user, status)
mathieui's avatar
mathieui committed
603
            self.on_user_banned(presence, user, from_nick)
604
        elif kick and not server_initiated:
mathieui's avatar
mathieui committed
605
            self.core.events.trigger('muc_kick', presence, self)
mathieui's avatar
mathieui committed
606 607
            self.core.on_user_left_private_conversation(
                from_room, user, status)
mathieui's avatar
mathieui committed
608 609 610 611 612 613 614 615 616
            self.on_user_kicked(presence, user, from_nick)
        elif shutdown:
            self.core.events.trigger('muc_shutdown', presence, self)
            self.on_muc_shutdown()
        elif non_member:
            self.core.events.trigger('muc_shutdown', presence, self)
            self.on_non_member_kicked()
        # user quit
        elif typ == 'unavailable':
mathieui's avatar
mathieui committed
617
            self.on_user_leave_groupchat(user, jid, status, from_nick,
618
                                         from_room, server_initiated)
mathieui's avatar
mathieui committed
619
        # status change
620
        else:
mathieui's avatar
mathieui committed
621 622
            self.on_user_change_status(user, from_nick, from_room, affiliation,
                                       role, show, status)
623

mathieui's avatar
mathieui committed
624 625 626
    def on_non_member_kicked(self):
        """We have been kicked because the MUC is members-only"""
        self.add_message(
mathieui's avatar
mathieui committed
627 628 629
            '\x19%(info_col)s}You have been kicked because you '
            'are not a member and the room is now members-only.' %
            {'info_col': dump_tuple(get_theme().COLOR_INFORMATION_TEXT)},
mathieui's avatar
mathieui committed
630 631 632 633 634 635
            typ=2)
        self.disconnect()

    def on_muc_shutdown(self):
        """We have been kicked because the MUC service is shutting down"""
        self.add_message(
mathieui's avatar
mathieui committed
636 637 638
            '\x19%(info_col)s}You have been kicked because the'
            ' MUC service is shutting down.' %
            {'info_col': dump_tuple(get_theme().COLOR_INFORMATION_TEXT)},
mathieui's avatar
mathieui committed
639 640 641
            typ=2)
        self.disconnect()

mathieui's avatar
mathieui committed
642 643
    def on_user_join(self, from_nick, affiliation, show, status, role, jid,
                     color):
644
        """
mathieui's avatar
mathieui committed
645
        When a new user joins the groupchat
646
        """
mathieui's avatar
mathieui committed
647 648 649 650
        deterministic = config.get_by_tabname('deterministic_nick_colors',
                                              self.name)
        user = User(from_nick, affiliation, show, status, role, jid,
                    deterministic, color)
mathieui's avatar
mathieui committed
651 652 653 654 655 656 657 658 659
        bisect.insort_left(self.users, user)
        hide_exit_join = config.get_by_tabname('hide_exit_join',
                                               self.general_jid)
        if hide_exit_join != 0:
            if config.get_by_tabname('display_user_color_in_join_part',
                                     self.general_jid):
                color = dump_tuple(user.color)
            else:
                color = 3
660 661 662 663
            theme = get_theme()
            info_col = dump_tuple(theme.COLOR_INFORMATION_TEXT)
            spec_col = dump_tuple(theme.COLOR_JOIN_CHAR)
            char_join = theme.CHAR_JOIN
mathieui's avatar
mathieui committed
664 665 666
            if not jid.full:
                msg = ('\x19%(color_spec)s}%(spec)s \x19%(color)s}%(nick)s'
                       '\x19%(info_col)s} joined the room') % {
mathieui's avatar
mathieui committed
667 668 669 670 671 672
                           'nick': from_nick,
                           'spec': char_join,
                           'color': color,
                           'info_col': info_col,
                           'color_spec': spec_col,
                       }
mathieui's avatar
mathieui committed
673 674 675 676
            else:
                msg = ('\x19%(color_spec)s}%(spec)s \x19%(color)s}%(nick)s'
                       '\x19%(info_col)s} (\x19%(jid_color)s}%(jid)s\x19'
                       '%(info_col)s}) joined the room') % {
mathieui's avatar
mathieui committed
677 678 679 680 681
                           'spec': char_join,
                           'nick': from_nick,
                           'color': color,
                           'jid': jid.full,
                           'info_col': info_col,
682
                           'jid_color': dump_tuple(theme.COLOR_MUC_JID),
mathieui's avatar
mathieui committed
683 684
                           'color_spec': spec_col,
                       }
mathieui's avatar
mathieui committed
685 686 687 688
            self.add_message(msg, typ=2)
        self.core.on_user_rejoined_private_conversation(self.name, from_nick)

    def on_user_nick_change(self, presence, user, from_nick, from_room):
mathieui's avatar
mathieui committed
689 690
        new_nick = presence.xml.find(
            '{%s}x/{%s}item' % (NS_MUC_USER, NS_MUC_USER)).attrib['nick']
691
        old_color = user.color
mathieui's avatar
mathieui committed
692 693 694 695
        if user.nick == self.own_nick:
            self.own_nick = new_nick
            # also change our nick in all private discussions of this room
            self.core.handler.on_muc_own_nickchange(self)
696
            user.change_nick(new_nick)
697
        else:
698
            user.change_nick(new_nick)
mathieui's avatar
mathieui committed
699 700
            deterministic = config.get_by_tabname('deterministic_nick_colors',
                                                  self.name)
701 702
            color = config.get_by_tabname(new_nick, 'muc_colors') or None
            if color or deterministic:
mathieui's avatar
mathieui committed
703 704 705
                user.change_color(color, deterministic)
        self.users.remove(user)
        bisect.insort_left(self.users, user)
706

mathieui's avatar
mathieui committed
707 708 709
        if config.get_by_tabname('display_user_color_in_join_part',
                                 self.general_jid):
            color = dump_tuple(user.color)
710
            old_color = dump_tuple(old_color)
711
        else:
712
            old_color = color = 3
mathieui's avatar
mathieui committed
713
        info_col = dump_tuple(get_theme().COLOR_INFORMATION_TEXT)
mathieui's avatar
mathieui committed
714
        self.add_message(
715
            '\x19%(old_color)s}%(old)s\x19%(info_col)s} is'
mathieui's avatar
mathieui committed
716 717 718 719
            ' now known as \x19%(color)s}%(new)s' % {
                'old': from_nick,
                'new': new_nick,
                'color': color,
720
                'old_color': old_color,
mathieui's avatar
mathieui committed
721 722 723
                'info_col': info_col
            },
            typ=2)
mathieui's avatar
mathieui committed
724 725
        # rename the private tabs if needed
        self.core.rename_private_tabs(self.name, from_nick, user)
726

mathieui's avatar
mathieui committed
727
    def on_user_banned(self, presence, user, from_nick):
728
        """
mathieui's avatar
mathieui committed
729
        When someone is banned from a muc
730
        """
mathieui's avatar
mathieui committed
731 732 733 734 735 736 737 738 739
        self.users.remove(user)
        by = presence.xml.find('{%s}x/{%s}item/{%s}actor' %
                               (NS_MUC_USER, NS_MUC_USER, NS_MUC_USER))
        reason = presence.xml.find('{%s}x/{%s}item/{%s}reason' %
                                   (NS_MUC_USER, NS_MUC_USER, NS_MUC_USER))
        if by:
            by = by.get('jid') or by.get('nick') or None
        else:
            by = None
740

741 742 743
        theme = get_theme()
        info_col = dump_tuple(theme.COLOR_INFORMATION_TEXT)
        char_kick = theme.CHAR_KICK
744

mathieui's avatar
mathieui committed
745
        if from_nick == self.own_nick:  # we are banned
mathieui's avatar
mathieui committed
746 747 748
            if by:
                kick_msg = ('\x191}%(spec)s \x193}You\x19%(info_col)s}'
                            ' have been banned by \x194}%(by)s') % {
mathieui's avatar
mathieui committed
749 750 751 752
                                'spec': char_kick,
                                'by': by,
                                'info_col': info_col
                            }
mathieui's avatar
mathieui committed
753 754 755
            else:
                kick_msg = ('\x191}%(spec)s \x193}You\x19'
                            '%(info_col)s} have been banned.') % {
mathieui's avatar
mathieui committed
756 757 758
                                'spec': char_kick,
                                'info_col': info_col
                            }
mathieui's avatar
mathieui committed
759 760 761
            self.core.disable_private_tabs(self.name, reason=kick_msg)
            self.disconnect()
            self.refresh_tab_win()
762
            self.core.tabs.current_tab.refresh_input()
mathieui's avatar
mathieui committed
763 764 765 766 767 768 769
            if config.get_by_tabname('autorejoin', self.general_jid):
                delay = config.get_by_tabname('autorejoin_delay',
                                              self.general_jid)
                delay = common.parse_str_to_secs(delay)
                if delay <= 0:
                    muc.join_groupchat(self.core, self.name, self.own_nick)
                else:
mathieui's avatar
mathieui committed
770 771 772
                    self.core.add_timed_event(
                        timed_events.DelayedEvent(delay, muc.join_groupchat,
                                                  self.core, self.name,
mathieui's avatar
mathieui committed
773
                                                  self.own_nick))
774

mathieui's avatar
mathieui committed
775 776 777 778 779 780 781 782 783 784 785
        else:
            if config.get_by_tabname('display_user_color_in_join_part',
                                     self.general_jid):
                color = dump_tuple(user.color)
            else:
                color = 3

            if by:
                kick_msg = ('\x191}%(spec)s \x19%(color)s}'
                            '%(nick)s\x19%(info_col)s} '
                            'has been banned by \x194}%(by)s') % {
mathieui's avatar
mathieui committed
786 787 788 789 790 791
                                'spec': char_kick,
                                'nick': from_nick,
                                'color': color,
                                'by': by,
                                'info_col': info_col
                            }
mathieui's avatar
mathieui committed
792 793 794
            else:
                kick_msg = ('\x191}%(spec)s \x19%(color)s}%(nick)s'
                            '\x19%(info_col)s} has been banned') % {
mathieui's avatar
mathieui committed
795 796 797 798 799
                                'spec': char_kick,
                                'nick': from_nick,
                                'color': color,
                                'info_col': info_col
                            }
mathieui's avatar
mathieui committed
800 801 802
        if reason is not None and reason.text:
            kick_msg += ('\x19%(info_col)s} Reason: \x196}'
                         '%(reason)s\x19%(info_col)s}') % {
mathieui's avatar
mathieui committed
803 804 805
                             'reason': reason.text,
                             'info_col': info_col
                         }
mathieui's avatar
mathieui committed
806 807 808
        self.add_message(kick_msg, typ=2)

    def on_user_kicked(self, presence, user, from_nick):
809
        """
mathieui's avatar
mathieui committed
810
        When someone is kicked from a muc
811
        """
mathieui's avatar
mathieui committed
812 813 814 815 816 817
        self.users.remove(user)
        actor_elem = presence.xml.find('{%s}x/{%s}item/{%s}actor' %
                                       (NS_MUC_USER, NS_MUC_USER, NS_MUC_USER))
        reason = presence.xml.find('{%s}x/{%s}item/{%s}reason' %
                                   (NS_MUC_USER, NS_MUC_USER, NS_MUC_USER))
        by = None
818 819 820
        theme = get_theme()
        info_col = dump_tuple(theme.COLOR_INFORMATION_TEXT)
        char_kick = theme.CHAR_KICK
mathieui's avatar
mathieui committed
821 822
        if actor_elem is not None:
            by = actor_elem.get('nick') or actor_elem.get('jid')
mathieui's avatar
mathieui committed
823
        if from_nick == self.own_nick:  # we are kicked
mathieui's avatar
mathieui committed
824 825 826 827
            if by:
                kick_msg = ('\x191}%(spec)s \x193}You\x19'
                            '%(info_col)s} have been kicked'
                            ' by \x193}%(by)s') % {
mathieui's avatar
mathieui committed
828 829 830 831
                                'spec': char_kick,
                                'by': by,
                                'info_col': info_col
                            }
mathieui's avatar
mathieui committed
832 833 834
            else:
                kick_msg = ('\x191}%(spec)s \x193}You\x19%(info_col)s}'
                            ' have been kicked.') % {
mathieui's avatar
mathieui committed
835 836 837
                                'spec': char_kick,
                                'info_col': info_col
                            }
mathieui's avatar
mathieui committed
838 839 840
            self.core.disable_private_tabs(self.name, reason=kick_msg)
            self.disconnect()
            self.refresh_tab_win()
841
            self.core.tabs.current_tab.refresh_input()
mathieui's avatar
mathieui committed
842 843 844 845 846 847 848 849
            # try to auto-rejoin
            if config.get_by_tabname('autorejoin', self.general_jid):
                delay = config.get_by_tabname('autorejoin_delay',
                                              self.general_jid)
                delay = common.parse_str_to_secs(delay)
                if delay <= 0:
                    muc.join_groupchat(self.core, self.name, self.own_nick)
                else:
mathieui's avatar
mathieui committed
850 851 852 853
                    self.core.add_timed_event(
                        timed_events.DelayedEvent(delay, muc.join_groupchat,
                                                  self.core, self.name,
                                                  self.own_nick))
mathieui's avatar
mathieui committed
854 855 856 857 858 859 860 861 862 863
        else:
            if config.get_by_tabname('display_user_color_in_join_part',
                                     self.general_jid):
                color = dump_tuple(user.color)
            else:
                color = 3
            if by:
                kick_msg = ('\x191}%(spec)s \x19%(color)s}%(nick)s'
                            '\x19%(info_col)s} has been kicked by '
                            '\x193}%(by)s') % {
mathieui's avatar
mathieui committed
864 865 866 867 868 869
                                'spec': char_kick,
                                'nick': from_nick,
                                'color': color,
                                'by': by,
                                'info_col': info_col
                            }
mathieui's avatar
mathieui committed
870 871 872
            else:
                kick_msg = ('\x191}%(spec)s \x19%(color)s}%(nick)s'
                            '\x19%(info_col)s} has been kicked') % {
mathieui's avatar
mathieui committed
873 874 875 876 877
                                'spec': char_kick,
                                'nick': from_nick,
                                'color': color,
                                'info_col': info_col
                            }
mathieui's avatar
mathieui committed
878 879 880
        if reason is not None and reason.text:
            kick_msg += ('\x19%(info_col)s} Reason: \x196}'
                         '%(reason)s') % {
mathieui's avatar
mathieui committed
881 882 883
                             'reason': reason.text,
                             'info_col': info_col
                         }
mathieui's avatar
mathieui committed
884 885
        self.add_message(kick_msg, typ=2)

mathieui's avatar
mathieui committed
886
    def on_user_leave_groupchat(self,
887 888 889 890 891
                                user: User,
                                jid: JID,
                                status: str,
                                from_nick: str,
                                from_room: JID,
892
                                server_initiated=False):
mathieui's avatar
mathieui committed
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
        """
        When an user leaves a groupchat
        """
        self.users.remove(user)
        if self.own_nick == user.nick:
            # We are now out of the room.
            # Happens with some buggy (? not sure) servers
            self.disconnect()
            self.core.disable_private_tabs(from_room)
            self.refresh_tab_win()

        hide_exit_join = config.get_by_tabname('hide_exit_join',
                                               self.general_jid)

        if hide_exit_join <= -1 or user.has_talked_since(hide_exit_join):
            if config.get_by_tabname('display_user_color_in_join_part',
                                     self.general_jid):
                color = dump_tuple(user.color)
            else:
                color = 3
913 914 915
            theme = get_theme()
            info_col = dump_tuple(theme.COLOR_INFORMATION_TEXT)
            spec_col = dump_tuple(theme.COLOR_QUIT_CHAR)
mathieui's avatar
mathieui committed
916

917 918 919 920
            error_leave_txt = ''
            if server_initiated:
                error_leave_txt = ' due to an error'

mathieui's avatar
mathieui committed
921 922 923
            if not jid.full:
                leave_msg = ('\x19%(color_spec)s}%(spec)s \x19%(color)s}'
                             '%(nick)s\x19%(info_col)s} has left the '
924
                             'room%(error_leave)s') % {
mathieui's avatar
mathieui committed
925 926
                                 'nick': from_nick,
                                 'color': color,
927
                                 'spec': theme.CHAR_QUIT,
mathieui's avatar
mathieui committed
928
                                 'info_col': info_col,
929 930
                                 'color_spec': spec_col,
                                 'error_leave': error_leave_txt,
mathieui's avatar
mathieui committed
931
                             }
mathieui's avatar
mathieui committed
932
            else:
933
                jid_col = dump_tuple(theme.COLOR_MUC_JID)
mathieui's avatar
mathieui committed
934 935 936
                leave_msg = ('\x19%(color_spec)s}%(spec)s \x19%(color)s}'
                             '%(nick)s\x19%(info_col)s} (\x19%(jid_col)s}'
                             '%(jid)s\x19%(info_col)s}) has left the '
937
                             'room%(error_leave)s') % {
938
                                 'spec': theme.CHAR_QUIT,
mathieui's avatar
mathieui committed
939 940 941 942 943
                                 'nick': from_nick,
                                 'color': color,
                                 'jid': jid.full,
                                 'info_col': info_col,
                                 'color_spec': spec_col,
944 945
                                 'jid_col': jid_col,
                                 'error_leave': error_leave_txt,
mathieui's avatar
mathieui committed
946
                             }
mathieui's avatar
mathieui committed
947 948 949
            if status:
                leave_msg += ' (\x19o%s\x19%s})' % (status, info_col)
            self.add_message(leave_msg, typ=2)
mathieui's avatar
mathieui committed
950
        self.core.on_user_left_private_conversation(from_room, user, status)
mathieui's avatar
mathieui committed
951

mathieui's avatar
mathieui committed
952 953
    def on_user_change_status(self, user, from_nick, from_room, affiliation,
                              role, show, status):
mathieui's avatar
mathieui committed
954 955 956 957
        """
        When an user changes her status
        """
        # build the message
mathieui's avatar
mathieui committed
958 959
        display_message = False  # flag to know if something significant enough
        # to be displayed has changed
mathieui's avatar
mathieui committed
960 961 962 963 964
        if config.get_by_tabname('display_user_color_in_join_part',
                                 self.general_jid):
            color = dump_tuple(user.color)
        else:
            color = 3
965
        info_col = dump_tuple(get_theme().COLOR_INFORMATION_TEXT)
mathieui's avatar
mathieui committed
966 967
        if from_nick == self.own_nick:
            msg = '\x19%(color)s}You\x19%(info_col)s} changed: ' % {
968
                'info_col': info_col,
mathieui's avatar
mathieui committed
969 970
                'color': color
            }
mathieui's avatar
mathieui committed
971 972
        else:
            msg = '\x19%(color)s}%(nick)s\x19%(info_col)s} changed: ' % {
mathieui's avatar
mathieui committed
973 974
                'nick': from_nick,
                'color': color,
975
                'info_col': info_col
mathieui's avatar
mathieui committed
976
            }
mathieui's avatar
mathieui committed
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
        if affiliation != user.affiliation:
            msg += 'affiliation: %s, ' % affiliation
            display_message = True
        if role != user.role:
            msg += 'role: %s, ' % role
            display_message = True
        if show != user.show and show in SHOW_NAME:
            msg += 'show: %s, ' % SHOW_NAME[show]
            display_message = True
        if status != user.status:
            # if the user sets his status to nothing
            if status:
                msg += 'status: %s, ' % status
                display_message = True
            elif show in SHOW_NAME and show == user.show:
                msg += 'show: %s, ' % SHOW_NAME[show]
                display_message = True
        if not display_message:
            return
mathieui's avatar
mathieui committed
996
        msg = msg[:-2]  # remove the last ", "
mathieui's avatar
mathieui committed
997 998 999 1000
        hide_status_change = config.get_by_tabname('hide_status_change',
                                                   self.general_jid)
        if hide_status_change < -1:
            hide_status_change = -1
1001 1002 1003 1004 1005 1006
        if ((hide_status_change == -1
             or user.has_talked_since(hide_status_change)
             or user.nick == self.own_nick) and
            (affiliation != user.affiliation or role != user.role
             or show != user.show or status != user.status)) or (
                 affiliation != user.affiliation or role != user.role):
mathieui's avatar
mathieui committed
1007 1008
            # display the message in the room
            self._text_buffer.add_message(msg)
mathieui's avatar
mathieui committed
1009 1010
        self.core.on_user_changed_status_in_private(
            '%s/%s' % (from_room, from_nick), Status(show, status))