import os
import re

from util import hook


@hook.sieve
def sieve_suite(bot, input, func, args):
    events = args.get('events', ['PRIVMSG'])

    if input.command not in events and events != '*':
        return None

    if input.nick.lower()[-3:] == 'bot' and args.get('ignorebots', True):
        return None

    regex = args.get('re')
    if not regex:
        hook = args.get('hook', r'(.*)')

        if args.get('prefix', True):
            if input.chan == input.nick: # private message, prefix not required
                prefix = r'^(?:[.!]?|'
            else:
                prefix = r'^(?:[.!]|'
            hook = prefix + input.conn.nick + r'[:,]*\s)' + hook

        regex = re.compile(hook, flags=re.I)
        args['re'] = regex

    input.re = regex.match(input.msg)
    if input.re is None:
        return None

    acl = bot.config.get('acls', {}).get(func.__name__)
    if acl:
        if 'deny-except' in acl:
            allowed_channels = map(str.lower, acl['deny-except'])
            if input.chan.lower() not in allowed_channels:
                return None
        if 'allow-except' in acl:
            denied_channels = map(str.lower, acl['allow-except'])
            if input.chan.lower() in denied_channels:
                return None

    input.inp_unstripped = ' '.join(input.re.groups())
    input.inp = input.inp_unstripped.strip()

    return input

@@ -311,57 +311,8 @@
 ne%0A%0A
-    regex = args.get('re')%0A    if not regex:%0A    
     
@@ -345,20 +345,16 @@
 (.*)')%0A%0A
-    
     if a
@@ -378,36 +378,32 @@
  True):%0A        
-    
 if input.chan ==
@@ -458,36 +458,32 @@
 red%0A            
-    
 prefix = r'%5E(?:%5B
@@ -501,22 +501,14 @@
     
-  
-  
 else:%0A
-    
     
@@ -537,20 +537,16 @@
 :%5B.!%5D%7C'%0A
-    
         
@@ -603,81 +603,8 @@
 ok%0A%0A
-        regex = re.compile(hook, flags=re.I)%0A        args%5B're'%5D = regex%0A%0A
     
@@ -620,18 +620,21 @@
 = re
-gex
 .match(
+hook, 
 inpu
@@ -638,16 +638,28 @@
 nput.msg
+, flags=re.I
 )%0A    if

revert sieve to version without broken caching