def message_to_event(direction, message):
server = message.server<EOL>if message.verb in numerics:<EOL><INDENT>message.verb = numerics[message.verb]<EOL><DEDENT>verb = message.verb.lower()<EOL>if verb == '<STR_LIT>':<EOL><INDENT>if server.is_channel(message.params[<NUM_LIT:0>]):<EOL><INDENT>verb = '<STR_LIT>'<EOL><DEDENT><DEDENT>if verb == '<STR_LIT>':<EOL><INDENT>verb = '<STR_LIT>'<EOL>if server.is_channel(message.params[<NUM_LIT:0>]):<EOL><INDENT>verb = '<STR_LIT>'<EOL><DEDENT><DEDENT>elif verb == '<STR_LIT>':<EOL><INDENT>verb = '<STR_LIT>'<EOL>if server.is_channel(message.params[<NUM_LIT:0>]):<EOL><INDENT>verb = '<STR_LIT>'<EOL><DEDENT><DEDENT>info = message.__dict__<EOL>info['<STR_LIT>'] = direction<EOL>info['<STR_LIT>'] = verb<EOL>if '<STR_LIT:time>' in info['<STR_LIT>']:<EOL><INDENT>info['<STR_LIT>'] = time.strptime(info['<STR_LIT>']['<STR_LIT:time>'],<EOL>'<STR_LIT>')<EOL><DEDENT>infos = [[verb, info], ]<EOL>if verb in ('<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>infos = ctcp_unpack_message(info)<EOL><DEDENT>i = -<NUM_LIT:1><EOL>while i < (len(infos) - <NUM_LIT:1>):<EOL><INDENT>i += <NUM_LIT:1><EOL>name = infos[i][NAME_ATTR]<EOL>for attr, param_map in _verb_param_map.items():<EOL><INDENT>escaped = False<EOL>if attr.startswith('<STR_LIT>'):<EOL><INDENT>attr = attr.lstrip('<STR_LIT>')<EOL>escaped = True<EOL><DEDENT>for param_number, verbs in param_map.items():<EOL><INDENT>if len(infos[i][INFO_ATTR]['<STR_LIT>']) > param_number and name in verbs:<EOL><INDENT>value = infos[i][INFO_ATTR]['<STR_LIT>'][param_number]<EOL>if escaped:<EOL><INDENT>value = escape(value)<EOL><DEDENT>infos[i][INFO_ATTR][attr] = value<EOL><DEDENT><DEDENT><DEDENT>if name == '<STR_LIT>':<EOL><INDENT>server.nick = server.istring(infos[i][INFO_ATTR]['<STR_LIT>'])<EOL><DEDENT>if name == '<STR_LIT>':<EOL><INDENT>if infos[i][INFO_ATTR]['<STR_LIT>'] == '<STR_LIT:action>':<EOL><INDENT>info = dict(infos[i][INFO_ATTR])<EOL>info['<STR_LIT:message>'] = info['<STR_LIT>']<EOL>if server.is_channel(info['<STR_LIT:target>']):<EOL><INDENT>name = '<STR_LIT>'<EOL>info['<STR_LIT>'] = info['<STR_LIT:target>']<EOL><DEDENT>else:<EOL><INDENT>name = '<STR_LIT>'<EOL><DEDENT>infos.append([name, info])<EOL><DEDENT><DEDENT>if name == '<STR_LIT>' and len(infos[i][INFO_ATTR]['<STR_LIT>']) > <NUM_LIT:1>:<EOL><INDENT>modestring = infos[i][INFO_ATTR]['<STR_LIT>'][<NUM_LIT:1>:]<EOL>modes = parse_modes(modestring)<EOL>infos[i][INFO_ATTR]['<STR_LIT>'] = '<STR_LIT:U+0020>'.join(modestring).strip()<EOL>infos[i][INFO_ATTR]['<STR_LIT>'] = modes<EOL><DEDENT>if name == '<STR_LIT>' and len(infos[i][INFO_ATTR]['<STR_LIT>']) > <NUM_LIT:1>:<EOL><INDENT>modestring = infos[i][INFO_ATTR]['<STR_LIT>'][<NUM_LIT:1>:]<EOL>chanmodes = server.features.get('<STR_LIT>')<EOL>prefixes = list(server.features.get('<STR_LIT>').keys())<EOL>modes = parse_modes(modestring, chanmodes, prefixes)<EOL>infos[i][INFO_ATTR]['<STR_LIT>'] = '<STR_LIT:U+0020>'.join(modestring).strip()<EOL>infos[i][INFO_ATTR]['<STR_LIT>'] = modes<EOL><DEDENT>if name == '<STR_LIT>':<EOL><INDENT>if len(infos[i][INFO_ATTR]['<STR_LIT>']) > <NUM_LIT:2>:<EOL><INDENT>modestring = infos[i][INFO_ATTR]['<STR_LIT>'][<NUM_LIT:2>:]<EOL>chanmodes = server.features.get('<STR_LIT>')<EOL>modes = parse_modes(modestring, chanmodes)<EOL>infos[i][INFO_ATTR]['<STR_LIT>'] = '<STR_LIT:U+0020>'.join(modestring).strip()<EOL>infos[i][INFO_ATTR]['<STR_LIT>'] = modes<EOL><DEDENT>else:<EOL><INDENT>infos[i][INFO_ATTR]['<STR_LIT>'] = '<STR_LIT>'<EOL>infos[i][INFO_ATTR]['<STR_LIT>'] = []<EOL><DEDENT><DEDENT>if name == '<STR_LIT>':<EOL><INDENT>channel_name = infos[i][INFO_ATTR]['<STR_LIT>'][<NUM_LIT:2>]<EOL>server.info.create_channel(channel_name)<EOL>channel = server.info.channels.get(channel_name)<EOL>nice_names = []<EOL>channel_prefixes = {}<EOL>if len(infos[i][INFO_ATTR]['<STR_LIT>']) > <NUM_LIT:3>:<EOL><INDENT>raw_names = infos[i][INFO_ATTR]['<STR_LIT>'][<NUM_LIT:3>].split('<STR_LIT:U+0020>')<EOL><DEDENT>else:<EOL><INDENT>raw_names = []<EOL><DEDENT>for name in raw_names:<EOL><INDENT>if not len(name):<EOL><INDENT>continue<EOL><DEDENT>prefixes = '<STR_LIT>'<EOL>while name[<NUM_LIT:0>] in server.features.available['<STR_LIT>'].values():<EOL><INDENT>prefixes += name[<NUM_LIT:0>]<EOL>name = name[<NUM_LIT:1>:]<EOL><DEDENT>nick = NickMask(name).nick<EOL>server.info.create_user(nick)<EOL>nice_names.append(name)<EOL>server.info.create_user(name)<EOL>user = server.info.users.get(nick)<EOL>channel_prefixes[user] = prefixes<EOL>channel.add_user(nick, prefixes=prefixes)<EOL><DEDENT>infos[i][INFO_ATTR]['<STR_LIT>'] = '<STR_LIT:U+002C>'.join(nice_names)<EOL>infos[i][INFO_ATTR]['<STR_LIT>'] = channel_prefixes<EOL><DEDENT>for attr in ('<STR_LIT:source>', '<STR_LIT:target>', '<STR_LIT>'):<EOL><INDENT>if attr in infos[i][INFO_ATTR] and infos[i][INFO_ATTR][attr]:<EOL><INDENT>source = infos[i][INFO_ATTR][attr]<EOL>if server.is_channel(source):<EOL><INDENT>server.info.create_channel(source)<EOL>infos[i][INFO_ATTR][attr] = server.info.channels.get(source)<EOL><DEDENT>elif '<STR_LIT:.>' in source and server.is_server(source):<EOL><INDENT>server.info.create_server(source)<EOL>infos[i][INFO_ATTR][attr] = server.info.servers.get(source)<EOL><DEDENT>elif server.is_nick(source):<EOL><INDENT>server.info.create_user(source)<EOL>infos[i][INFO_ATTR][attr] = server.info.users.get(NickMask(source).nick)<EOL><DEDENT>else:  <EOL><INDENT>server.info.create_user(source)<EOL>infos[i][INFO_ATTR][attr] = server.info.users.get(NickMask(source).nick)<EOL><DEDENT><DEDENT><DEDENT>if '<STR_LIT>' in infos[i][INFO_ATTR] and infos[i][INFO_ATTR]['<STR_LIT>']:<EOL><INDENT>channels = []<EOL>for chan in infos[i][INFO_ATTR]['<STR_LIT>'].split('<STR_LIT:U+002C>'):<EOL><INDENT>server.info.create_channel(chan)<EOL>channels.append(server.info.channels.get(chan))<EOL><DEDENT>infos[i][INFO_ATTR]['<STR_LIT>'] = channels<EOL><DEDENT>if '<STR_LIT>' in infos[i][INFO_ATTR] and infos[i][INFO_ATTR]['<STR_LIT>']:<EOL><INDENT>users = []<EOL>for user in infos[i][INFO_ATTR]['<STR_LIT>'].split('<STR_LIT:U+002C>'):<EOL><INDENT>server.info.create_user(user)<EOL>users.append(server.info.users.get(NickMask(user).nick))<EOL><DEDENT>infos[i][INFO_ATTR]['<STR_LIT>'] = users<EOL><DEDENT>verb = infos[i][INFO_ATTR]['<STR_LIT>']<EOL>dir = infos[i][INFO_ATTR]['<STR_LIT>']<EOL>source = infos[i][INFO_ATTR].get('<STR_LIT:source>')<EOL>target = infos[i][INFO_ATTR].get('<STR_LIT:target>')<EOL>if verb in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>infos[i][INFO_ATTR]['<STR_LIT>'] = target<EOL><DEDENT>elif verb in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>if dir == '<STR_LIT>':<EOL><INDENT>infos[i][INFO_ATTR]['<STR_LIT>'] = target<EOL><DEDENT>elif dir == '<STR_LIT>':<EOL><INDENT>if '<STR_LIT>' in server.capabilities.enabled:<EOL><INDENT>infos[i][INFO_ATTR]['<STR_LIT>'] = target<EOL><DEDENT>else:<EOL><INDENT>infos[i][INFO_ATTR]['<STR_LIT>'] = source<EOL><DEDENT><DEDENT><DEDENT>if '<STR_LIT>' in infos[i][INFO_ATTR] and infos[i][INFO_ATTR]['<STR_LIT>'] is None:<EOL><INDENT>del infos[i][INFO_ATTR]['<STR_LIT>']<EOL><DEDENT>infos[i][INFO_ATTR]['<STR_LIT>'] = False<EOL>if verb in ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>if dir == '<STR_LIT>' and '<STR_LIT>' in server.capabilities.enabled:<EOL><INDENT>infos[i][INFO_ATTR]['<STR_LIT>'] = True<EOL><DEDENT><DEDENT>if '<STR_LIT>' in infos[i][INFO_ATTR] and infos[i][INFO_ATTR]['<STR_LIT>'].is_server:<EOL><INDENT>del infos[i][INFO_ATTR]['<STR_LIT>']<EOL><DEDENT><DEDENT>return infos<EOL>
Prepare an ``RFC1459Message`` for event dispatch.

    We do this because we have to handle special things as well, such as CTCP
    and deconstructing verbs properly.