Takes a girc-escaped message and returns a raw IRC message

def unescape(msg, extra_format_dict={}):
    """Takes a girc-escaped message and returns a raw IRC message"""
    new_msg = ''

    extra_format_dict.update(format_dict)

    while len(msg):
        char = msg[0]
        msg = msg[1:]
        if char == escape_character:
            escape_key = msg[0]
            msg = msg[1:]

            # we handle this character separately, otherwise we mess up and
            #   double escape characters while escaping and unescaping
            if escape_key == escape_character:
                new_msg += escape_character

            elif escape_key == '{':
                buf = ''
                new_char = ''
                while True:
                    new_char = msg[0]
                    msg = msg[1:]

                    if new_char == '}':
                        break
                    else:
                        buf += new_char

                new_msg += _get_from_format_dict(extra_format_dict, buf)

            else:
                new_msg += _get_from_format_dict(extra_format_dict, escape_key)

            if escape_key == 'c':
                fill_last = len(msg) and msg[0] in digits
                colours, msg = extract_girc_colours(msg, fill_last)
                new_msg += colours
        else:
            new_msg += char

    return new_msg