From ffb13598892fcb7a4c9e549982ad1dbcc6167227 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 11 Aug 2013 01:35:11 +0100 Subject: [PATCH] Added get_current_recipient to api Browser plugin now uses last link received in a chat window --- plugins/www-view.py | 40 +++++++++++++++++++++++++++++----------- src/plugins/api.c | 13 +++++++++++++ 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/plugins/www-view.py b/plugins/www-view.py index 76cdf675..81ec6cd3 100644 --- a/plugins/www-view.py +++ b/plugins/www-view.py @@ -3,33 +3,51 @@ import os import webbrowser import re -lastlink = None +lastlink = {} # hooks def prof_init(version, status): - prof.register_command("/browser", 0, 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, if no argument is supplied, " + + "the last received URL will be used.", + 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] + lastlink[jid] = links[len(links)-1] # commands def cmd_browser(url): global lastlink - link = None + + # use arg if supplied 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.") + jid = prof.get_current_recipient(); + + # check if in chat window + if (jid != None): + + # check for link from recipient + if jid in lastlink.keys(): + link = lastlink[jid] + else: + prof.cons_show("No links found from " + jid); + + # not in chat window + else: + prof.cons_show("You must supply a URL to the /browser command") + + # open the browser if link found + if (link != None): + prof.cons_show("Opening " + link + " in browser") + open_browser(link) def open_browser(url): savout = os.dup(1) diff --git a/src/plugins/api.c b/src/plugins/api.c index 6b393698..098cbda6 100644 --- a/src/plugins/api.c +++ b/src/plugins/api.c @@ -133,6 +133,18 @@ api_send_line(PyObject *self, PyObject *args) return Py_BuildValue(""); } +static PyObject * +api_get_current_recipient(void) +{ + win_type_t win_type = ui_current_win_type(); + if (win_type == WIN_CHAT) { + char *recipient = ui_current_recipient(); + return Py_BuildValue("s", recipient); + } else { + return Py_BuildValue(""); + } +} + static PyMethodDef apiMethods[] = { { "cons_alert", api_cons_alert, METH_NOARGS, "Highlight the console window in the status bar." }, { "cons_show", api_cons_show, METH_VARARGS, "Print a line to the console." }, @@ -140,6 +152,7 @@ static PyMethodDef apiMethods[] = { { "register_timed", api_register_timed, METH_VARARGS, "Register a timed function." }, { "send_line", api_send_line, METH_VARARGS, "Send a line of input." }, { "notify", api_notify, METH_VARARGS, "Send desktop notification." }, + { "get_current_recipient", api_get_current_recipient, METH_VARARGS, "Return the jid of the recipient of the current window." }, { NULL, NULL, 0, NULL } };