| 77 # |
78 # |
| 78 # response_function |
79 # response_function |
| 79 # |
80 # |
| 80 g_responsePages = [[]] |
81 g_responsePages = [[]] |
| 81 g_responsePageNum = 0 |
82 g_responsePageNum = 0 |
| |
83 g_lastConfirm = 0 |
| |
84 g_confirmCommand = None |
| |
85 |
| |
86 class Confirmxeption (Exception): |
| |
87 def __init__ (self, id, value): |
| |
88 self.id = id |
| |
89 self.message = value |
| |
90 def __str__ (self): |
| |
91 return self.message |
| 82 |
92 |
| 83 def response_function (message): |
93 def response_function (message): |
| 84 global g_responsePages |
94 global g_responsePages |
| 85 |
95 |
| 86 if len (g_responsePages[-1]) > 4: |
96 if len (g_responsePages[-1]) > 4: |
| 87 g_responsePages.append ([message]) |
97 g_responsePages.append ([message]) |
| 88 else: |
98 else: |
| 89 g_responsePages[-1].append (message) |
99 g_responsePages[-1].append (message) |
| 90 |
100 |
| 91 def print_responses (bot, replyto): |
101 def confirm_function (id, message): |
| |
102 raise Confirmxeption (id, message) |
| |
103 |
| |
104 def print_responses (commandObject): |
| 92 global g_responsePages |
105 global g_responsePages |
| 93 global g_responsePageNum |
106 global g_responsePageNum |
| |
107 bot = commandObject['bot'] |
| |
108 replyto = commandObject['replyto'] |
| 94 |
109 |
| 95 # Check bounds |
110 # Check bounds |
| 96 if g_responsePageNum >= len (g_responsePages): |
111 if g_responsePageNum >= len (g_responsePages): |
| 97 bot.privmsg (replyto, "No more messages.") |
112 bot.privmsg (replyto, "No more messages.") |
| 98 return |
113 return |
| 109 num = (len (g_responsePages) - g_responsePageNum) |
124 num = (len (g_responsePages) - g_responsePageNum) |
| 110 bot.privmsg (replyto, "%d more page%s, use .more to continue output" \ |
125 bot.privmsg (replyto, "%d more page%s, use .more to continue output" \ |
| 111 % (num, 's' if num != 1 else '')) |
126 % (num, 's' if num != 1 else '')) |
| 112 |
127 |
| 113 # |
128 # |
| |
129 # check_same_caller (comm1, comm2) |
| |
130 # |
| |
131 # Are the two commands called by the same person? |
| |
132 # |
| |
133 def check_same_caller (comm1, comm2): |
| |
134 return comm1['bot'].name == comm2['bot'].name \ |
| |
135 and comm1['sender'] == comm2['sender'] \ |
| |
136 and comm1['ident'] == comm2['ident'] \ |
| |
137 and comm1['host'] == comm2['host'] |
| |
138 |
| |
139 def exec_command (commandObject): |
| |
140 global g_lastConfirm |
| |
141 global g_confirmCommand |
| |
142 cmdname = commandObject['cmdname'] |
| |
143 |
| |
144 try: |
| |
145 func = getattr (commandObject['module'], 'cmd_' + cmdname) |
| |
146 except AttributeError: |
| |
147 command_error ('command "%s" is not defined!' % cmdname) |
| |
148 |
| |
149 try: |
| |
150 func (**commandObject) |
| |
151 except Confirmxeption as e: |
| |
152 if time.time() - g_lastConfirm < 15 and g_confirmCommand != None: |
| |
153 command_error ('another confirm is underway') |
| |
154 |
| |
155 g_lastConfirm = time.time() |
| |
156 response_function (str (e) + ' (.yes/.no)') |
| |
157 commandObject['confirmed'] = e.id |
| |
158 g_confirmCommand = commandObject |
| |
159 |
| |
160 # |
| 114 # call_command (bot, message, cmdname, **kvargs) |
161 # call_command (bot, message, cmdname, **kvargs) |
| 115 # |
162 # |
| 116 # Calls a cobalt command |
163 # Calls a cobalt command |
| 117 # |
164 # |
| 118 def call_command (bot, message, cmdname, **kvargs): |
165 def call_command (bot, message, cmdname, **kvargs): |
| 120 global g_responsePageNum |
167 global g_responsePageNum |
| 121 |
168 |
| 122 try: |
169 try: |
| 123 cmd = Commands[cmdname] |
170 cmd = Commands[cmdname] |
| 124 except KeyError: |
171 except KeyError: |
| 125 return False |
172 return |
| 126 |
173 |
| 127 if not is_available (cmd, kvargs['ident'], kvargs['host']): |
174 if not is_available (cmd, kvargs['ident'], kvargs['host']): |
| 128 command_error ("you may not use %s" % cmdname) |
175 command_error ("you may not use %s" % cmdname) |
| 129 |
176 |
| 130 match = re.compile (cmd['regex']).match (message) |
177 match = re.compile (cmd['regex']).match (message) |
| 131 |
178 |
| 132 if match == None: |
179 if match == None: |
| 133 # didn't match |
180 # didn't match |
| 134 print "regex: %s" % cmd['regex'] |
181 print "regex: %s" % cmd['regex'] |
| 135 command_error ('invalid arguments\nusage: %s %s' % (cmd['name'], cmd['args'])) |
182 command_error ('invalid arguments\nusage: %s %s' % (cmd['name'], cmd['args'])) |
| 136 |
183 |
| 146 for argname in cmd['argnames']: |
193 for argname in cmd['argnames']: |
| 147 args[argname] = match.group (i) |
194 args[argname] = match.group (i) |
| 148 i += 1 |
195 i += 1 |
| 149 |
196 |
| 150 print "ModuleCore: %s called by %s" % (cmdname, kvargs['sender']) |
197 print "ModuleCore: %s called by %s" % (cmdname, kvargs['sender']) |
| 151 getattr (cmd['module'], "cmd_%s" % cmdname) (bot=bot, cmdname=cmdname, args=args, reply=response_function, **kvargs) |
198 commandObject = kvargs |
| |
199 commandObject['bot'] = bot |
| |
200 commandObject['cmdname'] = cmdname |
| |
201 commandObject['args'] = args |
| |
202 commandObject['reply'] = response_function |
| |
203 commandObject['confirm'] = confirm_function |
| |
204 commandObject['confirmed'] = 0 |
| |
205 commandObject['commandObject'] = commandObject |
| |
206 commandObject['info'] = cmd |
| |
207 commandObject['module'] = cmd['module'] |
| |
208 exec_command (commandObject) |
| 152 |
209 |
| 153 # Print the first page of responses. |
210 # Print the first page of responses. |
| 154 if cmdname != 'more': |
211 if cmdname != 'more': |
| 155 print_responses (bot, kvargs['replyto']) |
212 print_responses (commandObject) |
| 156 |
213 |
| 157 return True |
214 def confirm (cmd, yes): |
| |
215 global g_confirmCommand |
| |
216 |
| |
217 if g_confirmCommand == None: |
| |
218 cmd['reply'] ('%s to what?' % cmd['cmdname']) |
| |
219 return |
| |
220 |
| |
221 if not check_same_caller (cmd, g_confirmCommand): |
| |
222 return |
| |
223 |
| |
224 if yes: |
| |
225 exec_command (g_confirmCommand) |
| |
226 else: |
| |
227 cmd['reply'] ('okay then') |
| |
228 |
| |
229 g_confirmCommand = None |
| 158 |
230 |
| 159 # |
231 # |
| 160 # get_available_commands |
232 # get_available_commands |
| 161 # |
233 # |
| 162 # Gets a list of commands available to the given user |
234 # Gets a list of commands available to the given user |