allow console.pl to work in older browsers
[spider.git] / perl / DXChannel.pm
1 #
2 # module to manage channel lists & data
3 #
4 # This is the base class for all channel operations, which is everything to do 
5 # with input and output really.
6 #
7 # The instance variable in the outside world will be generally be called $dxchan
8 #
9 # This class is 'inherited' (if that is the goobledegook for what I am doing)
10 # by various other modules. The point to understand is that the 'instance variable'
11 # is in fact what normal people would call the state vector and all useful info
12 # about a connection goes in there.
13 #
14 # Another point to note is that a vector may contain a list of other vectors. 
15 # I have simply added another variable to the vector for 'simplicity' (or laziness
16 # as it is more commonly called)
17 #
18 # PLEASE NOTE - I am a C programmer using this as a method of learning perl
19 # firstly and OO about ninthly (if you don't like the design and you can't 
20 # improve it with better OO and thus make it smaller and more efficient, then tough). 
21 #
22 # Copyright (c) 1998-2000 - Dirk Koopman G1TLH
23 #
24 # $Id$
25 #
26 package DXChannel;
27
28 use Msg;
29 use DXM;
30 use DXUtil;
31 use DXVars;
32 use DXDebug;
33 use Filter;
34 use Prefix;
35 use Route;
36
37 use strict;
38 use vars qw(%channels %valid @ISA $count);
39
40 %channels = ();
41 $count = 0;
42
43 %valid = (
44                   call => '0,Callsign',
45                   conn => '9,Msg Conn ref',
46                   user => '9,DXUser ref',
47                   startt => '0,Start Time,atime',
48                   t => '9,Time,atime',
49                   pc50_t => '5,Last PC50 Time,atime',
50                   priv => '9,Privilege',
51                   state => '0,Current State',
52                   oldstate => '5,Last State',
53                   list => '9,Dep Chan List',
54                   name => '0,User Name',
55                   consort => '5,Connection Type',
56                   'sort' => '5,Type of Channel',
57                   wwv => '0,Want WWV,yesno',
58                   wcy => '0,Want WCY,yesno',
59                   wx => '0,Want WX,yesno',
60                   talk => '0,Want Talk,yesno',
61                   ann => '0,Want Announce,yesno',
62                   here => '0,Here?,yesno',
63                   conf => '0,In Conference?,yesno',
64                   dx => '0,DX Spots,yesno',
65                   redirect => '0,Redirect messages to',
66                   lang => '0,Language',
67                   func => '5,Function',
68                   loc => '9,Local Vars', # used by func to store local variables in
69                   beep => '0,Want Beeps,yesno',
70                   lastread => '5,Last Msg Read',
71                   outbound => '5,outbound?,yesno',
72                   remotecmd => '9,doing rcmd,yesno',
73                   pagelth => '0,Page Length',
74                   pagedata => '9,Page Data Store',
75                   group => '0,Access Group,parray',     # used to create a group of users/nodes for some purpose or other
76                   isolate => '5,Isolate network,yesno',
77                   delayed => '5,Delayed messages,parray',
78                   annfilter => '5,Ann Filt-out',
79                   wwvfilter => '5,WWV Filt-out',
80                   wcyfilter => '5,WCY Filt-out',
81                   spotsfilter => '5,Spot Filt-out',
82                   routefilter => '5,Route Filt-out',
83                   inannfilter => '5,Ann Filt-inp',
84                   inwwvfilter => '5,WWV Filt-inp',
85                   inwcyfilter => '5,WCY Filt-inp',
86                   inspotsfilter => '5,Spot Filt-inp',
87                   inroutefilter => '5,Route Filt-inp',
88                   passwd => '9,Passwd List,parray',
89                   pingint => '5,Ping Interval ',
90                   nopings => '5,Ping Obs Count',
91                   lastping => '5,Ping last sent,atime',
92                   pingtime => '5,Ping totaltime,parray',
93                   pingave => '0,Ping ave time',
94                   logininfo => '9,Login info req,yesno',
95                   talklist => '0,Talk List,parray',
96                   cluster => '5,Cluster data',
97                   isbasic => '9,Internal Connection', 
98                   errors => '9,Errors',
99                   route => '9,Route Data',
100                   dxcc => '0,Country Code',
101                   itu => '0,ITU Zone',
102                   cq => '0,CQ Zone',
103                   enhanced => '5,Enhanced Client,yesno',
104                   senddbg => '8,Sending Debug,yesno',
105                   width => '0,Column Width',
106                   disconnecting => '9,Disconnecting,yesno',
107                   ann_talk => '0,Suppress Talk Anns,yesno',
108                  );
109
110 use vars qw($VERSION $BRANCH);
111 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
112 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ ) || 0;
113 $main::build += $VERSION;
114 $main::branch += $BRANCH;
115
116 # object destruction
117 sub DESTROY
118 {
119         my $self = shift;
120         for (keys %$self) {
121                 if (ref($self->{$_})) {
122                         delete $self->{$_};
123                 }
124         }
125         dbg("DXChannel $self->{call} destroyed ($count)") if isdbg('chan');
126         $count--;
127 }
128
129 # create a new channel object [$obj = DXChannel->new($call, $msg_conn_obj, $user_obj)]
130 sub alloc
131 {
132         my ($pkg, $call, $conn, $user) = @_;
133         my $self = {};
134   
135         die "trying to create a duplicate channel for $call" if $channels{$call};
136         $self->{call} = $call;
137         $self->{priv} = 0;
138         $self->{conn} = $conn if defined $conn; # if this isn't defined then it must be a list
139         if (defined $user) {
140                 $self->{user} = $user;
141                 $self->{lang} = $user->lang;
142                 $user->new_group() if !$user->group;
143                 $self->{group} = $user->group;
144                 $self->{sort} = $user->sort;
145         }
146         $self->{startt} = $self->{t} = time;
147         $self->{state} = 0;
148         $self->{oldstate} = 0;
149         $self->{lang} = $main::lang if !$self->{lang};
150         $self->{func} = "";
151
152         # add in all the dxcc, itu, zone info
153         my @dxcc = Prefix::extract($call);
154         if (@dxcc > 0) {
155                 $self->{dxcc} = $dxcc[1]->dxcc;
156                 $self->{itu} = $dxcc[1]->itu;
157                 $self->{cq} = $dxcc[1]->cq;                                             
158         }
159
160         $count++;
161         dbg("DXChannel $self->{call} created ($count)") if isdbg('chan');
162         bless $self, $pkg; 
163         return $channels{$call} = $self;
164 }
165
166 # obtain a channel object by callsign [$obj = DXChannel->get($call)]
167 sub get
168 {
169         my ($pkg, $call) = @_;
170         return $channels{$call};
171 }
172
173 # obtain all the channel objects
174 sub get_all
175 {
176         my ($pkg) = @_;
177         return values(%channels);
178 }
179
180 #
181 # gimme all the ak1a nodes
182 #
183 sub get_all_nodes
184 {
185         my $ref;
186         my @out;
187         foreach $ref (values %channels) {
188                 push @out, $ref if $ref->is_node;
189         }
190         return @out;
191 }
192
193 # return a list of all users
194 sub get_all_users
195 {
196         my $ref;
197         my @out;
198         foreach $ref (values %channels) {
199                 push @out, $ref if $ref->is_user;
200         }
201         return @out;
202 }
203
204 # return a list of all user callsigns
205 sub get_all_user_calls
206 {
207         my $ref;
208         my @out;
209         foreach $ref (values %channels) {
210                 push @out, $ref->{call} if $ref->is_user;
211         }
212         return @out;
213 }
214
215 # obtain a channel object by searching for its connection reference
216 sub get_by_cnum
217 {
218         my ($pkg, $conn) = @_;
219         my $self;
220   
221         foreach $self (values(%channels)) {
222                 return $self if ($self->{conn} == $conn);
223         }
224         return undef;
225 }
226
227 # get rid of a channel object [$obj->del()]
228 sub del
229 {
230         my $self = shift;
231
232         $self->{group} = undef;         # belt and braces
233         delete $channels{$self->{call}};
234 }
235
236 # is it a bbs
237 sub is_bbs
238 {
239         my $self = shift;
240         return $self->{'sort'} eq 'B';
241 }
242
243 sub is_node
244 {
245         my $self = shift;
246         return $self->{'sort'} =~ /[ACRSX]/;
247 }
248 # is it an ak1a node ?
249 sub is_ak1a
250 {
251         my $self = shift;
252         return $self->{'sort'} eq 'A';
253 }
254
255 # is it a user?
256 sub is_user
257 {
258         my $self = shift;
259         return $self->{'sort'} eq 'U';
260 }
261
262 # is it a clx node
263 sub is_clx
264 {
265         my $self = shift;
266         return $self->{'sort'} eq 'C';
267 }
268
269 # is it a spider node
270 sub is_spider
271 {
272         my $self = shift;
273         return $self->{'sort'} eq 'S';
274 }
275
276 # is it a DXNet node
277 sub is_dxnet
278 {
279         my $self = shift;
280         return $self->{'sort'} eq 'X';
281 }
282
283 # is it a ar-cluster node
284 sub is_arcluster
285 {
286         my $self = shift;
287         return $self->{'sort'} eq 'R';
288 }
289
290 # for perl 5.004's benefit
291 sub sort
292 {
293         my $self = shift;
294         return @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
295 }
296
297 # handle out going messages, immediately without waiting for the select to drop
298 # this could, in theory, block
299 sub send_now
300 {
301         my $self = shift;
302         my $conn = $self->{conn};
303         return unless $conn;
304         my $sort = shift;
305         my $call = $self->{call};
306         
307         for (@_) {
308 #               chomp;
309         my @lines = split /\n/;
310                 for (@lines) {
311                         $conn->send_now("$sort$call|$_");
312                         dbg("-> $sort $call $_") if isdbg('chan');
313                 }
314         }
315         $self->{t} = time;
316 }
317
318 #
319 # send later with letter (more control)
320 #
321
322 sub send_later
323 {
324         my $self = shift;
325         my $conn = $self->{conn};
326         return unless $conn;
327         my $sort = shift;
328         my $call = $self->{call};
329         
330         for (@_) {
331 #               chomp;
332         my @lines = split /\n/;
333                 for (@lines) {
334                         $conn->send_later("$sort$call|$_");
335                         dbg("-> $sort $call $_") if isdbg('chan');
336                 }
337         }
338         $self->{t} = time;
339 }
340
341 #
342 # the normal output routine
343 #
344 sub send                                                # this is always later and always data
345 {
346         my $self = shift;
347         my $conn = $self->{conn};
348         return unless $conn;
349         my $call = $self->{call};
350
351         for (@_) {
352 #               chomp;
353         my @lines = split /\n/;
354                 for (@lines) {
355                         $conn->send_later("D$call|$_");
356                         dbg("-> D $call $_") if isdbg('chan');
357                 }
358         }
359         $self->{t} = time;
360 }
361
362 # send a file (always later)
363 sub send_file
364 {
365         my ($self, $fn) = @_;
366         my $call = $self->{call};
367         my $conn = $self->{conn};
368         my @buf;
369   
370         open(F, $fn) or die "can't open $fn for sending file ($!)";
371         @buf = <F>;
372         close(F);
373         $self->send(@buf);
374 }
375
376 # this will implement language independence (in time)
377 sub msg
378 {
379         my $self = shift;
380         return DXM::msg($self->{lang}, @_);
381 }
382
383 # stick a broadcast on the delayed queue (but only up to 20 items)
384 sub delay
385 {
386         my $self = shift;
387         my $s = shift;
388         
389         $self->{delayed} = [] unless $self->{delayed};
390         push @{$self->{delayed}}, $s;
391         if (@{$self->{delayed}} >= 20) {
392                 shift @{$self->{delayed}};   # lose oldest one
393         }
394 }
395
396 # change the state of the channel - lots of scope for debugging here :-)
397 sub state
398 {
399         my $self = shift;
400         if (@_) {
401                 $self->{oldstate} = $self->{state};
402                 $self->{state} = shift;
403                 $self->{func} = '' unless defined $self->{func};
404                 dbg("$self->{call} channel func $self->{func} state $self->{oldstate} -> $self->{state}\n") if isdbg('state');
405
406                 # if there is any queued up broadcasts then splurge them out here
407                 if ($self->{delayed} && ($self->{state} eq 'prompt' || $self->{state} eq 'talk')) {
408                         $self->send (@{$self->{delayed}});
409                         delete $self->{delayed};
410                 }
411         }
412         return $self->{state};
413 }
414
415 # disconnect this channel
416 sub disconnect
417 {
418         my $self = shift;
419         my $user = $self->{user};
420         
421         $user->close() if defined $user;
422         $self->{conn}->disconnect;
423         $self->del();
424 }
425
426 #
427 # just close all the socket connections down without any fiddling about, cleaning, being
428 # nice to other processes and otherwise telling them what is going on.
429 #
430 # This is for the benefit of forked processes to prepare for starting new programs, they
431 # don't want or need all this baggage.
432 #
433
434 sub closeall
435 {
436         my $ref;
437         foreach $ref (values %channels) {
438                 $ref->{conn}->disconnect() if $ref->{conn};
439         }
440 }
441
442 #
443 # Tell all the users that we have come in or out (if they want to know)
444 #
445 sub tell_login
446 {
447         my ($self, $m) = @_;
448         
449         # send info to all logged in thingies
450         my @dxchan = get_all_users();
451         my $dxchan;
452         foreach $dxchan (@dxchan) {
453                 next if $dxchan == $self;
454                 next if $dxchan->{call} eq $main::mycall;
455                 $dxchan->send($dxchan->msg($m, $self->{call})) if $dxchan->{logininfo};
456         }
457 }
458
459 # various access routines
460
461 #
462 # return a list of valid elements 
463
464
465 sub fields
466 {
467         return keys(%valid);
468 }
469
470 #
471 # return a prompt for a field
472 #
473
474 sub field_prompt
475
476         my ($self, $ele) = @_;
477         return $valid{$ele};
478 }
479
480 # take a standard input message and decode it into its standard parts
481 sub decode_input
482 {
483         my $dxchan = shift;
484         my $data = shift;
485         my ($sort, $call, $line) = $data =~ /^([A-Z])([A-Z0-9\-]{3,9})\|(.*)$/;
486
487         my $chcall = (ref $dxchan) ? $dxchan->call : "UN.KNOWN";
488         
489         # the above regexp must work
490         unless (defined $sort && defined $call && defined $line) {
491 #               $data =~ s/([\x00-\x1f\x7f-\xff])/uc sprintf("%%%02x",ord($1))/eg;
492                 dbg("DUFF Line on $chcall: $data") if isdbg('err');
493                 return ();
494         }
495
496         if(ref($dxchan) && $call ne $chcall) {
497                 dbg("DUFF Line come in for $call on wrong channel $chcall") if isdbg('err');
498                 return();
499         }
500         
501         return ($sort, $call, $line);
502 }
503
504 sub rspfcheck
505 {
506         my ($self, $flag, $node, $user) = @_;
507         my $nref = Route::Node::get($node);
508         if ($nref) {
509             if ($nref->dxchan == $self) {
510                         return 1 unless $user;
511                         my @users = $nref->users;
512                         return 1 if @users == 0 || grep $user eq $_, @users;
513                         dbg("RSPF: $user not on $node") if isdbg('rspf');
514                 } else {
515                         dbg("RSPF: Shortest path for $node is " . $nref->dxchan->{call}) if isdbg('rspf');
516                 }
517         } else {
518                 return 1 if $flag;
519                 dbg("RSPF: required $node not found" ) if isdbg('rspf');
520         }
521         return 0;
522 }
523
524 no strict;
525 sub AUTOLOAD
526 {
527         my $self = shift;
528         my $name = $AUTOLOAD;
529         return if $name =~ /::DESTROY$/;
530         $name =~ s/.*:://o;
531   
532         confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
533
534         # this clever line of code creates a subroutine which takes over from autoload
535         # from OO Perl - Conway
536         *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
537     @_ ? $self->{$name} = shift : $self->{$name} ;
538 }
539
540
541 1;
542 __END__;