]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/Wingate.pl
dunno
[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 = "$::infobot_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++
71           if ( $buf =~ /^Too many connected users - try again later$/ );
72
73         if ($wingate) {
74             &::status("Wingate: RUNNING ON $host BY $::who.");
75
76             if ( &::IsChanConf('wingateBan') > 0 ) {
77                 &::ban( "*!*\@$host", '' );
78             }
79
80             my $reason = &::getChanConf('wingateKick');
81             if ($reason) {
82                 &::kick( $::who, '', $reason );
83             }
84
85             push( @::wingateBad, "$host\*" );
86             &::wingateWriteFile();
87         }
88         else {
89 ###         &::DEBUG("no wingate.");
90         }
91
92         ### TODO: close telnet connection correctly!
93         $select->remove($luser);
94         close($luser);
95     }
96
97     return;
98 }
99
100 1;
101
102 # vim:ts=4:sw=4:expandtab:tw=80