summaryrefslogtreecommitdiffstats
path: root/scripts/track.pl
blob: bc73e088a72786f54f2b0a081f88aca76ffea09d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
use Irssi qw(servers);
use warnings; use strict;
use File::Glob qw/:bsd_glob/;
use vars qw($VERSION %IRSSI);

my $quiet     = 0;
my $dupcount  = 0;
$VERSION      = "2.2";

%IRSSI = (
      authors => "Ziddy",
      contact => "DALnet",
      name => "track",
      description => "Keeps track of users by building a database" .
                     "of online, joining and nickchanges. Regex-cabable" .
                     "for the most part, AKA import available. Search by" .
                     "ident, nick or host",
      license => "Public Domain",
      url => "none"
);

my $track_file;

sub whois_signal {
    my ($server, $data, $txtserver) = @_;
    my ($me, $nick, $ident, $host) = split(" ", $data);
    open(my $fh, '>>', $track_file);
    open(my $fh2, '<', $track_file);
    my @list = <$fh2>;
    close($fh2);
    $nick    = conv($nick);
    ($ident  = $ident) =~ s/^~//;
    $ident   = conv($ident);

    if(!grep(/$nick;$ident;$host/, @list)) {
        print $fh "$nick;$ident;$host\n";
        if (!$quiet) { Irssi::print("%G$nick has been added to the database"); }
    } else {
        if (!$quiet) { Irssi::print("%R$nick exists in the database"); }
    }

    close($fh);
}

sub joining {
    my ($server, $channame, $nick, $host) = @_;
    open(my $fh, '>>', $track_file);
    open(my $fh2, '<', $track_file);
    $nick     = conv($nick);
    my @list  = <$fh2>;
    close($fh2);
    my @spl   = split(/@/, $host);
    my $ident = $spl[0];
    my $mask  = $spl[1];
    ($ident   = $ident) =~ s/^~//;
    $ident    = conv($ident);
    $dupcount++;

    if(!grep(/$nick;$ident;$mask/, @list)) {
        print $fh "$nick;$ident;$mask\n";
        if (!$quiet) { Irssi::print("%GADDED $nick;$ident;$mask"); }
    } else {
        if (!$quiet) { Irssi::print("%REXIST $nick;$ident;$mask"); }
    }

    close($fh);

    if ($dupcount >= 100) {
        open(my $fhr, '<', $track_file);
        my @list   = <$fhr>;
        close($fhr);
        my @duprem = uniq(@list);
        open(my $fhw, '>', $track_file);
        print $fhw @duprem;
        close($fhw);
        $dupcount = 0;
    }


}

sub nchange {
    my ($server, $newnick, $oldnick, $host) = @_;
    open(my $fh, '>>', $track_file);
    open(my $fh2, '<', $track_file);
    $newnick  = conv($newnick);
    my @list  = <$fh2>;
    close($fh2);
    my @spl   = split(/@/, $host);
    my $ident = $spl[0];
    my $mask  = $spl[1];
    ($ident   = $ident) =~ s/^~//;
    $ident    = conv($ident);

    if(!grep(/$newnick;$ident;$mask/, @list)){
        print $fh "$newnick;$ident;$mask\n";
        if (!$quiet) { Irssi::print("%GADDED $newnick;$ident;$mask)"); }
    } else {
        if (!$quiet) { Irssi::print("%REXIST $newnick;$ident;$mask"); }
    }

    close($fh);
}

sub track {
    my $input  = $_[0];
    chomp($input);
    my @spl    = split(/\s/, $input);
    my $type;
	if (defined $spl[0] ) {
		$type   = $spl[0];
	} else {
		$type='';
	}
    my $data   = $spl[1];
    $data      = conv($data);
    my $match  = 0;
    open(my $fh, '<', $track_file);
    my @list = <$fh>;
    close($fh);

    if ($type eq "count") {
        Irssi::print("%GDatabase entries%n: " . scalar(@list));
        return;
    }

    if ($type eq "quiet") {
        if ($quiet) { $quiet = 0; } else { $quiet = 1; }
        Irssi::print("%GQuiet mode set to $quiet");
        return;
    }

    if ($type eq "help") {
        Irssi::print("\n%GHelp%n\n" .
                     "      /gather  -  Join your channels then run this\n" .
                     "                  to gather nicks already online\n" .
                     "                  This may take a while on first run\n" .
                     " /track quiet  -  Toggle quiet. If this is on, it wont\n" .
                     "                  show when a person is added or already\n" .
                     "                  exists in the database\n" .
                     "/track count   -  Print amount of database entries\n" .
                     "/import [file] -  This allows you to import AKA data-\n" .
                     "                  bases. AKA is a popular mIRC script\n" .
                     "                  which allows you to keep track of people\n" .
                     "                  by nickname and hostmask. This imports\n" .
                     "                  all of the nicknames and hosts and fills\n" .
                     "                  in the ident with AKAImport, since AKA does\n" .
                     "                  not keep track of idents\n\nCommon usage:\n" .
                     "/track ident <input>  -  Search for entries by supplied ident\n" .
                     "/track nick  <input>  -  Search for entries by supplied nick\n" .
                     "/track host  <input>  -  Search for entries by supplied " .
                     "IP address\n" . " " x 25 . "or hostmask, IPv4 or IPv6\n" .
                     "\n%RNote%n: Regular expressions are acceptable! Be\n" .
                     "careful though. It has no protection to stop you from \n" .
                     "sucking at regex. If you don't match something, it'll\n" .
                     "crash the script (unmatched quantifiers)\nLove,\n  --Ziddy\n");
        return;
    }

    foreach my $line (@list) {
        my ($unick, $ident, $host);
        if ($type eq "ident" && defined $data) {
            if ($line =~ m/^(.*?);($data);(.*)$/i) {
                ($unick, $ident, $host) = (unconv($1), unconv($2), $3);
                Irssi::print("%GIdent[%n$data%G]%n: $unick used $ident on $host");
                $match = 1;
            }
        } elsif ($type eq "host" && defined $data) {
            if ($line =~ m/^(.*?);(.*?);($data)$/i) {
                ($unick, $ident, $host) = (unconv($1), unconv($2), $3);
                Irssi::print("%GHost[%n$data%G]%n: $unick used $ident on $host");
                $match = 1;
            }
        } elsif ($type eq "nick" && defined $data) {
            if ($line =~ m/^($data);(.*?);(.*)$/i) {
                ($unick, $ident, $host) = (unconv($1), unconv($2), $3);
                Irssi::print("%GNick[%n$data%G]%n: $unick used $ident on $host");
                $match = 1;
            }
        } else {
            Irssi::print("%RUsage%n: /track [{ident|host|nick} <input>]");
            last;
        }
    }

    if (!$match) {
        Irssi::print("%RNo data to return");
    }
}

sub uniq {
    my %seen;
    grep !$seen{$_}++, @_;
}

sub namechan {
    my ($null, $cserv) = @_;
    my $count = 0;
    $cserv = $cserv->{tag};
    foreach my $serv (Irssi::channels()) {
        my $curserv = $serv->{server}->{tag};
        if ($cserv eq $curserv) {
            foreach my $nname ($serv->nicks()) {
                my $nickc = conv($nname->{nick});
                my $nick  = $nname->{nick};
                open(my $fh, '<', $track_file);
                my @list  = <$fh>;
                close($fh);

                if(!grep(/$nickc;/, @list)) {
                    Irssi::active_server->send_raw("WHOIS " . $nick);
                    $count++;
                } else {
                    if (!$quiet) { Irssi::print("%RAlready gathered $nick"); }
                }

            }
        }
    }
    Irssi::print("%GGathering complete - Added $count new entries");
}

sub conv {
    my $data = $_[0];
    if (!$data) { return; }
    ($data = $data) =~ s/\]/~~/g;
    ($data = $data) =~ s/\[/@@/g;
    ($data = $data) =~ s/\^/##/g;
    ($data = $data) =~ s/\\/&&/g;
    return $data;
}

sub unconv {
    my $data = $_[0];
    if (!$data) { return; }
    ($data = $data) =~ s/~~/\]/g;
    ($data = $data) =~ s/@@/\[/g;
    ($data = $data) =~ s/##/\^/g;
    ($data = $data) =~ s/%%/\\/g;
    return $data;
}

#Messy for now
sub importAKA {
    my $input = $_[0];
    if (-e $input) {
        open(my $fh, '<', $input);
        my @list = <$fh>;
        close($fh);
        my $ip = 0;
        my ($string, $import);
        foreach my $line (@list) {
            chomp($line);
            my @nicks;
            if ($line =~ /(.*?)@(.*+)/g) {
                $ip = $2;
            } elsif ($line =~ /(.*)~/g) {
                my @nicksplit = split(/~/, $1);
                foreach my $ns (@nicksplit) {
                    push(@nicks, $ns);
                }
            }
            foreach my $nick (@nicks) {
                my $snick = conv($nick);
                if ($snick and $ip) {
                    if (length($snick) > 1 and length($ip) > 1) {
                        $string .= "$snick;AKAImport;$ip;;;";
                    }
                }
            }
        }
        my @arrn = split(/;;;/, $string);
        open(my $fh2, '>>', $track_file);
        foreach my $out (@arrn) {
            if (length($out) > 1) {
                $out =~ s/\r//g;
                print $fh2 "$out\n";
                $import++;
            }
        }
        close($fh2);
        Irssi::print("%GImported $import users into the database%n");
    }
}

sub sig_setup_changed {
	$track_file= Irssi::settings_get_str('track_file');
	$track_file= bsd_glob($track_file);
	if (! (-e $track_file)) {
		open my $fa, '>', $track_file;
		close $fa;
	}
}

Irssi::settings_add_str($IRSSI{name} ,'track_file', Irssi::get_irssi_dir() . "/scripts/track.lst");

Irssi::command_bind('track' => \&track);
Irssi::command_bind('track nick' => \&track);
Irssi::command_bind('track ident' => \&track);
Irssi::command_bind('track host' => \&track);
Irssi::command_bind('track help' => \&track);
Irssi::command_bind('track quiet' => \&track);
Irssi::command_bind('gather' => \&namechan);
Irssi::command_bind('import' => \&importAKA);
Irssi::signal_add('message join', 'joining');
Irssi::signal_add('message nick', 'nchange');
Irssi::signal_add_first('event 311', 'whois_signal');
Irssi::signal_add('setup changed', \&sig_setup_changed);

sig_setup_changed();