summaryrefslogtreecommitdiff
path: root/plugins/lastlog.py
blob: ae946f463e9341820a14f1742158846adb4286cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2018 Maxime “pep” Buquet <pep@bouah.net>
#
# Distributed under terms of the zlib license. See the COPYING file.

"""
    Search provided string in the buffer and return all results on the screen
"""

import re
from poezio.plugin import BasePlugin
from poezio.tabs import ConversationTab, PrivateTab, MucTab
from poezio.text_buffer import Message, TextBuffer


def add_line(text_buffer: TextBuffer, text: str) -> None:
    """Adds a textual entry in the TextBuffer"""
    text_buffer.add_message(
        text,
        None,  # Time
        None,  # Nickname
        None,  # Nick Color
        False,  # History
        None,  # User
        False,  # Highlight
        None,  # Identifier
        None,  # str_time
        None,  # Jid
    )


def add_message(text_buffer: TextBuffer, msg: Message) -> None:
    """Adds a message to the TextBuffer"""
    text_buffer.add_message(
        msg.txt,
        msg.time,
        None,  # Nickname
        None,  # Nick Color
        False,  # History
        None,  # User
        msg.highlight,
        msg.identifier,
        msg.str_time,
        None,  # Jid
    )


class Plugin(BasePlugin):
    """Lastlog Plugin"""

    def init(self):
        self.api.add_tab_command(
            ConversationTab, 'lastlog', self.command_lastlog,
            usage='<keyword>', help=(
                'Search <keyword> in the buffer and returns results'
                'on the screen'
            ),
        )
        self.api.add_tab_command(
            MucTab, 'lastlog', self.command_lastlog, usage='<keyword>',
            help=('Search <keyword> in the buffer and returns results'
                  'on the screen'),
        )
        self.api.add_tab_command(
            PrivateTab, 'lastlog', self.command_lastlog, usage='<keyword>',
            help=('Search <keyword> in the buffer and returns results'
                  'on the screen'),
        )

    def command_lastlog(self, input_):
        """Define lastlog command"""

        text_buffer = self.api.current_tab()._text_buffer
        search_re = re.compile(input_)

        res = []
        for message in text_buffer.messages:
            if message.nickname is not None:
                self.core.information('Foo: %s> %s' % (message.nickname, message.txt), 'Info')
            if message.nickname is not None and \
               search_re.search(message.txt) is not None:
                res.append(message)

        add_line(text_buffer, "Lastlog for '%s', %d match(es)" % (input_, len(res)))

        for message in res:
            message.nickname = None
            add_message(text_buffer, message)