-
Notifications
You must be signed in to change notification settings - Fork 1
/
Connection.pm
337 lines (300 loc) · 8.85 KB
/
Connection.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/perl
#
# Connection.pm
# Created: Sat Feb 13 14:28:01 1999 by [email protected]
# Revised: Sat Dec 9 23:54:09 2000 by [email protected]
# Copyright 1999 Jay F. Kominek ([email protected])
# This program comes with ABSOLUTELY NO WARRANTY.
#
#####################################################################
# Connection module for Jiten, The Perl DICT Server
#####################################################################
package Connection;
use strict;
use Socket;
use Sys::Hostname;
use Dictionary;
my %commandhandlers = ('DEFINE' => \&handle_define,
'MATCH' => \&handle_match,
'XMATCH' => \&handle_xmatch,
'SHOW' => \&handle_show,
'CLIENT' => \&handle_client,
'STATUS' => \&handle_status,
'HELP' => \&handle_help,
'QUIT' => \&handle_quit,
'OPTION' => \&handle_option,
'AUTH' => \&handle_auth,
'SASLAUTH' => \&handle_saslauth,
'SASLRESP' => \&handle_saslresp);
my @options = ('mime', 'xmatch');
sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
my $this = { };
$this->{'socket'} = shift;
$this->{'outbuffer'} = shift;
my($port,$iaddr) = sockaddr_in(getpeername($this->{'socket'}));
$this->{'host'} = inet_ntoa($iaddr);
$this->{'define'} = $this->{'match'} = 0;
$this->{'last_active'} = $this->{'connected'} = time();
$this->{'msgid'} = sprintf("<%i.%i\@%s>",$$,time,hostname);
$this->{'mime'} = 0;
bless($this, $class);
$this->sendnumeric(220,sprintf("%s jiten %1.1f <%s> %s",
hostname,
0.6,
join(".",@options),
$this->{'msgid'}));
return $this;
}
sub lastactive {
my $this = shift;
return $this->{'last_active'};
}
sub handle {
my $this = shift;
my $line = shift;
$line =~ s/^\s+//; $line =~ s/\s+$//;
$this->{'last_active'} = time();
$line =~ /^(\S+)/;
chomp(my $command = $1);
$command =~ tr/a-z/A-Z/;
if(defined($commandhandlers{$command}) && ref($commandhandlers{$command})) {
# print "$line\n";
return (&{$commandhandlers{$command}}($this,$line));
} else {
$this->sendnumeric(500,"Syntax error, command not recognized");
}
}
#####################################################################
# Protocol command handlers
#####################################################################
# DEFINE database word
sub handle_define {
my $this = shift;
my $line = shift;
my($command,$database,$word) = &tokenize($line);
if(Dictionary::havedatabase($database) || $database eq "!") {
my @results = Dictionary::define($database,$word);
if(scalar @results>0) {
my %dbs = Dictionary::databases();
$this->sendnumeric(150,($#results+1)." definitions retrived");
foreach my $result (@results) {
my @tmp = @{$result};
$this->sendnumericmultiline(151,"\"$tmp[1]\" $tmp[0] \"$dbs{$tmp[0]}\"",$tmp[2]);
}
$this->sendnumeric(250,"ok");
} else {
$this->sendnumeric(552,"no match");
}
} else {
$this->sendnumeric(550,
"Invalid database, use \"SHOW DB\" for list of databases");
}
$this->{'define'}++;
}
# MATCH database strategy word
sub handle_match {
my $this = shift;
my $line = shift;
my($command,$database,$strategy,$word) = &tokenize($line);
if(Dictionary::havedatabase($database) || $database eq "!") {
if(Dictionary::havestrat($strategy) || $strategy eq ".") {
my @results = Dictionary::match($database,$strategy,$word);
if(scalar @results>0) {
$this->sendnumericmultiline(152,($#results+1)." matches found",
map { sprintf("%s \"%s\"",@{$_}) }
@results);
$this->sendnumeric(250,"ok");
} else {
$this->sendnumeric(552,"no match");
}
} else {
$this->sendnumeric(551,
"Invalid strategy, use \"SHOW STRAT\" for a list of strategies");
}
} else {
$this->sendnumeric(550,
"Invalid database, use \"SHOW DB\" for list of databases");
}
$this->{'match'}++;
}
# XMATCH database strategy word [arguments]...
sub handle_xmatch {
my $this = shift;
my $line = shift;
my($command,$database,$strategy,$word,@args) = &tokenize($line);
if((Dictionary::havedatabase($database) || $database eq "!") &&
(Dictionary::havestrat($strategy) || $strategy eq ".")) {
my @results = Dictionary::match($database,$strategy,$word,@args);
if(scalar @results>0) {
$this->sendnumericmultiline(152,($#results+1)." matches found",
map { sprintf("%s \"%s\"",@{$_}) }
@results);
$this->sendnumeric(250,"ok");
} else {
$this->sendnumeric(552,"no match");
}
} else {
$this->sendnumeric(501,"Syntax error, illegal parameters");
}
$this->{'match'}++;
}
# SHOW what
sub handle_show {
my $this = shift;
my $line = shift;
my($command,$thing,@args) = &tokenize($line);
$thing =~ tr/a-z/A-Z/;
if($thing eq "DB" or $thing eq "DATABASES") {
my %dbs = Dictionary::databases();
if($dbs{'*'}) { delete($dbs{'*'}); }
$this->sendnumericmultiline(110,(scalar keys %dbs)." databases present",
map { "$_ \"$dbs{$_}\"" } keys %dbs);
$this->sendnumeric(250,"ok");
} elsif($thing eq "STRAT" or $thing eq "STRATEGIES") {
my %strats = Dictionary::strategies();
$this->sendnumericmultiline(111,(scalar keys %strats)." strategies present", map { "$_ \"$strats{$_}\"" } keys %strats);
$this->sendnumeric(250,"ok");
} elsif($thing eq "INFO") {
my $db = $args[0];
if(Dictionary::havedatabase($db)) {
$this->sendnumericmultiline(112,"Information for $db",
Dictionary::getdbdesc($db));
$this->sendnumeric(250,"ok");
} else {
$this->sendnumeric(550,"Invalid database");
}
} elsif($thing eq "SERVER") {
$this->sendnumericmultiline(114,"server information",
"Jiten 0.6",
"This information is worthless and hardcoded.");
$this->sendnumeric(250,"ok");
} else {
$this->sendnumeric(501,"Syntax error, illegal parameters");
}
}
# CLIENT string
sub handle_client {
my $this = shift;
my $line = shift;
$line =~ /^(\S+) (.+)$/;
$this->{'client'} = $2;
$this->sendnumeric(250,"ok");
}
# STATUS
sub handle_status {
my $this = shift;
my @times = times();
$this->sendnumeric(210,sprintf("running [%s u/s/cu/cs ; %s/%s d/m]",
join("/",times),
join("/",@{$this}{'define','match'})));
}
my @helptext = ("The following commands are valid:",
"DEFINE db word",
"MATCH db strat word",
"XMATCH db strat word [args]..",
"SHOW {DB,DATABASES}",
"SHOW STRAT{EGIES,}",
"SHOW INFO db",
"SHOW SERVER",
"CLIENT infostring",
"OPTION MIME",
"STATUS",
"HELP",
"QUIT");
# HELP
sub handle_help {
my $this = shift;
$this->sendnumericmultiline(113,"help text follows",
@helptext);
$this->sendnumeric(250,"ok");
}
# QUIT
sub handle_quit {
my $this = shift;
$this->sendnumeric(221,"Closing Connection. Goodbye");
return -1;
}
# OPTION name
sub handle_option {
my $this = shift;
my $line = shift;
my($command,$option,@args) = &tokenize($line);
if(uc($option) eq "MIME") {
$this->{'mime'} = 1;
$this->sendnumeric(250,"ok - using MIME headers");
} else {
$this->sendnumeric(501,"Syntax error, illegal parameters");
}
}
# AUTH userid authentication-string
sub handle_auth {
my $this = shift;
$this->sendnumeric(502,"Command not implemented");
}
# SASLAUTH mechanism initial-response
sub handle_saslauth {
my $this = shift;
$this->sendnumeric(502,"Command not implemented");
}
# SASLRESP response
sub handle_saslresp {
my $this = shift;
$this->sendnumeric(502,"Command not implemented");
}
#####################################################################
# Data transmitting whatnot
#####################################################################
sub sendnumeric {
my $this = shift;
my $numeric = shift;
if(length($numeric)<3) {
$numeric = ("0" x (3 - length($numeric))).$numeric;
}
$this->sendline(join(' ',$numeric,@_));
}
sub sendnumericmultiline {
my $this = shift;
my $numeric = shift;
my $numericline = shift;
my @lines = @_;
$this->sendnumeric($numeric,$numericline);
if($this->{'mime'}==1) {
$this->sendline(""); # send blank line. (default mime headers)
}
foreach my $line (@lines) {
foreach my $thisline (split(/\n/, $line)) {
$this->sendline($thisline);
}
}
$this->sendline(".");
}
sub sendline {
my $this = shift;
my $line = shift;
$this->senddata($line."\r\n");
}
sub senddata {
my $this = shift;
$this->{'outbuffer'}->{$this->{'socket'}} .= shift;
}
#####################################################################
# Other
sub tokenize {
my $line = shift;
my @tokens;
while($line=~s/(\".+?\"|\S+)\s*/push @tokens,$1;'';/e) { ; }
return map{s/^\"//;s/\"$//;$_;} @tokens;
}
sub last_active {
my $this = shift;
return $this->{'last_active'};
}
##################
# Class destructor
sub DESTROY {
my $this = shift;
my $duration = time()-$this->{'connected'};
}
1;