From 9abde7b60fb40b380be19c3d947f84693a2ec092 Mon Sep 17 00:00:00 2001 From: Greg Taylor Date: Sat, 25 Apr 2009 07:13:19 +0000 Subject: [PATCH] End all 'say' messages with a 'normal' ansi character to prevent bleedage. Also, added the beginnings of an IMC2 ansi parser. --- src/ansi.py | 3 +- src/commands/general.py | 3 +- src/commands/imc2.py | 15 ++++++++++ src/imc2/imc_ansi.py | 60 ++++++++++++++++++++++++++++++++++++++ src/imc2/reply_listener.py | 6 +++- 5 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 src/imc2/imc_ansi.py diff --git a/src/ansi.py b/src/ansi.py index 17c70a8de0..3cf9ad19b1 100755 --- a/src/ansi.py +++ b/src/ansi.py @@ -106,8 +106,7 @@ class MuxANSIParser(BaseParser): (r'%cW', ANSITable.ansi["back_white"]), ] -ANSI_PARSER = MuxANSIParser() -def parse_ansi(string, strip_ansi=False, strip_formatting=False, parser=ANSI_PARSER): +def parse_ansi(string, strip_ansi=False, strip_formatting=False, parser=MuxANSIParser()): """ Parses a string, subbing color codes as needed. """ diff --git a/src/commands/general.py b/src/commands/general.py index 16491490dc..37ae4a1693 100644 --- a/src/commands/general.py +++ b/src/commands/general.py @@ -415,7 +415,8 @@ def cmd_say(command): speech = command.command_argument # Feedback for the object doing the talking. - source_object.emit_to("You say, '%s'" % (speech,)) + source_object.emit_to("You say, '%s%s'" % (speech, + ANSITable.ansi['normal'])) # Build the string to emit to neighbors. emit_string = "%s says, '%s'" % (source_object.get_name(show_dbref=False), diff --git a/src/commands/imc2.py b/src/commands/imc2.py index 5a28f29417..9e9a9edf8b 100644 --- a/src/commands/imc2.py +++ b/src/commands/imc2.py @@ -9,6 +9,8 @@ from src import defines_global from src import ansi from src.util import functions_general from src.cmdtable import GLOBAL_CMD_TABLE +from src.ansi import parse_ansi +from src.imc2.imc_ansi import IMCANSIParser from src.imc2 import connection as imc2_conn from src.imc2.packets import * from src.imc2.trackers import IMC2_MUDLIST @@ -27,6 +29,19 @@ def cmd_imcwhois(command): imc2_conn.IMC2_PROTOCOL_INSTANCE.send_packet(packet) GLOBAL_CMD_TABLE.add_command("imcwhois", cmd_imcwhois) +def cmd_imcansi(command): + """ + Test IMC ANSI conversion. + """ + source_object = command.source_object + if not command.command_argument: + source_object.emit_to("You must provide a string to convert.") + return + else: + retval = parse_ansi(command.command_argument, parser=IMCANSIParser()) + source_object.emit_to(retval) +GLOBAL_CMD_TABLE.add_command("imcansi", cmd_imcansi) + def cmd_imckeepalive(command): """ Sends an is-alive packet to the network. diff --git a/src/imc2/imc_ansi.py b/src/imc2/imc_ansi.py new file mode 100644 index 0000000000..1be97d750c --- /dev/null +++ b/src/imc2/imc_ansi.py @@ -0,0 +1,60 @@ +from src import ansi +from src.ansi import BaseParser, ANSITable + +class IMCANSIParser(BaseParser): + """ + This parser is per the IMC2 specification. + """ + def __init__(self): + self.ansi_subs = [ + # Random + (r'~Z', ANSITable.ansi["normal"]), + # Dark Grey + (r'~D', ANSITable.ansi["hilite"] + ANSITable.ansi["black"]), + (r'~z', ANSITable.ansi["hilite"] + ANSITable.ansi["black"]), + # Grey/reset + (r'~w', ANSITable.ansi["normal"]), + (r'~d', ANSITable.ansi["normal"]), + (r'~!', ANSITable.ansi["normal"]), + # Bold/hilite + (r'~L', ANSITable.ansi["hilite"]), + # White + (r'~W', ANSITable.ansi["normal"] + ANSITable.ansi["hilite"]), + # Dark Green + (r'~g', ANSITable.ansi["normal"] + ANSITable.ansi["green"]), + # Green + (r'~G', ANSITable.ansi["hilite"] + ANSITable.ansi["green"]), + # Dark magenta + (r'~p', ANSITable.ansi["normal"] + ANSITable.ansi["magenta"]), + (r'~m', ANSITable.ansi["normal"] + ANSITable.ansi["magenta"]), + # Magenta + (r'~M', ANSITable.ansi["hilite"] + ANSITable.ansi["magenta"]), + (r'~P', ANSITable.ansi["hilite"] + ANSITable.ansi["magenta"]), + # Black + (r'~x', ANSITable.ansi["normal"] + ANSITable.ansi["black"]), + # Cyan + (r'~c', ANSITable.ansi["normal"] + ANSITable.ansi["cyan"]), + # Dark Yellow (brown) + (r'~Y', ANSITable.ansi["hilite"] + ANSITable.ansi["yellow"]), + # Yellow + (r'~y', ANSITable.ansi["normal"] + ANSITable.ansi["yellow"]), + # Dark Blue + (r'~B', ANSITable.ansi["normal"] + ANSITable.ansi["blue"]), + # Blue + (r'~C', ANSITable.ansi["hilite"] + ANSITable.ansi["blue"]), + # Dark Red + (r'~r', ANSITable.ansi["normal"] + ANSITable.ansi["red"]), + # Red + (r'~R', ANSITable.ansi["normal"] + ANSITable.ansi["red"]), + # Dark Blue + (r'~b', ANSITable.ansi["normal"] + ANSITable.ansi["blue"]), + ## Formatting + (r'\\r', ANSITable.ansi["normal"]), + (r'\\n', ANSITable.ansi["return"]), + ] + +def parse_ansi(*args, **kwargs): + """ + Shortcut to use the IMC2 ANSI parser. + """ + return ansi.parse_ansi(parser=IMCANSIParser(), *args, **kwargs) \ No newline at end of file diff --git a/src/imc2/reply_listener.py b/src/imc2/reply_listener.py index b3352ce2fb..485398b59c 100644 --- a/src/imc2/reply_listener.py +++ b/src/imc2/reply_listener.py @@ -2,11 +2,15 @@ This module handles some of the -reply packets like whois-reply. """ from src.objects.models import Object +from src.imc2 import imc_ansi def handle_whois_reply(packet): try: pobject = Object.objects.get(id=packet.target) - pobject.emit_to('Whois reply: %s' % packet.optional_data.get('text', 'Unknown')) + response_text = imc_ansi.parse_ansi(packet.optional_data.get('text', + 'Unknown')) + pobject.emit_to('Whois reply from %s: %s' % (packet.origin, + response_text)) except Object.DoesNotExist: # No match found for whois sender. Ignore it. pass \ No newline at end of file