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.
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.
19 # This is the IRC channel command hash table.
20 # This determines which functions are called
21 # when someone says certain things on IRC.
24 # This is the IRC channel addressed hash table.
25 # This determines which functions are called
26 # when someone addresses us on IRC.
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.
34 # This is the CTCP command hash table.
35 # This determines which functions are called
36 # when someone sends a certain 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
))
45 if cmd_type
[command
]['first']:
47 start_new_thread(cmd_type
[command
]['first'], args
)
49 cmd_type
[command
]['first'](*args
)
51 for func
in cmd_type
[command
]['funcs']:
53 start_new_thread(func
, args
)
57 if cmd_type
[command
]['last']:
59 start_new_thread(cmd_type
[command
]['last'], args
)
61 cmd_type
[command
]['last'](*args
)
65 def add(event
, func
, cmd_type
):
66 '''Add a function to an event's list of functions.'''
71 test
= cmd_type
[event
]
73 cmd_type
[event
] = { 'first' : None,
77 if func
in cmd_type
[event
]['funcs']:
80 cmd_type
[event
]['funcs'].append(func
)
83 logger
.debug('Created new command %s assigned to function %s (low-priority)' % (event
, func
))
84 event
.dispatch('OnCommandAdd', event
, func
, cmd_type
)
86 def add_first(event
, func
, cmd_type
):
87 '''Add a function as an event's first function.'''
92 test
= cmd_type
[event
]
94 cmd_type
[event
] = { 'first' : None,
98 if cmd_type
[event
]['first']:
101 cmd_type
[event
]['first'] = func
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']:
115 cmd_type
[eventname
]['funcs'].remove(func
)
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
)