1
2
3
4
5 """ callbacks triggered by ircevent CMND """
6
7 __copyright__ = 'this file is in the public domain'
8
9 from gozerbot.generic import rlog, handle_exception, calledfrom, makeargrest, \
10 lockdec
11 from gozerbot.dol import Dol
12 from gozerbot.thr import start_new_thread
13 from gozerbot.runner import cbrunners
14 import sys, copy, thread
15
16 callbacklock = thread.allocate_lock()
17 locked = lockdec(callbacklock)
20
21 """ class representing a callback """
22
23 - def __init__(self, func, prereq, plugname, kwargs, threaded=False, \
24 speed=5):
25
26 self.func = func
27
28 self.prereq = prereq
29
30 self.plugname = plugname
31
32 self.kwargs = kwargs
33
34 self.threaded = copy.deepcopy(threaded)
35 self.speed = copy.deepcopy(speed)
36
38
39 """ dict of lists containing callbacks """
40
42
43
44 self.cbs = Dol()
45
47 """ return nr of callbacks """
48 return len(self.cbs)
49
50 - def add(self, what, func, prereq=None, kwargs=None, threaded=False, \
51 nr=False, speed=5):
52 """ add a callback """
53 what = what.upper()
54
55 plugname = calledfrom(sys._getframe())
56
57 if not kwargs:
58 kwargs = {}
59
60 if nr != False:
61 self.cbs.insert(nr, what, Callback(func, prereq, plugname, \
62 kwargs, threaded, speed))
63 else:
64 self.cbs.add(what, Callback(func, prereq, plugname, kwargs, \
65 threaded, speed))
66 rlog(-3, 'callbacks', 'added %s (%s)' % (what, plugname))
67
69 """ unload plugin """
70 unload = []
71
72 for name, cblist in self.cbs.iteritems():
73 index = 0
74 for item in cblist:
75 if item.plugname == plug:
76 unload.append((name, index))
77 index += 1
78
79 for callback in unload[::-1]:
80 self.cbs.delete(callback[0], callback[1])
81 rlog(1, 'callbacks', 'unloaded %s' % callback[0])
82
84 """ show where ircevent.CMND callbacks are registered """
85 result = []
86 what = what.upper()
87 for cmnd, callback in self.cbs.iteritems():
88 if cmnd == what:
89 for item in callback:
90 if not item.plugname in result:
91 result.append(item.plugname)
92 return result
93
95
96 """ callback on ircevent """
97
98 - def check(self, bot, ievent):
99 """ check for callbacks to be fired """
100
101 if self.cbs.has_key('ALL'):
102 for cb in self.cbs['ALL']:
103 self.callback(cb, bot, ievent)
104 cmnd = ievent.cmnd.upper()
105
106 if self.cbs.has_key(cmnd):
107 for cb in self.cbs[cmnd]:
108 self.callback(cb, bot, ievent)
109
110 @locked
112 """ callback cb with bot and ievent as arguments """
113
114
115
116
117
118
119
120 try:
121
122 if cb.prereq:
123 rlog(-10, 'callback', 'excecuting in loop %s' % \
124 str(cb.prereq))
125 if not cb.prereq(bot, ievent):
126 return
127
128 if not cb.func:
129 return
130
131 rlog(-10, 'callback', 'excecuting callback %s' % \
132 str(cb.func))
133 if cb.threaded:
134 start_new_thread(cb.func, (bot, ievent), cb.kwargs)
135 else:
136 cbrunners[10-cb.speed].put("cb-%s" % cb.plugname, cb.func, \
137 bot, ievent, **cb.kwargs)
138
139 except Exception, ex:
140 handle_exception()
141
143
144 """ callback on ircevent """
145
146 - def check(self, bot, ievent):
147 """ check for callbacks to be fired """
148
149 if self.cbs.has_key('ALL'):
150 for cb in self.cbs['ALL']:
151 self.callback(cb, bot, ievent)
152 cmnd = ievent.cmnd.upper()
153
154 if self.cbs.has_key(cmnd):
155 for cb in self.cbs[cmnd]:
156 self.callback(cb, bot, ievent)
157
159 """ callback cb with bot and ievent as arguments """
160 if not bot:
161 return
162 if str(bot.jid) in str(ievent.getFrom()):
163 return
164 try:
165
166 if cb.prereq:
167 rlog(-10, 'callback', 'excecuting in loop %s' % \
168 str(cb.prereq))
169 if not cb.prereq(bot, ievent):
170 return
171
172 if not cb.func:
173 return
174
175 rlog(-10, 'callback', 'excecuting callback %s' % \
176 str(cb.func))
177 if cb.threaded:
178 start_new_thread(cb.func, (bot, ievent), cb.kwargs)
179 else:
180 cbrunners[10-cb.speed].put("cb-%s" % cb.plugname, cb.func, bot, ievent, \
181 **cb.kwargs)
182
183 except Exception, ex:
184 handle_exception()
185
186 callbacks = Botcallbacks()
187 jcallbacks = Jabbercallbacks()
188