use strict;
use Irssi;
use Irssi::Irc;

use vars qw($VERSION %IRSSI);

$VERSION = "0.3";
%IRSSI = (
        "authors"       => 'Morten Hersson',
        "contact"       => 'mhersson at gmail dot com',
        "name"          => 'allpubmsgs',
        "description"   => 'Sends all public messsages from all channels to one
window, exluding the currently active window and entries in the exclude list. Se
parates channels with different colors.',
        "license"       => 'Public Domain',
        "changed"       => '2011-28-03'
);


# Change the names to channels you visit and color to your liking
my %chan_colors = (
        '#archlinux' => '%m',
        '##mac' => '%y',
        '#macosx' => '%r',
        '#freenode' => '%g',
);
# Channels to exclude
my @exclude_list = qw{#mac1};

sub sig_printtext
{
        my ($dest, $text, $stripped) = @_;

        my $color="%N"; #Set color to default

        # If channel is a key in hash change color
        while(my ($key, $value) = each(%chan_colors)){
                if (lc $dest->{target} eq lc $key) {
                        $color = $value;
                }
        }

        # Fix so any % char are printed
        $text =~ s/\%/%%/g;

        if (grep {m|^$dest->{target}$|} @exclude_list) {} else {

                if ($dest->{level} & MSGLEVEL_PUBLIC) {
                        my $windowname = Irssi::window_find_name('allpubmsgs');

                        my $witem = Irssi::active_win()->{active}; #Get active w
indow
                        if (lc $witem->{name} ne lc $dest->{target} ) {  #If msg
 is not from active window print it to allpubmsg
                                $windowname->print($color . $dest->{target} . ":
%n " . $text, MSGLEVEL_CLIENTCRAP) if ($windowname);
                        }
                }
        }
}

my $windowname = Irssi::window_find_name('allpubmsgs');
if (!$windowname)
{
  Irssi::command("window new");
  Irssi::command("window name allpubmsgs");
}

Irssi::signal_add(
{
  'print text', 'sig_printtext'
}
);