-
Notifications
You must be signed in to change notification settings - Fork 3
/
Utils.pm
199 lines (174 loc) · 5.42 KB
/
Utils.pm
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
#!/usr/bin/perl
#
# Utils.pm
# Created: Wed Sep 23 21:56:44 1998 by [email protected]
# Revised: Wed Jun 5 13:01:40 2002 by [email protected]
# Copyright 1998 Jay F. Kominek ([email protected])
#
# Consult the file 'LICENSE' for the complete terms under which you
# may use this file.
#
#####################################################################
# Methods to access global data structures, and the data structures
# themselves.
#####################################################################
package Utils;
#use User;
#use Server;
#use Channel;
use Sys::Syslog ();
use strict;
use vars qw($VERSION %params);
use Tie::IRCUniqueHash;
# We store these structures in a globalish location,
# to let everything get at the same data.
tie my %users, 'Tie::IRCUniqueHash';
tie my %servers, 'Tie::IRCUniqueHash';
tie my %channels, 'Tie::IRCUniqueHash';
my @nickhistory;
my $syslogsetup = 0;
$VERSION="v0.9";
%params=(syslog=>0, # use syslog for log messages?
logfile=>undef, # filename for log, use STDERR if undef
);
sub lookup {
my $name = shift;
chomp $name;
# If it starts with a # or &, then it is a channel that we're
# trying to look up.
if(($name =~ /^\#/) || ($name =~ /^\&/)) {
if($channels{$name}) {
return $channels{$name};
} else {
return undef;
}
} elsif($name =~ /\./) {
# If it has a period in it, then it has to be a server. Assuming
# we did proper checking on new nicks. Which we do. (IIRC)
if($servers{$name}) {
return $servers{$name};
} else {
return undef;
}
} elsif($users{$name}) {
# If its anything else, then it must be a user.
return $users{$name};
} else {
return undef;
}
}
# This only looks up users, in case you know exactly what you're
# looking for.
sub lookupuser {
my $name = shift;
my $dochase = shift;
chomp $name;
my $user = $users{$name};
if($dochase && !defined($user)) {
my $irclctarget = irclc($name);
for(my $i=0;$i<=$#Utils::nickhistory;$i++) {
last if (time-$Utils::nickhistory[$i]->{'time'}>15);
if($irclctarget eq irclc($Utils::nickhistory[$i]->{'nick'})) {
$user = $users{$Utils::nickhistory[$i]->{'newnick'}};
last;
}
}
}
return $user;
}
# This only looks up channels
sub lookupchannel {
my $name = shift;
chomp $name;
return $channels{$name};
}
# This only looks up servers.
sub lookupserver {
my $name = shift;
chomp $name;
return $servers{$name};
}
sub users { return \%users; }
sub channels { return \%channels; }
sub servers { return \%servers; }
# Log something
sub syslog {
my $data = shift;
my $date = localtime();
if(!$syslogsetup) {
if($params{syslog}) {
Sys::Syslog::openlog('pircd','ndelay,pid','daemon');
} elsif($params{logfile}) {
open(STDERR, ">>$params{logfile}")
or die "Unable to open $params{logfile}: $!";
}
$syslogsetup = 1;
}
if($params{syslog}) {
unshift @_, $date;
Sys::Syslog::syslog(@_);
} else {
print STDERR "$date: $data: ",shift,"\n";
}
}
sub irclc ($) {
my $data = shift;
$data =~ tr/A-Z\[\]\\/a-z\{\}\|/;
return $data;
}
# Called when a line of input has been read from a connection. Given a class
# object, the line of input, and a ref to a hash of handler functions, parses
# out the command and data from the input line and calls the appropriate
# handler function. Updates the 'last_active' and 'ping_waiting' values on the
# class object.
# The handler function is called with the given class object as the first
# argument, the entire given line as the second argument, and one additional
# argument for each (irc) argument on the given line.
sub do_handle {
my($object, $line, $handlers)=(shift,shift,shift);
my $command;
my $args;
my @arglist;
my $func;
my($foo,$bar);
$object->{'last_active'} = time();
undef $object->{'ping_waiting'};
# The command that we key on is the first string of alphabetic
# characters (and _, but we'll ignore that)
# note that we leave the leading whitespace in $args. the * after the space
# character is just to make the regex still match an argumentless line.
# since the \w+ greedily grabs everything up to a space, there will always
# be a leading space in $args if $args is non-empty.
($command,$args)=$line =~ /^(\w+)( *.*)/;
$func=$handlers->{uc($command)};
if(defined($func)) {
@arglist=();
if(scalar(($foo,$bar)=split(/ +:/,$args,2))==2) {
# if an arg starts with a colon, the entire part of the line
# following the colon must be treated as a single arg
$args=$foo;
push @arglist, $bar;
}
# } else {
# we needed the leading whitespace on the first arg for the above
# check, but it'd just fuck the below split to hell, so if the
# check wasn't true, get rid of the whitespace.
$args=~s/^ +//;
# }
unshift @arglist,split(/ +/,$args);
# print "arglist is ".join(':',@arglist)."\n";
&{$func}($object,$line,@arglist);
} elsif($line!~/^\s*$/) {
# Utils::syslog('notice',"Received unknown command string '$line' from $$object{nick}");
$object->sendnumeric($object->server,421,($command),"Unknown command");
}
}
# returns 1 if the given string would be a valid irc nick, undef otherwise
sub validnick {
my $str=shift;
return undef if(length($str)<1 || length($str)>32);
# valid characters given in rfc1459 2.3.1
return undef if($str=~/[^A-Za-z0-9-\[\]\\\`\^\{\}\_]/);
return 1;
}
1;