summaryrefslogtreecommitdiff
path: root/plugins/link.py
diff options
context:
space:
mode:
authorFlorent Le Coz <louiz@louiz.org>2011-11-09 01:26:54 +0100
committerFlorent Le Coz <louiz@louiz.org>2011-11-09 01:26:54 +0100
commit75ae1772e49a59b373c26d1c942f25edd473921c (patch)
tree65d8faee081db6c3800a48eff7007a4b626e4252 /plugins/link.py
parent8f02b6eb8e022ed1d34e0a0b0c7376eaf58acc32 (diff)
parent23d81901e7725bf67363113bc36a6e171bdb2033 (diff)
downloadpoezio-75ae1772e49a59b373c26d1c942f25edd473921c.tar.gz
poezio-75ae1772e49a59b373c26d1c942f25edd473921c.tar.bz2
poezio-75ae1772e49a59b373c26d1c942f25edd473921c.tar.xz
poezio-75ae1772e49a59b373c26d1c942f25edd473921c.zip
Merge branch 'master' of http://git.louiz.org/poezio
Diffstat (limited to 'plugins/link.py')
-rw-r--r--plugins/link.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/plugins/link.py b/plugins/link.py
new file mode 100644
index 00000000..8ef52982
--- /dev/null
+++ b/plugins/link.py
@@ -0,0 +1,47 @@
+# A plugin that adds the /link command, letting you open links that are pasted
+# in the conversation, without having to click them.
+
+import os
+import re
+
+from plugin import BasePlugin, PluginConfig
+from xhtml import clean_text
+import common
+
+url_pattern = re.compile(r'\b(http[s]?://(?:\S+))\b', re.I|re.U)
+
+class Plugin(BasePlugin):
+ def init(self):
+ self.add_command('link', self.command_link, "Usage: /link\nLink: opens the last link from the conversation into a browser.")
+
+ def find_link(self, nb):
+ messages = self.core.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.core.command_help('link')
+ else:
+ nb = 1
+ link = self.find_link(nb)
+ if link:
+ self.core.exec_command('%s %s' % (self.config.get('browser', 'firefox'), link))
+ else:
+ self.core.information('No URL found.', 'Warning')
+
+ def cleanup(self):
+ del self.config