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