/var/www/www.irssi.org-old/scripts/html/armeija.pl


   1 #!/usr/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 
  10 use Irssi;
  11 
  12 use vars qw($VERSION %IRSSI);
  13 $VERSION = "0.4";
  14 %IRSSI = (
  15     authors     => "Erkki Seppälä",
  16     contact     => "flux\@inside.org",
  17     name        => "Armeija Ignore",
  18     description => "Ignores people bringin up boring/repeated subjects, plus replies.",
  19     license     => "Public Domain",
  20     url         => "http://xulfad.inside.org/~flux/software/irssi/",
  21     changed     => "Tue Mar  5 00:06:35 EET 2002"
  22 );
  23 
  24 
  25 use Irssi::Irc;
  26 use strict;
  27 
  28 my $log = 0;
  29 my $logFile = "$ENV{HOME}/.irssi/armeija.log";
  30 
  31 my $retrigger = 0;
  32 my $wordFile = "$ENV{HOME}/.irssi/armeija.words";
  33 my $channelFile = "$ENV{HOME}/.irssi/armeija.channels";
  34 my $overflowLimit = 3;
  35 
  36 my @channels = ("#linux.fi");
  37 
  38 my @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 
  54 my %infected;
  55 my $timeout = 60;
  56 
  57 my $who = "";
  58 my $why = "";
  59 
  60 sub p0 {
  61   my $a = $_[0];
  62   while (length($a) < 2) {
  63     $a = "0$a";
  64   }
  65   return $a;
  66 }
  67 
  68 sub why {
  69   if ($who ne "") {
  70     Irssi::print "$who was ignored: $why";
  71   }
  72 }
  73 
  74 sub 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, ">>$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 
 144 sub 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 
 163 sub load {
 164   local $/ = "\n";
 165   local *F;
 166   if (open(F, "< $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, "< $channelFile")) {
 177     @channels = ();
 178     while (<F>) {
 179       chomp;
 180       push @channels, $_;
 181     }
 182     close(F);
 183   }
 184 }
 185 
 186 sub save {
 187   local *F;
 188   if (open(F, "> $wordFile")) {
 189     for (my $c = 0; $c < @keywords; ++$c) {
 190       print F $keywords[$c], "\n";
 191     }
 192     close(F);
 193   }
 194   if (open(F, "> $channelFile")) {
 195     for (my $c = 0; $c < @channels; ++$c) {
 196       print F $channels[$c], "\n";
 197     }
 198     close(F);
 199   }
 200 }
 201 
 202 sub 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 
 218 sub 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 
 263 Irssi::signal_add("message public", "public");
 264 Irssi::command_bind("armeija", "armeija");
 265 
 266 Irssi::print "Armeija-ignore v$VERSION by $IRSSI{contact}";
 267 load();