import datetime
from events.common import Replyto,  Lastact, MessageStatistics

def process_event(self, connection, event):
    
    # Update last act.
    Lastact.update(self, event.source.nick, "msg", channel=event.target, lastact=event.arguments[0])
    
    # Save statistic to database.
    MessageStatistics.update(self, event, "message")
    
    # Stop if channelfunction chat if off.
    if not self.db.one("SELECT chat FROM channels WHERE name='" + event.target + "' AND network='" + self.network + "'"):
        return
    
    if connection.get_nickname().lower() in event.arguments[0].lower() and event.source.nick is not connection.get_nickname(): # Bot's name was mentioned
        if event.arguments[0].startswith(self.cmdchar):
            return  # Stop if it's a command.
        Replyto.name(connection, event)
    
    # Character lame.
    elif event.arguments[0] == len(event.arguments[0]) * event.arguments[0][0]:   # Trigger exclusively same character.
    
        # Do not say KKK.
        if event.arguments[0] == "kk":
            return
        
        # Stop if lamed recently.
        lastlame = self.db.one("SELECT last_lame FROM channels WHERE name='" + event.target + "' AND network='" + self.network + "'")
        if lastlame and lastlame > datetime.datetime.now() - datetime.timedelta(minutes=2):    # In the last 2 minutes.
            return
        
        # Update lastlame.
        self.db.run("UPDATE channels SET last_lame='" + str(datetime.datetime.now()) + "' WHERE name='" + event.target + "' AND network='" + self.network + "'")
        
        connection.privmsg(event.target, event.arguments[0] + event.arguments[0][:1])
