# Buggy code
def prepare_privmsg(self, nick, cmd, message, mc=None):
    # should we encrypt?
    box, encrypt = self.get_encryption_box(cmd, nick)
    if encrypt:
        if not box:
            log.debug(
                "error, dont have encryption box object for "
                + nick
                + ", dropping message"
            )
            return
        message = encrypt_encode(message.encode("ascii"), box)

    # Anti-replay measure: append the message channel identifier
    # to the signature; this prevents cross-channel replay but NOT
    # same-channel replay (in case of snooper after dropped connection
    # on this channel).
    if nick in self.active_channels:
        hostid = self.active_channels[nick].hostid
    else:
        log.info(
            "Failed to send message to: "
            + str(nick)
            + "; cannot find on any message channel."
        )
        return
    msg_to_be_signed = message + str(hostid)

    self.daemon.request_signed_message(nick, cmd, message, msg_to_be_signed, hostid)


# Traceback (exceptions.Exception)
Unhandled Error
Traceback (most recent call last):
File "/lib/python2.7/site-packages/twisted/protocols/amp.py", line 1043, in _commandReceived
deferred = self.dispatchCommand(box)
File "/lib/python2.7/site-packages/twisted/protocols/amp.py", line 1101, in dispatchCommand
return maybeDeferred(responder, box)
File "/lib/python2.7/site-packages/twisted/internet/defer.py", line 150, in maybeDeferred
result = f(*args, **kw)
File "/lib/python2.7/site-packages/twisted/protocols/amp.py", line 1188, in doit
return maybeDeferred(aCallable, **kw).addCallback(
--- <exception caught here> ---
File "/lib/python2.7/site-packages/twisted/internet/defer.py", line 150, in maybeDeferred
result = f(*args, **kw)
File "/lib/python2.7/site-packages/jmdaemon/daemon_protocol.py", line 202, in on_JM_MSGSIGNATURE
self.mcc.privmsg(nick, cmd, msg_to_return, mc=hostid)
File "/lib/python2.7/site-packages/jmdaemon/message_channel.py", line 257, in privmsg
"Tried to privmsg on an unavailable message channel.")
exceptions.Exception: Tried to privmsg on an unavailable message channel.

# Fixed code
def prepare_privmsg(self, nick, cmd, message, mc=None):
    # should we encrypt?
    box, encrypt = self.get_encryption_box(cmd, nick)
    if encrypt:
        if not box:
            log.debug(
                "error, dont have encryption box object for "
                + nick
                + ", dropping message"
            )
            return
        message = encrypt_encode(message.encode("ascii"), box)

    # Anti-replay measure: append the message channel identifier
    # to the signature; this prevents cross-channel replay but NOT
    # same-channel replay (in case of snooper after dropped connection
    # on this channel).
    if mc is None:
        if nick in self.active_channels:
            hostid = self.active_channels[nick].hostid
        else:
            log.info(
                "Failed to send message to: "
                + str(nick)
                + "; cannot find on any message channel."
            )
            return
    else:
        hostid = mc.hostid

    msg_to_be_signed = message + str(hostid)

    self.daemon.request_signed_message(nick, cmd, message, msg_to_be_signed, hostid)