2 # module to manage channel lists & data
4 # This is the base class for all channel operations, which is everything to do
5 # with input and output really.
7 # The instance variable in the outside world will be generally be called $dxchan
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.
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)
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).
22 # Copyright (c) 1998-2000 - Dirk Koopman G1TLH
36 use Time::HiRes qw(gettimeofday tv_interval);
40 %channels %pings %valid @ISA $count
44 %pings = (); # outstanding ping requests outbound
45 %channels = (); # the channel list
47 $pingint = 5*60; # default pinginterval
48 $obscount = 2; # default obscount for pings
52 conn => '9,Msg Conn ref',
53 user => '9,DXUser ref',
54 startt => '0,Start Time,atime',
56 pc50_t => '5,Last PC50 Time,atime',
57 priv => '9,Privilege',
58 state => '0,Current State',
59 oldstate => '5,Last State',
60 list => '9,Dep Chan List',
61 name => '0,User Name',
62 consort => '5,Connection Type',
63 'sort' => '5,Type of Channel',
64 wwv => '0,Want WWV,yesno',
65 wcy => '0,Want WCY,yesno',
66 wx => '0,Want WX,yesno',
67 talk => '0,Want Talk,yesno',
68 ann => '0,Want Announce,yesno',
69 here => '0,Here?,yesno',
70 conf => '0,In Conference?,yesno',
71 dx => '0,DX Spots,yesno',
72 redirect => '0,Redirect messages to',
75 loc => '9,Local Vars', # used by func to store local variables in
76 beep => '0,Want Beeps,yesno',
77 lastread => '5,Last Msg Read',
78 outbound => '5,outbound?,yesno',
79 remotecmd => '9,doing rcmd,yesno',
80 pagelth => '0,Page Length',
81 pagedata => '9,Page Data Store',
82 group => '0,Access Group,parray', # used to create a group of users/nodes for some purpose or other
83 isolate => '5,Isolate network,yesno',
84 delayed => '5,Delayed messages,parray',
85 annfilter => '5,Ann Filt-out',
86 wwvfilter => '5,WWV Filt-out',
87 wcyfilter => '5,WCY Filt-out',
88 spotsfilter => '5,Spot Filt-out',
89 routefilter => '5,Route Filt-out',
90 inannfilter => '5,Ann Filt-inp',
91 inwwvfilter => '5,WWV Filt-inp',
92 inwcyfilter => '5,WCY Filt-inp',
93 inspotsfilter => '5,Spot Filt-inp',
94 inroutefilter => '5,Route Filt-inp',
95 passwd => '9,Passwd List,yesno',
96 pingint => '5,Ping Interval ',
97 nopings => '5,Ping Obs Count',
98 lastping => '5,Ping last sent,atime',
99 pingtime => '5,Ping totaltime,parray',
100 pingave => '0,Ping ave time',
101 logininfo => '9,Login info req,yesno',
102 talklist => '0,Talk List,parray',
103 cluster => '5,Cluster data',
104 isbasic => '9,Internal Connection',
105 errors => '9,Errors',
106 route => '9,Route Data',
107 dxcc => '0,Country Code',
110 enhanced => '5,Enhanced Client,yesno',
111 senddbg => '8,Sending Debug,yesno',
112 width => '0,Column Width',
113 disconnecting => '9,Disconnecting,yesno',
114 ann_talk => '0,Suppress Talk Anns,yesno',
115 metric => '1,Route metric',
116 badcount => '1,Bad Word Count',
117 edit => '7,Edit Function',
118 registered => '9,Registered?,yesno',
119 prompt => '0,Required Prompt',
120 version => '1,Node Version',
121 build => '1,Node Build',
122 verified => '9,Verified?,yesno',
125 use vars qw($VERSION $BRANCH);
126 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
127 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
128 $main::build += $VERSION;
129 $main::branch += $BRANCH;
136 if (ref($self->{$_})) {
140 dbg("DXChannel $self->{call} destroyed ($count)") if isdbg('chan');
144 # create a new channel object [$obj = DXChannel->new($call, $msg_conn_obj, $user_obj)]
147 my ($pkg, $call, $conn, $user) = @_;
150 die "trying to create a duplicate channel for $call" if $channels{$call};
151 $self->{call} = $call;
153 $self->{conn} = $conn if defined $conn; # if this isn't defined then it must be a list
155 $self->{user} = $user;
156 $self->{lang} = $user->lang;
157 $user->new_group() if !$user->group;
158 $self->{group} = $user->group;
159 $self->{sort} = $user->sort;
161 $self->{startt} = $self->{t} = time;
163 $self->{oldstate} = 0;
164 $self->{lang} = $main::lang if !$self->{lang};
167 # add in all the dxcc, itu, zone info
168 my @dxcc = Prefix::extract($call);
170 $self->{dxcc} = $dxcc[1]->dxcc;
171 $self->{itu} = $dxcc[1]->itu;
172 $self->{cq} = $dxcc[1]->cq;
176 dbg("DXChannel $self->{call} created ($count)") if isdbg('chan');
178 return $channels{$call} = $self;
181 # obtain a channel object by callsign [$obj = DXChannel->get($call)]
184 my ($pkg, $call) = @_;
185 return $channels{$call};
188 # obtain all the channel objects
192 return values(%channels);
196 # route a message down an appropriate interface for a callsign
198 # is called route(to, pcline);
203 my ($self, $call, $line) = @_;
205 if (ref $self && $call eq $self->{call}) {
206 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
210 # always send it down the local interface if available
211 my $dxchan = DXChannel->get($call);
213 my $cl = Route::get($call);
214 $dxchan = $cl->dxchan if $cl;
216 if (ref $self && $dxchan eq $self) {
217 dbg("PCPROT: Trying to route back to source, dropped") if isdbg('chanerr');
223 my $routeit = $dxchan->adjust_hops($line); # adjust its hop count by node name
225 $dxchan->send($routeit) unless $dxchan == $main::me;
228 dbg("PCPROT: No route available, dropped") if isdbg('chanerr');
233 # gimme all the ak1a nodes
239 foreach $ref (values %channels) {
240 push @out, $ref if $ref->is_node;
245 # return a list of all users
250 foreach $ref (values %channels) {
251 push @out, $ref if $ref->is_user;
256 # return a list of all user callsigns
257 sub get_all_user_calls
261 foreach $ref (values %channels) {
262 push @out, $ref->{call} if $ref->is_user;
267 # obtain a channel object by searching for its connection reference
270 my ($pkg, $conn) = @_;
273 foreach $self (values(%channels)) {
274 return $self if ($self->{conn} == $conn);
279 # get rid of a channel object [$obj->del()]
284 $self->{group} = undef; # belt and braces
285 delete $channels{$self->{call}};
292 return $self->{'sort'} eq 'B';
298 return $self->{'sort'} =~ /[ACRSX]/;
301 # is a node and uses old protocol
305 return $self->is_node && !$self->user->wantnp;
308 # is a node and uses new protocol
312 return $self->is_node && $self->user->wantnp;
315 # is it an ak1a node ?
319 return $self->{'sort'} eq 'A';
326 return $self->{'sort'} eq 'U';
333 return $self->{'sort'} eq 'C';
336 # is it a spider node
340 return $self->{'sort'} eq 'S';
347 return $self->{'sort'} eq 'X';
350 # is it a ar-cluster node
354 return $self->{'sort'} eq 'R';
357 # for perl 5.004's benefit
361 return @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
364 # handle out going messages, immediately without waiting for the select to drop
365 # this could, in theory, block
369 my $conn = $self->{conn};
372 my $call = $self->{call};
376 my @lines = split /\n/;
378 $conn->send_now("$sort$call|$_");
379 dbg("-> $sort $call $_") if isdbg('chan');
386 # send later with letter (more control)
392 my $conn = $self->{conn};
395 my $call = $self->{call};
399 my @lines = split /\n/;
401 $conn->send_later("$sort$call|$_");
402 dbg("-> $sort $call $_") if isdbg('chan');
409 # the normal output routine
411 sub send # this is always later and always data
414 my $conn = $self->{conn};
416 my $call = $self->{call};
420 my @lines = split /\n/;
422 $conn->send_later("D$call|$_");
423 dbg("-> D $call $_") if isdbg('chan');
429 # send a file (always later)
432 my ($self, $fn) = @_;
433 my $call = $self->{call};
434 my $conn = $self->{conn};
437 open(F, $fn) or die "can't open $fn for sending file ($!)";
443 # this will implement language independence (in time)
447 return DXM::msg($self->{lang}, @_);
450 # stick a broadcast on the delayed queue (but only up to 20 items)
456 $self->{delayed} = [] unless $self->{delayed};
457 push @{$self->{delayed}}, $s;
458 if (@{$self->{delayed}} >= 20) {
459 shift @{$self->{delayed}}; # lose oldest one
463 # change the state of the channel - lots of scope for debugging here :-)
468 $self->{oldstate} = $self->{state};
469 $self->{state} = shift;
470 $self->{func} = '' unless defined $self->{func};
471 dbg("$self->{call} channel func $self->{func} state $self->{oldstate} -> $self->{state}\n") if isdbg('state');
473 # if there is any queued up broadcasts then splurge them out here
474 if ($self->{delayed} && ($self->{state} eq 'prompt' || $self->{state} eq 'talk')) {
475 $self->send (@{$self->{delayed}});
476 delete $self->{delayed};
479 return $self->{state};
482 # disconnect this channel
486 my $user = $self->{user};
488 # remove outstanding pings
489 delete $pings{$self->{call}};
491 $user->close() if defined $user;
492 $self->{conn}->disconnect;
497 # just close all the socket connections down without any fiddling about, cleaning, being
498 # nice to other processes and otherwise telling them what is going on.
500 # This is for the benefit of forked processes to prepare for starting new programs, they
501 # don't want or need all this baggage.
507 foreach $ref (values %channels) {
508 $ref->{conn}->disconnect() if $ref->{conn};
513 # Tell all the users that we have come in or out (if they want to know)
519 # send info to all logged in thingies
520 my @dxchan = get_all_users();
522 foreach $dxchan (@dxchan) {
523 next if $dxchan == $self;
524 next if $dxchan->{call} eq $main::mycall;
525 $dxchan->send($dxchan->msg($m, $self->{call})) if $dxchan->{logininfo};
529 # various access routines
532 # return a list of valid elements
541 # return a prompt for a field
546 my ($self, $ele) = @_;
550 # take a standard input message and decode it into its standard parts
555 my ($sort, $call, $line) = $data =~ /^([A-Z])([A-Z0-9\-]{3,9})\|(.*)$/;
557 my $chcall = (ref $dxchan) ? $dxchan->call : "UN.KNOWN";
559 # the above regexp must work
560 unless (defined $sort && defined $call && defined $line) {
561 # $data =~ s/([\x00-\x1f\x7f-\xff])/uc sprintf("%%%02x",ord($1))/eg;
562 dbg("DUFF Line on $chcall: $data");
566 if(ref($dxchan) && $call ne $chcall) {
567 dbg("DUFF Line come in for $call on wrong channel $chcall");
571 return ($sort, $call, $line);
576 my ($self, $flag, $node, $user) = @_;
577 my $nref = Route::Node::get($node);
578 my $dxchan = $nref->dxchan if $nref;
579 if ($nref && $dxchan) {
580 if ($dxchan == $self) {
581 return 1 unless $user;
582 return 1 if $user eq $node;
583 my @users = $nref->users;
584 return 1 if @users == 0 || grep $user eq $_, @users;
585 dbg("RSPF: $user not on $node") if isdbg('chanerr');
587 dbg("RSPF: Shortest path for $node is " . $nref->dxchan->{call}) if isdbg('chanerr');
591 dbg("RSPF: required $node not found" ) if isdbg('chanerr');
596 # broadcast a message to all clusters taking into account isolation
597 # [except those mentioned after buffer]
600 my $s = shift; # the line to be rebroadcast
601 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
602 my @dxchan = DXChannel::get_all_nodes();
605 # send it if it isn't the except list and isn't isolated and still has a hop count
606 foreach $dxchan (@dxchan) {
607 next if grep $dxchan == $_, @except;
608 next if $dxchan == $main::me;
610 my $routeit = $dxchan->can('adjust_hops') ? $dxchan->adjust_hops($s) : $s; # adjust its hop count by node name
612 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
616 # broadcast a message to all clusters ignoring isolation
617 # [except those mentioned after buffer]
618 sub broadcast_all_nodes
620 my $s = shift; # the line to be rebroadcast
621 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
622 my @dxchan = DXChannel::get_all_nodes();
625 # send it if it isn't the except list and isn't isolated and still has a hop count
626 foreach $dxchan (@dxchan) {
627 next if grep $dxchan == $_, @except;
628 next if $dxchan == $main::me;
630 my $routeit = $dxchan->can('adjust_hops') ? $dxchan->adjust_hops($s) : $s; # adjust its hop count by node name
631 $dxchan->send($routeit);
635 # broadcast to all users
636 # storing the spot or whatever until it is in a state to receive it
639 my $s = shift; # the line to be rebroadcast
640 my $sort = shift; # the type of transmission
641 my $fref = shift; # a reference to an object to filter on
642 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
643 my @dxchan = DXChannel::get_all_users();
647 foreach $dxchan (@dxchan) {
648 next if grep $dxchan == $_, @except;
651 broadcast_list($s, $sort, $fref, @out);
655 # broadcast to a list of users
663 foreach $dxchan (@_) {
665 next if $dxchan == $main::me;
668 next unless $dxchan->{dx};
669 ($filter) = $dxchan->{spotsfilter}->it(@{$fref}) if ref $fref;
672 next if $sort eq 'ann' && !$dxchan->{ann} && $s !~ /^To\s+LOCAL\s+de\s+(?:$main::myalias|$main::mycall)/i;
673 next if $sort eq 'wwv' && !$dxchan->{wwv};
674 next if $sort eq 'wcy' && !$dxchan->{wcy};
675 next if $sort eq 'wx' && !$dxchan->{wx};
677 $s =~ s/\a//og unless $dxchan->{beep};
679 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
689 my ($self, $from) = @_;
691 my $ref = $pings{$from};
693 my $tochan = DXChannel->get($from);
696 my $dxchan = DXChannel->get($r->{call});
698 my $t = tv_interval($r->{t}, [ gettimeofday ]);
699 if ($dxchan->is_user) {
700 my $s = sprintf "%.2f", $t;
701 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
702 $dxchan->send($dxchan->msg('pingi', $from, $s, $ave))
703 } elsif ($dxchan->is_node) {
705 my $nopings = $tochan->user->nopings || 2;
706 push @{$tochan->{pingtime}}, $t;
707 shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
709 # cope with a missed ping, this means you must set the pingint large enough
710 if ($t > $tochan->{pingint} && $t < 2 * $tochan->{pingint} ) {
711 $t -= $tochan->{pingint};
714 # calc smoothed RTT a la TCP
715 if (@{$tochan->{pingtime}} == 1) {
716 $tochan->{pingave} = $t;
718 $tochan->{pingave} = $tochan->{pingave} + (($t - $tochan->{pingave}) / 6);
720 $tochan->{nopings} = $nopings; # pump up the timer
732 my $name = $AUTOLOAD;
733 return if $name =~ /::DESTROY$/;
736 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
738 # this clever line of code creates a subroutine which takes over from autoload
739 # from OO Perl - Conway
740 *$AUTOLOAD = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};
741 &$AUTOLOAD($self, @_);
742 # *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
743 # @_ ? $self->{$name} = shift : $self->{$name} ;