<s> import re <EOL> import traceback <EOL> from collections import OrderedDict <EOL> from . . const import * <EOL> from . import EventManager , BaseHandler <EOL> from . . model import DataDict , Channel , User <EOL> from . . import util , settings <EOL> manager = EventManager ( ) <EOL> @ manager . hookmsg ( INVITE ) <EOL> def on_invite ( connection , sender , target , channel ) : <EOL> connopt = settings . connections [ connection . name ] <EOL> if connopt . invite not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> return <EOL> if connopt . invite == '<STR_LIT>' and not connopt . is_admin ( sender ) : <EOL> return <EOL> connection . join ( channel ) <EOL> class BotEventContext ( object ) : <EOL> def __init__ ( self , manager , connection , ident , msgtype , target , ** kwargs ) : <EOL> self . manager = manager <EOL> self . connection = connection <EOL> self . ident = ident <EOL> self . msgtype = msgtype <EOL> self . target = target <EOL> for k , v in kwargs . items ( ) : <EOL> setattr ( self , k , v ) <EOL> def reply ( self , * msgs ) : <EOL> msg = u'<STR_LIT:U+0020>' . join ( [ unicode ( msg ) for msg in msgs ] ) <EOL> self . connection . sendl ( self . msgtype , self . target , msg ) <EOL> @ property <EOL> def nick ( self ) : <EOL> return self . ident . nick <EOL> class BaseBotCommandManager ( BaseHandler ) : <EOL> def __init__ ( self , handlers = None ) : <EOL> self . handlers = handlers if handlers is not None else OrderedDict ( ) <EOL> def extends ( self , events ) : <EOL> if isinstance ( events , EventManager ) : <EOL> events = events . handlers <EOL> self . handlers += events <EOL> class PrefixBotCommandManager ( BaseBotCommandManager ) : <EOL> def __init__ ( self , prefix , handlers = None ) : <EOL> BaseBotCommandManager . __init__ ( self , handlers ) <EOL> self . prefix = prefix <EOL> def __call__ ( self , connection , line ) : <EOL> ln = util . msgsplit ( line ) <EOL> msg = ln [ - <NUM_LIT:1> ] <EOL> if not msg . startswith ( self . prefix ) : <EOL> return False <EOL> parts = msg . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> prefix = parts [ <NUM_LIT:0> ] [ len ( self . prefix ) : ] <EOL> try : <EOL> action = self . handlers [ prefix ] <EOL> except KeyError : <EOL> return <EOL> sender , msgtype , target = ln [ <NUM_LIT:0> ] , ln [ <NUM_LIT:1> ] , ln [ <NUM_LIT:2> ] <EOL> sender = util . parseid ( sender ) <EOL> if target [ <NUM_LIT:0> ] not in ( '<STR_LIT:#>' , '<STR_LIT:&>' , '<STR_LIT:%>' ) : <EOL> target = sender . nick <EOL> context = BotEventContext ( self , connection , sender , msgtype , target , prefix = parts [ <NUM_LIT:0> ] ) <EOL> try : <EOL> action ( context , parts [ <NUM_LIT:1> ] if len ( parts ) > <NUM_LIT:1> else None ) <EOL> except : <EOL> traceback . print_exc ( ) <EOL> def hook ( self , * prefixes ) : <EOL> def decorator ( action ) : <EOL> def on_event ( context , message = None ) : <EOL> action ( context , message ) <EOL> on_event . __doc__ = action . __doc__ <EOL> for prefix in prefixes : <EOL> self . handlers [ prefix ] = on_event <EOL> return action <EOL> return decorator <EOL> def hookback ( self , * prefixes ) : <EOL> def decorator ( action ) : <EOL> def on_event ( context , message = None ) : <EOL> result = action ( context , message ) <EOL> if not result : <EOL> return <EOL> context . reply ( result ) <EOL> on_event . __doc__ = action . __doc__ <EOL> for prefix in prefixes : <EOL> self . handlers [ prefix ] = on_event <EOL> return action <EOL> return decorator <EOL> class RegexBotCommandManager ( BaseBotCommandManager ) : <EOL> def __init__ ( self , handlers = None ) : <EOL> self . handlers = [ ] if handlers is None else handlers <EOL> def __call__ ( self , connection , line ) : <EOL> ln = util . msgsplit ( line ) <EOL> msg = ln [ - <NUM_LIT:1> ] <EOL> sender , msgtype = ln [ <NUM_LIT:0> ] , ln [ <NUM_LIT:1> ] <EOL> sender = util . parseid ( sender ) <EOL> if len ( ln ) > <NUM_LIT:2> : <EOL> target = ln [ <NUM_LIT:2> ] <EOL> if target [ <NUM_LIT:0> ] not in ( '<STR_LIT:#>' , '<STR_LIT:&>' , '<STR_LIT:%>' ) : <EOL> target = sender . nick <EOL> else : <EOL> target = None <EOL> context = BotEventContext ( self , connection , sender , msgtype , target ) <EOL> for regex , action in self . handlers : <EOL> m = re . search ( regex , msg ) <EOL> if m is None : continue <EOL> action ( context , msg , * m . groups ( ) ) <EOL> def hook ( self , pattern ) : <EOL> def decorator ( action ) : <EOL> def on_event ( context , msg , * regexargs ) : <EOL> action ( context , msg , * regexargs ) <EOL> on_event . __doc__ = action . __doc__ <EOL> self . handlers . append ( ( pattern , on_event ) ) <EOL> return action <EOL> return decorator <EOL> def hookback ( self , pattern ) : <EOL> def decorator ( action ) : <EOL> def on_event ( context , msg , * regexargs ) : <EOL> result = action ( context , msg , * regexargs ) <EOL> if not result : <EOL> return <EOL> context . reply ( result ) <EOL> on_event . __doc__ = action . __doc__ <EOL> self . handlers . append ( ( pattern , on_event ) ) <EOL> return action <EOL> return decorator <EOL> msgprefix = PrefixBotCommandManager ( '<STR_LIT:/>' ) <EOL> manager . hook ( msgprefix ) <EOL> msgregex = RegexBotCommandManager ( ) <EOL> manager . hook ( msgregex ) <EOL> @ msgprefix . hook ( '<STR_LIT>' ) <EOL> def on_help ( context , message = None ) : <EOL> """<STR_LIT>""" <EOL> if message is None : <EOL> items = u'<STR_LIT:U+0020>' . join ( context . manager . handlers . keys ( ) ) <EOL> context . reply ( items ) <EOL> else : <EOL> try : <EOL> handler = context . manager . handlers [ message ] <EOL> except KeyError : <EOL> context . reply ( u'<STR_LIT>' . format ( message ) ) <EOL> if handler . __doc__ : <EOL> context . reply ( handler . __doc__ . decode ( '<STR_LIT:utf-8>' ) ) <EOL> else : <EOL> context . reply ( u'<STR_LIT>' . format ( message ) ) </s>
