End all 'say' messages with a 'normal' ansi character to prevent bleedage. Also, added the beginnings of an IMC2 ansi parser.

This commit is contained in:
Greg Taylor 2009-04-25 07:13:19 +00:00
parent 0ad8c88c22
commit 9abde7b60f
5 changed files with 83 additions and 4 deletions

View file

@ -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.
"""

View file

@ -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),

View file

@ -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.

60
src/imc2/imc_ansi.py Normal file
View file

@ -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)

View file

@ -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