def te_chat(work, slot, colour, text): no_echo = [False] echo_lines = [] event_type = None agent = work.terraria.user if slot == 255: match = re.match(r'((?P<sn>\[\S+\])|\*(?P<an>\S+))\s*(?P<m>.*)', text) if match: event_type = 'MESSAGE' if match.group('sn') else 'ACTION' event_name = match.group('sn') or match.group('an') event_text = match.group('m') else: event_type = 'MESSAGE' event_name = work.terraria_protocol.players.get(slot, slot) event_text = text if event_type is not None: reply = bridge.substitute_reply( context = work.terraria.name, local_name = event_name, msg_local = lambda m: terraria_protocol.chat(work, strip_codes(m)), msg_bridge = lambda m: echo_lines.append('<%s> %s' % (agent, m)), cancel_bridge = lambda: operator.setitem(no_echo, 0, True)) yield util.msign(ab_mode, ('BRIDGE', event_type), ab_mode, event_name, work.terraria.name, event_text, reply) if not no_echo[0] and slot != work.terraria_protocol.slot: if slot != 255: text = '<%s> %s' % (name, text) echo_lines.insert(0, text) for line in echo_lines: line = bridge.substitute_text(work.terraria.name, line) yield sign('TERRARIA', work, line)
def mc_found(work, line): line = re.sub(r'§.', '', line) match = re.match('!query (\S+) (\S+) (.*)', line) if match: type, key, body = match.groups() head = 'QUERY_' + type.upper() for event in head, (head, key): yield sign(event, work, type, key, body) return agent = getattr(work.minecraft_state, 'agent', None) if agent: for fmt in '<%s>', '* %s ', '%s ': if line.startswith(fmt % agent): return no_echo = [False] echo_lines = [] match = re.match(r'((?P<sn>\[\S+\])|<(?P<mn>\S+)>|\* (?P<an>\S+)) ' '(?P<msg>.*)', line) if match: msg = match.group('msg') name = match.group('sn') or match.group('mn') or match.group('an') event = 'ACTION' if match.group('an') else 'MESSAGE' reply = bridge.substitute_reply( context = work.minecraft.name, local_name = name, msg_local = lambda m: work.dump(strip_codes(m) + '\n'), msg_bridge = lambda m: echo_lines.append(( '<%s> %s' % (sub_name(work, agent), m), {'no_proxy': True})), cancel_bridge = lambda: operator.setitem(no_echo, 0, True)) ab_mode.drive(('BRIDGE', event), ab_mode, name, work.minecraft.name, msg, reply) if not no_echo[0]: if match: name_group = next(g for g in ('sn','mn','an') if match.group(g)) name_s, name_e = match.span(name_group) echo(work, line[:name_s] + sub_name(work, name) + line[name_e:]) else: echo(work, sub_text(work, line)) for line, kwds in echo_lines: echo(work, line, **kwds)