basetabs.py 36.7 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
"""

import logging
import string
import time
19
from datetime import datetime
20
from xml.etree import cElementTree as ET
21
from typing import Any, Callable, Dict, List, Optional, Union
mathieui's avatar
mathieui committed
22

23
from slixmpp import JID, InvalidJID, Message
24

25
from poezio.core.structs import Command, Completion, Status
26 27 28
from poezio import timed_events
from poezio import windows
from poezio import xhtml
29 30 31
from poezio import poopt
from math import ceil, log10
from poezio.windows.funcs import truncate_nick, parse_attrs
32 33 34 35 36
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
37
from poezio.theming import to_curses_attr, get_theme, dump_tuple
38
from poezio.decorators import command_args_parser
39

mathieui's avatar
mathieui committed
40 41
log = logging.getLogger(__name__)

42
# getters for tab colors (lambdas, so that they are dynamic)
43
STATE_COLORS = {
mathieui's avatar
mathieui committed
44 45 46 47 48 49 50 51 52 53 54 55
    'disconnected': lambda: get_theme().COLOR_TAB_DISCONNECTED,
    'scrolled': lambda: get_theme().COLOR_TAB_SCROLLED,
    'nonempty': lambda: get_theme().COLOR_TAB_NONEMPTY,
    'joined': lambda: get_theme().COLOR_TAB_JOINED,
    'message': lambda: get_theme().COLOR_TAB_NEW_MESSAGE,
    'composing': lambda: get_theme().COLOR_TAB_COMPOSING,
    '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,
}
56
VERTICAL_STATE_COLORS = {
mathieui's avatar
mathieui committed
57 58 59 60 61 62 63 64 65 66 67 68
    'disconnected': lambda: get_theme().COLOR_VERTICAL_TAB_DISCONNECTED,
    'scrolled': lambda: get_theme().COLOR_VERTICAL_TAB_SCROLLED,
    'nonempty': lambda: get_theme().COLOR_VERTICAL_TAB_NONEMPTY,
    'joined': lambda: get_theme().COLOR_VERTICAL_TAB_JOINED,
    'message': lambda: get_theme().COLOR_VERTICAL_TAB_NEW_MESSAGE,
    'composing': lambda: get_theme().COLOR_VERTICAL_TAB_COMPOSING,
    '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,
}
69

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

86
SHOW_NAME = {
mathieui's avatar
mathieui committed
87 88 89 90 91 92 93
    'dnd': 'busy',
    'away': 'away',
    'xa': 'not available',
    'chat': 'chatty',
    '': 'available'
}

94

95
class Tab:
96
    plugin_commands = {}  # type: Dict[str, Command]
mathieui's avatar
mathieui committed
97
    plugin_keys = {}  # type: Dict[str, Callable]
98 99 100
    # Placeholder values, set on resize
    height = 1
    width = 1
mathieui's avatar
mathieui committed
101

102 103
    def __init__(self, core):
        self.core = core
104
        self.nb = 0
105 106
        if not hasattr(self, 'name'):
            self.name = self.__class__.__name__
107
        self.input = None
mathieui's avatar
mathieui committed
108
        self.closed = False
109
        self._state = 'normal'
110
        self._prev_state = None
111

112
        self.need_resize = False
mathieui's avatar
mathieui committed
113 114 115
        self.key_func = {}  # each tab should add their keys in there
        # and use them in on_input
        self.commands = {}  # and their own commands
116

117
    @property
mathieui's avatar
mathieui committed
118
    def size(self) -> int:
119
        return self.core.size
120

121
    @staticmethod
mathieui's avatar
mathieui committed
122
    def tab_win_height() -> int:
123 124 125 126
        """
        Returns 1 or 0, depending on if we are using the vertical tab list
        or not.
        """
127
        if config.get('enable_vertical_tab_list'):
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
            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
mathieui's avatar
mathieui committed
144
    def state(self) -> str:
145 146 147
        return self._state

    @state.setter
mathieui's avatar
mathieui committed
148
    def state(self, value: str):
149
        if value not in STATE_COLORS:
150 151 152 153
            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'):
mathieui's avatar
mathieui committed
154 155 156 157 158 159 160
            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')
161 162
        else:
            self._state = value
163 164 165
            if self._state == 'current':
                self._prev_state = None

mathieui's avatar
mathieui committed
166
    def set_state(self, value: str):
167 168
        self._state = value

169 170 171 172 173 174 175 176 177 178
    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'
179 180 181

    @staticmethod
    def resize(scr):
182
        Tab.height, Tab.width = scr.getmaxyx()
183
        windows.base_wins.TAB_WIN = scr
184

185 186 187 188 189 190 191 192
    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

mathieui's avatar
mathieui committed
193
    def register_commands_batch(self, commands: List[Dict[str, Any]]):
194 195 196 197 198 199 200 201 202 203
        """
        Add several commands in a row, using a list of dictionaries
        """
        for command in commands:
            name = command['name']
            func = command['func']
            desc = command.get('desc', '')
            shortdesc = command.get('shortdesc', '')
            completion = command.get('completion')
            usage = command.get('usage', '')
mathieui's avatar
mathieui committed
204 205 206 207 208 209 210 211 212
            self.register_command(
                name,
                func,
                desc=desc,
                shortdesc=shortdesc,
                completion=completion,
                usage=usage)

    def register_command(self,
mathieui's avatar
mathieui committed
213 214
                         name: str,
                         func: Callable,
mathieui's avatar
mathieui committed
215 216 217
                         *,
                         desc='',
                         shortdesc='',
mathieui's avatar
mathieui committed
218
                         completion: Optional[Callable] = None,
mathieui's avatar
mathieui committed
219
                         usage=''):
220 221 222 223 224 225 226
        """
        Add a command
        """
        if name in self.commands:
            return
        if not desc and shortdesc:
            desc = shortdesc
227
        self.commands[name] = Command(func, desc, completion, shortdesc, usage)
228

mathieui's avatar
mathieui committed
229
    def complete_commands(self, the_input: windows.Input) -> bool:
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
        """
        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
247
                # one possibility. The next tab will complete the argument.
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
                # Otherwise we would need to add a useless space before being
                # able to complete the arguments.
                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]
mathieui's avatar
mathieui committed
263
                else:  # Unknown command, cannot complete
264
                    return False
265
                if command.comp is None:
mathieui's avatar
mathieui committed
266
                    return False  # There's no completion function
mathieui's avatar
mathieui committed
267 268 269 270
                comp = command.comp(the_input)
                if comp:
                    return comp.run()
                return comp
271 272
        return False

mathieui's avatar
mathieui committed
273
    def execute_command(self, provided_text: str) -> bool:
274 275 276 277 278 279 280 281
        """
        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:]
mathieui's avatar
mathieui committed
282
            arg = txt[2 + len(command):]  # jump the '/' and the ' '
283
            func = None
mathieui's avatar
mathieui committed
284
            if command in self.commands:  # check tab-specific commands
285
                func = self.commands[command].func
mathieui's avatar
mathieui committed
286
            elif command in self.core.commands:  # check global commands
287
                func = self.core.commands[command].func
288 289 290
            else:
                low = command.lower()
                if low in self.commands:
291
                    func = self.commands[low].func
292
                elif low in self.core.commands:
293
                    func = self.core.commands[low].func
294
                else:
295 296 297
                    if self.missing_command_callback is not None:
                        error_handled = self.missing_command_callback(low)
                    if not error_handled:
mathieui's avatar
mathieui committed
298 299
                        self.core.information(
                            "Unknown command (%s)" % (command), 'Error')
mathieui's avatar
mathieui committed
300
            if command in ('correct', 'say'):  # hack
301 302 303 304
                arg = xhtml.convert_simple_to_full_colors(arg)
            else:
                arg = xhtml.clean_text_simple(arg)
            if func:
305 306
                if hasattr(self.input, "reset_completion"):
                    self.input.reset_completion()
307 308 309 310 311 312
                func(arg)
            return True
        else:
            return False

    def refresh_tab_win(self):
313
        if config.get('enable_vertical_tab_list'):
314 315 316
            left_tab_win = self.core.left_tab_win
            if left_tab_win and not self.size.core_degrade_x:
                left_tab_win.refresh()
317
        elif not self.size.core_degrade_y:
318
            self.core.tab_win.refresh()
319

320 321 322 323 324 325
    def refresh_input(self):
        """Refresh the current input if any"""
        if self.input is not None:
            self.input.refresh()
            self.core.doupdate()

326 327 328 329 330 331 332 333 334 335
    def refresh(self):
        """
        Called on each screen refresh (when something has changed)
        """
        pass

    def get_name(self):
        """
        get the name of the tab
        """
336
        return self.name
337

mathieui's avatar
mathieui committed
338
    def get_nick(self) -> str:
339 340 341
        """
        Get the nick of the tab (defaults to its name)
        """
342
        return self.name
343

mathieui's avatar
mathieui committed
344
    def get_text_window(self) -> Optional[windows.TextWin]:
345 346 347 348 349
        """
        Returns the principal TextWin window, if there's one
        """
        return None

mathieui's avatar
mathieui committed
350
    def on_input(self, key: str, raw: bool):
351 352 353 354 355 356 357
        """
        raw indicates if the key should activate the associated command or not.
        """
        pass

    def update_commands(self):
        for c in self.plugin_commands:
358
            if c not in self.commands:
359 360 361 362
                self.commands[c] = self.plugin_commands[c]

    def update_keys(self):
        for k in self.plugin_keys:
363
            if k not in self.key_func:
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 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
                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):
        """
416
        Called when the window with the information is resized
417 418 419 420 421 422 423 424 425
        """
        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
426
        self.closed = True
427

mathieui's avatar
mathieui committed
428
    def matching_names(self) -> List[str]:
429 430 431 432 433 434 435 436 437 438 439 440
        """
        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__)


mathieui's avatar
mathieui committed
441
class GapTab(Tab):
442 443 444 445 446 447
    def __bool__(self):
        return False

    def __len__(self):
        return 0

448 449
    @property
    def name(self):
450 451 452
        return ''

    def refresh(self):
mathieui's avatar
mathieui committed
453 454 455
        log.debug(
            'WARNING: refresh() called on a gap tab, this should not happen')

456 457 458 459 460 461 462 463

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
    """
464 465
    plugin_commands = {}  # type: Dict[str, Command]
    plugin_keys = {}  # type: Dict[str, Callable]
mathieui's avatar
mathieui committed
466
    message_type = 'chat'
mathieui's avatar
mathieui committed
467

468
    def __init__(self, core, jid: Union[JID, str]):
469
        Tab.__init__(self, core)
470 471 472 473 474 475

        if not isinstance(jid, JID):
            jid = JID(jid)
        assert jid.domain
        self._jid = jid

476
        self._name = jid.full  # type: Optional[str]
477
        self.text_win = None
mathieui's avatar
mathieui committed
478
        self.directed_presence = None
479
        self._text_buffer = TextBuffer()
mathieui's avatar
mathieui committed
480
        self.chatstate = None  # can be "active", "composing", "paused", "gone", "inactive"
louiz’'s avatar
louiz’ committed
481
        # We keep a reference of the event that will set our chatstate to "paused", so that
482 483
        # we can delete it or change it if we need to
        self.timed_event_paused = None
484
        self.timed_event_not_paused = None
485
        # Keeps the last sent message to complete it easily in completion_correct, and to replace it.
mathieui's avatar
mathieui committed
486
        self.last_sent_message = {}
487 488 489 490
        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
mathieui's avatar
mathieui committed
491 492 493 494 495
        self.register_command(
            'say',
            self.command_say,
            usage='<message>',
            shortdesc='Send the message.')
Madhur Garg's avatar
Madhur Garg committed
496 497 498
        self.register_command(
            'sb',
            self.command_sb,
499
            usage="end home clear status goto <+|-linecount>|<linenum>|<timestamp>",
500
            shortdesc='Scrollback to the given line number, message, or clear the buffer.')
mathieui's avatar
mathieui committed
501 502 503 504 505 506 507 508 509 510 511 512 513
        self.register_command(
            'xhtml',
            self.command_xhtml,
            usage='<custom xhtml>',
            shortdesc='Send custom XHTML.')
        self.register_command(
            'clear', self.command_clear, shortdesc='Clear the current buffer.')
        self.register_command(
            'correct',
            self.command_correct,
            desc='Fix the last message with whatever you want.',
            shortdesc='Correct the last message.',
            completion=self.completion_correct)
514 515 516 517 518
        self.chat_state = None
        self.update_commands()
        self.update_keys()

        # Get the logs
519
        log_nb = config.get('load_log')
520 521 522 523 524 525
        logs = self.load_logs(log_nb)

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

526 527
    @property
    def name(self) -> str:
528 529 530
        if self._name is not None:
            return self._name
        return self._jid.full
531 532 533 534

    @name.setter
    def name(self, value: Union[JID, str]) -> None:
        if isinstance(value, JID):
535
            self.jid = value
536 537 538 539
        elif isinstance(value, str):
            try:
                value = JID(value)
                if value.domain:
540
                    self._jid = value
541
            except InvalidJID:
542
                self._name = value
543
        else:
544
            raise TypeError("Name %r must be of type JID or str." % value)
545 546

    @property
547
    def jid(self) -> JID:
548
        return self._jid
549 550

    @jid.setter
551
    def jid(self, value: JID) -> None:
552
        if not isinstance(value, JID):
553 554
            raise TypeError("Jid %r must be of type JID." % value)
        assert value.domain
555
        self._jid = value
556

mathieui's avatar
mathieui committed
557
    @property
mathieui's avatar
mathieui committed
558
    def general_jid(self) -> JID:
559
        raise NotImplementedError
mathieui's avatar
mathieui committed
560

mathieui's avatar
mathieui committed
561
    def load_logs(self, log_nb: int) -> Optional[List[Dict[str, Any]]]:
562
        return logger.get_logs(self.jid.bare, log_nb)
563

mathieui's avatar
mathieui committed
564 565 566 567 568
    def log_message(self,
                    txt: str,
                    nickname: str,
                    time: Optional[datetime] = None,
                    typ=1):
569 570 571
        """
        Log the messages in the archives.
        """
572 573 574
        name = self.jid.bare
        if not logger.log_message(name, nickname, txt, date=time, typ=typ):
            self.core.information('Unable to write in the log file', 'Error')
575

mathieui's avatar
mathieui committed
576 577 578 579 580 581 582 583 584 585 586
    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):
587
        self.log_message(txt, nickname, time=time, typ=typ)
mathieui's avatar
mathieui committed
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
        self._text_buffer.add_message(
            txt,
            time=time,
            nickname=nickname,
            highlight=highlight,
            nick_color=nick_color,
            history=history,
            user=forced_user,
            identifier=identifier,
            jid=jid)

    def modify_message(self,
                       txt,
                       old_id,
                       new_id,
                       user=None,
                       jid=None,
                       nickname=None):
606
        self.log_message(txt, nickname, typ=1)
mathieui's avatar
mathieui committed
607 608
        message = self._text_buffer.modify_message(
            txt, old_id, new_id, time=time, user=user, jid=jid)
609 610 611 612 613 614 615 616 617 618 619
        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
mathieui's avatar
mathieui committed
620
        char_we_dont_want = string.punctuation + ' ’„“”…«»'
621
        words = []
622 623 624 625 626 627 628 629 630
        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)
631
        words.extend([word for word in config.get('words').split(':') if word])
632 633 634 635 636 637 638 639 640 641 642
        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()

643 644
    @command_args_parser.raw
    def command_xhtml(self, xhtml):
645 646 647
        """"
        /xhtml <custom xhtml>
        """
648
        message = self.generate_xhtml_message(xhtml)
649 650 651
        if message:
            message.send()

mathieui's avatar
mathieui committed
652
    def generate_xhtml_message(self, arg: str) -> Message:
653 654 655
        if not arg:
            return
        try:
mathieui's avatar
mathieui committed
656 657
            body = xhtml.clean_text(
                xhtml.xhtml_to_poezio_colors(arg, force=True))
658 659 660 661 662 663 664 665 666 667 668 669
            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

mathieui's avatar
mathieui committed
670
    def get_dest_jid(self) -> JID:
Maxime Buquet's avatar
Maxime Buquet committed
671
        return self.jid
672 673

    @refresh_wrapper.always
674
    def command_clear(self, ignored):
675 676 677 678 679 680
        """
        /clear
        """
        self._text_buffer.messages = []
        self.text_win.rebuild_everything(self._text_buffer)

mathieui's avatar
mathieui committed
681 682 683 684
    def check_send_chat_state(self):
        "If we should send a chat state"
        return True

685 686 687 688
    def send_chat_state(self, state, always_send=False):
        """
        Send an empty chatstate message
        """
mathieui's avatar
mathieui committed
689
        if self.check_send_chat_state():
mathieui's avatar
mathieui committed
690 691
            if state in ('active', 'inactive',
                         'gone') and self.inactive and not always_send:
692
                return
693
            if config.get_by_tabname('send_chat_states', self.general_jid):
694 695 696 697 698
                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()
699
                return True
700 701 702 703 704 705 706

    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
707
        if config.get_by_tabname('send_chat_states', name):
708 709 710 711 712 713 714 715 716 717 718 719 720 721
            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
        """
722
        if not config.get_by_tabname('send_chat_states', self.general_jid):
723
            return
724 725 726
        # First, cancel the delay if it already exists, before rescheduling
        # it at a new date
        self.cancel_paused_delay()
mathieui's avatar
mathieui committed
727 728
        new_event = timed_events.DelayedEvent(4, self.send_chat_state,
                                              'paused')
729
        self.core.add_timed_event(new_event)
730
        self.timed_event_paused = new_event
mathieui's avatar
mathieui committed
731 732 733
        new_event = timed_events.DelayedEvent(
            30, self.send_chat_state, 'inactive'
            if self.inactive else 'active')
734
        self.core.add_timed_event(new_event)
735
        self.timed_event_not_paused = new_event
736 737 738 739 740 741 742

    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
        """
743 744 745
        if self.timed_event_paused is not None:
            self.core.remove_timed_event(self.timed_event_paused)
            self.timed_event_paused = None
746 747
            self.core.remove_timed_event(self.timed_event_not_paused)
            self.timed_event_not_paused = None
748

749
    @command_args_parser.raw
750 751 752 753 754
    def command_correct(self, line):
        """
        /correct <fixed message>
        """
        if not line:
755
            self.core.command.help('correct')
756 757
            return
        if not self.last_sent_message:
758
            self.core.information('There is no message to correct.', 'Error')
759 760 761 762 763
            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:
mathieui's avatar
mathieui committed
764 765 766 767
            return Completion(
                the_input.auto_completion, [self.last_sent_message['body']],
                '',
                quotify=False)
768
        return True
769 770

    @property
mathieui's avatar
mathieui committed
771
    def inactive(self) -> bool:
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
        """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'

789
    @command_args_parser.raw
790 791 792
    def command_say(self, line, correct=False):
        pass

Madhur Garg's avatar
Madhur Garg committed
793 794 795 796 797 798
    @command_args_parser.quoted(0, 2)
    def command_sb(self, args):
        """
        /sb
        """
        if args is None or len(args) == 0:
Madhur Garg's avatar
Madhur Garg committed
799 800
            args = ['end']
        if len(args) == 1:
Madhur Garg's avatar
Madhur Garg committed
801 802 803
            if args[0] == 'end':
                self.text_win.scroll_down(len(self.text_win.built_lines))
                self.core.refresh_window()
804
                return
Madhur Garg's avatar
Madhur Garg committed
805 806 807
            elif args[0] == 'home':
                self.text_win.scroll_up(len(self.text_win.built_lines))
                self.core.refresh_window()
808
                return
Madhur Garg's avatar
Madhur Garg committed
809 810 811 812
            elif args[0] == 'clear':
                self._text_buffer.messages = []
                self.text_win.rebuild_everything(self._text_buffer)
                self.core.refresh_window()
813
                return
Madhur Garg's avatar
Madhur Garg committed
814
            elif args[0] == 'status':
815
                self.core.information('Total %s lines in this tab.' % len(self.text_win.built_lines), 'Info')
816
                return
Madhur Garg's avatar
Madhur Garg committed
817
        elif len(args) == 2 and args[0] == 'goto':
818 819 820
            for fmt in ('%d %H:%M', '%d %H:%M:%S', '%d:%m %H:%M', '%d:%m %H:%M:%S', '%H:%M', '%H:%M:%S'):
                try:
                    new_date = datetime.strptime(args[1], fmt)
821
                    if 'd' in fmt and 'm' in fmt:
822 823 824 825 826 827 828
                        new_date = new_date.replace(year=datetime.now().year)
                    elif 'd' in fmt:
                        new_date = new_date.replace(year=datetime.now().year, month=datetime.now().month)
                    else:
                        new_date = new_date.replace(year=datetime.now().year, month=datetime.now().month, day=datetime.now().day)
                except ValueError:
                    pass
829
            if args[1].startswith('-'):
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
                if ' ' in args[1]:
                    new_args = args[1].split(' ')
                    new_args[0] = new_args[0].strip('-')
                    new_date = datetime.now()
                    if new_args[0].isdigit():
                        new_date = new_date.replace(day=new_date.day - int(new_args[0]))
                    for fmt in ('%H:%M', '%H:%M:%S'):
                        try:
                            arg_date = datetime.strptime(new_args[1], fmt)
                            new_date = new_date.replace(hour=arg_date.hour, minute=arg_date.minute, second=arg_date.second)
                        except ValueError:
                            pass
                else:
                    scroll_len = args[1].strip('-')
                    if scroll_len.isdigit():
                        self.text_win.scroll_down(int(scroll_len))
                        self.core.refresh_window()
                        return
848
            elif args[1].startswith('+'):
849 850
                scroll_len = args[1].strip('+')
                if scroll_len.isdigit():
Madhur Garg's avatar
Madhur Garg committed
851 852 853
                    self.text_win.scroll_up(int(scroll_len))
                    self.core.refresh_window()
                    return
854 855 856
            elif args[1].isdigit():
                if len(self.text_win.built_lines) - self.text_win.height >= int(args[1]):
                    self.text_win.pos = len(self.text_win.built_lines) - self.text_win.height - int(args[1])
Madhur Garg's avatar
Madhur Garg committed
857 858
                    self.core.refresh_window()
                    return
Madhur Garg's avatar
Madhur Garg committed
859 860 861 862 863 864
                else:
                    self.text_win.pos = 0
                    self.core.refresh_window()
                    return
            elif args[1] == '0':
                args = ['home']
Madhur Garg's avatar
Madhur Garg committed
865
            text_buffer = self._text_buffer
866 867
            built_lines = []
            message_count = 0
Madhur Garg's avatar
Madhur Garg committed
868
            for message in text_buffer.messages:
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
                txt = message.txt
                timestamp = config.get('show_timestamps')
                nick_size = config.get('max_nick_length')
                nick = truncate_nick(message.nickname, nick_size)
                offset = 0
                theme = get_theme()
                if message.ack:
                    if message.ack > 0:
                        offset += poopt.wcswidth(theme.CHAR_ACK_RECEIVED) + 1
                    else:
                        offset += poopt.wcswidth(theme.CHAR_NACK) + 1
                if nick:
                    offset += poopt.wcswidth(nick) + 2
                if message.revisions > 0:
                    offset += ceil(log10(message.revisions + 1))
                if message.me:
                    offset += 1
                if timestamp:
                    if message.str_time:
                        offset += 1 + len(message.str_time)
                    if theme.CHAR_TIME_LEFT and message.str_time:
                        offset += 1
                    if theme.CHAR_TIME_RIGHT and message.str_time:
                        offset += 1
                lines = poopt.cut_text(txt, self.text_win.width - offset - 1)
                for line in lines:
                    built_lines.append(line)
                if message.time <= new_date:
                    message_count += 1
                    if len(self.text_win.built_lines) - self.text_win.height >= len(built_lines):
                        self.text_win.pos = len(self.text_win.built_lines) - self.text_win.height - len(built_lines) + 1
Madhur Garg's avatar
Madhur Garg committed
900 901
                    else:
                        self.text_win.pos = 0
902 903
            if message_count == 0:
                self.text_win.scroll_up(len(self.text_win.built_lines))
904
            self.core.refresh_window()
Madhur Garg's avatar
Madhur Garg committed
905

906 907 908 909 910 911 912
    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):
mathieui's avatar
mathieui committed
913
        return self.text_win.scroll_up(self.text_win.height - 1)
914 915

    def on_scroll_down(self):
mathieui's avatar
mathieui committed
916
        return self.text_win.scroll_down(self.text_win.height - 1)
917 918

    def on_half_scroll_up(self):
mathieui's avatar
mathieui committed
919
        return self.text_win.scroll_up((self.text_win.height - 1) // 2)
920 921

    def on_half_scroll_down(self):
mathieui's avatar
mathieui committed
922
        return self.text_win.scroll_down((self.text_win.height - 1) // 2)
923 924 925 926 927

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

928

mathieui's avatar
mathieui committed
929
class OneToOneTab(ChatTab):
930
    def __init__(self, core, jid):
931
        ChatTab.__init__(self, core, jid)
932

933 934 935
        self.__status = Status("", "")
        self.last_remote_message = datetime.now()

936 937
        # Set to true once the first disco is done
        self.__initial_disco = False
938
        self.check_features()
mathieui's avatar
mathieui committed
939 940 941 942
        self.register_command(
            'unquery', self.command_unquery, shortdesc='Close the tab.')
        self.register_command(
            'close', self.command_unquery, shortdesc='Close the tab.')
943
        self.register_command(
mathieui's avatar
mathieui committed
944 945
            'attention',
            self.command_attention,
946 947 948
            usage='[message]',
            shortdesc='Request the attention.',
            desc='Attention: Request the attention of the contact.  Can also '
mathieui's avatar
mathieui committed
949
            'send a message along with the attention.')
950

951 952 953 954 955
    def remote_user_color(self):
        return dump_tuple(get_theme().COLOR_REMOTE_USER)

    def update_status(self, status):
        old_status = self.__status
mathieui's avatar
mathieui committed
956 957
        if not (old_status.show != status.show
                or old_status.message != status.message):
958 959 960
            return
        self.__status = status
        hide_status_change = config.get_by_tabname('hide_status_change',
Maxime Buquet's avatar
Maxime Buquet committed
961
                                                   self.jid.bare)
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
        now = datetime.now()
        dff = now - self.last_remote_message
        if hide_status_change > -1 and dff.total_seconds() > hide_status_change:
            return

        info_c = dump_tuple(get_theme().COLOR_INFORMATION_TEXT)
        nick = self.get_nick()
        remote = self.remote_user_color()
        msg = '\x19%(color)s}%(nick)s\x19%(info)s} changed: '
        msg %= {'color': remote, 'nick': nick, 'info': info_c}
        if status.message != old_status.message and status.message:
            msg += 'status: %s, ' % status.message
        if status.show in SHOW_NAME:
            msg += 'show: %s, ' % SHOW_NAME[status.show]
        self.add_message(msg[:-2], typ=2)

978
    def ack_message(self, msg_id: str, msg_jid: JID):
979 980 981
        """
        Ack a message
        """
982
        new_msg = self._text_buffer.ack_message(msg_id, msg_jid)
983 984 985 986
        if new_msg:
            self.text_win.modify_message(msg_id, new_msg)
            self.core.refresh_window()

987
    def nack_message(self, error: str, msg_id: str, msg_jid: JID):
988
        """
989
        Non-ack a message (e.g. timeout)
990 991 992 993 994 995 996 997
        """
        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

998 999 1000 1001
    @command_args_parser.raw
    def command_xhtml(self, xhtml_data):
        message = self.generate_xhtml_message(xhtml_data)
        if message:
1002
            message['type'] = 'chat'
1003
            message._add_receipt = True
1004
            message['chat_sate'] = 'active'
1005 1006
            message.send()
            body = xhtml.xhtml_to_poezio_colors(xhtml_data, force=True)
mathieui's avatar
mathieui committed
1007 1008 1009
            self._text_buffer.add_message(
                body,
                nickname=self.core.own_nick,
1010
                nick_color=get_theme().COLOR_OWN_NICK,
mathieui's avatar
mathieui committed
1011
                identifier=message['id'],
mathieui's avatar
mathieui committed
1012
                jid=self.core.xmpp.boundjid)
1013 1014
            self.refresh()

1015 1016
    def check_features(self):
        "check the features supported by the other party"
1017 1018
        if safeJID(self.get_dest_jid()).resource:
            self.core.xmpp.plugin['xep_0030'].get_info(
mathieui's avatar
mathieui committed
1019 1020 1021
                jid=self.get_dest_jid(),
                timeout=5,
                callback=self.features_checked)
1022

1023 1024 1025
    @command_args_parser.raw
    def command_attention(self, message):
        """/attention [message]"""
1026 1027 1028 1029 1030 1031 1032 1033
        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()

1034
    @command_args_parser.raw
1035 1036 1037
    def command_say(self, line, correct=False, attention=False):
        pass

1038 1039 1040 1041 1042
    @command_args_parser.ignored
    def command_unquery(self):
        """
        /unquery
        """
mathieui's avatar
mathieui committed
1043
        self.send_chat_state('gone', always_send=True)
1044 1045
        self.core.close_tab(self)

1046 1047 1048 1049 1050
    def missing_command_callback(self, command_name):
        if command_name not in ('correct', 'attention'):
            return False

        if command_name == 'correct':
1051
            feature = 'message correction'
1052
        elif command_name == 'attention':
1053 1054 1055
            feature = 'attention requests'
        msg = ('%s does not support %s, therefore the /%s '
               'command is currently disabled in this tab.')
1056 1057 1058 1059
        msg = msg % (self.name, feature, command_name)
        self.core.information(msg, 'Info')
        return True

1060 1061 1062
    def features_checked(self, iq):
        "Features check callback"
        features = iq['disco_info'].get_features() or []