1
2
3
4
5 """ implement RE (regular expression) dispatcher """
6
7 __copyright__ = 'this file is in the public domain'
8
9 from gozerbot.generic import rlog, calledfrom, handle_exception, lockdec
10 from gozerbot.runner import cmndrunners
11 import gozerbot.thr as thr
12 import sys, re, copy, types, thread
13
14 relock = thread.allocate_lock()
15 locked = lockdec(relock)
16
18
19 """ a regular expression callback """
20
21 - def __init__(self, index, regex, func, perm, plugname, speed=5, \
22 threaded=True, allowqueue=True, options={}):
23
24 self.name = "noname"
25 self.index = index
26
27 self.regex = regex
28 self.compiled = re.compile(regex)
29
30 self.func = func
31
32 if type(perm) == types.ListType:
33 self.perms = list(perm)
34 else:
35 self.perms = [perm, ]
36
37 self.plugname = plugname
38
39 self.speed = copy.deepcopy(speed)
40
41
42 self.threaded = copy.deepcopy(threaded)
43 self.allowqueue = copy.deepcopy(allowqueue)
44 self.options = dict(options)
45
47
48 """ this is were the regexs callbacks live """
49
52
54 """ nr of callbacks """
55 return len(self.relist)
56
58 """ return possible permissions """
59 result = []
60 for i in self.relist:
61 for j in i.perms:
62 if j not in result:
63 result.append(j)
64 return result
65
66 - def list(self, perm):
67 """ list re with permission perm """
68 result = []
69 perm = perm.upper()
70 for recom in self.relist:
71 if perm in recom.perms:
72 result.append(recom)
73 return result
74
76 """ return function names in plugin """
77 result = []
78 for i in self.relist:
79 if i.plugname == plug:
80 result.append(i.func.func_name)
81 return result
82
84 """ overload permission of function with funcname """
85 perms = [perm.upper() for perm in perms]
86 got = 0
87 for nr in range(len(self.relist)):
88 try:
89 if self.relist[nr].func.func_name == funcname:
90 self.relist[nr].perms = list(perms)
91 rlog(0, 'redispatcher', '%s function overloaded with %s' \
92 % (funcname, perms))
93 got = 1
94 except AttributeError:
95 rlog(10, 'redispatcher', 'permoverload: no %s function' % \
96 funcname)
97 if got:
98 return 1
99
100 - def add(self, index, regex, func, perm, speed=5, threaded=True, \
101 allowqueue=True, options={}):
102 """ add a command """
103 try:
104
105 plugname = calledfrom(sys._getframe())
106
107 self.relist.append(Recallback(index, regex, func, perm, plugname, \
108 speed, threaded, allowqueue, options))
109
110 self.relist.sort(lambda a, b: cmp(a.index, b.index))
111 rlog(0, 'redispatcher', 'added %s (%s) ' % (regex, plugname))
112 finally:
113 pass
114
116 """ unload regexs commands """
117 got = 0
118 try:
119 for i in range(len(self.relist)-1, -1 , -1):
120 if self.relist[i].plugname == plugname:
121 rlog(1, 'redispatcher', 'unloading %s (%s)' % \
122 (self.relist[i].regex, plugname))
123 del self.relist[i]
124 got = 1
125 finally:
126 pass
127 if got:
128 return 1
129
131 """ get re callback if txt matches """
132 for i in self.relist:
133 try:
134 result = re.search(i.compiled, txt)
135 if result:
136 return i
137 except:
138 pass
139
153
155
156 """ dispatcher on ircevent """
157
158 - def dispatch(self, callback, bot, ievent):
172
173 rebefore = Botredispatcher()
174 reafter = Botredispatcher()
175