summaryrefslogtreecommitdiff
path: root/plugins/link.py
diff options
context:
space:
mode:
authorFlorent Le Coz <louiz@louiz.org>2011-10-29 07:18:19 +0200
committerFlorent Le Coz <louiz@louiz.org>2011-10-29 07:18:19 +0200
commitb98880b5269764ad69bb19262790d27b2575b174 (patch)
tree66922677670f713d0a5c4fb2e4749787254fa4dd /plugins/link.py
parent881de6a7c02e4c11957a1bb0dd3b31b8884fa8b0 (diff)
downloadpoezio-b98880b5269764ad69bb19262790d27b2575b174.tar.gz
poezio-b98880b5269764ad69bb19262790d27b2575b174.tar.bz2
poezio-b98880b5269764ad69bb19262790d27b2575b174.tar.xz
poezio-b98880b5269764ad69bb19262790d27b2575b174.zip
add the link plugin
Diffstat (limited to 'plugins/link.py')
-rw-r--r--plugins/link.py48
1 files changed, 48 insertions, 0 deletions
diff --git a/plugins/link.py b/plugins/link.py
new file mode 100644
index 00000000..0cd1a0f1
--- /dev/null
+++ b/plugins/link.py
@@ -0,0 +1,48 @@
+# 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]:
+ match = url_pattern.search(clean_text(message.txt))
+ if match:
+ self.core.information('[%s]' % (match.groups(),))
+ for url in list(match.groups())[::-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