#!/usr/bin/env python # This module attempts to capture URLs from any interaction on the mud. # It keeps a list of the n most recent (specified by capture_limit below) # URLs. # # Beware that there are problem security holes in this program. It attempts # to filter out some nastiness but be warned. # # /url -h Print help # /url list List captured URLs # /url launch [index] Launch the most recent URL, or that # specified by index. # # Written by Alexander Else. http://www.else.id.au import GnomeMud, re import pygtk import gtk import os # Limit the url list to the 10 most recently captured urls. capture_limit = 10 urlre = re.compile("((?xi)(?:(?:ht|f)tp://|)[\w\d\-\.]+\.(?:com|net|org|cc|edu|au|uk)(?:\S+|))") last_url = None captured_url_list = [] recmd = "/url" launchre = re.compile(recmd); def launch_url(c, url): c.write("Launching %s\n" % url) os.popen("%s %s &" % ("firefox", url)) def handle_url_cmd(c, s): global last_url global captured_url_list global recmd if re.match("%s\s+-h" % recmd, s): c.write("/url list\n") c.write("/url launch [num]\n") if re.match("%s\s+list" % recmd, s): i = 0 for url in captured_url_list: c.write("%2d %s\n" % (i, url)) i = i + 1 m = re.search("%s\s+(launch(?:\s+(\d+))?)" % recmd, s) if m: num = 0 if m.group(2) != None: num = int(m.group(2)) if len(captured_url_list) < num: c.write("No such URL index\n") else: launch_url(c, captured_url_list[num]) else: launch_url(c, captured_url_list[num]) # effectively gag our command from hitting the mud s = "" return s def input(c, s): m = urlre.search(s) if m: global last_url global captured_url_list global capture_limit new_url = m.group(1) badchars = re.search("[\"`]", new_url) if not badchars and new_url != last_url: last_url = new_url captured_url_list = [ new_url ] + captured_url_list[: (capture_limit - 1)] s = s + "\n(captured " + new_url + ")\n" return s def output(c, s): m = launchre.match(s) if m: s = handle_url_cmd(c, s) return s c = GnomeMud.connection() c.write("Registering URL capture callbacks... ") GnomeMud.register_input_handler(input) GnomeMud.register_output_handler(output) c.write("done\n")