use Irssi;
use Irssi::Irc;
use POSIX;
use vars qw($VERSION %IRSSI);
use strict;
use HTTP::Request::Common;
use LWP::UserAgent;

$VERSION = "1.00";
%IRSSI = (
    authors     => "Sai",
    contact     => "sai at kaatajat.net",
    name        => "ircpics",
    description => "ircpics client",
    license     => "GPLv2"
);
#	IRCPICS Script, modified from IRC-URLS.NET logging client,
#	so big thanks goes there to (hannu at hannu.biz)
#	Original urllog-client.pl script can be downloaded from 
#	http://irc-urls.net/client/urllog-client.pl
#	
#	Allowed channels:
#	(List syntax is: #chan1@*,#chan2@* etc.
#	All channels need to be separated with a comma.
#	Also *@* is a valid entry)
#	/set ircpics_channels_allow #chan1@*,#chan2@*
#
#	Denied channels:
#	(Same syntax as allowed channels)
#	/set ircpics_channels_deny #chan1@*,#chan2@*
#
#	Denied nicks:
#	(Nicks separated with comma)
#	/set ircpics_nicks_deny nick1,nick2
#	
#	Save settings using
#	/save
#
#	All changes take effect immediately, no restart is needed

$SIG{CHLD}="IGNORE";

my %ircpics;

sub log_ircpics_public {
    my ($server, $data, $nick, $mask, $target) = @_;
	return picurl($server->{chatnet}, $nick, $data, $target);
}
sub log_ircpics_own{
    my ($server, $data, $target) = @_;
    return picurl($server->{chatnet}, $server->{nick}, $data, $target);
}
sub log_ircpics_topic {
    my ($server, $target, $data, $nick, $mask) = @_;
    return picurl($server->{chatnet}, $nick, $data, $target);
}
sub parse_ircpics_url {
    my ($url) = @_;
    if($url =~ /((\.http|\.https|\.www):\/\/[a-zA-Z0-9\|\[\]\/\\\:\?\%\.\,\&\;=#\-\_\!\+\~]*)/i) {
	return 0;
    }
    if($url =~ /((http|https):\/\/[a-zA-Z0-9\|\[\]\/\\\:\?\%\.\,\&\;=#\-\_\!\+\~]*)/i){
        return $1;
    }elsif($url =~ /(www\.[a-zA-Z0-9\/\\\:\?\%\.\,\&\;=#\-\_\!\+\~]*)/i){
        return "http://".$1;
    }
    return 0;
}
sub picurl {
    my ($ircnet, $nick, $data, $channel) = @_;
	my $url=parse_ircpics_url($data);
	if ($url) {
		my @channels_allow=split(',', lc $ircpics{'channels_allow'});
		my @channels_deny=split(',', lc $ircpics{'channels_deny'});
		my @nicks_deny=split(',', lc $ircpics{'nicks_deny'});
		my $allowed=0;
		$ircnet = lc $ircnet;
		$channel = lc $channel;
		foreach my $target_a(@channels_allow) {
			if (($target_a eq '*') || ($target_a eq $channel.'@'.$ircnet) || 
			($target_a eq '*@*') || ($target_a eq $channel.'@*') || ($target_a eq '*@'.$ircnet)) {
				$allowed=1;
			}
		}
		if ($allowed) {
			foreach my $target_d(@channels_deny) {
				if (($target_d eq '*') || ($target_d eq $channel.'@'.$ircnet) ||
					($target_d eq '*@*') || ($target_d eq $channel.'@*') || ($target_d eq '*@'.$ircnet)) {
					$allowed=0;
				}
			}
			foreach my $nick_d(@nicks_deny) {
				if ($nick_d eq lc $nick) {
					$allowed=0;
				}
			}
			if ($allowed) {
				my $forkpid;
				$forkpid = fork() or Irssi::print("ircpics: Failed to fork");
				if ($forkpid == 0) {
					send_ircpics_url($ircnet, $nick, $channel, $url);
					POSIX::_exit(1);
				}
			}
		}
	}
	return 0;
}
sub send_ircpics_url {
	my ($ircnet, $nick, $channel, $url) = @_;
	my $con = LWP::UserAgent->new;
  	my $urltopost = 'http://ircpics.com/add.php';
	$con->post( $urltopost, [ 'url' => $url, 'channel' => $channel, 'nick' => $nick, 'ircnet' => $ircnet, ]  );
	return 0;
}
sub refresh_ircpics_config {
	$ircpics{'channels_allow'} = Irssi::settings_get_str('ircpics_channels_allow');
	$ircpics{'channels_deny'} = Irssi::settings_get_str('ircpics_channels_deny');
	$ircpics{'nicks_deny'} = Irssi::settings_get_str('ircpics_nicks_deny');
	return 0;
}; 

Irssi::signal_add_last('message public', 'log_ircpics_public');
Irssi::signal_add_last('message own_public', 'log_ircpics_own');
Irssi::signal_add_last('message topic', 'log_ircpics_topic');
Irssi::signal_add_last('setup changed', 'refresh_ircpics_config');

Irssi::settings_add_str('ircpics', 'ircpics_channels_allow', '');
Irssi::settings_add_str('ircpics', 'ircpics_channels_deny', '');
Irssi::settings_add_str('ircpics', 'ircpics_nicks_deny', '');

refresh_ircpics_config();
Irssi::print("Ircpics-client v$VERSION loaded");
