]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/Wingate.pl
da9deed0667cb7e1ea94ab69a3dd0ffd01dc111f
[infobot.git] / src / Modules / Wingate.pl
1 #
2 #  Wingate.pl: Wingate checker.
3 #      Author: dms
4 #     Version: v0.3 (20000526).
5 #     Created: 20000116
6 #        NOTE: based on wingate.pl by fooz.
7 #
8
9 package Wingate;
10
11 use strict;
12 my $select = IO::Select->new;
13
14 sub Wingates {
15     my $file = "$::blootbot_base_dir/$::param{'ircUser'}.wingate";
16     my @hosts;
17
18     open(IN, $file);
19     while (<IN>) {
20         chop;
21         next if (/\*$/);        # wingate. or forget about it?
22         push(@hosts,$_);
23     }
24     close IN;
25
26     foreach (@_) {
27         next if (grep /^$_$/, @hosts);
28
29         &::DEBUG("W: _ => '$_'.");
30         &Wingate($_);
31     }
32 }
33
34 sub Wingate {
35     my ($host) = @_;
36
37     my $sock = IO::Socket::INET->new(
38         PeerAddr        => $host,
39         PeerPort        => 'telnet(23)',
40         Proto           => 'tcp'
41 ###     Timeout         => 10,          # enough :)
42     );
43
44     if (!defined $sock) {
45         &::status("Wingate: connection refused to $host");
46         return;
47     }
48
49     $sock->timeout(10);
50     $select->add($sock);
51
52     my $errors = 0;
53     my ($luser);
54     foreach $luser ($select->can_read(1)) {
55         my $buf;
56         my $len = 0;
57         if (!defined($len = sysread($luser, $buf, 512))) {
58             &::status("Wingate: connection lost to $luser/$host.");
59             $select->remove($luser);
60             close($luser);
61             next;
62         }
63
64         if ($len == 9) {
65             $len = sysread($luser, $buf, 512);
66         }
67
68         my $wingate = 0;
69         $wingate++ if ($buf =~ /^WinGate\>/);
70         $wingate++ if ($buf =~ /^Too many connected users - try again later$/);
71
72         if ($wingate) {
73             &::status("Wingate: RUNNING ON $host BY $::who.");
74
75             if (&::IsChanConf('wingateBan') > 0) {
76                 &::ban("*!*\@$host", '');
77             }
78
79             my $reason  = &::getChanConf('wingateKick');
80             if ($reason) {
81                 &::kick($::who, '', $reason);
82             }
83
84             push(@::wingateBad, "$host\*");
85             &::wingateWriteFile();
86         } else {
87 ###         &::DEBUG("no wingate.");
88         }
89
90         ### TODO: close telnet connection correctly!
91         $select->remove($luser);
92         close($luser);
93     }
94
95     return;
96 }
97
98 1;