summaryrefslogtreecommitdiffstats
path: root/scripts/notonline.pl
blob: 57a28a89573106508cd5b6d12c433d094c2fd843 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Answers "$nick: No." if you're away and someone asks are you online on a channel.

use strict;
use Irssi;
use locale;

use vars qw($VERSION %IRSSI %answers $floodlimit %floodi);

$VERSION = '0.9';
%IRSSI = (
    authors     => 'Johan "Ion" Kiviniemi',
    contact     => 'ion at hassers.org',
    name        => 'NotOnline',
    description =>
'Answers "$nick: No." if you\'re away and someone asks are you online on a channel',
    license => 'Public Domain',
    url     => 'http://ion.amigafin.org/irssi/',
    changed => 'Tue Mar 12 22:20 EET 2002',
);

%answers = (
    'online'    => 'Offline.',
    'there'     => 'Not here.',
    'idle'      => 'Of course.',
    'paikalla'  => 'En, vaan paikassa.',
    'siell�'    => 'Ei kun tuolla.',
    'siellä'   => 'Ei kun tuolla.',
    'hereill�'  => 'Nukkumassa.',
    'hereillä' => 'Nukkumassa.',
);

$floodlimit = 600;    # notice the same channel only once in N seconds
%floodi     = ();

Irssi::signal_add_last(
    'message public' => sub {
        my ($server, $msg, $nick, $address, $target) = @_;

        # Am i away?
        return unless $server->{usermode_away};

        # Am i asked about something?
        my $own_nick = $server->{nick};
        $own_nick =~ s/\W//g;
        return
          unless $msg =~ /^(\Q$server->{nick}\E|\Q$own_nick\E)\s*[,:].+\?/i;

        # Is it me who's talking?
        return if $nick eq $server->{nick};

        # Are you asking the right question?
        my $answer;
        foreach (keys %answers) {
            $answer = $answers{$_} if $msg =~ /\b\Q$_\E\b/i;
        }
        return unless $answer;

        # You aren't flooding, are you?
        if (defined $floodi{$target}) {
            if (time - $floodi{$target} < $floodlimit) {
                return;
            } else {
                undef $floodi{$target};
            }
        }

        $nick =~ s/\W//g;
        $nick = lc $nick
          if Irssi::settings_get_bool('completion_nicks_lowercase');
        $nick .= Irssi::settings_get_str('completion_char') || ":";

        $floodi{$target} = time;
        $server->command("msg $target $nick $answer");
        # Irssi::print("msg $target $nick $answer");
    }
);