def check_alerts_callback(bot=None): current_time = int(timegm(gmtime())) timecheck = current_time + int(LOOP_INTERVAL) # This seems like it might be a bit of a waste. But it should stop the rare occurance of "double tell delivery" (I've only seen it happen once.) alerts = bot.dbQuery('''SELECT id, target_user, alert_time, created_time, source, source_user, msg FROM alert WHERE delivered=0 AND alert_time<? ORDER BY alert_time;''', (timecheck,)) deliver_now = {} deliver_soon = {} for a in alerts: chan_or_user = a['source'].lower() delay = a['alert_time'] - current_time if delay <= 0: deliver_now.setdefault(chan_or_user, []).append(a) else: deliver_soon.setdefault(chan_or_user, []).append(a) for chan_or_user, alerts in deliver_now.iteritems(): deliver_alerts(chan_or_user, alerts) for chan_or_user, alerts in deliver_soon.iteritems(): ids = '_'.join(str(x['id']) for x in alerts) timer_name = '%s_%s' % (TIMER_NAME, ids) try: Timers.addtimer(timer_name, delay, deliver_alerts, reps=1, chan_or_user=chan_or_user, alerts=alerts, bot=bot) except TimerExists: pass
def nickChanged(event, bot): if bot.getOption("restorenick", module="pbm_nicktools"): snick = bot.getOption("nick") if event.newname != snick: # start timer to checkandrecov try: Timers.addtimer("NICKTOOLS_%s" % bot.network, float(bot.getOption("checkevery", module="pbm_nicktools")), nickCheckAndRecover, reps=-1, bot=bot ) except TimerExists: pass else: # have desired nick, delete check timers try: Timers.deltimer("NICKTOOLS_%s" % bot.network) except TimerNotFound: pass # TODO: like below, when state can track +r, this should be checked before attempting identify identify(bot, snick)
def timers(event, bot): command, args = commandSplit(event.argument) if command == "show": bot.say("Timers:") for timer in Timers.getTimers().itervalues(): bot.say(" - %s: reps = %s, delay = %s, f = %s" % (timer.name, timer.reps, timer.interval, timer.f)) elif command == "add": args = argumentSplit(args, 4) #add timername delay reps msg if not args: bot.say("Not enough arguments. Need: timername delay reps message (reps <= 0 means forever)") return try: if Timers.addtimer(args[0], float(args[1]), timercallback, reps=int(args[2]), msg=args[3], bot=bot, channel=event.target): bot.say("Timer added (%s)" % args[0]) else: bot.say("Timer not added for some reason?") except TimerExists: bot.say("Timer not added because it exists already.") except TimerInvalidName: bot.say("Timer not added because it has an invalid name.") elif command == "stop": try: Timers.deltimer(args) bot.say("Timer stopped (%s)" % args) except (TimerNotFound, TimerInvalidName): bot.say("Can't stop (%s) because timer not found or internal timer." % args)
def timers(event, bot): command, args = commandSplit(event.argument) if command == "show": bot.say("Timers:") for timer in Timers.getTimers().itervalues(): bot.say(" - %s: reps = %s, delay = %s, f = %s" % (timer.name, timer.reps, timer.interval, timer.f)) elif command == "add": args = argumentSplit(args, 4) #add timername delay reps msg if not args: bot.say( "Not enough arguments. Need: timername delay reps message (reps <= 0 means forever)" ) return msg = Timers.addtimer(args[0], float(args[1]), timercallback, reps=int(args[2]), msg=args[3], bot=bot, channel=event.target)[1] bot.say("%s (%s)" % (msg, args[0])) elif command == "stop": bot.say(Timers.deltimer(args)[1])
def nickChanged(event, bot): if bot.getOption("restorenick", module="pbm_nicktools"): snick = bot.getOption("nick") if event.newname != snick: # start timer to checkandrecov try: Timers.addtimer("NICKTOOLS_%s" % bot.network, float( bot.getOption("checkevery", module="pbm_nicktools")), nickCheckAndRecover, reps=-1, bot=bot) except TimerExists: pass else: # have desired nick, delete check timers try: Timers.deltimer("NICKTOOLS_%s" % bot.network) except TimerNotFound: pass # TODO: like below, when state can track +r, this should be checked before attempting identify identify(bot, snick)
def timers(event, bot): command, args = commandSplit(event.argument) if command == "show": bot.say("Timers:") for timer in Timers.getTimers().itervalues(): bot.say(" - %s: reps = %s, delay = %s, f = %s" % (timer.name, timer.reps, timer.interval, timer.f)) elif command == "add": args = argumentSplit(args, 4) #add timername delay reps msg if not args: bot.say( "Not enough arguments. Need: timername delay reps message (reps <= 0 means forever)" ) return try: if Timers.addtimer(args[0], float(args[1]), timercallback, reps=int(args[2]), msg=args[3], bot=bot, channel=event.target): bot.say("Timer added (%s)" % args[0]) else: bot.say("Timer not added for some reason?") except TimerExists: bot.say("Timer not added because it exists already.") except TimerInvalidName: bot.say("Timer not added because it has an invalid name.") elif command == "stop": try: Timers.deltimer(args) bot.say("Timer stopped (%s)" % args) except (TimerNotFound, TimerInvalidName): bot.say( "Can't stop (%s) because timer not found or internal timer." % args)
def setup_timer(event, bot): Timers.addtimer(TIMER_NAME, LOOP_INTERVAL, check_alerts_callback, reps=-1, startnow=False, bot=bot)