import commands.public, commands.admin, commands.games, commands.statistics
from common import log, queries, font
from events.common import Inform


def process_event(self, connection, event):
    log.info(event) # Log to console.

    # Ignore server connect messages.
    if event.source == None:
        return

    # Get and update resources.
    user = queries.create_or_get_and_update_last_event(self, 'user', 'pn', user_name=event.source.nick, event_subject_name=event.target)
    channel = None

    commands.public.do_command(self, connection, event, user, channel)
    commands.admin.do_command(self, connection, event, user, channel)
    commands.statistics.do_command(self, connection, event, user, channel)
    commands.games.do_command(self, connection, event, user, channel)

    if event.source.nick == connection.get_nickname():  # Message came from myself.
        return
    elif event.source.nick == "NickServ":
        if event.arguments[0].startswith("This nickname is registered"):
            connection.privmsg('NickServ', 'identify %s %s' % (self.network.nickname, self.network.password)) # Identify with NickServ.
        if event.arguments[0].startswith("You are already identified."):
            return

            # Nick \x02RotBot\x02 isn't registered.
        if event.arguments[0].endswith(' is not a registered nickname.') or event.arguments[0].startswith('Nick ') and event.arguments[0].endswith(' isn\'t registered.') or event.arguments[0] == 'Your nick isn\'t registered.': # Username from database is not registered.
            connection.privmsg('NickServ', 'register %s spamtBK@xs4all.nl' % (self.network.password)) # Register with NickServ.
            connection.privmsg(self.network.home_channel, 'Regisring %s%s%s with %sNickServ%s.' % (font.red, self.network.nickname, font.reset, font.red, font.reset))  # Recover control of nickname via NickServ, old style.
            log.info('Registerring with NickServ.')
        if event.arguments[0].startswith('Nickname ') and event.arguments[0].endswith(' registered.'):
            Inform.home_channel(seld, connection, 'Registerred nickname %s%s%s with NickServ.' % font.red, self.network.nickname, font.reset)
    # elif event.source.nick == "ChanServ":
    #     if event.arguments[0].startswith("Key for channel ") and len(event.arguments[0]) > 5:   # Received channel key.
    #         self.channelkeys[event.arguments[0].split(' ')[3]] = event.arguments[0].split(' ')[5][:-1]
    #         connection.join(event.arguments[0].split(' ')[3], event.arguments[0].split(' ')[5][:-1])
    #         Inform.owners(self, connection, "Received " + red + event.arguments[0].split(" ")[3] + reset + " key: " + event.arguments.split(" ")[5][:-1])
    #     if event.arguments[0] == "Password authentication required for that command.":  # Not authenticated with NisckServ.
    #         Inform.notice_owners(self, connection, "Not authenticated with NickServ. See " + blue + self.helpchar + "recovernick " + reset + "and " + blue + self.helpchar + "registernick" + reset + ".")
    #         return
    #     if event.arguments[0].startswith("You have been unbanned from ") or event.arguments[0].endswith(" autokick list is empty.") or event.arguments[0].startswith("You are already in ") or event.arguments[0] == "Syntax: UNBAN channel [nick]" or event.arguments[0] == "/msg ChanServ HELP UNBAN for more information":
    #         return
    #     if event.arguments[0].startswith("Channel ") and event.arguments[0].endswith(" has no key."):
    #         return
    if event.source.nick != "Global":
        Inform.notice_owners(self, connection, 'Notice from %s %s %s %s: %s %s' % (font.red, font.red, event.source.nick, font.grey, font.reset, event.arguments[0]))
