Add tracking of IMC2 channels and the new 'imcchanlist' command.

This commit is contained in:
Greg Taylor 2009-04-30 03:56:52 +00:00
parent 50e70327fe
commit 181133d917
4 changed files with 92 additions and 15 deletions

View file

@ -100,6 +100,8 @@ class IMC2Protocol(StatefulTelnetProtocol):
# IMC2 protocol states that KeepAliveRequests should be followed
# up by the requester sending an IsAlive packet.
self.send_packet(IMC2PacketIsAlive())
# Get a listing of channels.
self.send_packet(IMC2PacketIceRefresh())
def _handle_channel_mappings(self, packet):
"""
@ -135,10 +137,23 @@ class IMC2Protocol(StatefulTelnetProtocol):
self._parse_auth_response(line)
else:
if settings.IMC2_DEBUG and 'is-alive' not in line:
# if IMC2_DEBUG mode is on, print the contents of the packet
# to stdout.
logger.log_infomsg("PACKET: %s" % line)
# Parse the packet and encapsulate it for easy access
packet = IMC2Packet(packet_str = line)
if settings.IMC2_DEBUG and packet.packet_type not in ['is-alive', 'keepalive-request']:
# Print the parsed packet's __str__ representation.
# is-alive and keepalive-requests happen pretty frequently.
# Don't bore us with them in stdout.
logger.log_infomsg(packet)
"""
Figure out what kind of packet we're dealing with and hand it
off to the correct handler.
"""
if packet.packet_type == 'is-alive':
IMC2_MUDLIST.update_mud_from_packet(packet)
elif packet.packet_type == 'keepalive-request':
@ -151,6 +166,8 @@ class IMC2Protocol(StatefulTelnetProtocol):
reply_listener.handle_whois_reply(packet)
elif packet.packet_type == 'close-notify':
IMC2_MUDLIST.remove_mud_from_packet(packet)
elif packet.packet_type == 'ice-update':
IMC2_CHANLIST.update_channel_from_packet(packet)
elif packet.packet_type == 'tell':
sessions = session_mgr.find_sessions_from_username(packet.target)
for session in sessions:

View file

@ -274,7 +274,12 @@ class IMC2PacketIceRefresh(IMC2Packet):
Example:
*@YourMUD 1234567890 YourMUD!Hub1 ice-refresh IMC@$
"""
pass
def __init__(self):
super(IMC2PacketIceRefresh, self).__init__()
self.sender = '*'
self.packet_type = 'ice-refresh'
self.target = 'IMC'
self.destination = '$'
class IMC2PacketIceUpdate(IMC2Packet):
"""

View file

@ -55,6 +55,55 @@ class IMC2MudList(object):
except KeyError:
# No matching entry, no big deal.
pass
class IMC2Channel(object):
"""
Stores information about channels available on the network.
"""
def __init__(self, packet):
self.name = packet.optional_data.get('localname', None)
self.full_name = packet.optional_data.get('channel', None)
self.level = packet.optional_data.get('level', None)
self.owner = packet.optional_data.get('owner', None)
self.policy = packet.optional_data.get('policy', None)
self.last_updated = time()
class IMC2ChanList(object):
"""
Keeps track of other MUDs connected to the IMC network.
"""
def __init__(self):
# Chan list is stored in a dict, key being the IMC Mud name.
self.chan_list = {}
def get_channel_list(self):
"""
Returns a sorted list of cached channels.
"""
channels = self.chan_list.items()
channels.sort()
return [value for key, value in channels]
def update_channel_from_packet(self, packet):
"""
This grabs relevant info from the packet and stuffs it in the
channel list for later retrieval.
"""
channel = IMC2Channel(packet)
self.chan_list[channel.name] = channel
def remove_channel_from_packet(self, packet):
"""
Removes a channel from the Channel list when given a packet.
"""
channel = IMC2Channel(packet)
try:
del self.chan_list[channel.name]
except KeyError:
# No matching entry, no big deal.
pass
# Use this instance to keep track of the other games on the network.
IMC2_MUDLIST = IMC2MudList()
IMC2_MUDLIST = IMC2MudList()
# Tracks the list of available channels on the network.
IMC2_CHANLIST = IMC2ChanList()