#================================================================================
# $Revision: 2601 $
# $Date: 2008-12-08 23:01:38 +0100 (Mon, 08 Dec 2008) $
# $Author: jnieuwen $
# Copyright (c) 2008, Jeroen C. van Nieuwenhuizen
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the <organization> nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY Jeroen C. van Nieuwenhuizen ''AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL Jeroen C. van Nieuwenhuizen BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#===============================================================================

use strict;
use Irssi ;
use DBI ;
use POSIX qw(strftime);
use Time::Local 'timelocal';
use vars qw($VERSION %IRSSI); 

$VERSION = "1";

%IRSSI = (
    authors     => 'Jeroen van Nieuwenhuizen',
    name        => 'dblogger',
    contact     => 'dblogger@jeroen.se',
    description => 'irssi to mysql logger',
    license     => 'BSD license'
);

# Database logging.
# CHANGE THESE
my $dbname='dbname' ;
my $host='host' ;
my $username='user' ;
my $passwd='passwd' ;
my $ownnick='ownnick' ;
# END CHANGE THESE

# connection, database, username, password
my @connection = ("DBI:mysql:dbname=$dbname;host=$host","$username","$passwd") ;
my %servers = () ;
my $db_con = DBI->connect_cached(@connection) ;

#================================================================================
# Add to chat db
sub chatdb {
	my ($channel, $server, $type, $nick, $message) = @_ ;
	my $datetime = strftime("%Y-%m-%d %H:%M:%S",localtime(time)) ;

	$db_con = DBI->connect_cached(@connection) ;
	eval {
		my $sql = "insert into chat (datetime,server, channel,type,nick,message) 
				values(?,?,?,?,?,?)";
		my $sth = $db_con->prepare($sql) ;
		$sth->execute($datetime,$server,$channel,$type,$nick,$message);
	} ;
}

#================================================================================
#================================================================================

#================================================================================
# Find the command and execute its options when necessary.
# Public message handle
sub irc_public {
	my ($server, $msg, $nick, $address, $target) = @_ ;
	chatdb($target, $server->{tag}, 'public', $nick, $msg) ;
}
#================================================================================

#================================================================================
# Find the command and execute its options when necessary.
# Public message handle
sub own_public {
	my ($server, $msg, $target) = @_ ;
	chatdb($target, $server->{tag}, 'public', $ownnick, $msg) ;
}
#================================================================================

#================================================================================
# SERVER_REC, char *msg, char *nick, char *address, char *target
sub irc_action {
	my ($server, $msg, $nick, $address, $target) = @_ ;
	chatdb($target, $server->{tag}, 'action', $nick, $msg) ;
}

#================================================================================
# SERVER_REC, char *msg, char *nick, char *address, char *target
sub irc_own_action {
	my ($server, $msg, $target) = @_ ;
	chatdb($target, $server->{tag}, 'action', $ownnick, $msg) ;
}

# joins
sub irc_join {
	my ($server, $target, $nick, $address) = @_ ;
	chatdb($target, $server->{tag}, 'join', $nick, '') ;
}

# Topic change
sub irc_topic {
	my ($server, $target, $msg, $nick, $address) = @_ ;
	chatdb($target, $server->{tag}, 'topic', $nick, $msg) ;
}

sub irc_quit {
	my ($server, $nick, $address, $msg) = @_ ;
	chatdb('', $server->{tag}, 'quit', $nick, $msg) ;
}

sub irc_kick {
	my ($server, $target, $nick, $kicker, $address, $msg) = @_ ;
	chatdb($target, $server->{tag}, 'kick', $kicker, $nick.": ".$msg) ;
}

sub irc_part {
	my ($server, $target, $nick, $address, $msg) = @_ ;
	chatdb($target, $server->{tag}, 'part', $nick, $msg) ;
}

sub irc_nick {
	my ($server, $newnick, $nick, $address) = @_ ;
	chatdb('', $server->{tag}, 'nick', $nick, $newnick) ;
}

#================================================================================
# The signal handlers
Irssi::signal_add('message public', 'irc_public') ;
Irssi::signal_add('message own_public', 'own_public') ;
Irssi::signal_add('message irc action', 'irc_action') ;
Irssi::signal_add('message irc own_action', 'irc_own_action') ;
Irssi::signal_add('message join', 'irc_join') ;
Irssi::signal_add('message topic', 'irc_topic') ;
Irssi::signal_add('message quit', 'irc_quit') ;
Irssi::signal_add('message kick', 'irc_kick') ;
Irssi::signal_add('message part', 'irc_part') ;
Irssi::signal_add('message nick', 'irc_nick') ;

