Implemented imcwhois command as part of the imcinfo command cluster.

This commit is contained in:
Griatch 2011-04-19 21:05:18 +00:00
parent 36bc29865b
commit b856cb8faf
6 changed files with 45 additions and 229 deletions

View file

@ -328,6 +328,11 @@ class IMC2Protocol(telnet.StatefulTelnetProtocol):
destination = data.get("destination", "Unknown")
self.send_packet(pck.IMC2PacketTell(from_name, target, destination, message))
elif packet_type == "imcwhois":
# send a whois request
if type(data) == dict:
target = data.get("target", "Unknown")
self.send_packet(pck.IMC2PacketWhois(from_obj.id, target))
class IMC2Factory(protocol.ClientFactory):
"""

View file

@ -5,14 +5,15 @@ special markup strings.
This is a IMC2 complacent version.
"""
from src.utils.ansi import ANSIParser, ANSITable
import re
from src.utils.ansi import ANSIParser, ANSITable, parse_ansi
class IMCANSIParser(ANSIParser):
"""
This parser is per the IMC2 specification.
"""
def __init__(self):
self.ansi_subs = [
self.ansi_map = [
# Random
(r'~Z', ANSITable.ansi["normal"]),
# Dark Grey
@ -58,9 +59,17 @@ class IMCANSIParser(ANSIParser):
(r'\\r', ANSITable.ansi["normal"]),
(r'\\n', ANSITable.ansi["return"]),
]
# prepare regex matching
self.ansi_sub = [(re.compile(sub[0], re.DOTALL), sub[1])
for sub in self.ansi_map]
# prepare matching ansi codes overall
self.ansi_regex = re.compile("\033\[[0-9;]+m")
ANSI_PARSER = IMCANSIParser()
def parse_ansi(*args, **kwargs):
def parse_ansi(string, strip_ansi=False, parser=ANSI_PARSER):
"""
Shortcut to use the IMC2 ANSI parser.
"""
return ansi.parse_ansi(parser=IMCANSIParser(), *args, **kwargs)
return parser.parse_ansi(string, strip_ansi=strip_ansi)

View file

@ -1,5 +1,6 @@
"""
This module handles some of the -reply packets like whois-reply.
"""
from src.objects.models import ObjectDB
from src.comms.imc2lib import imc2_ansi
@ -7,10 +8,9 @@ from src.comms.imc2lib import imc2_ansi
def handle_whois_reply(packet):
try:
pobject = ObjectDB.objects.get(id=packet.target)
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:
response_text = imc2_ansi.parse_ansi(packet.optional_data.get('text', 'Unknown'))
string = 'Whois reply from %s: %s' % (packet.origin, response_text)
pobject.msg(string.strip())
except ObjectDB.DoesNotExist:
# No match found for whois sender. Ignore it.
pass

View file

@ -653,10 +653,9 @@ class IMC2PacketWhois(IMC2Packet):
Example:
You@YourMUD 1234567890 YourMUD whois dude@* level=5
"""
def __init__(self, pobject, whois_target):
def __init__(self, pobject_id, whois_target):
super(IMC2PacketWhois, self).__init__()
# Use the dbref, it's easier to trace back for the whois-reply.
self.sender = pobject.id
self.sender = pobject_id # Use the dbref, it's easier to trace back for the whois-reply.
self.packet_type = 'whois'
self.target = whois_target
self.destination = '*'