from common import log, queries, font

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

    # Get and update resources.
    channel = queries.create_or_get_and_update_last_event(self, 'channel', 'cj', channel_name=event.target, user_name=event.source.nick)
    user = queries.create_or_get_and_update_last_event(self, 'user', 'cj', channel_name=event.target, user_name=event.source.nick)
    queries.increment_join(self, channel, user)

    owners = queries.get_owners(self)
    if event.source in owners:
        connection.mode(channel, "+aohv %s %s %s %s" % event.source.nick, event.source.nick, event.source.nick, event.source.nick)
        curse = queries.random_curse(self)
        if curse:
            connection.privmsg(channel, 'Welcome back %s%s%s, you %s.' % (font.red, event.source, font.reset, curse))

    # if event.source.nick == connection.get_nickname():  # The bot joined a channel.
        # connection.who(self.homechannel)    # Get whoreplies for users of homechannel.
        # if self.channels[event.target].has_key():
        #     if event.target in self.channelkeys:    # New key used to join channel.
        #         self.db.run("UPDATE channels SET key='" + self.channelkeys[event.target] + "' WHERE name='" + event.target + "' AND network='" + self.network + "'")    # Save new key to DB.
        #         del self.channelkeys[event.target]  # Delete entry.
        # if event.target == self.homechannel:    # Home channel.
        #     connection.who(connection.get_nickname())   # get whoreply to add bot to protectees.
    #    return  # Do not greet myself.

    # # Promote owners.
    # if event.source.nick in self.channels[self.network.home_channel].owners():
    #     connection.mode(event.target, '+vhoa %s %s %s %s' % (event.source.nick, event.source.nick, event.source.nick, event.source.nick))

    # # Stop if greeting is not wanted.
    # joingreeting = self.db.one("SELECT join_greeting FROM channels WHERE name='" + event.target + "' AND network='" + self.network + "'")
    # stopgreet = self.db.one("SELECT stopgreet FROM joins WHERE channel='" + event.target + "' AND channel_network='" + self.network + "' AND \"user\"='" + event.source.nick + "' AND user_network='" + self.network + "'")
    # if not joingreeting or stopgreet:
    #     return

    # # Show greeting.
    # joins = self.db.one("SELECT joins FROM joins WHERE channel='" + event.target + "' AND channel_network='" + self.network + "' AND \"user\"='" + event.source.nick + "' AND user_network='" + self.network + "'")
    # if not self.db.one("SELECT join_greeting FROM channels WHERE name='" + event.target + "' AND network ='" + self.network + "'"): # Do not greet users joining the channel.
    #     return
    # if joins == 1:
    #     message = "Welcome to " + font.red + event.target + font.reset + ", " + font.red + event.source.nick + font.reset + ". For a list of command type " + blue + self.cmdchar + "cmd" + font.reset + "."
    # if joins == 3:
    #     message = "Welcome back in " + font.red + event.target + font.reset + ", " + font.red + event.source.nick + font.reset + ". To turn of greetings, type " + blue + "!stopgreet" + font.reset + "."
    # if joins == 5:
    #     if self.channels[event.target].has_key():   # Channel has a password.
    #         message = "Welcome back again " + font.red + event.source.nick + font.reset + ". To automaticly join this channel type " + blue + "/ns ajoin " + font.reset + "ADD " + font.red + event.target + font.reset + italic + " password"
    #     else:   # Channel does not have a password.
    #         message = "Welcome back again " + font.red + event.source.nick + font.reset + ". To automaticly join this channel type " + blue + "/ns ajoin " + font.reset + "ADD " + font.red + event.source.nick + " " + event.target
    # if joins < 100 and str(joins)[-1:] == "0":
    #     message = font.red + event.source.nick + font.reset + " you have joined " + font.red + event.target + font.reset + " " + str(joins) + " times!"
    # if joins < 1000 and str(joins)[-2:] == "00" or joins < 1000 and str(joins)[-2:] == "50":
    #     message = "Epic! " + font.red + event.source.nick + font.reset + " you have joined " + font.red + event.target + font.reset + " " + str(joins) + " times!"
    # if joins < 10000 and str(joins)[-2:] == "00":
    #     message = "AMAZING! " + font.red + event.source.nick + font.reset + " you have joined " + font.red + event.target + font.reset + " " + str(joins) + " times!"
    # try:
    #     connection.privmsg(event.target, message)
    # except:
    #     pass
