From 76f14b4779558b7db74b8e481dd4a0a5fc2be0ce Mon Sep 17 00:00:00 2001 From: Greg Taylor Date: Thu, 30 Apr 2009 03:13:46 +0000 Subject: [PATCH] Clean up send_cmessage() a bit. Changed noheader keyword arg to show_header and defaulted it to True. This should read a little better. Also updated some of the various function calls. More importantly, check to see if the channel argument is a string or unicode object and find the correct channel object as needed, which was the previous behavior. However, if it's not a string or unicode object, assume it's a CommChannel object and avoid querying for it again. This may be a performance win in some cases down the road. --- src/commands/comsys.py | 26 +++++++++++++++----------- src/comsys.py | 37 ++++++++++++++++++++++--------------- src/imc2/connection.py | 10 ++++------ src/session.py | 3 +-- 4 files changed, 42 insertions(+), 34 deletions(-) diff --git a/src/commands/comsys.py b/src/commands/comsys.py index e79aef7dd3..7d80ccc403 100644 --- a/src/commands/comsys.py +++ b/src/commands/comsys.py @@ -47,11 +47,11 @@ def cmd_addcom(command): chan_name_parsed, True) # Announce the user's joining. - join_msg = "[%s] %s has joined the channel." % \ - (chan_name_parsed, source_object.get_name(show_dbref=False)) + join_msg = "%s has joined the channel." % \ + (source_object.get_name(show_dbref=False),) src.comsys.send_cmessage(chan_name_parsed, join_msg) else: - source_object.emit_to("Could not find channel %s." % (chan_name,)) + source_object.emit_to("Could not find channel %s." % chan_name) GLOBAL_CMD_TABLE.add_command("addcom", cmd_addcom), def cmd_delcom(command): @@ -72,12 +72,12 @@ def cmd_delcom(command): return chan_name = command.session.channels_subscribed[command.command_argument][0] - source_object.emit_to("You have left %s." % (chan_name,)) + source_object.emit_to("You have left %s." % chan_name) src.comsys.plr_del_channel(command.session, command.command_argument) # Announce the user's leaving. - leave_msg = "[%s] %s has left the channel." % \ - (chan_name, source_object.get_name(show_dbref=False)) + leave_msg = "%s has left the channel." % \ + (source_object.get_name(show_dbref=False),) src.comsys.send_cmessage(chan_name, leave_msg) GLOBAL_CMD_TABLE.add_command("delcom", cmd_delcom), @@ -235,29 +235,33 @@ def cmd_cemit(command): source_object.emit_to("Could not find channel %s." % (cname,)) return + # If this is False, don't show the channel header before + # the message. For example: [Public] Woohoo! + show_channel_header = True if "noheader" in command.command_switches: if not source_object.has_perm("objects.emit_commchannel"): source_object.emit_to(defines_global.NOPERMS_MSG) return final_cmessage = cmessage + show_channel_header = False else: if "sendername" in command.command_switches: if not src.comsys.plr_has_channel(command.session, cname_parsed, return_muted=False): source_object.emit_to("You must be on %s to do that." % (cname_parsed,)) return - final_cmessage = "[%s] %s: %s" % (cname_parsed, - source_object.get_name(show_dbref=False), - cmessage) + final_cmessage = "%s: %s" % (source_object.get_name(show_dbref=False), + cmessage) else: if not source_object.has_perm("objects.emit_commchannel"): source_object.emit_to(defines_global.NOPERMS_MSG) return - final_cmessage = "[%s] %s" % (cname_parsed, cmessage) + final_cmessage = cmessage if not "quiet" in command.command_switches: source_object.emit_to("Sent - %s" % (name_matches[0],)) - src.comsys.send_cmessage(cname_parsed, final_cmessage) + src.comsys.send_cmessage(cname_parsed, final_cmessage, + show_header=show_channel_header) if settings.IMC2_ENABLED: # Look for IMC2 channel maps. If one is found, send an ice-msg-b diff --git a/src/comsys.py b/src/comsys.py index 20079565f2..a6803b0abd 100644 --- a/src/comsys.py +++ b/src/comsys.py @@ -53,8 +53,8 @@ def plr_chan_off(session, calias): cobj = get_cobj_from_name(cname) plr_set_channel_listening(session, calias, False) session.msg("You have left %s." % (cname,)) - send_cmessage(cname, "%s %s has left the channel." % (cobj.get_header(), - session.get_pobject().get_name(show_dbref=False))) + send_cmessage(cname, "%s has left the channel." % ( + session.get_pobject().get_name(show_dbref=False),)) def plr_chan_on(session, calias): """ @@ -70,8 +70,8 @@ def plr_chan_on(session, calias): else: cname = plr_cname_from_alias(session, calias) cobj = get_cobj_from_name(cname) - send_cmessage(cname, "%s %s has joined the channel." % (cobj.get_header(), - session.get_pobject().get_name(show_dbref=False))) + send_cmessage(cname, "%s has joined the channel." % ( + session.get_pobject().get_name(show_dbref=False),)) plr_set_channel_listening(session, calias, True) session.msg("You have joined %s." % (cname,)) @@ -220,24 +220,31 @@ def load_object_channels(pobject): for session in sessions: session.channels_subscribed = simplejson.loads(chan_list) -def send_cmessage(channel_name, message, noheader=True): +def send_cmessage(channel, message, show_header=True): """ Sends a message to all players on the specified channel. - channel_name: (string) The name of the channel. - message: (string) Message to send. - noheader: (bool) If False, prefix the message with the channel's name. + channel: (string or CommChannel) Name of channel or a CommChannel object. + message: (string) Message to send. + show_header: (bool) If False, don't prefix message with the channel header. """ - try: - channel_obj = get_cobj_from_name(channel_name) - except: - logger.log_errmsg("send_cmessage(): Can't find channel: %s" % (channel_name,)) - return + if isinstance(channel, unicode) or isinstance(channel, str): + # If they've passed a string as the channel argument, look up the + # correct channel object. + try: + channel_obj = get_cobj_from_name(channel) + except: + logger.log_errmsg("send_cmessage(): Can't find channel: %s" % channel) + return + else: + # Else, assume that it's a channel object and skip re-querying for + # the channel. + channel_obj = channel - if noheader == False: + if show_header == True: message = "%s %s" % (channel_obj.ansi_name, message) - for user in get_cwho_list(channel_name, return_muted=False): + for user in get_cwho_list(channel_obj.name, return_muted=False): user.msg(message) chan_message = CommChannelMessage() diff --git a/src/imc2/connection.py b/src/imc2/connection.py index e7c3649ada..0c8f933f9f 100644 --- a/src/imc2/connection.py +++ b/src/imc2/connection.py @@ -24,8 +24,7 @@ def cemit_info(message): Channel emits info to the appropriate info channel. By default, this is MUDInfo. """ - comsys.send_cmessage(settings.COMMCHAN_IMC2_INFO, 'IMC: %s' % message, - noheader=False) + comsys.send_cmessage(settings.COMMCHAN_IMC2_INFO, 'IMC: %s' % message) class IMC2Protocol(StatefulTelnetProtocol): """ @@ -118,10 +117,9 @@ class IMC2Protocol(StatefulTelnetProtocol): mapping = IMC2ChannelMapping.objects.get(imc2_channel_name=chan_name) ingame_chan_name = mapping.channel.name # Format the message to cemit to the local channel. - message = '[%s] %s@%s: %s' % (ingame_chan_name, - packet.sender, - packet.origin, - packet.optional_data.get('text')) + message = '%s@%s: %s' % (packet.sender, + packet.origin, + packet.optional_data.get('text')) # Bombs away. comsys.send_cmessage(ingame_chan_name, message) except IMC2ChannelMapping.DoesNotExist: diff --git a/src/session.py b/src/session.py index d1aee5bc6b..0c83dd7ec0 100755 --- a/src/session.py +++ b/src/session.py @@ -219,5 +219,4 @@ class SessionProtocol(StatefulTelnetProtocol): is MUDConnections. """ src.comsys.send_cmessage(settings.COMMCHAN_MUD_CONNECTIONS, - 'Session: %s' % message, - noheader=False) + 'Session: %s' % message)