-
Notifications
You must be signed in to change notification settings - Fork 3
/
LocalServer.pm
315 lines (273 loc) · 7.41 KB
/
LocalServer.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
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
311
312
313
314
315
#!/usr/bin/perl
#
# LocalServer.pm
# Created: Sat Sep 26 18:11:12 1998 by jay.kominek@colorado.edu
# Revised: Fri Mar 9 12:47:25 2001 by jay.kominek@colorado.edu
# Copyright 1998 Jay F. Kominek (jay.kominek@colorado.edu)
#
# Consult the file 'LICENSE' for the complete terms under which you
# may use this file.
#
#####################################################################
# Local Server object for The Perl Internet Relay Chat Daemon
#####################################################################
package LocalServer;
use User;
use Channel;
use Server;
use strict;
use Utils;
use vars qw(@ISA $VERSION);
@ISA=qw{Server};
use Tie::IRCUniqueHash;
$VERSION=$Utils::VERSION;
###################
# CLASS CONSTRUCTOR
###################
# Pass it the name of its configuration file
sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
my $this = { };
$this->{'conffile'} = shift || "server.conf";
my(%opertmp,%usertmp,%childtmp);
tie %opertmp, 'Tie::IRCUniqueHash';
$this->{'opers'} = \%opertmp;
tie %usertmp, 'Tie::IRCUniqueHash';
$this->{'users'} = \%usertmp;
tie %childtmp, 'Tie::IRCUniqueHash';
$this->{'children'} = \%childtmp;
$this->{'version'} = $Utils::VERSION;
&loadconffile($this);
bless($this, $class);
return $this;
}
sub loadconffile {
my $this = shift;
# Clear everything out so that if things have been removed from
# the configuration file, they actually disappear.
$this->{'opers'} = { };
$this->{'klines'} = [ ];
$this->{'connections'} = { };
$this->{'nets'} = { };
$this->{'motd'} = [undef];
$this->{'admin'} = [undef,undef,undef];
$this->{'mechanics'} = { PINGCHECKTIME => 90,
PINGOUTTIME => 90,
FLOODOFFBYTES => 1,
FLOODOFFLINES => 10 };
open(CONF,$this->{'conffile'});
my @lines = <CONF>;
close(CONF);
my $line;
my $i = 0;
CONFPARSE: foreach $line (@lines) {
$i++;
next if ($line =~ m/^#/ or $line =~ m/^\s*$/);
chomp($line);
# Machine line
if($line =~ /^M:([\w\d\.]+):\*:([^:]+):(\d+)/) {
$this->{'name'} = $1;
$this->{'description'} = $2;
$this->{'port'} = $3;
next CONFPARSE;
}
# Admin line
if($line =~ /^A:([^:]+):([^:]+):([^:]+)/) {
@{$this->{'admin'}} = ($1,$2,$3);
next CONFPARSE;
}
# MOTD line
if($line =~ /^MOTD:(.+)$/) {
if(open(MOTD,$1)) {
@{$this->{'motd'}} = <MOTD>;
close(MOTD);
chomp(@{$this->{'motd'}});
} else {
@{$this->{'motd'}} = ($1);
}
next CONFPARSE;
}
# OPER line
if($line =~ /^O:([^:]+):([^:]+):([^:]+)$/) {
my($nick,$mask,$password) = ($1,$2,$3);
$mask =~ s/\./\\\./g;
$mask =~ s/\?/\./g;
$mask =~ s/\*/\.\*/g;
$this->{'opers'}->{ $nick }->{'mask'} = $mask;
$this->{'opers'}->{ $nick }->{'password'} = $password;
next CONFPARSE;
}
# Port line
if($line =~ /^P:([^:]+):([^:]+)$/) {
if($2) {
# SSL socket
push @{ $this->{'sslports'} }, $1;
} else {
# Normal socket
push @{ $this->{'ports'} }, $1;
}
next CONFPARSE;
}
# Kill Line
if($line =~ /^(I?)K:([^:]+):([^:]+):([^.]+)$/) {
my($inverse,$mask,$reason,$usermask) = ($1,$2,$3,$4);
$mask =~ s/\./\\\./g;
$mask =~ s/\?/\./g;
$mask =~ s/\*/\.\*/g;
$usermask =~ s/\./\\\./g;
$usermask =~ s/\?/\./g;
$usermask =~ s/\*/\.\*/g;
$inverse = ($inverse eq 'I');
push @{ $this->{'klines'} }, [$inverse,$usermask,$mask,$reason];
next CONFPARSE;
}
# Connection Line
if($line =~ /^C:([^:]+):([^:]+):([^:]+):?(\d*)$/) {
my($server,$address,$password,$port) = ($1,$2,$3,$4 || 0);
$this->{'connections'}->{$server} = [$server,$address,$password,$port];
next CONFPARSE;
}
# Network Line
if($line =~ /^N:([^:]+):([^:]+):([^:]+)$/) {
my($server,$address,$password) = ($1,$2,$3);
$this->{'nets'}->{$server} = [$server,$address,$password];
next CONFPARSE;
}
# Mechanics Line
if($line =~ /^MECH:(\d+)?:(\d+)?:(\d+)?:(\d+)?$/) {
my($pc,$po,$fb,$fl) = ($1,$2,$3,$4);
if(!defined($pc)) { $pc = $this->{'mechanics'}->{PINGCHECKTIME}; }
if($pc < 0) { die('Bad ping check time in mechanics line'); }
if(!defined($po)) { $po = $this->{'mechanics'}->{PINGOUTTIME}; }
if($po <= 0) { die('Bad ping out time in mechanics line'); }
if(!defined($fb)) { $fb = $this->{'mechanics'}->{FLOODOFFBYTES}; }
if($fb <= 0) { die('Bad flood bytes in mechanics line'); }
if(!defined($fl)) { $fl = $this->{'mechanics'}->{FLOODOFFLINES}; }
if($fl <= 0) { die('Bad flood lines time in mechanics line'); }
$this->{'mechanics'} = { PINGCHECKTIME => $pc,
PINGOUTTIME => $po,
FLOODOFFBYTES => $fb,
FLOODOFFLINES => $fl };
next CONFPARSE;
}
die('Line '.$i.' in '.$this->{'conffile'}.' invalid.');
}
}
# Things That Do Things
sub rehash {
my($this,$from)=(shift,shift);
my $user;
foreach $user (values(%{$this->{'users'}})) {
if($user->ismode('s')) {
$user->privmsgnotice("NOTICE",$this,"*** Notice -- ".$from->{nick}." is rehashing Server config file");
}
}
&loadconffile($this);
}
sub opernotify {
my $this = shift;
my $msg = shift;
my $user;
foreach $user (values(%{$this->{'users'}})) {
if($user->ismode('o')) {
$user->privmsgnotice("NOTICE",$this,"*** Notice --- $msg");
}
}
Utils::syslog('info',$msg);
}
############################
# DATA ACCESSING SUBROUTINES
############################
# Provides the name of this IRC server
sub name {
my $this = shift;
return $this->{'name'};
}
sub description {
my $this = shift;
return $this->{'description'};
}
sub users {
my $this = shift;
my @foo = values(%{$this->{'users'}});
return @foo;
}
sub children {
my $this = shift;
my @foo = values(%{$this->{'children'}});
return @foo;
}
sub parent {
# We're the local server. We don't have a parent
# (from our perspective)
return undef;
}
sub hops {
return 0;
}
sub creation {
return time();
}
# Returns the MOTD of the server as an array of lines.
sub getmotd {
my $this = shift;
return @{$this->{'motd'}};
}
# Same thing as getmotd, except for the admin information
sub getadmin {
my $this = shift;
return @{$this->{'admin'}} if defined $this->{'admin'};
return ('','','');
}
sub getopers {
my $this = shift;
my @foo = keys %{$this->{'opers'}};
return $this->{'opers'};
}
sub lookupconnection {
my $this = shift;
my $server = shift;
return $this->{'connections'}->{$server};
}
sub lookupnetwork {
my $this = shift;
my $server = shift;
return $this->{'nets'}->{$server};
}
###############################
# DATA MANIPULATING SUBROUTINES
###############################
sub adduser {
my $this = shift;
my $user = shift;
$this->{'users'}->{$user->nick()} = $user;
}
sub removeuser {
my $this = shift;
my $user = shift;
my $nick;
if(ref($user)) {
$nick = $user->nick();
} else {
$nick = $user;
}
delete($this->{'users'}->{$nick});
}
sub addchildserver {
my $this = shift;
my $child = shift;
$this->{'children'}->{$child->name()} = $child;
}
sub removechildserver {
my $this = shift;
my $child = shift;
my $name;
if(ref($child)) {
$name = $child->name();
} else {
$name = $child;
}
delete($this->{'children'}->{$name});
}
1;