The URL to Link Plugin

This is a passive plugin that while enabled will convert URL's sent to the chat into actual links

import os
import orpg.pluginhandler
import re

class Plugin(orpg.pluginhandler.PluginHandler):
    # Initialization subroutine.
    #
    # !self : instance of self
    # !chat : instance of the chat window to write to
    def __init__(self, openrpg, plugindb, parent):
        orpg.pluginhandler.PluginHandler.__init__(self, openrpg, plugindb, parent)

        # The Following code should be edited to contain the proper information
        self.name = 'URL to link conversion'
        self.author = 'tdb30 tbaleno@wrathof.com'
        self.help = "This plugin automaticaly wraps urls in link tags\n"
        self.help += "making them clickable."

        self.url_regex = None
        self.mailto_regex = None

    def plugin_enabled(self):
        #This is where you set any variables that need to be initalized when your plugin starts
        self.url_regex = re.compile(r"""\w{3,}://[A-Za-z0-9.=,:/&;?_%~+!$#-]{2,63}\.[A-Za-z0-9.=,:/&;?_%~+!$#-]{2,63}|[\w-]{2,63}\.[\w-]{2,63}\.[A-Za-z]{2,6}(/[A-Za-z0-9.=,:/&;?_%~+!$#-]+)?|[0-9]{2,3}\.[0-9]{2,3}\.[0-9]{1,3}\.[0-9]{1,3}""", re.I)

        self.mailto_regex = re.compile(r"""(mailto:)?[\w._-]+@[A-Za-z0-9.-]+""", re.I)

    def plugin_disabled(self):
        #Here you need to remove any commands you added, and anything else you want to happen when you disable the plugin
        #such as closing windows created by the plugin
        pass

    def send_msg(self, text, send):
        text = self.link_emails(text)
        text = self.link_urls(text)
        return text, send

    def plugin_incoming_msg(self, text, type, name, player):
        text = self.link_emails(text)
        text = self.link_urls(text)
        return text, type, name

    def link_urls(self, text):
        #The modified text accumulates into text2 so that the indices of the regex matches
        #in the original text variable don't get screwed up by the replacement.
        text2 = ""
        url = self.url_regex.search(text)
        if not url:#so that it doesn't accidentally delete the message
            text2 = text
            textafterurl = ""
        while url:
            urltext = url.group()
            if urltext.find("://")<0:#it might not load the web browser but is usable. For example, "maps.google.com"
                urltext = "http://"+urltext
            textbeforeurl = text[:url.start()]
            textafterurl = text[url.end():]
            if not (len(re.findall("<",textbeforeurl)) > len(re.findall(">",textbeforeurl))) and text[url.start()-1]!="@":
                text2 += textbeforeurl + "<a href='" + urltext + "'>" + url.group() + "</a>"
            else:
                text2 += textbeforeurl + url.group()#not urltext, because we don't wanna screw with it
            text = textafterurl
            url = self.url_regex.search(text)
        else:#once it's done -- this happens whether or not it found a URL to begin with
            text2 += textafterurl
        return text2

    def link_emails(self, text):
        #The modified text accumulates into text2 so that the indices of the regex matches
        #in the original text variable don't get screwed up by the replacement.
        #the main differences between this and the link_urls function are:
        #  (a) this one uses the mailto_regex instead of url_regex
        #  (b) this one doesn't append http:// but rather mailto:
        text2 = ""
        url = self.mailto_regex.search(text)
        if not url:#so that it doesn't accidentally delete the message
            text2 = text
            textafterurl = ""
        while url:
            urltext = url.group()
            if urltext.find("mailto:")<0:#it's just a plain e-mail like mduo13@yahoo.com instead of a mailto URL
                urltext = "mailto:"+urltext
            textbeforeurl = text[:url.start()]
            textafterurl = text[url.end():]
            if not (len(re.findall("<",textbeforeurl)) > len(re.findall(">",textbeforeurl))):
                #here it doesn't use urltext, but rather the "prettier" version (without mailto:) in the displayed text
                #even though the href URL is actually a mailto.
                text2 += textbeforeurl + "<a href='" + urltext + "'>" + url.group() + "</a>"
            else:
                text2 += textbeforeurl + url.group()#not urltext, because we don't wanna screw with it
            text = textafterurl
            url = self.mailto_regex.search(text)
        else:#once it's done -- this happens whether or not it found a URL to begin with
            text2 += textafterurl
        return text2


CategoryPlugins

URL to link conversion (last edited 2006-05-12 21:34:10 by DjGilcrease)