2011-10-29 05:18:19 +00:00
|
|
|
# A plugin that adds the /link command, letting you open links that are pasted
|
|
|
|
# in the conversation, without having to click them.
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
2012-05-13 16:45:40 +00:00
|
|
|
from plugin import BasePlugin
|
2011-10-29 05:18:19 +00:00
|
|
|
from xhtml import clean_text
|
|
|
|
import common
|
2012-05-13 16:45:40 +00:00
|
|
|
import tabs
|
2011-10-29 05:18:19 +00:00
|
|
|
|
|
|
|
url_pattern = re.compile(r'\b(http[s]?://(?:\S+))\b', re.I|re.U)
|
|
|
|
|
|
|
|
class Plugin(BasePlugin):
|
|
|
|
def init(self):
|
2013-03-01 18:25:31 +00:00
|
|
|
for _class in (tabs.MucTab, tabs.PrivateTab, tabs.ConversationTab):
|
2013-03-08 21:53:35 +00:00
|
|
|
self.api.add_tab_command(_class, 'link', self.command_link,
|
2013-03-01 18:25:31 +00:00
|
|
|
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')
|
2011-10-29 05:18:19 +00:00
|
|
|
|
|
|
|
def find_link(self, nb):
|
2013-03-08 21:53:35 +00:00
|
|
|
messages = self.api.get_conversation_messages()
|
2011-10-29 05:18:19 +00:00
|
|
|
if not messages:
|
|
|
|
return None
|
|
|
|
for message in messages[::-1]:
|
2011-10-29 15:20:18 +00:00
|
|
|
matches = url_pattern.findall(clean_text(message.txt))
|
|
|
|
if matches:
|
|
|
|
for url in matches[::-1]:
|
2011-10-29 05:18:19 +00:00
|
|
|
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:
|
2013-03-08 21:53:35 +00:00
|
|
|
return self.api.run_command('/help link')
|
2011-10-29 05:18:19 +00:00
|
|
|
else:
|
|
|
|
nb = 1
|
|
|
|
link = self.find_link(nb)
|
|
|
|
if link:
|
2012-10-22 15:14:21 +00:00
|
|
|
self.core.exec_command([self.config.get('browser', 'firefox'), link])
|
2011-10-29 05:18:19 +00:00
|
|
|
else:
|
2013-03-08 21:53:35 +00:00
|
|
|
self.api.information('No URL found.', 'Warning')
|
2011-10-29 05:18:19 +00:00
|
|
|
|
|
|
|
def cleanup(self):
|
|
|
|
del self.config
|