2013-08-07 20:40:03 -04:00
|
|
|
import prof
|
|
|
|
import os
|
|
|
|
import webbrowser
|
2013-08-10 20:00:21 -04:00
|
|
|
import re
|
2013-08-07 20:40:03 -04:00
|
|
|
|
2013-08-10 20:35:11 -04:00
|
|
|
lastlink = {}
|
2013-08-07 20:40:03 -04:00
|
|
|
|
|
|
|
# hooks
|
|
|
|
def prof_init(version, status):
|
2013-08-10 20:35:11 -04:00
|
|
|
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)
|
2013-08-10 20:00:21 -04:00
|
|
|
|
2013-08-17 19:26:14 -04:00
|
|
|
def prof_on_message_received(jid, message):
|
2013-08-10 20:00:21 -04:00
|
|
|
global lastlink
|
|
|
|
links = re.findall(r'(https?://\S+)', message)
|
|
|
|
if (len(links) > 0):
|
2013-08-10 20:35:11 -04:00
|
|
|
lastlink[jid] = links[len(links)-1]
|
2013-08-07 20:40:03 -04:00
|
|
|
|
|
|
|
# commands
|
|
|
|
def cmd_browser(url):
|
2013-08-10 20:00:21 -04:00
|
|
|
global lastlink
|
|
|
|
link = None
|
2013-08-10 20:35:11 -04:00
|
|
|
|
|
|
|
# use arg if supplied
|
2013-08-10 20:00:21 -04:00
|
|
|
if (url != None):
|
|
|
|
link = url
|
2013-08-10 20:35:11 -04:00
|
|
|
else:
|
|
|
|
jid = prof.get_current_recipient();
|
|
|
|
|
|
|
|
# check if in chat window
|
|
|
|
if (jid != None):
|
2013-08-10 20:00:21 -04:00
|
|
|
|
2013-08-10 20:35:11 -04:00
|
|
|
# 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
|
2013-08-10 20:00:21 -04:00
|
|
|
if (link != None):
|
2013-08-10 20:35:11 -04:00
|
|
|
prof.cons_show("Opening " + link + " in browser")
|
2013-08-10 20:00:21 -04:00
|
|
|
open_browser(link)
|
|
|
|
|
|
|
|
def open_browser(url):
|
2013-08-07 20:40:03 -04:00
|
|
|
savout = os.dup(1)
|
|
|
|
saverr = os.dup(2)
|
|
|
|
os.close(1)
|
|
|
|
os.close(2)
|
|
|
|
os.open(os.devnull, os.O_RDWR)
|
|
|
|
try:
|
|
|
|
webbrowser.open(url, new=2)
|
|
|
|
finally:
|
|
|
|
os.dup2(savout, 1)
|
|
|
|
os.dup2(saverr, 2)
|