# fulllogwin.pl
# by Konsta 'sooda' Hölttä,
# based on hilightwin.pl by Timo Sirainen and Mark Sangster
# channel coloring from nickcolor.pl by Timo Sirainen and Ian Peters

# Dumps all msgs and such (joins, parts etc if you want) to a window
# named "fulllog". The window is meant to be used as a split window 
# that is easy to show and hide.
# This helps to keep an eye on other channels and /lastlog'ing for an url
# or something that just came by somewhere when you have a lot of windows.

# 1) Create the window
# 1.1) /window new <split/hide>
# 1.2) /window name fulllog
# (you can switch between split windows with alt+up and down arrows)

# 2) Configuration
# 2.1) flw_size: window height
# 2.2) flw_level: what message levels to print
#      (see http://irssi.org/documentation/perl for all applicable levels)
# 2.3) flw_ignore: a comma-separated list of msg dests that won't get printed

# 3) Shortcuts
# 3.1) /flw_hide to hide it (easier than /window hide <refnum>)
# 3.2) /flw_show to show it again
# 3.3) /flw_grow to temporarily grow the window by one
#      (the setting flw_size won't change)
# 3.4) /flw_shrink to shrink it by one.

# 4) Even more shortcuts
#    I use also these so that ctrl+arrows and pgup/down help a lot. I don't
#    know how your terminal/shell affects these; I tested in Konsole + bash.
# /bind meta2-1;5A key cup
# /bind meta2-1;5B key cdown
# /bind cup command flw_hide 
# /bind cdown command flw_show 
# /bind meta2-5;5~ key cpgup
# /bind meta2-6;5~ key cpgdown
# /bind cpgup command flw_shrink
# /bind cpgdown command flw_grow

# "Bugs":

# - I don't know how to hide the annoying 'Irssi: window set sticky'
#   and 'Irssi: Window is not sticky anymore' messages. :(
# - The script doesn't protect from user errors, i.e. trying to show
#   the window if it's already there, or trying to hide it when it's hidden.
#   It won't break anything, though. Irssi would just complain about hiding
#   the last window and such.
# - Showing/hiding or resizing the window with the shortcuts causes focus to
#   go temporarily in the fulllog window. That messes up the previous window
#   history in Irssi, but I don't personally care about that, as I don't use 
#   those features that frequently.


use Irssi;
use POSIX;
use vars qw($VERSION %IRSSI); 

$VERSION = "20101212";
%IRSSI = (
	authors     => "Konsta 'sooda' Hölttä",
	contact     => "sooda @ ircnet", 
	name        => "fulllogwin",
	description => "Print all msgs and such to a window named fulllog",
	license     => "Public Domain",
	url         => "http://irssi.org/",
	changed     => "12.12.2010"
);

my @colors = qw/2 3 4 5 6 7 9 10 11 12 13/;
sub simple_hash {
	my ($string) = @_;
	chomp $string;
	my @chars = split //, $string;
	my $counter;

	foreach my $char (@chars) {
		$counter += ord $char;
	}

	$counter = $colors[$counter % 11];

return $counter;
}

sub sig_printtext {
	my ($dest, $text, $stripped) = @_;
	my $lev = Irssi::settings_get_level("flw_level");
	my $ignores = "," . lc(Irssi::settings_get_str("flw_ignore")) . ",";
	my $tgt = $dest->{target};
	my $tgt2 = "," . lc($tgt) . ",";
	
	# target is empty if we're printing to the flw itself
	if ($dest->{level} & $lev && $tgt ne "" && $ignores !~ /$tgt2/) {
		$window = Irssi::window_find_name("fulllog");
		return unless $window;
		
		$text =~ s/%/%%/g; # escape the escapes in the message
		$text = chr(3) . simple_hash(lc($tgt)) . $tgt . "%N" . ": " . $text;
		# debug mode ;-)
		#$text = $dest->{level} . "/" . Irssi::bits2level($dest->{level}) . $text;

		$window->print($text, MSGLEVEL_CLIENTCRAP);
	}
}

sub cmd_flw_show {
	$w = Irssi::window_find_name("fulllog");
	return unless $w;
	$active = Irssi::active_win();
	Irssi::command("window show " . $w->{refnum});
	Irssi::command("window goto " . $w->{refnum});
	Irssi::command("window stick");
	Irssi::command("window size " . Irssi::settings_get_int("flw_size"));
	Irssi::command("window goto " . $active->{refnum});
}

sub cmd_flw_hide {
	$w = Irssi::window_find_name('fulllog');
	return unless $w;
	Irssi::command("window stick " . $w->{refnum} . " off");
	Irssi::command("window hide " . $w->{refnum});
}

sub cmd_flw_grow {
	$w = Irssi::window_find_name("fulllog");
	return unless $w;
	$active = Irssi::active_win();
	Irssi::command("window goto " . $w->{refnum});
	Irssi::command("window grow");
	Irssi::command("window goto " . $active->{refnum});
}

sub cmd_flw_shrink {
	$w = Irssi::window_find_name("fulllog");
	return unless $w;
	$active = Irssi::active_win();
	Irssi::command("window goto " . $w->{refnum});
	Irssi::command("window shrink");
	Irssi::command("window goto " . $active->{refnum});
}

$window = Irssi::window_find_name("fulllog");
Irssi::print("Create a window named 'fulllog'") if (!$window);

Irssi::settings_add_int("fulllogwin", "flw_size", 10);
Irssi::settings_add_level("fulllogwin", "flw_level", "MSGS PUBLIC TOPICS");
Irssi::settings_add_str("fulllogwin", "flw_ignore", "");
Irssi::command_bind("flw_show", "cmd_flw_show");
Irssi::command_bind("flw_hide", "cmd_flw_hide");
Irssi::command_bind("flw_grow", "cmd_flw_grow");
Irssi::command_bind("flw_shrink", "cmd_flw_shrink");
Irssi::signal_add("print text", "sig_printtext");
