# Sergiusz Roznanski Irssi 4 RTG, GPLv2
#
# based on David Leadbeater, dgl@dgl.cx, GPLv2
# http://www.dgl.cx/irssi/older/irssi-userlist/unix_socket.pl

use strict;
use Irssi;
use IO::Socket;
use POSIX;
use Fcntl;
use MIME::Base64;

use vars qw($VERSION %IRSSI);

$VERSION = "0.1";
%IRSSI = (
    authors         => "Sergiusz Elita Rozanski",
    contact         => "rozanski\@sergiusz.com",
    name            => "rtg",
    description     => "interface to RTG",
    license         => "GNU GPLv2 or later",
    changed         => "today ;)"
);

my($listener, $listenfh);
my(%socket, %buffer);
my %command;

# Open the pipe
sub listen_new {
    my $path = Irssi::settings_get_str("socket_path");

    unlink $path if -S $path and -o $path;
    POSIX::mkfifo($path,0600);
    open($listenfh, "+< " . $path) or die "Error opening listen pipe: $!";
    fcntl($listenfh, O_NONBLOCK, 1);
    $listener = Irssi::input_add(fileno $listenfh, INPUT_READ, \&input_in, $listenfh);
    }

# Gets the input and (hopefully) does something useful with it
# Called by the Irssi input system
sub input_in {
   my($fh) = @_;
	my $ret = sysread($fh, $buffer{$fh}, POSIX::BUFSIZ);

	# undef = error (except when it would block), 0 = eof
	if((not defined $ret and $! != POSIX::EAGAIN) or $ret == 0) {
	   input_close($fh);
		return;
	}

	return unless $buffer{$fh} =~ /\n/;

	while($buffer{$fh} =~ s/^(.*?)\r?\n//g) {
		my($command, $data) = split / /, $1, 2;
		command_run(lc $command, $fh, $data);
	}
}

#sub input_out {
#	my($fh, $data) = @_;
#	syswrite($fh, $data, length $data);
#}

# Removes and closes an input handle
sub input_close {
	my($fh) = @_;
	Irssi::input_remove($socket{$fh});
	close $fh;
	
	delete $socket{$fh};
	delete $buffer{$fh};
}

# Adds a new command, accepts a reference to a subroutine that will be passed
# the filehandle, data after the command and the command name itself as
# arguments.
sub command_add {
	my($command, $ref) = @_;
	$command{lc $command} = $ref;
}

# Called by input_in to actually run the command.
sub command_run {
	my($command, $fh, $data) = @_;
	if(not exists $command{$command}) {
		Irssi::print( "Attempt to run unknown command '$command' over pipe");
		return;
	}
	$command{$command}->($fh, $data, $command);
}

# Example command..
command_add "close" => sub {
	my($fh, $data) = @_;
	input_close($fh);
};

sub input_snd {
	my($data) = @_;
	my($stag, $reszta) = split(/ /,$data,2);

	my $server = Irssi::server_find_tag($stag);
	if($server) {
	    $server->command($reszta);
	    }
	else {
	    Irssi::active_server()->command($reszta);
	    }
};

command_add "snd" => sub {
	my($fh, $data) = @_;
	input_snd($data);
};

Irssi::signal_add 'event privmsg' => sub {
    my ($server, $data, $nick, $address) = @_;
    my ($target, $text) = split(/ :/, $data, 2);
    my $path = Irssi::settings_get_str("rtgdir");
    my $plik;
    my $okno;
    my $stag = $server->{tag};

    mkdir($path);
    if ( $target eq $server->{nick} ) {
# priv
	$okno=encode_base64(tolower($nick).'@'.$stag,'');
	}
    else {
# channel
	$okno = encode_base64(tolower($target).'@'.$stag,'');
	$text = $nick . ": " . $text;
	}
    mkdir($path . "/" . $okno);
    open($plik, "> " . $path . "/" . $okno . "/" . time() );
    print $plik ($text . "\n");
    close($plik);
    };

Irssi::settings_add_str("misc", "socket_path", Irssi::get_irssi_dir . "/rtg");
Irssi::settings_add_str("misc", "rtgdir", Irssi::get_irssi_dir . "/scripts/rtg");
listen_new();
1; # vim:set noexpandtab:
