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