initial commit
[synarere.git] / core / command.py
blob4d76bef6acc7dfd861fc328c27f67dd42dde8221
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 module.
11 import logger
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('dispatch(): 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(event, func, cmd_type):
66 '''Add a function to an event's list of functions.'''
68 event = event.upper()
70 try:
71 test = cmd_type[event]
72 except KeyError:
73 cmd_type[event] = { 'first' : None,
74 'funcs' : [],
75 'last' : None }
77 if func in cmd_type[event]['funcs']:
78 return True
80 cmd_type[event]['funcs'].append(func)
81 return True
83 logger.debug('add(): created new command %s assigned to function %s' % (event, func))
85 def add_first(event, func, cmd_type):
86 '''Add a function as an event's first function.'''
88 event = event.upper()
90 try:
91 test = cmd_type[event]
92 except KeyError:
93 cmd_type[event] = { 'first' : None,
94 'funcs' : [],
95 'last' : None }
97 if cmd_type[event]['first']:
98 return False
100 cmd_type[event]['first'] = func
101 return True
103 logger.debug('add_first(): created new command %s assigned to %s' % (event, func))
105 def delete(event, func, cmd_type):
106 '''Remove a function from an event's list of functions.'''
108 event = event.upper()
110 if func not in cmd_type[event]['funcs']:
111 return False
113 cmd_type[event]['funcs'].remove(func)
114 return True
116 logger.debug('delete(): deleted command %s assigned to %s' % (event, func))
118 def delete_first(event, func, cmd_type):
119 '''Remove a function as an event's first function.'''
121 event = event.upper()
123 cmd_type[event]['first'] = None
124 logger.debug('delete_first(): deleted command %s assigned to %s' % (event, func))