whoops, forgot this bit
[synarere.git] / core / command.py
bloba6698477b00504bb2710d502b0828e8d384d33ab
1 # synarere -- a highly modular and stable IRC bot.
2 # Copyright (C) 2010 Michael Rodriguez.
3 # Rights to this code are documented in docs/LICENSE.
5 '''Command handlers.'''
7 # Import required Python function.
8 from thread import start_new_thread
10 # Import required core modules.
11 import logger, event
13 # This is the IRC command hash table.
14 # This determines which functions are called
15 # when certain IRC events happen.
16 # This is initialised in irc.py.
17 irc = {}
19 # This is the IRC channel command hash table.
20 # This determines which functions are called
21 # when someone says certain things on IRC.
22 chan = {}
24 # This is the IRC channel addressed hash table.
25 # This determines which functions are called
26 # when someone addresses us on IRC.
27 chanme = {}
29 # This is the private message command hash table.
30 # This determines which functions are called
31 # when someone sends a certian PRIVMSG to the bot.
32 priv = {}
34 # This is the CTCP command hash table.
35 # This determines which functions are called
36 # when someone sends a certain CTCP.
37 ctcp = {}
39 def dispatch(on_thread, cmd_type, command, *args):
40 '''Dispatch commands.'''
42 logger.debug('Dispatching %s of type %s (threaded = %s)' % (command, cmd_type, on_thread))
44 try:
45 if cmd_type[command]['first']:
46 if on_thread:
47 start_new_thread(cmd_type[command]['first'], args)
48 else:
49 cmd_type[command]['first'](*args)
51 for func in cmd_type[command]['funcs']:
52 if on_thread:
53 start_new_thread(func, args)
54 else:
55 func(*args)
57 if cmd_type[command]['last']:
58 if on_thread:
59 start_new_thread(cmd_type[command]['last'], args)
60 else:
61 cmd_type[command]['last'](*args)
62 except:
63 pass
65 def add(eventname, func, cmd_type):
66 '''Add a function to an event's list of functions.'''
68 eventname = event.upper()
70 try:
71 test = cmd_type[eventname]
72 except KeyError:
73 cmd_type[eventname] = { 'first' : None,
74 'funcs' : [],
75 'last' : None }
77 if func in cmd_type[eventname]['funcs']:
78 return True
80 cmd_type[eventname]['funcs'].append(func)
81 return True
83 logger.debug('Created new command %s assigned to function %s (low-priority)' % (eventname, func))
84 event.dispatch('OnCommandAdd', eventname, func, cmd_type)
86 def add_first(event, func, cmd_type):
87 '''Add a function as an event's first function.'''
89 event = event.upper()
91 try:
92 test = cmd_type[event]
93 except KeyError:
94 cmd_type[event] = { 'first' : None,
95 'funcs' : [],
96 'last' : None }
98 if cmd_type[event]['first']:
99 return False
101 cmd_type[event]['first'] = func
102 return True
104 logger.debug('Created new command %s assigned to %s (high-priority)' % (event, func))
105 event.dispatch('OnCommandAddFirst', event, func, cmd_type)
107 def delete(eventname, func, cmd_type):
108 '''Remove a function from an event's list of functions.'''
110 eventname = eventname.upper()
112 if func not in cmd_type[eventname]['funcs']:
113 return False
115 cmd_type[eventname]['funcs'].remove(func)
116 return True
118 logger.debug('Deleted command %s assigned to %s (low-priority)' % (eventname, func))
119 event.dispatch('OnCommandDelete', eventname, func, cmd_type)
121 def delete_first(eventname, func, cmd_type):
122 '''Remove a function as an event's first function.'''
124 eventname = eventname.upper()
126 cmd_type[eventname]['first'] = None
127 logger.debug('Deleted command %s assigned to %s (high-priority)' % (eventname, func))
128 event.dispatch('OnCommandDeleteFirst', eventname, func, cmd_type)