]> jfr.im git - erebus.git/blobdiff - modules/subtext.py
help - if nofakelag, showcommands should send the list instead of URL
[erebus.git] / modules / subtext.py
index 2ab1a04d53229b4266c327a3b8f44d152dda2fd6..88a2368e5747dc37f705f9938e10338f5587db86 100644 (file)
@@ -1,20 +1,18 @@
 # Erebus IRC bot - Author: Erebus Team
-# simple module example
+# vim: fileencoding=utf-8
+# module for 's/regex/replacement/' style correction
+# warning: arbitrary regex's are generally capable of various DoS attacks on CPU/memory usage. use with caution.
+# see for usage examples: https://github.com/zonidjan/erebus/commit/d7e9802778477f1faa26a03078cb1b3c018a5e5c
 # This file is released into the public domain; see http://unlicense.org/
 
 # module info
 modinfo = {
        'author': 'Erebus Team',
        'license': 'public domain',
-       'compatible': [2], # compatible module API versions
+       'compatible': [0], # compatible module API versions
        'depends': [], # other modules required to work properly?
-       'softdeps': ['help'], # modules which are preferred but not required
+       'softdeps': [], # modules which are preferred but not required
 }
-# note: softdeps will be loaded before this module, IF not disabled in the configuration (autoload.module = 0) (and if it exists)
-# however, if it is disabled it will be silently ignored, and if it is unloaded at runtime it won't cause this one to unload.
-#
-# basically, softdeps are things this module will use if available, but does not require (no errors will occur if it's not loaded)
-# for example, @lib.help() will attempt to use the help module, but swallow errors if it is not loaded
 
 # preamble
 import modlib
@@ -25,7 +23,7 @@ modstop = lib.modstop
 # module code
 import re
 from collections import namedtuple
-re_findsub = re.compile(r"s(.)(?P<search>[^\1]+)\1(?P<replace>[^\1]+)\1(?P<global>g)?;?")
+re_findsub = re.compile(r"""s([/'";:!@#$%^&-_=`~])(?P<search>.+?)\1(?P<replace>.+?)(?:\1(?P<global>g)?|$)""")
 Line = namedtuple('Line', ['sender', 'msg'])
 lastline = {}
 @lib.hooknum("PRIVMSG")
@@ -36,16 +34,13 @@ def privmsg_hook(bot, line):
        msg = pieces[3][1:]
        mo = re_findsub.match(msg)
        if mo:
-               print lastline[chan]
-               print mo.groupdict()
                if mo.group('global') is not None:
                        count = 0 # unlimited
                else:
                        count = 1 # only first
                try:
                        newline = re.sub(mo.group('search'), mo.group('replace'), lastline[chan].msg, count)
-               except Exception as e: print e; return # ignore it if it doesn't work
-               print newline
+               except Exception as e: return # ignore it if it doesn't work
                if newline != lastline[chan].msg:
                        if lastline[chan].sender == fromnick:
                                bot.msg(chan, "<%s> %s" % (lastline[chan].sender, newline))