wip
[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,yesno',
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                   metric => '1,Route metric',
109                   badcount => '1,Bad Word Count',
110                   edit => '7,Edit Function',
111                   registered => '9,Registered?,yesno',
112                   prompt => '0,Required Prompt',
113                   version => '1,Node Version',
114                   build => '1,Node Build',
115                   verified => '9,Verified?,yesno',
116                   newroute => '1,New Style Routing,yesno',
117                  );
118
119 use vars qw($VERSION $BRANCH);
120 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
121 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/  || (0,0));
122 $main::build += $VERSION;
123 $main::branch += $BRANCH;
124
125 # object destruction
126 sub DESTROY
127 {
128         my $self = shift;
129         for (keys %$self) {
130                 if (ref($self->{$_})) {
131                         delete $self->{$_};
132                 }
133         }
134         dbg("DXChannel $self->{call} destroyed ($count)") if isdbg('chan');
135         $count--;
136 }
137
138 # create a new channel object [$obj = DXChannel->new($call, $msg_conn_obj, $user_obj)]
139 sub alloc
140 {
141         my ($pkg, $call, $conn, $user) = @_;
142         my $self = {};
143   
144         die "trying to create a duplicate channel for $call" if $channels{$call};
145         $self->{call} = $call;
146         $self->{priv} = 0;
147         $self->{conn} = $conn if defined $conn; # if this isn't defined then it must be a list
148         if (defined $user) {
149                 $self->{user} = $user;
150                 $self->{lang} = $user->lang;
151                 $user->new_group() if !$user->group;
152                 $self->{group} = $user->group;
153                 $self->{sort} = $user->sort;
154         }
155         $self->{startt} = $self->{t} = time;
156         $self->{state} = 0;
157         $self->{oldstate} = 0;
158         $self->{lang} = $main::lang if !$self->{lang};
159         $self->{func} = "";
160
161         # add in all the dxcc, itu, zone info
162         my @dxcc = Prefix::extract($call);
163         if (@dxcc > 0) {
164                 $self->{dxcc} = $dxcc[1]->dxcc;
165                 $self->{itu} = $dxcc[1]->itu;
166                 $self->{cq} = $dxcc[1]->cq;                                             
167         }
168
169         $count++;
170         dbg("DXChannel $self->{call} created ($count)") if isdbg('chan');
171         bless $self, $pkg; 
172         return $channels{$call} = $self;
173 }
174
175 # obtain a channel object by callsign [$obj = DXChannel->get($call)]
176 sub get
177 {
178         my ($pkg, $call) = @_;
179         return $channels{$call};
180 }
181
182 # obtain all the channel objects
183 sub get_all
184 {
185         my ($pkg) = @_;
186         return values(%channels);
187 }
188
189 #
190 # gimme all the ak1a nodes
191 #
192 sub get_all_nodes
193 {
194         my $ref;
195         my @out;
196         foreach $ref (values %channels) {
197                 push @out, $ref if $ref->is_node;
198         }
199         return @out;
200 }
201
202 # return a list of all users
203 sub get_all_users
204 {
205         my $ref;
206         my @out;
207         foreach $ref (values %channels) {
208                 push @out, $ref if $ref->is_user;
209         }
210         return @out;
211 }
212
213 # return a list of all user callsigns
214 sub get_all_user_calls
215 {
216         my $ref;
217         my @out;
218         foreach $ref (values %channels) {
219                 push @out, $ref->{call} if $ref->is_user;
220         }
221         return @out;
222 }
223
224 # obtain a channel object by searching for its connection reference
225 sub get_by_cnum
226 {
227         my ($pkg, $conn) = @_;
228         my $self;
229   
230         foreach $self (values(%channels)) {
231                 return $self if ($self->{conn} == $conn);
232         }
233         return undef;
234 }
235
236 # get rid of a channel object [$obj->del()]
237 sub del
238 {
239         my $self = shift;
240
241         $self->{group} = undef;         # belt and braces
242         delete $channels{$self->{call}};
243 }
244
245 # is it a bbs
246 sub is_bbs
247 {
248         my $self = shift;
249         return $self->{'sort'} eq 'B';
250 }
251
252 sub is_node
253 {
254         my $self = shift;
255         return $self->{'sort'} =~ /[ACRSX]/;
256 }
257 # is it an ak1a node ?
258 sub is_ak1a
259 {
260         my $self = shift;
261         return $self->{'sort'} eq 'A';
262 }
263
264 # is it a user?
265 sub is_user
266 {
267         my $self = shift;
268         return $self->{'sort'} eq 'U';
269 }
270
271 # is it a clx node
272 sub is_clx
273 {
274         my $self = shift;
275         return $self->{'sort'} eq 'C';
276 }
277
278 # is it a spider node
279 sub is_spider
280 {
281         my $self = shift;
282         return $self->{'sort'} eq 'S';
283 }
284
285 # is it a DXNet node
286 sub is_dxnet
287 {
288         my $self = shift;
289         return $self->{'sort'} eq 'X';
290 }
291
292 # is it a ar-cluster node
293 sub is_arcluster
294 {
295         my $self = shift;
296         return $self->{'sort'} eq 'R';
297 }
298
299 # for perl 5.004's benefit
300 sub sort
301 {
302         my $self = shift;
303         return @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
304 }
305
306 # handle out going messages, immediately without waiting for the select to drop
307 # this could, in theory, block
308 sub send_now
309 {
310         my $self = shift;
311         my $conn = $self->{conn};
312         return unless $conn;
313         my $sort = shift;
314         my $call = $self->{call};
315         
316         for (@_) {
317 #               chomp;
318         my @lines = split /\n/;
319                 for (@lines) {
320                         $conn->send_now("$sort$call|$_");
321                         dbg("-> $sort $call $_") if isdbg('chan');
322                 }
323         }
324         $self->{t} = time;
325 }
326
327 #
328 # send later with letter (more control)
329 #
330
331 sub send_later
332 {
333         my $self = shift;
334         my $conn = $self->{conn};
335         return unless $conn;
336         my $sort = shift;
337         my $call = $self->{call};
338         
339         for (@_) {
340 #               chomp;
341         my @lines = split /\n/;
342                 for (@lines) {
343                         $conn->send_later("$sort$call|$_");
344                         dbg("-> $sort $call $_") if isdbg('chan');
345                 }
346         }
347         $self->{t} = time;
348 }
349
350 #
351 # the normal output routine
352 #
353 sub send                                                # this is always later and always data
354 {
355         my $self = shift;
356         my $conn = $self->{conn};
357         return unless $conn;
358         my $call = $self->{call};
359
360         for (@_) {
361 #               chomp;
362         my @lines = split /\n/;
363                 for (@lines) {
364                         $conn->send_later("D$call|$_");
365                         dbg("-> D $call $_") if isdbg('chan');
366                 }
367         }
368         $self->{t} = time;
369 }
370
371 # send a file (always later)
372 sub send_file
373 {
374         my ($self, $fn) = @_;
375         my $call = $self->{call};
376         my $conn = $self->{conn};
377         my @buf;
378   
379         open(F, $fn) or die "can't open $fn for sending file ($!)";
380         @buf = <F>;
381         close(F);
382         $self->send(@buf);
383 }
384
385 # this will implement language independence (in time)
386 sub msg
387 {
388         my $self = shift;
389         return DXM::msg($self->{lang}, @_);
390 }
391
392 # stick a broadcast on the delayed queue (but only up to 20 items)
393 sub delay
394 {
395         my $self = shift;
396         my $s = shift;
397         
398         $self->{delayed} = [] unless $self->{delayed};
399         push @{$self->{delayed}}, $s;
400         if (@{$self->{delayed}} >= 20) {
401                 shift @{$self->{delayed}};   # lose oldest one
402         }
403 }
404
405 # change the state of the channel - lots of scope for debugging here :-)
406 sub state
407 {
408         my $self = shift;
409         if (@_) {
410                 $self->{oldstate} = $self->{state};
411                 $self->{state} = shift;
412                 $self->{func} = '' unless defined $self->{func};
413                 dbg("$self->{call} channel func $self->{func} state $self->{oldstate} -> $self->{state}\n") if isdbg('state');
414
415                 # if there is any queued up broadcasts then splurge them out here
416                 if ($self->{delayed} && ($self->{state} eq 'prompt' || $self->{state} eq 'talk')) {
417                         $self->send (@{$self->{delayed}});
418                         delete $self->{delayed};
419                 }
420         }
421         return $self->{state};
422 }
423
424 # disconnect this channel
425 sub disconnect
426 {
427         my $self = shift;
428         my $user = $self->{user};
429         
430         main::clean_inqueue($self);          # clear out any remaining incoming frames
431         $user->close() if defined $user;
432         $self->{conn}->disconnect;
433         $self->del();
434 }
435
436 #
437 # just close all the socket connections down without any fiddling about, cleaning, being
438 # nice to other processes and otherwise telling them what is going on.
439 #
440 # This is for the benefit of forked processes to prepare for starting new programs, they
441 # don't want or need all this baggage.
442 #
443
444 sub closeall
445 {
446         my $ref;
447         foreach $ref (values %channels) {
448                 $ref->{conn}->disconnect() if $ref->{conn};
449         }
450 }
451
452 #
453 # Tell all the users that we have come in or out (if they want to know)
454 #
455 sub tell_login
456 {
457         my ($self, $m) = @_;
458         
459         # send info to all logged in thingies
460         my @dxchan = get_all_users();
461         my $dxchan;
462         foreach $dxchan (@dxchan) {
463                 next if $dxchan == $self;
464                 next if $dxchan->{call} eq $main::mycall;
465                 $dxchan->send($dxchan->msg($m, $self->{call})) if $dxchan->{logininfo};
466         }
467 }
468
469 # various access routines
470
471 #
472 # return a list of valid elements 
473
474
475 sub fields
476 {
477         return keys(%valid);
478 }
479
480 #
481 # return a prompt for a field
482 #
483
484 sub field_prompt
485
486         my ($self, $ele) = @_;
487         return $valid{$ele};
488 }
489
490 # take a standard input message and decode it into its standard parts
491 sub decode_input
492 {
493         my $dxchan = shift;
494         my $data = shift;
495         my ($sort, $call, $line) = $data =~ /^([A-Z])([A-Z0-9\-]{3,9})\|(.*)$/;
496
497         my $chcall = (ref $dxchan) ? $dxchan->call : "UN.KNOWN";
498         
499         # the above regexp must work
500         unless (defined $sort && defined $call && defined $line) {
501 #               $data =~ s/([\x00-\x1f\x7f-\xff])/uc sprintf("%%%02x",ord($1))/eg;
502                 dbg("DUFF Line on $chcall: $data");
503                 return ();
504         }
505
506         if(ref($dxchan) && $call ne $chcall) {
507                 dbg("DUFF Line come in for $call on wrong channel $chcall");
508                 return();
509         }
510         
511         return ($sort, $call, $line);
512 }
513
514 sub rspfcheck
515 {
516         my ($self, $flag, $node, $user) = @_;
517         my $nref = Route::Node::get($node);
518         my $dxchan = $nref->dxchan if $nref;
519         if ($nref && $dxchan) {
520             if ($dxchan == $self) {
521                         return 1 unless $user;
522                         return 1 if $user eq $node;
523                         my @users = $nref->users;
524                         return 1 if @users == 0 || grep $user eq $_, @users;
525                         dbg("RSPF: $user not on $node") if isdbg('chanerr');
526                 } else {
527                         dbg("RSPF: Shortest path for $node is " . $nref->dxchan->{call}) if isdbg('chanerr');
528                 }
529         } else {
530                 return 1 if $flag;
531                 dbg("RSPF: required $node not found" ) if isdbg('chanerr');
532         }
533         return 0;
534 }
535
536 # broadcast a message to all clusters taking into account isolation
537 # [except those mentioned after buffer]
538 sub broadcast_nodes
539 {
540         my $s = shift;                          # the line to be rebroadcast
541         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
542         my @dxchan = DXChannel::get_all_nodes();
543         my $dxchan;
544         
545         # send it if it isn't the except list and isn't isolated and still has a hop count
546         foreach $dxchan (@dxchan) {
547                 next if grep $dxchan == $_, @except;
548                 next if $dxchan == $main::me;
549                 
550                 my $routeit = $dxchan->can('adjust_hops') ? $dxchan->adjust_hops($s) : $s;      # adjust its hop count by node name
551
552                 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
553         }
554 }
555
556 # broadcast a message to all clusters ignoring isolation
557 # [except those mentioned after buffer]
558 sub broadcast_all_nodes
559 {
560         my $s = shift;                          # the line to be rebroadcast
561         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
562         my @dxchan = DXChannel::get_all_nodes();
563         my $dxchan;
564         
565         # send it if it isn't the except list and isn't isolated and still has a hop count
566         foreach $dxchan (@dxchan) {
567                 next if grep $dxchan == $_, @except;
568                 next if $dxchan == $main::me;
569
570                 my $routeit = $dxchan->can('adjust_hops') ? $dxchan->adjust_hops($s) : $s;      # adjust its hop count by node name
571                 $dxchan->send($routeit);
572         }
573 }
574
575 # broadcast to all users
576 # storing the spot or whatever until it is in a state to receive it
577 sub broadcast_users
578 {
579         my $s = shift;                          # the line to be rebroadcast
580         my $sort = shift;           # the type of transmission
581         my $fref = shift;           # a reference to an object to filter on
582         my @except = @_;                        # to all channels EXCEPT these (dxchannel refs)
583         my @dxchan = DXChannel::get_all_users();
584         my $dxchan;
585         my @out;
586         
587         foreach $dxchan (@dxchan) {
588                 next if grep $dxchan == $_, @except;
589                 push @out, $dxchan;
590         }
591         broadcast_list($s, $sort, $fref, @out);
592 }
593
594
595 # broadcast to a list of users
596 sub broadcast_list
597 {
598         my $s = shift;
599         my $sort = shift;
600         my $fref = shift;
601         my $dxchan;
602         
603         foreach $dxchan (@_) {
604                 my $filter = 1;
605                 next if $dxchan == $main::me;
606                 
607                 if ($sort eq 'dx') {
608                     next unless $dxchan->{dx};
609                         ($filter) = $dxchan->{spotsfilter}->it(@{$fref}) if ref $fref;
610                         next unless $filter;
611                 }
612                 next if $sort eq 'ann' && !$dxchan->{ann} && $s !~ /^To\s+LOCAL\s+de\s+(?:$main::myalias|$main::mycall)/i;
613                 next if $sort eq 'wwv' && !$dxchan->{wwv};
614                 next if $sort eq 'wcy' && !$dxchan->{wcy};
615                 next if $sort eq 'wx' && !$dxchan->{wx};
616
617                 $s =~ s/\a//og unless $dxchan->{beep};
618
619                 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
620                         $dxchan->send($s);      
621                 } else {
622                         $dxchan->delay($s);
623                 }
624         }
625 }
626
627
628 #no strict;
629 sub AUTOLOAD
630 {
631         no strict;
632         my $name = $AUTOLOAD;
633         return if $name =~ /::DESTROY$/;
634         $name =~ s/^.*:://o;
635   
636         confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
637
638         # this clever line of code creates a subroutine which takes over from autoload
639         # from OO Perl - Conway
640         *$AUTOLOAD = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};
641         goto &$AUTOLOAD;
642 }
643
644
645 1;
646 __END__;