From d8eb320b8540ceb4ab80c1774a11f49678055972 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 11 Aug 2013 01:00:21 +0100 Subject: [PATCH] Added on_message hook Browser plugin now uses last received hyperlink when no arg supplied --- plugins/www-view.py | 25 ++++++++++++++++++++++++- src/plugins/callbacks.c | 8 +++++++- src/plugins/plugins.c | 8 ++++++++ src/plugins/plugins.h | 1 + src/profanity.c | 4 ++++ 5 files changed, 44 insertions(+), 2 deletions(-) diff --git a/plugins/www-view.py b/plugins/www-view.py index 3434d88a..76cdf675 100644 --- a/plugins/www-view.py +++ b/plugins/www-view.py @@ -1,14 +1,37 @@ import prof import os import webbrowser +import re +lastlink = None # hooks def prof_init(version, status): - prof.register_command("/browser", 1, 1, "/browser url", "View a URL in the browser.", "View a URL in the browser", cmd_browser) + prof.register_command("/browser", 0, 1, "/browser url", "View a URL in the browser.", "View a URL in the browser", cmd_browser) + +def prof_on_message(jid, message): + global lastlink + links = re.findall(r'(https?://\S+)', message) + if (len(links) > 0): + lastlink = links[len(links)-1] # commands def cmd_browser(url): + global lastlink + + link = None + if (url != None): + link = url + elif (lastlink != None): + link = lastlink + + if (link != None): + prof.cons_show("Opening " + link + " in browser.") + open_browser(link) + else: + prof.cons_show("No link supplied.") + +def open_browser(url): savout = os.dup(1) saverr = os.dup(2) os.close(1) diff --git a/src/plugins/callbacks.c b/src/plugins/callbacks.c index 388892af..04412ed0 100644 --- a/src/plugins/callbacks.c +++ b/src/plugins/callbacks.c @@ -67,7 +67,13 @@ plugins_command_run(const char * const input) int num_args = g_strv_length(args); PyObject *p_args = NULL; if (num_args == 0) { - PyObject_CallObject(command->p_callback, p_args); + if (command->max_args == 1) { + p_args = Py_BuildValue("(O)", Py_BuildValue("")); + PyObject_CallObject(command->p_callback, p_args); + Py_XDECREF(p_args); + } else { + PyObject_CallObject(command->p_callback, p_args); + } } else if (num_args == 1) { p_args = Py_BuildValue("(s)", args[0]); PyObject_CallObject(command->p_callback, p_args); diff --git a/src/plugins/plugins.c b/src/plugins/plugins.c index a6e3805b..8dbe4485 100644 --- a/src/plugins/plugins.c +++ b/src/plugins/plugins.c @@ -104,6 +104,14 @@ plugins_on_connect(void) _run_plugins("prof_on_connect", NULL); } +void +plugins_on_message(const char * const jid, const char * const message) +{ + PyObject *p_args = Py_BuildValue("ss", jid, message); + _run_plugins("prof_on_message", p_args); + Py_XDECREF(p_args); +} + static GSList * _get_module_names(void) { diff --git a/src/plugins/plugins.h b/src/plugins/plugins.h index 50d96cdd..ea2723ba 100644 --- a/src/plugins/plugins.h +++ b/src/plugins/plugins.h @@ -26,6 +26,7 @@ void plugins_init(void); void plugins_on_start(void); void plugins_on_connect(void); +void plugins_on_message(const char * const jid, const char * const message); void plugins_shutdown(void); gboolean plugins_command_run(const char * const cmd); void plugins_run_timed(void); diff --git a/src/profanity.c b/src/profanity.c index 826e4e6f..5e8515da 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -119,6 +119,8 @@ prof_handle_incoming_message(char *from, char *message, gboolean priv) ui_incoming_msg(from, message, NULL, priv); ui_current_page_off(); + plugins_on_message(from, message); + if (prefs_get_boolean(PREF_CHLOG) && !priv) { Jid *from_jid = jid_create(from); const char *jid = jabber_get_fulljid(); @@ -136,6 +138,8 @@ prof_handle_delayed_message(char *from, char *message, GTimeVal tv_stamp, ui_incoming_msg(from, message, &tv_stamp, priv); ui_current_page_off(); + plugins_on_message(from, message); + if (prefs_get_boolean(PREF_CHLOG) && !priv) { Jid *from_jid = jid_create(from); const char *jid = jabber_get_fulljid();