def h_url_collect_urls(bot, urls, chan, id, orig_msg): if not chan: return chan = chan.lower() delete_indices = [] for url_spec, index in izip(urls, count()): url, is_nsfw = url_collect.url_nsfw(url_spec) for tries in xrange(UPLOAD_RETRIES): if tries: yield runtime.sleep(UPLOAD_RETRY_S) mirror_url = get_mirror_url(url, chan) if mirror_url: break else: continue nsfw_str = '\2NSFW:\2 ' if is_nsfw else '' msg = '%s%s copied to <%s>.' % (nsfw_str, url, mirror_url) message.reply(bot, id, chan, msg, prefix=False) url_collect.history[chan].append([ url_collect.nsfw_url(mirror_url, is_nsfw)]) delete_indices.append(index) new_urls = [u for (i,u) in izip(count(),urls) if i not in delete_indices] for index in xrange(len(url_collect.history[chan])-1, -1, -1): if url_collect.history[chan][index] == urls: if new_urls: url_collect.history[chan][index] = new_urls else: del url_collect.history[chan][index] break
def h_read(bot, id, chan, args, full_msg): if chan is not None: msgs = deliver_msgs(bot, id, chan, explicit=True) if not msgs: reply(bot, id, chan, 'You have no messages.') return state = get_state() all_msgs = [m for m in state.msgs if would_deliver(id, None, m) and m in state.last_notify] earliest = dict() for msg in all_msgs: earliest[msg.channel.lower()] = min( msg.time_sent, earliest.get(msg.channel.lower(), msg.time_sent)) all_msgs = sorted(all_msgs, key=lambda m: earliest[m.channel.lower()]) if not all_msgs: reply(bot, id, chan, 'No messages are available to read.') return msgs = all_msgs[:MAX_DELIVER_PM] remain_msgs = all_msgs[MAX_DELIVER_PM:] while 1 <= sum(1 for m in remain_msgs if m.channel == msgs[-1].channel) \ <= MAX_DELIVER_CHAN: msgs.append(remain_msgs.pop(0)) for msg, index in izip(msgs, count(1)): tag = '%d/%d: ' % (index, len(all_msgs)) deliver_msg(bot, id, None, msg, tag=tag) yield runtime.sleep(0) state = get_state() state.msgs = [m for m in state.msgs if m not in msgs] put_state(state) next_msgs = remain_msgs[:MAX_DELIVER_PM] next_remain_msgs = remain_msgs[MAX_DELIVER_PM:] while any(n <= MAX_DELIVER_CHAN for n in Counter(m.channel for m in next_remain_msgs).itervalues()): next_msgs.append(next_remain_msgs.pop(0)) noun = 'message' if len(remain_msgs) == 1 else 'messages' if next_remain_msgs: reply(bot, id, chan, 'Say \2!read\2 to read the next %d of %d %s.' % ( len(next_msgs), len(remain_msgs), noun)) elif next_msgs: reply(bot, id, chan, 'Say \2!read\2 to read the remaining %d %s.' % ( len(next_msgs), noun)) else: reply(bot, id, chan, 'End of messages.')
def h_url(bot, id, target, args, full_msg, reply): channel = (target or ('%s!%s@%s' % id)).lower() if args: dice = sys.modules.get('dice') match = re.match(r'!r(oll)?\s+(?P<args>.*)', args) if match and dice and bot in dice.link.installed_modes: args = match.group('args') args = dice.roll(bot, id, target, args, error_reply=reply) if args is None: return reply(args) urls = url_collect.extract_urls(args) yield sign('URL_CMD_URLS', bot, urls, target, id, full_msg) elif url_collect.history[channel]: urls = url_collect.history[channel].pop(-1) else: urls = None if not urls: reply('No URL found.') return for url in urls: try: result = get_title_proxy(url) reply(result['title']) # Generate a URL-suppressed proxy message for the basic component. btitle = result['title_bare'] p_target = '%s!%s@%s' % id if target is None else target if isinstance(btitle, unicode): btitle = btitle.encode('utf-8') yield later(sign('PROXY_MSG', bot, None, p_target, btitle, no_url=True)) if result.get('proxy'): # Generate a quiet proxy message for the parenthetical component. pmsg, fmsg = result['proxy'], result['proxy_full'] if isinstance(pmsg, unicode): pmsg = pmsg.encode('utf-8') if isinstance(fmsg, unicode): fmsg = fmsg.encode('utf-8') yield later(sign('PROXY_MSG', bot, None, p_target, pmsg, full_msg=fmsg, quiet=True, no_url=True)) yield runtime.sleep(0.01) except Exception as e: traceback.print_exc() url, is_nsfw = url_collect.url_nsfw(url) reply('Error: %s [%s%s]' % (e, abbrev_url(url), ' \2NSFW\2' if is_nsfw else ''))
def show_board(bot, game, priority=None): yield runtime.sleep(0) lines = { BLACK:[], WHITE:[] } for colour in BLACK, WHITE: chan = game.names[colour] lines[colour].extend(game.game_lines(viewer=colour, irc=True)) if priority: for colour in priority, other_colour(priority): for line in lines[colour]: bot.send_msg(game.names[colour], line, no_link=True) else: for b_line, w_line in izip_longest(lines[BLACK], lines[WHITE]): if b_line: bot.send_msg(game.names[BLACK], b_line, no_link=True) if w_line: bot.send_msg(game.names[WHITE], w_line, no_link=True)
def h_dominions_tick(bot, log_level=None): try: if not is_installed: return urls = [] latest = time.time() + UPDATE_PERIOD_S for chan, cobj in state.channels.iteritems(): for url in cobj.games: if url in urls: continue if url in state.games and state.games[url].time > latest: continue urls.append(url) yield update_urls(bot, urls, log_level=log_level) except: traceback.print_exc() yield runtime.sleep(TICK_PERIOD_S) yield sign('DOMINIONS_TICK', bot, log_level=log_level)
def h_nuke(bot, id, target, args, full_msg): if not target: return if not channel.has_op_in(bot, bot.nick, target, 'h'): return global nuclear_launch target_id = (target.lower(), ('%s!%s@%s' % id).lower()) try: if target_id in nuclear_launch: return except NameError: nuclear_launch = set() nuclear_launch.add(target_id) message.reply(bot, id, target, 'Nuclear launch detected.', prefix=False) yield runtime.sleep(15) bot.send_cmd('KICK %s %s :Akuryou taisan!' % (target, id.nick)) ERR_CHANOPRIVSNEEDED = '482' UNREAL_ERR_CANNOTDOCOMMAND = '972' timeout = yield runtime.timeout(10) while True: event, args = yield hold(bot, timeout, UNREAL_ERR_CANNOTDOCOMMAND, ERR_CHANOPRIVSNEEDED) if event == UNREAL_ERR_CANNOTDOCOMMAND: e_bot, e_src, e_tgt, e_cmd, e_args = args if e_cmd.upper() != 'KICK': continue message.reply(bot, id, target, 'Nuclear launch failed: "%s".' % e_args, prefix=False) elif event == ERR_CHANOPRIVSNEEDED: e_bot, e_src, e_tgt, e_chan, e_args = args if e_chan.lower() != target.lower(): continue message.reply(bot, id, target, 'Nuclear launch failed: "%s".' % e_args, prefix=False) elif event == timeout: break nuclear_launch.discard(target_id)
def h_heartbeat(work): while hasattr(work, 'terraria_protocol'): send_set_player_life(work, work.terraria_protocol.slot, 0, 0) yield runtime.sleep(1)
def delayed_op_in(bot, nicks, chan): yield runtime.sleep(DELAY_BASE_S + random.uniform(0, DELAY_RAND_S)) yield give_op_in(bot, nicks, chan)
while True: _, (e_bot, e_chan, msg) = yield hold(bot, 'FTO_MSG') strip_msg = strip(msg) if e_chan and e_chan.lower() == chan.lower(): break if time.clock() - start > 3600: return if 'bananabanana' in strip_msg: return if remaining: reply('\2GIANT ASPARAGUS!') #--------------------------------------------------------------------------- # HEYYEYAAEYAAAEYAEYAA # http://www.youtube.com/watch?v=6GggY4TEYbk elif re.search(r'ands?hetries', strip_msg) and not any(s in strip_msg \ for s in ('ohmygod', 'doitry', 'itryallthetime', 'inthisinstitution')): reply('Oh my god, do I try!') yield runtime.sleep(1) reply('I try all the time... in this institution!') elif re.search(r'ands?heprays', strip_msg) and not any(s in strip_msg \ for s in ('ohmygod', 'doipray', 'iprayeverysingleday', 'revolution')) \ and not re.search(r'[mn]y[ea]+', strip_msg): reply('Oh my god, do I pray!') yield runtime.sleep(1) reply('I pray every single day...') start = time.clock() while time.clock() - start < 60: (_, (e_bot, e_chan, msg)) = yield hold(bot, 'FTO_MSG') strip_msg = strip(msg) if not e_chan or e_chan.lower() != chan.lower(): continue if 'andheprays' in strip_msg:
def h_reconnect_work(work, version, delay): server = work.terraria disconnect_work(work, remove=False) yield runtime.sleep(delay or RECONNECT_DELAY_SECONDS) new_work = init_work(server, version=version, reconnect_from=work) te_work[server.name.lower()] = new_work
def h_qdbs_tick(bot, log_level=None): yield Private.class_refresh(bot) yield Public.class_refresh(bot) yield runtime.sleep(TICK_PERIOD_S) yield sign('QDBS_TICK', bot, log_level=log_level)
def mc_close_recv_error(work, *args): kill_work(work) yield runtime.sleep(RECONNECT_DELAY_SECONDS) init_work(work.minecraft, reconnect_from=work)
def ch_close_recv_error(work, *args): kill_work(work) yield runtime.sleep(RECONNECT_DELAY_SECONDS) init_work(work.address)