basetabs.py 29.8 KB
Newer Older
1
"""
2 3 4 5 6 7 8 9 10 11 12 13
Module for the base Tabs

The root class Tab defines the generic interface and attributes of a
tab. A tab organizes various Windows around the screen depending
of the tab specificity. If the tab shows messages, it will also
reference a buffer containing the messages.

Each subclass should redefine its own refresh() and resize() method
according to its windows.

This module also defines ChatTabs, the parent class for all tabs
revolving around chats.
14 15 16 17 18 19 20 21 22 23 24
"""

import logging
log = logging.getLogger(__name__)

import string
import time
import weakref
from datetime import datetime, timedelta
from xml.etree import cElementTree as ET

25
from poezio.core.structs import Command
26 27 28 29 30 31 32 33 34 35
from poezio import timed_events
from poezio import windows
from poezio import xhtml
from poezio.common import safeJID
from poezio.config import config
from poezio.decorators import refresh_wrapper
from poezio.logger import logger
from poezio.text_buffer import TextBuffer
from poezio.theming import get_theme, dump_tuple
from poezio.decorators import command_args_parser
36

37
# getters for tab colors (lambdas, so that they are dynamic)
38 39 40
STATE_COLORS = {
        'disconnected': lambda: get_theme().COLOR_TAB_DISCONNECTED,
        'scrolled': lambda: get_theme().COLOR_TAB_SCROLLED,
41
        'nonempty': lambda: get_theme().COLOR_TAB_NONEMPTY,
42 43
        'joined': lambda: get_theme().COLOR_TAB_JOINED,
        'message': lambda: get_theme().COLOR_TAB_NEW_MESSAGE,
44
        'composing': lambda: get_theme().COLOR_TAB_COMPOSING,
45 46 47 48 49 50 51 52 53
        'highlight': lambda: get_theme().COLOR_TAB_HIGHLIGHT,
        'private': lambda: get_theme().COLOR_TAB_PRIVATE,
        'normal': lambda: get_theme().COLOR_TAB_NORMAL,
        'current': lambda: get_theme().COLOR_TAB_CURRENT,
        'attention': lambda: get_theme().COLOR_TAB_ATTENTION,
    }
VERTICAL_STATE_COLORS = {
        'disconnected': lambda: get_theme().COLOR_VERTICAL_TAB_DISCONNECTED,
        'scrolled': lambda: get_theme().COLOR_VERTICAL_TAB_SCROLLED,
54
        'nonempty': lambda: get_theme().COLOR_VERTICAL_TAB_NONEMPTY,
55 56
        'joined': lambda: get_theme().COLOR_VERTICAL_TAB_JOINED,
        'message': lambda: get_theme().COLOR_VERTICAL_TAB_NEW_MESSAGE,
57
        'composing': lambda: get_theme().COLOR_VERTICAL_TAB_COMPOSING,
58 59 60 61 62 63 64 65
        'highlight': lambda: get_theme().COLOR_VERTICAL_TAB_HIGHLIGHT,
        'private': lambda: get_theme().COLOR_VERTICAL_TAB_PRIVATE,
        'normal': lambda: get_theme().COLOR_VERTICAL_TAB_NORMAL,
        'current': lambda: get_theme().COLOR_VERTICAL_TAB_CURRENT,
        'attention': lambda: get_theme().COLOR_VERTICAL_TAB_ATTENTION,
    }


66 67
# priority of the different tab states when using Alt+e
# higher means more priority, < 0 means not selectable
68 69 70 71
STATE_PRIORITY = {
        'normal': -1,
        'current': -1,
        'disconnected': 0,
72
        'nonempty': 0.1,
73
        'scrolled': 0.5,
74
        'joined': 0.8,
75
        'composing': 0.9,
76 77 78 79 80 81 82 83 84
        'message': 1,
        'highlight': 2,
        'private': 2,
        'attention': 3
    }

class Tab(object):
    plugin_commands = {}
    plugin_keys = {}
85 86
    def __init__(self, core):
        self.core = core
87 88
        if not hasattr(self, 'name'):
            self.name = self.__class__.__name__
89
        self.input = None
mathieui's avatar
mathieui committed
90
        self.closed = False
91
        self._state = 'normal'
92
        self._prev_state = None
93

94
        self.need_resize = False
95 96 97 98 99
        self.key_func = {}      # each tab should add their keys in there
                                # and use them in on_input
        self.commands = {}      # and their own commands


100 101
    @property
    def size(self):
102
        return self.core.size
103

104 105 106 107 108 109 110 111 112 113 114 115 116
    @property
    def nb(self):
        for index, tab in enumerate(self.core.tabs):
            if tab == self:
                return index
        return len(self.core.tabs)

    @staticmethod
    def tab_win_height():
        """
        Returns 1 or 0, depending on if we are using the vertical tab list
        or not.
        """
117
        if config.get('enable_vertical_tab_list'):
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
            return 0
        return 1

    @property
    def info_win(self):
        return self.core.information_win

    @property
    def color(self):
        return STATE_COLORS[self._state]()

    @property
    def vertical_color(self):
        return VERTICAL_STATE_COLORS[self._state]()

    @property
    def state(self):
        return self._state

    @state.setter
    def state(self, value):
        if not value in STATE_COLORS:
            log.debug("Invalid value for tab state: %s", value)
        elif STATE_PRIORITY[value] < STATE_PRIORITY[self._state] and \
                value not in ('current', 'disconnected') and \
                not (self._state == 'scrolled' and value == 'disconnected'):
            log.debug("Did not set state because of lower priority, asked: %s, kept: %s", value, self._state)
        elif self._state == 'disconnected' and value not in ('joined', 'current'):
            log.debug('Did not set state because disconnected tabs remain visible')
        else:
            self._state = value
149 150 151
            if self._state == 'current':
                self._prev_state = None

152 153 154
    def set_state(self, value):
        self._state = value

155 156 157 158 159 160 161 162 163 164
    def save_state(self):
        if self._state != 'composing':
            self._prev_state = self._state

    def restore_state(self):
        if self.state == 'composing' and self._prev_state:
            self._state = self._prev_state
            self._prev_state = None
        elif not self._prev_state:
            self._state = 'normal'
165 166 167

    @staticmethod
    def resize(scr):
168
        Tab.height, Tab.width = scr.getmaxyx()
169
        windows.TAB_WIN = scr
170

171 172 173 174 175 176 177 178
    def missing_command_callback(self, command_name):
        """
        Callback executed when a command is not found.
        Returns True if the callback took care of displaying
        the error message, False otherwise.
        """
        return False

179 180 181 182 183 184 185 186
    def register_command(self, name, func, *, desc='', shortdesc='', completion=None, usage=''):
        """
        Add a command
        """
        if name in self.commands:
            return
        if not desc and shortdesc:
            desc = shortdesc
187
        self.commands[name] = Command(func, desc, completion, shortdesc, usage)
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231

    def complete_commands(self, the_input):
        """
        Does command completion on the specified input for both global and tab-specific
        commands.
        This should be called from the completion method (on tab, for example), passing
        the input where completion is to be made.
        It can completion the command name itself or an argument of the command.
        Returns True if a completion was made, False else.
        """
        txt = the_input.get_text()
        # check if this is a command
        if txt.startswith('/') and not txt.startswith('//'):
            position = the_input.get_argument_position(quoted=False)
            if position == 0:
                words = ['/%s'% (name) for name in sorted(self.core.commands)] +\
                    ['/%s' % (name) for name in sorted(self.commands)]
                the_input.new_completion(words, 0)
                # Do not try to cycle command completion if there was only
                # one possibily. The next tab will complete the argument.
                # Otherwise we would need to add a useless space before being
                # able to complete the arguments.
                hit_copy = set(the_input.hit_list)
                while not hit_copy:
                    whitespace = the_input.text.find(' ')
                    if whitespace == -1:
                        whitespace = len(the_input.text)
                    the_input.text = the_input.text[:whitespace-1] + the_input.text[whitespace:]
                    the_input.new_completion(words, 0)
                    hit_copy = set(the_input.hit_list)
                if len(hit_copy) == 1:
                    the_input.do_command(' ')
                    the_input.reset_completion()
                return True
            # check if we are in the middle of the command name
            elif len(txt.split()) > 1 or\
                    (txt.endswith(' ') and not the_input.last_completion):
                command_name = txt.split()[0][1:]
                if command_name in self.commands:
                    command = self.commands[command_name]
                elif command_name in self.core.commands:
                    command = self.core.commands[command_name]
                else:           # Unknown command, cannot complete
                    return False
232
                if command.comp is None:
233 234
                    return False # There's no completion function
                else:
235
                    return command.comp(the_input)
236 237 238 239 240 241 242 243 244 245 246 247 248 249
        return False

    def execute_command(self, provided_text):
        """
        Execute the command in the input and return False if
        the input didn't contain a command
        """
        txt = provided_text or self.input.key_enter()
        if txt.startswith('/') and not txt.startswith('//') and\
                not txt.startswith('/me '):
            command = txt.strip().split()[0][1:]
            arg = txt[2+len(command):] # jump the '/' and the ' '
            func = None
            if command in self.commands: # check tab-specific commands
250
                func = self.commands[command].func
251
            elif command in self.core.commands: # check global commands
252
                func = self.core.commands[command].func
253 254 255
            else:
                low = command.lower()
                if low in self.commands:
256
                    func = self.commands[low].func
257
                elif low in self.core.commands:
258
                    func = self.core.commands[low].func
259
                else:
260 261 262
                    if self.missing_command_callback is not None:
                        error_handled = self.missing_command_callback(low)
                    if not error_handled:
263 264 265
                        self.core.information("Unknown command (%s)" %
                                              (command),
                                              'Error')
266 267 268 269 270
            if command in ('correct', 'say'): # hack
                arg = xhtml.convert_simple_to_full_colors(arg)
            else:
                arg = xhtml.clean_text_simple(arg)
            if func:
271 272
                if hasattr(self.input, "reset_completion"):
                    self.input.reset_completion()
273 274 275 276 277 278
                func(arg)
            return True
        else:
            return False

    def refresh_tab_win(self):
279
        if config.get('enable_vertical_tab_list'):
280 281 282
            left_tab_win = self.core.left_tab_win
            if left_tab_win and not self.size.core_degrade_x:
                left_tab_win.refresh()
283
        elif not self.size.core_degrade_y:
284
            self.core.tab_win.refresh()
285 286 287 288 289 290 291 292 293 294 295

    def refresh(self):
        """
        Called on each screen refresh (when something has changed)
        """
        pass

    def get_name(self):
        """
        get the name of the tab
        """
296
        return self.name
297 298 299 300 301

    def get_nick(self):
        """
        Get the nick of the tab (defaults to its name)
        """
302
        return self.name
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

    def get_text_window(self):
        """
        Returns the principal TextWin window, if there's one
        """
        return None

    def on_input(self, key, raw):
        """
        raw indicates if the key should activate the associated command or not.
        """
        pass

    def update_commands(self):
        for c in self.plugin_commands:
            if not c in self.commands:
                self.commands[c] = self.plugin_commands[c]

    def update_keys(self):
        for k in self.plugin_keys:
            if not k in self.key_func:
                self.key_func[k] = self.plugin_keys[k]

    def on_lose_focus(self):
        """
        called when this tab loses the focus.
        """
        self.state = 'normal'

    def on_gain_focus(self):
        """
        called when this tab gains the focus.
        """
        self.state = 'current'

    def on_scroll_down(self):
        """
        Defines what happens when we scroll down
        """
        pass

    def on_scroll_up(self):
        """
        Defines what happens when we scroll up
        """
        pass

    def on_line_up(self):
        """
        Defines what happens when we scroll one line up
        """
        pass

    def on_line_down(self):
        """
        Defines what happens when we scroll one line up
        """
        pass

    def on_half_scroll_down(self):
        """
        Defines what happens when we scroll half a screen down
        """
        pass

    def on_half_scroll_up(self):
        """
        Defines what happens when we scroll half a screen up
        """
        pass

    def on_info_win_size_changed(self):
        """
        Called when the window with the informations is resized
        """
        pass

    def on_close(self):
        """
        Called when the tab is to be closed
        """
        if self.input:
            self.input.on_delete()
mathieui's avatar
mathieui committed
386
        self.closed = True
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

    def matching_names(self):
        """
        Returns a list of strings that are used to name a tab with the /win
        command.  For example you could switch to a tab that returns
        ['hello', 'coucou'] using /win hel, or /win coucou
        If not implemented in the tab, it just doesn’t match with anything.
        """
        return []

    def __del__(self):
        log.debug('------ Closing tab %s', self.__class__.__name__)

class GapTab(Tab):

    def __bool__(self):
        return False

    def __len__(self):
        return 0

408 409
    @property
    def name(self):
410 411 412 413 414 415 416 417 418 419 420 421 422 423
        return ''

    def refresh(self):
        log.debug('WARNING: refresh() called on a gap tab, this should not happen')

class ChatTab(Tab):
    """
    A tab containing a chat of any type.
    Just use this class instead of Tab if the tab needs a recent-words completion
    Also, ^M is already bound to on_enter
    And also, add the /say command
    """
    plugin_commands = {}
    plugin_keys = {}
424 425
    def __init__(self, core, jid=''):
        Tab.__init__(self, core)
426 427 428 429
        self.name = jid
        self.text_win = None
        self._text_buffer = TextBuffer()
        self.chatstate = None   # can be "active", "composing", "paused", "gone", "inactive"
louiz’'s avatar
louiz’ committed
430
        # We keep a reference of the event that will set our chatstate to "paused", so that
431 432 433 434 435 436 437 438 439
        # we can delete it or change it if we need to
        self.timed_event_paused = None
        # Keeps the last sent message to complete it easily in completion_correct, and to replace it.
        self.last_sent_message = None
        self.key_func['M-v'] = self.move_separator
        self.key_func['M-h'] = self.scroll_separator
        self.key_func['M-/'] = self.last_words_completion
        self.key_func['^M'] = self.on_enter
        self.register_command('say', self.command_say,
440 441
                usage='<message>',
                shortdesc='Send the message.')
442
        self.register_command('xhtml', self.command_xhtml,
443 444
                usage='<custom xhtml>',
                shortdesc='Send custom XHTML.')
445
        self.register_command('clear', self.command_clear,
446
                shortdesc='Clear the current buffer.')
447
        self.register_command('correct', self.command_correct,
448 449
                desc='Fix the last message with whatever you want.',
                shortdesc='Correct the last message.',
450 451 452 453 454 455
                completion=self.completion_correct)
        self.chat_state = None
        self.update_commands()
        self.update_keys()

        # Get the logs
456
        log_nb = config.get('load_log')
457 458 459 460 461 462 463 464 465 466 467
        logs = self.load_logs(log_nb)

        if logs:
            for message in logs:
                self._text_buffer.add_message(**message)

    @property
    def is_muc(self):
        return False

    def load_logs(self, log_nb):
468
        logs = logger.get_logs(safeJID(self.name).bare, log_nb)
469
        return logs
470 471 472 473 474 475 476

    def log_message(self, txt, nickname, time=None, typ=1):
        """
        Log the messages in the archives.
        """
        name = safeJID(self.name).bare
        if not logger.log_message(name, nickname, txt, date=time, typ=typ):
477
            self.core.information('Unable to write in the log file', 'Error')
478

479 480 481
    def add_message(self, txt, time=None, nickname=None, forced_user=None,
                    nick_color=None, identifier=None, jid=None, history=None,
                    typ=1, highlight=False):
482 483 484
        self.log_message(txt, nickname, time=time, typ=typ)
        self._text_buffer.add_message(txt, time=time,
                nickname=nickname,
485
                highlight=highlight,
486 487 488 489 490 491
                nick_color=nick_color,
                history=history,
                user=forced_user,
                identifier=identifier,
                jid=jid)

492
    def modify_message(self, txt, old_id, new_id, user=None, jid=None, nickname=None):
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
        self.log_message(txt, nickname, typ=1)
        message = self._text_buffer.modify_message(txt, old_id, new_id, time=time, user=user, jid=jid)
        if message:
            self.text_win.modify_message(old_id, message)
            self.core.refresh_window()
            return True
        return False

    def last_words_completion(self):
        """
        Complete the input with words recently said
        """
        # build the list of the recent words
        char_we_dont_want = string.punctuation+' ’„“”…«»'
        words = list()
        for msg in self._text_buffer.messages[:-40:-1]:
            if not msg:
                continue
            txt = xhtml.clean_text(msg.txt)
            for char in char_we_dont_want:
                txt = txt.replace(char, ' ')
            for word in txt.split():
                if len(word) >= 4 and word not in words:
                    words.append(word)
517
        words.extend([word for word in config.get('words').split(':') if word])
518 519 520 521 522 523 524 525 526 527 528
        self.input.auto_completion(words, ' ', quotify=False)

    def on_enter(self):
        txt = self.input.key_enter()
        if txt:
            if not self.execute_command(txt):
                if txt.startswith('//'):
                    txt = txt[1:]
                self.command_say(xhtml.convert_simple_to_full_colors(txt))
        self.cancel_paused_delay()

529 530
    @command_args_parser.raw
    def command_xhtml(self, xhtml):
531 532 533
        """"
        /xhtml <custom xhtml>
        """
534
        message = self.generate_xhtml_message(xhtml)
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
        if message:
            message.send()

    def generate_xhtml_message(self, arg):
        if not arg:
            return
        try:
            body = xhtml.clean_text(xhtml.xhtml_to_poezio_colors(arg))
            ET.fromstring(arg)
        except:
            self.core.information('Could not send custom xhtml', 'Error')
            log.error('/xhtml: Unable to send custom xhtml', exc_info=True)
            return

        msg = self.core.xmpp.make_message(self.get_dest_jid())
        msg['body'] = body
        msg.enable('html')
        msg['html']['body'] = arg
        return msg

    def get_dest_jid(self):
556
        return self.name
557 558

    @refresh_wrapper.always
559
    def command_clear(self, ignored):
560 561 562 563 564 565 566 567 568 569 570 571 572
        """
        /clear
        """
        self._text_buffer.messages = []
        self.text_win.rebuild_everything(self._text_buffer)

    def send_chat_state(self, state, always_send=False):
        """
        Send an empty chatstate message
        """
        if not self.is_muc or self.joined:
            if state in ('active', 'inactive', 'gone') and self.inactive and not always_send:
                return
573 574
            if (config.get_by_tabname('send_chat_states', self.general_jid)
                    and self.remote_wants_chatstates is not False):
575 576 577 578 579
                msg = self.core.xmpp.make_message(self.get_dest_jid())
                msg['type'] = self.message_type
                msg['chat_state'] = state
                self.chat_state = state
                msg.send()
580
                return True
581 582 583 584 585 586 587

    def send_composing_chat_state(self, empty_after):
        """
        Send the "active" or "composing" chatstate, depending
        on the the current status of the input
        """
        name = self.general_jid
588 589
        if (config.get_by_tabname('send_chat_states', name)
                and self.remote_wants_chatstates):
590 591 592 593 594 595 596 597 598 599 600 601 602 603
            needed = 'inactive' if self.inactive else 'active'
            self.cancel_paused_delay()
            if not empty_after:
                if self.chat_state != "composing":
                    self.send_chat_state("composing")
                self.set_paused_delay(True)
            elif empty_after and self.chat_state != needed:
                self.send_chat_state(needed, True)

    def set_paused_delay(self, composing):
        """
        we create a timed event that will put us to paused
        in a few seconds
        """
604
        if not config.get_by_tabname('send_chat_states', self.general_jid):
605
            return
606 607 608
        # First, cancel the delay if it already exists, before rescheduling
        # it at a new date
        self.cancel_paused_delay()
609 610
        new_event = timed_events.DelayedEvent(4, self.send_chat_state, 'paused')
        self.core.add_timed_event(new_event)
611
        self.timed_event_paused = new_event
612 613 614 615 616 617 618

    def cancel_paused_delay(self):
        """
        Remove that event from the list and set it to None.
        Called for example when the input is emptied, or when the message
        is sent
        """
619 620 621
        if self.timed_event_paused is not None:
            self.core.remove_timed_event(self.timed_event_paused)
            self.timed_event_paused = None
622

623
    @command_args_parser.raw
624 625 626 627 628
    def command_correct(self, line):
        """
        /correct <fixed message>
        """
        if not line:
629
            self.core.command.help('correct')
630 631
            return
        if not self.last_sent_message:
632
            self.core.information('There is no message to correct.')
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
            return
        self.command_say(line, correct=True)

    def completion_correct(self, the_input):
        if self.last_sent_message and the_input.get_argument_position() == 1:
            return the_input.auto_completion([self.last_sent_message['body']], '', quotify=False)

    @property
    def inactive(self):
        """Whether we should send inactive or active as a chatstate"""
        return self.core.status.show in ('xa', 'away') or\
                (hasattr(self, 'directed_presence') and not self.directed_presence)

    def move_separator(self):
        self.text_win.remove_line_separator()
        self.text_win.add_line_separator(self._text_buffer)
        self.text_win.refresh()
        self.input.refresh()

    def get_conversation_messages(self):
        return self._text_buffer.messages

    def check_scrolled(self):
        if self.text_win.pos != 0:
            self.state = 'scrolled'

659
    @command_args_parser.raw
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
    def command_say(self, line, correct=False):
        pass

    def on_line_up(self):
        return self.text_win.scroll_up(1)

    def on_line_down(self):
        return self.text_win.scroll_down(1)

    def on_scroll_up(self):
        return self.text_win.scroll_up(self.text_win.height-1)

    def on_scroll_down(self):
        return self.text_win.scroll_down(self.text_win.height-1)

    def on_half_scroll_up(self):
        return self.text_win.scroll_up((self.text_win.height-1) // 2)

    def on_half_scroll_down(self):
        return self.text_win.scroll_down((self.text_win.height-1) // 2)

    @refresh_wrapper.always
    def scroll_separator(self):
        self.text_win.scroll_to_separator()

685 686
class OneToOneTab(ChatTab):

687 688
    def __init__(self, core, jid=''):
        ChatTab.__init__(self, core, jid)
689

690 691
        # Set to true once the first disco is done
        self.__initial_disco = False
692 693 694
        # change this to True or False when
        # we know that the remote user wants chatstates, or not.
        # None means we don’t know yet, and we send only "active" chatstates
695
        self._remote_wants_chatstates = None
696
        self.remote_supports_attention = True
697 698 699
        self.remote_supports_receipts = True
        self.check_features()

700 701 702 703 704 705 706 707 708 709 710 711 712 713
    @property
    def remote_wants_chatstates(self):
        return self._remote_wants_chatstates

    @remote_wants_chatstates.setter
    def remote_wants_chatstates(self, value):
        old_value = self._remote_wants_chatstates
        self._remote_wants_chatstates = value
        if (old_value is None and value != None) or \
                (old_value != value and value != None):
            ok = get_theme().CHAR_OK
            nope = get_theme().CHAR_EMPTY
            support = ok if value else nope
            if value:
714
                msg = '\x19%s}Contact supports chat states [%s].'
715
            else:
716
                msg = '\x19%s}Contact does not support chat states [%s].'
717 718 719 720 721
            color = dump_tuple(get_theme().COLOR_INFORMATION_TEXT)
            msg = msg % (color, support)
            self.add_message(msg, typ=0)
            self.core.refresh_window()

722
    def ack_message(self, msg_id, msg_jid):
723 724 725
        """
        Ack a message
        """
726
        new_msg = self._text_buffer.ack_message(msg_id, msg_jid)
727 728 729 730
        if new_msg:
            self.text_win.modify_message(msg_id, new_msg)
            self.core.refresh_window()

731 732 733 734 735 736 737 738 739 740 741
    def nack_message(self, error, msg_id, msg_jid):
        """
        Ack a message
        """
        new_msg = self._text_buffer.nack_message(error, msg_id, msg_jid)
        if new_msg:
            self.text_win.modify_message(msg_id, new_msg)
            self.core.refresh_window()
            return True
        return False

742 743 744 745
    @command_args_parser.raw
    def command_xhtml(self, xhtml_data):
        message = self.generate_xhtml_message(xhtml_data)
        if message:
746
            message['type'] = 'chat'
747 748 749 750 751 752 753 754 755 756
            if self.remote_supports_receipts:
                message._add_receipt = True
            if self.remote_wants_chatstates:
                message['chat_sate'] = 'active'
            message.send()
            body = xhtml.xhtml_to_poezio_colors(xhtml_data, force=True)
            self._text_buffer.add_message(body, nickname=self.core.own_nick,
                                          identifier=message['id'],)
            self.refresh()

757 758
    def check_features(self):
        "check the features supported by the other party"
759 760
        if safeJID(self.get_dest_jid()).resource:
            self.core.xmpp.plugin['xep_0030'].get_info(
761
                    jid=self.get_dest_jid(), timeout=5,
762
                    callback=self.features_checked)
763

764 765 766
    @command_args_parser.raw
    def command_attention(self, message):
        """/attention [message]"""
767 768 769 770 771 772 773 774
        if message is not '':
            self.command_say(message, attention=True)
        else:
            msg = self.core.xmpp.make_message(self.get_dest_jid())
            msg['type'] = 'chat'
            msg['attention'] = True
            msg.send()

775
    @command_args_parser.raw
776 777 778
    def command_say(self, line, correct=False, attention=False):
        pass

779 780 781 782 783
    def missing_command_callback(self, command_name):
        if command_name not in ('correct', 'attention'):
            return False

        if command_name == 'correct':
784
            feature = 'message correction'
785
        elif command_name == 'attention':
786 787 788
            feature = 'attention requests'
        msg = ('%s does not support %s, therefore the /%s '
               'command is currently disabled in this tab.')
789 790 791 792
        msg = msg % (self.name, feature, command_name)
        self.core.information(msg, 'Info')
        return True

793 794 795 796 797
    def _feature_attention(self, features):
        "Check for the 'attention' features"
        if 'urn:xmpp:attention:0' in features:
            self.remote_supports_attention = True
            self.register_command('attention', self.command_attention,
798 799 800 801 802
                                  usage='[message]',
                                  shortdesc='Request the attention.',
                                  desc='Attention: Request the attention of '
                                       'the contact. Can also send a message'
                                       ' along with the attention.')
803 804
        else:
            self.remote_supports_attention = False
805
        return self.remote_supports_attention
806 807 808 809 810 811 812 813

    def _feature_correct(self, features):
        "Check for the 'correction' feature"
        if not 'urn:xmpp:message-correct:0' in features:
            if 'correct' in self.commands:
                del self.commands['correct']
        elif not 'correct' in self.commands:
            self.register_command('correct', self.command_correct,
814 815
                    desc='Fix the last message with whatever you want.',
                    shortdesc='Correct the last message.',
816
                    completion=self.completion_correct)
817
        return 'correct' in self.commands
818 819 820 821 822 823 824

    def _feature_receipts(self, features):
        "Check for the 'receipts' feature"
        if 'urn:xmpp:receipts' in features:
            self.remote_supports_receipts = True
        else:
            self.remote_supports_receipts = False
825
        return self.remote_supports_receipts
826 827 828 829

    def features_checked(self, iq):
        "Features check callback"
        features = iq['disco_info'].get_features() or []
830 831 832
        before = ('correct' in self.commands,
                  self.remote_supports_attention,
                  self.remote_supports_receipts)
833 834 835 836
        correct = self._feature_correct(features)
        attention = self._feature_attention(features)
        receipts = self._feature_receipts(features)

837 838 839 840 841
        if (correct, attention, receipts) == before and self.__initial_disco:
            return
        else:
            self.__initial_disco = True

842 843 844
        if not (correct or attention or receipts):
            return # don’t display anything

845
        ok = get_theme().CHAR_OK
846
        nope = get_theme().CHAR_EMPTY
847 848 849 850 851

        correct = ok if correct else nope
        attention = ok if attention else nope
        receipts = ok if receipts else nope

852 853
        msg = ('\x19%s}Contact supports: correction [%s], '
               'attention [%s], receipts [%s].')
854
        color = dump_tuple(get_theme().COLOR_INFORMATION_TEXT)
855
        msg = msg % (color, correct, attention, receipts)
856
        self.add_message(msg, typ=0)
857
        self.core.refresh_window()
858