Commits

Anonymous committed d5a02cd

Added "regexp" command, ported from Urban Airship pmxbot at tag 1001.

The regexp command is like contains in that it watches all content,
however it allows you to monitor for a certain regexp condition and
pass a match.group() value instead of the normal message. Useful to
auto-generate links from a ticket number, for example.

  • Participants
  • Parent commits 0fb9783

Comments (0)

Files changed (1)

 import logging
 import itertools
 import pprint
+import re
 
 import irc.bot
 import irc.client
 					)	)
 					if not handler.allow_chain:
 						break
+
+			elif handler.type_ in ('regexp',):
+				search = handler.func.regexp.search(msg)
+				if not search:
+					continue
+				f = functools.partial(handler.func, c, e, channel, nick, search.group())
+				messages = pmxbot.itertools.trap_exceptions(
+					pmxbot.itertools.generate_results(f),
+					exception_handler
+				)
+                break
 		self._handle_output(channel, messages)
 
 
 	type_ = 'alias'
 	class_priority = 2
 
+class RegexpHandler(Handler):
+	type_ = 'regexp'
+	class_priority = 4
+
 def contains(name, channels=(), exclude=(), rate=1.0, priority=1,
 		doc=None, **kwargs):
 	def deco(func):
 				doc=doc)
 			ch.aliases.append(ah)
 			_handler_registry.append(ah)
-
 		_handler_registry.sort()
 		return func
 	return deco
 
+def regexp(name, regexp, doc=None):
+    def deco(func):
+		func.regexp = re.compile(regexp, re.IGNORECASE)
+		_handler_registry.append(RegexpHandler(
+			name=name.lower(),
+			func=func,
+			doc=doc)
+		)
+		_handler_registry.sort()
+        return func
+    return deco
+
 def execdelay(name, channel, howlong, args=[], doc=None, repeat=False):
 	def deco(func):
 		_delay_registry.append((name.lower(), channel, howlong, func, args,