Ignore:
Timestamp:
02/09/07 14:19:24 (17 years ago)
Author:
atzm
Message:
  • Add action and localtime command
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/amazonbot/amazonbot.py

    r20 r22  
    55__author__ = 'Atzm WATANABE <sitosito@p.chan.ne.jp>' 
    66__date__ = '$Date$' 
    7 __copyright__ = 'Copyright(C) 2006 Atzm WATANABE, all rights reserved.' 
     7__copyright__ = 'Copyright(C) 2006-2007 Atzm WATANABE, all rights reserved.' 
    88__license__ = 'Python' 
    99 
     
    147147            return method(c, e, to, words[1:]) # words[0] == command name 
    148148 
     149        self.message_action(msg, c, e, to) 
     150 
    149151        # silence 
    150152        self.silence(msg, c, e, to) 
     
    217219    def get_prev_time(self): 
    218220        return self._prev_time 
     221 
     222    def message_action(self, msg, c, e, to): 
     223        for i in xrange(100): 
     224            action = 'action%d' % i 
     225            if not config.has_section(action): 
     226                break 
     227 
     228            c_stime = config.get(action, 'start_time') 
     229            c_etime = config.get(action, 'end_time') 
     230 
     231            try: 
     232                if c_stime and c_etime: 
     233                    now = time.time() 
     234                    [now_y, now_m, now_d] = time.localtime(now)[:3] 
     235 
     236                    stime = '%04d/%02d/%02d %s' % (now_y, now_m, now_d, c_stime) 
     237                    etime = '%04d/%02d/%02d %s' % (now_y, now_m, now_d, c_etime) 
     238                    stime = time.mktime(time.strptime(stime, '%Y/%m/%d %H:%M')) 
     239                    etime = time.mktime(time.strptime(etime, '%Y/%m/%d %H:%M')) 
     240 
     241                    if not ((stime <= now) and (now <= etime)): 
     242                        _debug('Out of time: %s - %s' % (c_stime, c_etime)) 
     243                        continue 
     244            except: 
     245                _debug('Invalid time: %s - %s' % (str(c_stime), str(c_etime))) 
     246                continue 
     247 
     248            match = re.search(unicoding(config.get(action, 'input_pattern')), msg) 
     249            if not match: 
     250                continue 
     251 
     252            act = config.get(action, 'action') 
     253            fmt = config.get(action, 'message') 
     254            try: 
     255                _from   = nm_to_n(e.source()) 
     256                message = ununicoding(fmt % _from) 
     257 
     258                if not message: 
     259                    _debug('No message specified') 
     260                    continue 
     261 
     262                if not act: 
     263                    c.notice(to, message) 
     264                    continue 
     265 
     266                method = getattr(self, 'onact_%s' % act, lambda *arg: False) 
     267                method(message, c, e, to) 
     268 
     269            except KeyboardInterrupt: 
     270                _debug('Action failed: %s (%s)' % (str(act), str(fmt))) 
     271 
     272        return True 
    219273 
    220274class AmazonBot(AmazonBotBase): 
     
    244298        return 'AmazonBot by %s, based on python-irclib' % __author__ 
    245299 
     300    def onmsg_lt(self, c, e, to, args): return self.onmsg_localtime(c, e, to, args) 
     301    def onmsg_localtime(self, c, e, to, args): 
     302        """Syntax: !localtime <unix time> 
     303        """ 
     304        _debug('in localtime command: %s', str(args)) 
     305 
     306        _from = nm_to_n(e.source()) 
     307        try: 
     308            sec = float(args[0]) 
     309            c.notice(_from, time.strftime('%b %d %T', time.localtime(sec))) 
     310        except ValueError, e: 
     311            c.notice(_from, 'Invalid argument: %s' % args[0]) 
     312            return False 
     313 
     314        return True 
     315 
    246316    def onmsg_s(self, c, e, to, args): return self.onmsg_status(c, e, to, args) 
    247317    def onmsg_status(self, c, e, to, args): 
     
    385455        return [product_name, url] 
    386456 
     457    def onact_oper(self, msg, c, e, to): 
     458        nick = nm_to_n(e.source()) 
     459        _debug('in oper action: %s to %s in %s' % (msg, nick, to)) 
     460        c.notice(to, msg) 
     461        c.mode(to, '+o %s' % nick) 
     462        return True 
     463 
     464    def onact_nooper(self, msg, c, e, to): 
     465        nick = nm_to_n(e.source()) 
     466        _debug('in nooper action: %s to %s in %s' % (msg, nick, to)) 
     467        c.notice(to, msg) 
     468        c.mode(to, '-o %s' % nick) 
     469        return True 
     470 
     471    def onact_kick(self, msg, c, e, to): 
     472        nick = nm_to_n(e.source()) 
     473        _debug('in kick action: %s to %s in %s' % (msg, nick, to)) 
     474        c.kick(to, nick, msg) 
     475        return True 
     476 
     477    def onact_kick_and_invite(self, msg, c, e, to): 
     478        nick = nm_to_n(e.source()) 
     479        _debug('in kick_and_invite action: %s to %s in %s' % (msg, nick, to)) 
     480        c.kick(to, nick, msg) 
     481        c.invite(nick, to) 
     482        return True 
     483 
     484    def onact_nick(self, msg, c, e, to): 
     485        nick = nm_to_n(e.source()) 
     486        _debug('in nick action: %s to %s in %s' % (msg, nick, to)) 
     487        c.notice(to, msg) 
     488        c.nick('%s_' % nick) 
     489        return True 
     490 
     491    def onact_topic(self, msg, c, e, to): 
     492        nick = nm_to_n(e.source()) 
     493        _debug('in topic action: %s to %s in %s' % (msg, nick, to)) 
     494        c.topic(to, msg) 
     495        return True 
     496 
    387497if __name__ == '__main__': 
    388498    bot = AmazonBot() 
Note: See TracChangeset for help on using the changeset viewer.