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
38 use vars qw(%channels %valid @ISA $count);
45 conn => '9,Msg Conn ref',
46 user => '9,DXUser ref',
47 startt => '0,Start 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',
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',
99 route => '9,Route Data',
100 dxcc => '0,Country Code',
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',
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;
128 if (ref($self->{$_})) {
132 dbg("DXChannel $self->{call} destroyed ($count)") if isdbg('chan');
136 # create a new channel object [$obj = DXChannel->new($call, $msg_conn_obj, $user_obj)]
139 my ($pkg, $call, $conn, $user) = @_;
142 die "trying to create a duplicate channel for $call" if $channels{$call};
143 $self->{call} = $call;
145 $self->{conn} = $conn if defined $conn; # if this isn't defined then it must be a list
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;
153 $self->{startt} = $self->{t} = time;
155 $self->{oldstate} = 0;
156 $self->{lang} = $main::lang if !$self->{lang};
159 # add in all the dxcc, itu, zone info
160 my @dxcc = Prefix::extract($call);
162 $self->{dxcc} = $dxcc[1]->dxcc;
163 $self->{itu} = $dxcc[1]->itu;
164 $self->{cq} = $dxcc[1]->cq;
168 dbg("DXChannel $self->{call} created ($count)") if isdbg('chan');
170 return $channels{$call} = $self;
173 # obtain a channel object by callsign [$obj = DXChannel->get($call)]
176 my ($pkg, $call) = @_;
177 return $channels{$call};
180 # obtain all the channel objects
184 return values(%channels);
188 # gimme all the ak1a nodes
194 foreach $ref (values %channels) {
195 push @out, $ref if $ref->is_node;
200 # return a list of all users
205 foreach $ref (values %channels) {
206 push @out, $ref if $ref->is_user;
211 # return a list of all user callsigns
212 sub get_all_user_calls
216 foreach $ref (values %channels) {
217 push @out, $ref->{call} if $ref->is_user;
222 # obtain a channel object by searching for its connection reference
225 my ($pkg, $conn) = @_;
228 foreach $self (values(%channels)) {
229 return $self if ($self->{conn} == $conn);
234 # get rid of a channel object [$obj->del()]
239 $self->{group} = undef; # belt and braces
240 delete $channels{$self->{call}};
247 return $self->{'sort'} eq 'B';
253 return $self->{'sort'} =~ /[ACRSX]/;
255 # is it an ak1a node ?
259 return $self->{'sort'} eq 'A';
266 return $self->{'sort'} eq 'U';
273 return $self->{'sort'} eq 'C';
276 # is it a spider node
280 return $self->{'sort'} eq 'S';
287 return $self->{'sort'} eq 'X';
290 # is it a ar-cluster node
294 return $self->{'sort'} eq 'R';
297 # for perl 5.004's benefit
301 return @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
304 # handle out going messages, immediately without waiting for the select to drop
305 # this could, in theory, block
309 my $conn = $self->{conn};
312 my $call = $self->{call};
316 my @lines = split /\n/;
318 $conn->send_now("$sort$call|$_");
319 dbg("-> $sort $call $_") if isdbg('chan');
326 # send later with letter (more control)
332 my $conn = $self->{conn};
335 my $call = $self->{call};
339 my @lines = split /\n/;
341 $conn->send_later("$sort$call|$_");
342 dbg("-> $sort $call $_") if isdbg('chan');
349 # the normal output routine
351 sub send # this is always later and always data
354 my $conn = $self->{conn};
356 my $call = $self->{call};
360 my @lines = split /\n/;
362 $conn->send_later("D$call|$_");
363 dbg("-> D $call $_") if isdbg('chan');
369 # send a file (always later)
372 my ($self, $fn) = @_;
373 my $call = $self->{call};
374 my $conn = $self->{conn};
377 open(F, $fn) or die "can't open $fn for sending file ($!)";
383 # this will implement language independence (in time)
387 return DXM::msg($self->{lang}, @_);
390 # stick a broadcast on the delayed queue (but only up to 20 items)
396 $self->{delayed} = [] unless $self->{delayed};
397 push @{$self->{delayed}}, $s;
398 if (@{$self->{delayed}} >= 20) {
399 shift @{$self->{delayed}}; # lose oldest one
403 # change the state of the channel - lots of scope for debugging here :-)
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');
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};
419 return $self->{state};
422 # disconnect this channel
426 my $user = $self->{user};
428 $user->close() if defined $user;
429 $self->{conn}->disconnect;
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.
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.
444 foreach $ref (values %channels) {
445 $ref->{conn}->disconnect() if $ref->{conn};
450 # Tell all the users that we have come in or out (if they want to know)
456 # send info to all logged in thingies
457 my @dxchan = get_all_users();
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};
466 # various access routines
469 # return a list of valid elements
478 # return a prompt for a field
483 my ($self, $ele) = @_;
487 # take a standard input message and decode it into its standard parts
492 my ($sort, $call, $line) = $data =~ /^([A-Z])([A-Z0-9\-]{3,9})\|(.*)$/;
494 my $chcall = (ref $dxchan) ? $dxchan->call : "UN.KNOWN";
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");
503 if(ref($dxchan) && $call ne $chcall) {
504 dbg("DUFF Line come in for $call on wrong channel $chcall");
508 return ($sort, $call, $line);
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');
524 dbg("RSPF: Shortest path for $node is " . $nref->dxchan->{call}) if isdbg('chanerr');
528 dbg("RSPF: required $node not found" ) if isdbg('chanerr');
533 # broadcast a message to all clusters taking into account isolation
534 # [except those mentioned after buffer]
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();
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;
547 my $routeit = $dxchan->can('adjust_hops') ? $dxchan->adjust_hops($s) : $s; # adjust its hop count by node name
549 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
553 # broadcast a message to all clusters ignoring isolation
554 # [except those mentioned after buffer]
555 sub broadcast_all_nodes
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();
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;
567 my $routeit = $dxchan->can('adjust_hops') ? $dxchan->adjust_hops($s) : $s; # adjust its hop count by node name
568 $dxchan->send($routeit);
572 # broadcast to all users
573 # storing the spot or whatever until it is in a state to receive it
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();
584 foreach $dxchan (@dxchan) {
585 next if grep $dxchan == $_, @except;
588 broadcast_list($s, $sort, $fref, @out);
592 # broadcast to a list of users
600 foreach $dxchan (@_) {
602 next if $dxchan == $main::me;
605 next unless $dxchan->{dx};
606 ($filter) = $dxchan->{spotsfilter}->it(@{$fref}) if ref $fref;
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};
614 $s =~ s/\a//og unless $dxchan->{beep};
616 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
629 my $name = $AUTOLOAD;
630 return if $name =~ /::DESTROY$/;
633 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
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} ;