#!/usr/bin/perl -w
#
# Copyright (c) 2012 petschge <mail@petschge.de>
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

use strict;
use vars qw($VERSION %IRSSI);

$VERSION = '0.02';
%IRSSI = (
    authors	=> 'petschge',
    contact	=> 'mail@petschge.de',
    name	=> 'nomultichan',
    description => 'ignore sentances from a single user to multiple channels in all but the first channel',
    license	=> 'BSD 2-clause',
);


my $lock_ev = 0;   # To avoid calling ourselves infinitely
my %lastsaid = (); # Hash of ($user, $line) tuples indexed by second.nanosecond strings
my $timeout = 60;  # Timeout in seconds


# This is unfortunately linux (or at least unix) only
sub time_since_epoch {
    my $timestamp=`date +%s.%N`;
    chomp($timestamp);
    return $timestamp;
}

sub clean_recent {
    my ($nowsec, $nownano) = split(/\./, time_since_epoch(), 2);
    for (keys %lastsaid) {
        my ($second, $nano) = split(/\./, $_, 2);
        if(($nowsec - $second) > $timeout) {
            delete $lastsaid{$_};
        }
    }
}

sub event_pubmsg {
    if ($lock_ev) { return };

    clean_recent();

    my ($server, $text, $nick, $address, $chan) = @_;

    my $dub = 0;
    for (keys %lastsaid) {
        my ($lnick, $ltext) = @{$lastsaid{$_}};
        if(($lnick eq $nick) && ($ltext eq $text)) {
            $dub = 1;
            last;
        }
    }

    # Add _after_ test to avoid clashing with ourselves
    $lastsaid{time_since_epoch()} = [$nick, $text];

    if($dub == 1) {
        $lock_ev = 1;

        # Modifiy the Text and pass on. still creates activity but allows for nice insults
        #Irssi::signal_emit('message public', $server, "dup ".$text, $nick, $address, $chan);

        # Creates no activity, but has to guess the line format
        $server->print($chan, "< ".$nick."> ".$text, MSGLEVEL_CLIENTCRAP | MSGLEVEL_NOHILIGHT | MSGLEVEL_NO_ACT);

        Irssi::signal_stop();
        $lock_ev = 0;
    }

}

#sub cmd_dump {
#    Irssi::print("Dump last lines");
#    clean_recent();
#    for (keys %lastsaid) {
#        my ($nick, $text) = @{$lastsaid{$_}};
#        Irssi::print("timestamp ".$_.": nick = ".$nick.", text = ".$text);
#    }
#}

sub event_settings_reload {
    $timeout = Irssi::settings_get_int('timeout');
}

Irssi::settings_add_int("nomultichan", "timeout", "60");
Irssi::signal_add('message public', "event_pubmsg");
Irssi::signal_add('setup changed', "event_settings_reload");
#Irssi::command_bind('dump_last', 'cmd_dump');
