Changes in trunk/amazonbot/amazonbot.py [20:28]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/amazonbot/amazonbot.py
r20 r28 3 3 4 4 __version__ = '$Revision$' 5 __author__ = 'Atzm WATANABE < sitosito@p.chan.ne.jp>'5 __author__ = 'Atzm WATANABE <atzm@atzm.org>' 6 6 __date__ = '$Date$' 7 __copyright__ = 'Copyright(C) 2006 Atzm WATANABE, all rights reserved.'7 __copyright__ = 'Copyright(C) 2006-2007 Atzm WATANABE, all rights reserved.' 8 8 __license__ = 'Python' 9 9 10 10 import re 11 import string 11 12 import sys 12 13 import time … … 147 148 return method(c, e, to, words[1:]) # words[0] == command name 148 149 150 self.message_action(msg, c, e, to) 151 149 152 # silence 150 153 self.silence(msg, c, e, to) … … 217 220 def get_prev_time(self): 218 221 return self._prev_time 222 223 def message_action(self, msg, c, e, to): 224 for i in xrange(100): 225 action = 'action%d' % i 226 if not config.has_section(action): 227 break 228 229 c_stime = config.get(action, 'start_time') 230 c_etime = config.get(action, 'end_time') 231 232 try: 233 if c_stime and c_etime: 234 now = time.time() 235 [now_y, now_m, now_d] = time.localtime(now)[:3] 236 237 stime = '%04d/%02d/%02d %s' % (now_y, now_m, now_d, c_stime) 238 etime = '%04d/%02d/%02d %s' % (now_y, now_m, now_d, c_etime) 239 stime = time.mktime(time.strptime(stime, '%Y/%m/%d %H:%M')) 240 etime = time.mktime(time.strptime(etime, '%Y/%m/%d %H:%M')) 241 242 if not ((stime <= now) and (now <= etime)): 243 _debug('Out of time: %s - %s' % (c_stime, c_etime)) 244 continue 245 except: 246 _debug('Invalid time: %s - %s' % (str(c_stime), str(c_etime))) 247 continue 248 249 pattern = unicoding(config.get(action, 'input_pattern')) 250 match = None 251 try: 252 match = re.search(pattern, msg) 253 except: 254 _debug('Invalid regexp: %s', pattern) 255 continue 256 257 if not match: 258 continue 259 260 act = config.get(action, 'action') 261 fmt = config.get(action, 'message') 262 try: 263 _from = nm_to_n(e.source()) 264 message = ununicoding(fmt % _from) 265 266 if not message: 267 _debug('No message specified') 268 continue 269 270 if not act: 271 c.notice(to, message) 272 continue 273 274 method = getattr(self, 'onact_%s' % act, lambda *arg: False) 275 method(message, c, e, to) 276 277 except: 278 _debug('Action failed: %s (%s)' % (str(act), str(fmt))) 279 280 return True 219 281 220 282 class AmazonBot(AmazonBotBase): … … 244 306 return 'AmazonBot by %s, based on python-irclib' % __author__ 245 307 308 def onmsg_reload(self, c, e, to, args): 309 """Syntax: !reload 310 """ 311 _debug('in reload command: %s', str(args)) 312 config.read() 313 c.notice(to, 'reloaded config') 314 return True 315 316 def onmsg_lt(self, c, e, to, args): return self.onmsg_localtime(c, e, to, args) 317 def onmsg_localtime(self, c, e, to, args): 318 """Syntax: !localtime <unix time> 319 """ 320 _debug('in localtime command: %s', str(args)) 321 322 _from = nm_to_n(e.source()) 323 try: 324 sec = float(args[0]) 325 c.notice(_from, time.strftime('%b %d %T', time.localtime(sec))) 326 except ValueError, e: 327 c.notice(_from, 'Invalid argument: %s' % args[0]) 328 return False 329 330 return True 331 246 332 def onmsg_s(self, c, e, to, args): return self.onmsg_status(c, e, to, args) 247 333 def onmsg_status(self, c, e, to, args): … … 385 471 return [product_name, url] 386 472 473 def onact_oper(self, msg, c, e, to): 474 nick = nm_to_n(e.source()) 475 _debug('in oper action: %s to %s in %s' % (msg, nick, to)) 476 c.notice(to, msg) 477 c.mode(to, '+o %s' % nick) 478 return True 479 480 def onact_nooper(self, msg, c, e, to): 481 nick = nm_to_n(e.source()) 482 _debug('in nooper action: %s to %s in %s' % (msg, nick, to)) 483 c.notice(to, msg) 484 c.mode(to, '-o %s' % nick) 485 return True 486 487 def onact_kick(self, msg, c, e, to): 488 nick = nm_to_n(e.source()) 489 _debug('in kick action: %s to %s in %s' % (msg, nick, to)) 490 c.kick(to, nick, msg) 491 return True 492 493 def onact_kick_and_invite(self, msg, c, e, to): 494 nick = nm_to_n(e.source()) 495 _debug('in kick_and_invite action: %s to %s in %s' % (msg, nick, to)) 496 c.kick(to, nick, msg) 497 c.invite(nick, to) 498 return True 499 500 NICK_TRANS_TABLE = string.maketrans('-_i1z2o0s5bdnmft', '_-1i2z0o5sdbmntf') 501 def onact_nick(self, msg, c, e, to): 502 nick = nm_to_n(e.source()) 503 nickto = nick[0] + nick[1:].translate(self.NICK_TRANS_TABLE) 504 if nick == nickto: 505 nickto = nick + '_' 506 _debug('in nick action: %s to %s in %s (%s)' % (msg, nick, to, nickto)) 507 c.notice(to, msg) 508 c.nick(nickto) 509 return True 510 511 def onact_topic(self, msg, c, e, to): 512 nick = nm_to_n(e.source()) 513 _debug('in topic action: %s to %s in %s' % (msg, nick, to)) 514 c.topic(to, msg) 515 return True 516 387 517 if __name__ == '__main__': 388 518 bot = AmazonBot()
Note: See TracChangeset
for help on using the changeset viewer.