diff --git a/src/commands/comsys.py b/src/commands/comsys.py index 67432c848f..697c7e08f3 100644 --- a/src/commands/comsys.py +++ b/src/commands/comsys.py @@ -402,7 +402,7 @@ def cmd_cemit(command): else: if "sendername" in command.command_switches: if not comsys.plr_has_channel(command.session, cname_parsed, - return_muted=False): + return_muted=False): source_object.emit_to("You must be on %s to do that." % (cname_parsed,)) return final_cmessage = "%s: %s" % (source_object.get_name(show_dbref=False), @@ -419,7 +419,7 @@ def cmd_cemit(command): show_header=show_channel_header) #pipe to external channels (IRC, IMC) eventually mapped to this channel - comsys.send_cexternal(cname_parsed, "[%s] %s" % (cname_parsed,final_cmessage)) + comsys.send_cexternal(cname_parsed, cmessage, caller=source_object) GLOBAL_CMD_TABLE.add_command("@cemit", cmd_cemit), diff --git a/src/comsys.py b/src/comsys.py index 26e49887e3..6de9e05fae 100644 --- a/src/comsys.py +++ b/src/comsys.py @@ -257,20 +257,22 @@ def send_cmessage(channel, message, show_header=True, from_external=None): # the channel. channel_obj = channel + final_message = message if show_header == True: - message = "%s %s" % (channel_obj.ansi_name, message) + final_message = "%s %s" % (channel_obj.ansi_name, message) for user in get_cwho_list(channel_obj.name, return_muted=False): - user.msg(message) + user.msg(final_message) chan_message = CommChannelMessage() chan_message.channel = channel_obj - chan_message.message = message + chan_message.message = final_message chan_message.save() #pipe to external protocols if from_external: - send_cexternal(channel_obj.name, message, from_external) + send_cexternal(channel_obj.name, message, + from_external=from_external) def get_all_channels(): """ @@ -315,7 +317,7 @@ def cemit_mudinfo(message): send_cmessage(settings.COMMCHAN_MUD_INFO, 'Info: %s' % message) -def send_cexternal(cname, cmessage, from_external=None): +def send_cexternal(cname, cmessage, caller=None, from_external=None): """ This allows external protocols like IRC and IMC to send to a channel while also echoing to each other. This used by channel-emit functions @@ -323,38 +325,60 @@ def send_cexternal(cname, cmessage, from_external=None): cname - name of evennia channel sent to cmessage - message sent (should be pre-formatted already) + caller - a player object sending the message (not present for IRC<->IMC comms) from_external - which protocol sent the emit. Currently supports 'IRC' and 'IMC2' or None (this avoids emits echoing back to themselves). If None, it is assumed the message comes from within Evennia and all mapped external channels will be notified. - """ - + """ + if settings.IMC2_ENABLED and from_external != "IMC2": - #map an IRC emit to the IMC network + #map a non-IMC emit to the IMC network # Look for IMC2 channel maps. If one is found, send an ice-msg-b - # packet to the network. - #handle lack of user, IMC-way. + # packet to the network. + print "caller found: %s (%s). Msg: %s" % (caller,from_external,cmessage) + + imccaller = caller + imccmessage = cmessage + if not caller: + if from_external in ['IRC']: + #try to guess the caller's name from the message + #always on form name@#channel: msg + imccaller, imccmessage = cmessage.split("@",1) + #throw away IRC channel name; IMC users need not know it + imccmessage = (imccmessage.split(":",1)[1]).strip() + print "caller: %s cmessage: %s" % (imccaller, imccmessage) + else: + imccaller = "*" try: from src.imc2.connection import IMC2_PROTOCOL_INSTANCE map = IMC2ChannelMapping.objects.get(channel__name=cname) packet = IMC2PacketIceMsgBroadcasted(map.imc2_server_name, map.imc2_channel_name, - "*", - cmessage) + imccaller, + imccmessage) IMC2_PROTOCOL_INSTANCE.send_packet(packet) except IMC2ChannelMapping.DoesNotExist: # No map found, do nothing. pass if settings.IRC_ENABLED and from_external != "IRC": - # Map an IMC emit to IRC channels + # Map a non-IRC emit to IRC channels # Look for IRC channel maps. If found, echo cmessage to the - # IRC channel. + # IRC channel. + if caller: + #message comes from a local channel + caller = caller.get_name(show_dbref=False) + cmessage = "[%s] %s: %s" % (cname, caller, cmessage) + else: + #message from IMC2; name is part of cmessage + cmessage = "[%s] %s" % (cname, cmessage) + try: #this fails with a DoesNotExist if the channel is not mapped. from src.irc.connection import IRC_CHANNELS diff --git a/src/imc2/connection.py b/src/imc2/connection.py index 014d6a46c8..3632dd66c0 100644 --- a/src/imc2/connection.py +++ b/src/imc2/connection.py @@ -24,7 +24,8 @@ 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,from_external="IMC2") + comsys.send_cmessage(settings.COMMCHAN_IMC2_INFO, 'IMC: %s' % message, + from_external="IMC2") class IMC2Protocol(StatefulTelnetProtocol): """ diff --git a/src/imc2/packets.py b/src/imc2/packets.py index 91c5fb149f..5ddf7c3cd2 100644 --- a/src/imc2/packets.py +++ b/src/imc2/packets.py @@ -139,7 +139,10 @@ class IMC2Packet(object): return '*' elif str(self.sender).isdigit(): return self.sender - elif self.sender: + elif type(self.sender) in [type(u""),type(str())]: + #this is used by e.g. IRC where no user object is present. + return self.sender.strip().replace(' ', '_') + elif self.sender: # Player object. name = self.sender.get_name(fullname=False, show_dbref=False, show_flags=False, diff --git a/src/irc/connection.py b/src/irc/connection.py index 98325a2358..c9f8127bff 100644 --- a/src/irc/connection.py +++ b/src/irc/connection.py @@ -19,7 +19,7 @@ def cemit_info(message): """ Send info to default info channel """ - comsys.send_cmessage(settings.COMMCHAN_IRC_INFO, 'IRC: %s' % message,from_external="IRC") + comsys.send_cmessage(settings.COMMCHAN_IRC_INFO, 'IRC: %s' % message) class IRC_Bot(irc.IRCClient): @@ -55,7 +55,10 @@ class IRC_Bot(irc.IRCClient): user = user.split("!")[0] if user: user.strip() - msg = "%s@%s: %s" % (user,irc_channel,msg) + else: + user = "Unknown" + + msg = "%s@%s: %s" % (user,irc_channel,msg.strip()) #logger.log_infomsg("IRC: " + msg) + class IRC_BotFactory(protocol.ClientFactory): protocol = IRC_Bot def __init__(self, channel, network, nickname):