# NameCatcher
#  - Catch a specific users messages in a channel and show them in a seperate
#    window.
#
# Copyright (C) 2011 by Jonas Sandberg
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

use Irssi;
use POSIX;
use Data::Dumper;
use strict;
use vars qw($VERSION %IRSSI);

$VERSION = "1.0";
%IRSSI = (
        authors         => "Jonas Sandberg",
	contact         => "sorebeard\@sorebeard.com",
        name            => "namecatcher",
        decription      => "Catch specific users channel messages and show them in a seperate window.",
        license         => "MIT",
        changed         => "2011-09-11"
        );

# ChangeLog:
# version 1.1:
#    No longer private messages (highlighted) but written to specific window.
# version 1.0:
#    Hardcoded (below) channel and sender name captured and forwarded as
#    private message, as intended.
#
# ToDo:
#    * Configurable channel and sender names.
#    * Higlight users name/higlight targets if used in text.
#    * Find a better name.
#    * Test logging.
#    * Add option to use v1.0 higlighting if needed to log correctly.
#

my $channel  = "#xtralarge";
my $sender_name  = "xl-bot";
my $window;
   
    sub catch_mess {
        my ($server, $data) = @_;
        my ($sender, $text) = split(/ PRIVMSG/, $data, 2);
        if ($sender =~ /$sender_name/ && $text =~ $channel) {
            my $my_nick = $server->{nick};
            my ($nick, $address) = split(/!/,$sender, 2);
            $nick=~s/://;
            my ($chan, $message) = split(/:/,$text);
            Irssi::signal_stop();
            $window->print($message, MSGLEVEL_CLIENTCRAP) if ($window);
        }
    }

$window = Irssi::window_find_name($sender_name);
Irssi::print("Create a window named " . $sender_name . ". With /window new <hidden>  and after selecting that window:  /window name " . $sender_name . ".") if (!$window);

Irssi::signal_add("server incoming", "catch_mess")

