1# /WHOIS all the users who send you a private message.
2# v0.9 for irssi by Andreas 'ads' Scherbaum
3# idea and some code taken from autowhois.pl from Timo Sirainen
4use strict;
5use Irssi;
6use vars qw($VERSION %IRSSI);
7
8$VERSION = "0.9";
9%IRSSI = (
10    authors	=> "Andreas \'ads\' Scherbaum",
11    contact	=> "ads\@ufp.de",
12    name	=> "auto_whois",
13    description	=> "/WHOIS all the users who send you a private message.",
14    license	=> "GPL",
15    url		=> "http://irssi.org/",
16    changed	=> "2004-02-10",
17    changes	=> "v0.9: don't /WHOIS if query exists for the nick already"
18);
19
20# History:
21#  v0.9: don't /WHOIS if query exists for the nick already
22#        now we store all nicks we have seen in the last 10 minutes
23
24my @seen = ();
25
26sub msg_private_first {
27  my ($server, $msg, $nick, $address) = @_;
28
29  # go through every stored connection and remove, if timed out
30  my $time = time();
31  my ($connection);
32  my @new = ();
33  foreach $connection (@seen) {
34    if ($connection->{lasttime} >= $time - 600) {
35      # is ok, use it
36      push(@new, $connection);
37      # all timed out connections will be dropped
38    }
39  }
40  @seen = @new;
41}
42
43sub msg_private {
44  my ($server, $msg, $nick, $address) = @_;
45
46  # look, if we already know this connection
47  my ($connection, $a);
48  my $known_to_us = 0;
49  for ($a = 0; $a <= $#seen; $a++) {
50    $connection = $seen[$a];
51    # the lc() works not exact, because irc uses another charset
52    if ($connection->{server} eq $server->{address} and $connection->{port} eq $server->{port} and lc($connection->{nick}) eq lc($nick)) {
53      $known_to_us = 1;
54      # mark as refreshed
55      $seen[$a]->{lasttime} = time();
56      last;
57    }
58  }
59
60  if ($known_to_us == 1) {
61    # all ok, return
62    return;
63  }
64
65  # now store the new connection
66  $connection = {};
67  # store our own server data here
68  $connection->{server} = $server->{address};
69  $connection->{port} = $server->{port};
70  # and the nick who queried us
71  $connection->{nick} = $nick;
72  $connection->{lasttime} = time();
73  $connection->{starttime} = time();
74  push(@seen, $connection);
75
76  $server->command("whois $nick");
77}
78
79Irssi::signal_add_first('message private', 'msg_private_first');
80Irssi::signal_add('message private', 'msg_private');
81