summaryrefslogtreecommitdiff
path: root/plugins/link.py
blob: 9e8b6f9727537dd39f9f4f7abe42128b8e25e80c (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
# A plugin that adds the /link command, letting you open links that are pasted
# in the conversation, without having to click them.

import re

from plugin import BasePlugin
from xhtml import clean_text
import common
import tabs

url_pattern = re.compile(r'\b(http[s]?://(?:\S+))\b', re.I|re.U)

class Plugin(BasePlugin):
    def init(self):
        for _class in (tabs.MucTab, tabs.PrivateTab, tabs.ConversationTab):
            self.api.add_tab_command(_class, 'link', self.command_link,
                    usage='[num]',
                    help='Opens the last link from the conversation into a browser.\nIf [num] is given, then it will open the num-th link displayed.',
                    short='Open links into a browser')

    def find_link(self, nb):
        messages = self.api.get_conversation_messages()
        if not messages:
            return None
        for message in messages[::-1]:
            matches = url_pattern.findall(clean_text(message.txt))
            if matches:
                for url in matches[::-1]:
                    if nb == 1:
                        return url
                    else:
                        nb -= 1
        return None

    def command_link(self, args):
        args = common.shell_split(args)
        if len(args) == 1:
            try:
                nb = int(args[0])
            except:
                return self.api.run_command('/help link')
        else:
            nb = 1
        link = self.find_link(nb)
        if link:
            self.core.exec_command([self.config.get('browser', 'firefox'), link])
        else:
            self.api.information('No URL found.', 'Warning')

    def cleanup(self):
        del self.config