1#!/usr/local/bin/perl -w
2# script to ignore boring messages in irc
3# it has a list of keywords which on a public message will cause someone
4# to be ignored for 60 seconds (changeable). also it ignores (tries to)
5# every message back to ignored people.
6#  - flux@inside.org
7
8# check out my other irssi-stuff at http://xulfad.inside.org/~flux/software/irssi/
9
10use strict;
11use Irssi;
12
13use vars qw($VERSION %IRSSI);
14$VERSION = "0.4";
15%IRSSI = (
16    authors     => "Erkki Sepp�l�",
17    contact     => "flux\@inside.org",
18    name        => "Armeija Ignore",
19    description => "Ignores people bringin up boring/repeated subjects, plus replies.",
20    license     => "Public Domain",
21    url         => "http://xulfad.inside.org/~flux/software/irssi/",
22    changed     => "Tue Mar  5 00:06:35 EET 2002"
23);
24
25
26use Irssi::Irc;
27
28my $log = 0;
29my $logFile = "$ENV{HOME}/.irssi/armeija.log";
30
31my $retrigger = 0;
32my $wordFile = "$ENV{HOME}/.irssi/armeija.words";
33my $channelFile = "$ENV{HOME}/.irssi/armeija.channels";
34my $overflowLimit = 3;
35
36my @channels = ("#linux.fi");
37
38my @keywords = (
39
40# armeija
41  "\\barmeija", "\\brynkky", "\\bintti", "\\bintiss�", "\\bgines", "\\btj\\b"
42, "\\bsaapumiser�", "\\bvarus(mies|nainen|t�ti)", "\\bvemppa", "\\bvempula"
43, "\\bvempa", "\\bveksi", "\\bsulkeiset", "\\bsulkeisi"
44, "\\bvlv\\b", "\\bhl\\b"
45
46# offtopic
47, "\\bsalkkari", "\\bsalatut el�m". "\\bsalattuja el�m"
48
49# urheilu
50,"\\bhiiht", "\\bhiihd", "\\bformula", "\\bolympia"
51
52);
53
54my %infected;
55my $timeout = 60;
56
57my $who = "";
58my $why = "";
59
60sub p0 {
61  my $a = $_[0];
62  while (length($a) < 2) {
63    $a = "0$a";
64  }
65  return $a;
66}
67
68sub why {
69  if ($who ne "") {
70    Irssi::print "$who was ignored: $why";
71  }
72}
73
74sub public {
75  my ($server, $msg, $nick, $address, $target) = @_;
76
77  local *F;
78
79  my $now = time;
80
81  my $skip = 1;
82  foreach my $channel (@channels) {
83    if (lc($target) eq lc($channel)) {
84      $skip = 0;
85      last;
86    }
87  }
88
89  if ($skip) {
90    return 0;
91  }
92
93  # check for keywords
94
95  my $count = 0;
96  foreach my $word (@keywords) {
97    if ($msg =~ /$word/i) {
98      ++$count;
99    }
100  }
101
102  if (($count >= 1) && ($count < $overflowLimit)) {
103    Irssi::print "Ignoring $nick";
104    $why = $msg;
105    $who = $nick;
106    if ($log) {
107      open(F, q{>>}, $logFile);
108      my @t = localtime($now);
109      $t[5] += 1900;
110      print F "$t[5]-", p0($t[4] + 1), "-", p0($t[3]), " ",
111	p0($t[2]), ":", p0($t[1]), ":", p0($t[0]), " $who/$target: $why\n";
112      close(F);
113    }
114    if ($retrigger || !exists $infected{$nick}) {
115      $infected{$nick} = $now + $timeout;
116    }
117    Irssi::signal_stop();
118    return 1;
119  }
120
121  # check and expire old ignores
122  if (exists $infected{$nick}) {
123    if ($infected{$nick} < $now) {
124      Irssi::print "Timed out: $nick";
125      delete $infected{$nick};
126    } else {
127      Irssi::signal_stop();
128      return 1;
129    }
130  }
131
132  # check for messages targetted to ignored people
133  foreach my $nick (keys %infected) {
134    if ($msg =~ /^$nick/i) {
135      # ignore messages to these people
136      Irssi::signal_stop();
137      return 1;
138    }
139  }
140
141  return 0;
142}
143
144sub logging {
145  my (@args) = @_;
146  if (@args) {
147    if ($args[0] eq "on") {
148      $log = 1;
149      Irssi::print("Armeija-logging on to file $logFile");
150    } elsif ($args[0] eq "off") {
151      $log = 0;
152      Irssi::print("Armeija-logging stopped");
153    } else {
154      $logFile = $args[0];
155      Irssi::print("Armeija-logfile set to $logFile");
156    }
157  } else {
158    Irssi::print("usage: armeija log [on|off|new log file name]");
159    Irssi::print("Log is " . ($log ? "on" : "off") . ", logfile is $logFile");
160  }
161}
162
163sub load {
164  local $/ = "\n";
165  local *F;
166  if (open(F, q{<}, $wordFile)) {
167    @keywords = ();
168    while (<F>) {
169      chomp;
170      push @keywords, $_;
171    }
172    close(F);
173  } else {
174    Irssi::print("Failed to open wordfile $wordFile\n");
175  }
176  if (open(F, q{<}, $channelFile)) {
177    @channels = ();
178    while (<F>) {
179      chomp;
180      push @channels, $_;
181    }
182    close(F);
183  }
184}
185
186sub save {
187  local *F;
188  if (open(F, q{>}, $wordFile)) {
189    for (my $c = 0; $c < @keywords; ++$c) {
190      print F $keywords[$c], "\n";
191    }
192    close(F);
193  }
194  if (open(F, q{>}, $channelFile)) {
195    for (my $c = 0; $c < @channels; ++$c) {
196      print F $channels[$c], "\n";
197    }
198    close(F);
199  }
200}
201
202sub retrigger {
203  if (@_ == 1) {
204    if ($_[0] eq "on") {
205      Irssi::print "Armeija retrigger on";
206      $retrigger = 1;
207    } elsif ($_[0] eq "off") {
208      Irssi::print "Armeija retrigger off";
209      $retrigger = 0;
210    } else {
211      Irssi::print("Invalid armeija trigger state");
212    }
213  } else {
214    Irssi::print("usage: /armeija retrigger [on|off]");
215  }
216}
217
218sub armeija {
219  my (@args) = split(" ", $_[0]);
220  if (@args) {
221    if ($args[0] eq "why") {
222      why();
223    } elsif ($args[0] eq "log") {
224      my @a = @args;
225      shift @a;
226      logging(@a);
227    } elsif ($args[0] eq "load") {
228      load();
229    } elsif ($args[0] eq "save") {
230      save();
231    } elsif ($args[0] eq "+word") {
232      my @a = @args;
233      shift @a;
234      push @keywords, join(" ", @a);
235      save();
236    } elsif ($args[0] eq "-word") {
237      my @a = @args;
238      shift @a;
239      for (my $c = 0; $c < @keywords; ++$c) {
240        for (my $d = 0; $d < @a;) {
241          if ($a[$d] eq $keywords[$c]) {
242            splice @keywords, $c, 1;
243          } else {
244            ++$d;
245          }
246        }
247      }
248      save();
249    } elsif ($args[0] eq "words") {
250      Irssi::print(join(", ", @keywords));
251    } elsif ($args[0] eq "retrigger") {
252      my @a = @args;
253      shift @a;
254      retrigger(@a);
255    } else {
256      Irssi::print("Invalid armeija command");
257    }
258  } else {
259    Irssi::print("Armeija usage: armeija [log [off|on|filename]|load|save|+word word|-word word|words]");
260  }
261}
262
263Irssi::signal_add("message public", "public");
264Irssi::command_bind("armeija", "armeija");
265
266Irssi::print "Armeija-ignore v$VERSION by $IRSSI{contact}";
267load();
268