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-2016 - Dirk Koopman G1TLH
38 use vars qw(%channels %valid @ISA $count $maxerrors);
44 'sort' => '5,Type of Channel',
45 ann => '0,Want Announce,yesno',
46 ann_talk => '0,Suppress Talk Anns,yesno',
47 annfilter => '5,Ann Filt-out',
48 badcount => '1,Bad Word Count',
49 badip => '9,BAD IP address',
50 beep => '0,Want Beeps,yesno',
51 build => '1,Node Build',
53 cluster => '5,Cluster data',
54 conf => '0,In Conference?,yesno',
55 conn => '9,Msg Conn ref',
56 consort => '5,Connection Type',
58 delayed => '5,Delayed messages,parray',
59 disconnecting => '9,Disconnecting,yesno',
60 do_pc9x => '9,Handles PC9x,yesno',
61 dx => '0,DX Spots,yesno',
62 dxcc => '0,Country Code',
63 edit => '7,Edit Function',
64 enhanced => '5,Enhanced Client,yesno',
67 group => '0,Access Group,parray', # used to create a group of users/nodes for some purpose or other
68 gtk => '5,Using GTK,yesno',
69 handle_xml => '9,Handles XML,yesno',
70 here => '0,Here?,yesno',
71 hostname => '0,Hostname',
72 inannfilter => '5,Ann Filt-inp',
73 inpc92filter => '5,PC92 Route Filt-inp',
74 inqueue => '9,Input Queue,parray',
75 inrbnfilter => '5,RBN Filt-inp',
76 inroutefilter => '5,Route Filt-inp',
77 inscript => '9,In a script,yesno',
78 inspotsfilter => '5,Spot Filt-inp',
79 inwcyfilter => '5,WCY Filt-inp',
80 inwwvfilter => '5,WWV Filt-inp',
81 isbasic => '9,Internal Connection',
82 isolate => '5,Isolate network,yesno',
83 isslugged => '9,Still Slugged,yesno',
86 lastmsgpoll => '0,Last Msg Poll,atime',
87 lastping => '5,Ping last sent,atime',
88 lastread => '5,Last Msg Read',
89 list => '9,Dep Chan List',
90 loc => '9,Local Vars', # used by func to store local variables in
91 logininfo => '9,Login info req,yesno',
92 metric => '1,Route metric',
93 name => '0,User Name',
94 newroute => '1,New Style Routing,yesno',
95 next_pc92_keepalive => '9,Next PC92 KeepAlive,atime',
96 next_pc92_update => '9,Next PC92 Update,atime',
97 nopings => '5,Ping Obs Count',
98 oldstate => '5,Last State',
99 outbound => '5,outbound?,yesno',
100 pagedata => '9,Page Data Store',
101 pagelth => '0,Page Length',
102 passwd => '9,Passwd List,yesno',
103 pc50_t => '5,Last PC50 Time,atime',
104 pc92filter => '5,PC92 Route Filt-out',
105 pingave => '0,Ping ave time',
106 pingint => '5,Ping Interval ',
107 pingtime => '5,Ping totaltime,parray',
108 priv => '9,Privilege',
109 prompt => '0,Required Prompt',
110 rbnfilter => '5,RBN Filt-out',
111 redirect => '0,Redirect messages to',
112 registered => '9,Registered?,yesno',
113 remotecmd => '9,doing rcmd,yesno',
114 route => '9,Route Data',
115 routefilter => '5,Route Filt-out',
116 senddbg => '8,Sending Debug,yesno',
117 sluggedpcs => '9,Slugged PCxx Queue,parray',
118 spotsfilter => '5,Spot Filt-out',
119 startt => '0,Start Time,atime',
120 state => '0,Current State',
122 talk => '0,Want Talk,yesno',
123 talklist => '0,Talk List,parray',
124 user => '9,DXUser ref',
125 ve7cc => '0,VE7CC program special,yesno',
126 verified => '9,Verified?,yesno',
127 version => '1,Node Version',
128 wcy => '0,Want WCY,yesno',
129 wcyfilter => '5,WCY Filt-out',
130 width => '0,Column Width',
131 wwv => '0,Want WWV,yesno',
132 wwvfilter => '5,WWV Filt-out',
133 wx => '0,Want WX,yesno',
136 $maxerrors = 20; # the maximum number of concurrent errors allowed before disconnection
143 if (ref($self->{$_})) {
147 dbg("DXChannel $self->{call} destroyed ($count)") if isdbg('chan');
151 # create a new channel object [$obj = DXChannel->new($call, $msg_conn_obj, $user_obj)]
154 my ($pkg, $call, $conn, $user) = @_;
157 die "trying to create a duplicate channel for $call" if $channels{$call};
158 $self->{call} = $call;
160 $self->{conn} = $conn if defined $conn; # if this isn't defined then it must be a list
162 $self->{user} = $user;
163 $self->{lang} = $user->lang;
164 $user->new_group unless $user->group;
165 $user->new_buddies unless $user->buddies;
166 $self->{group} = $user->group;
167 $self->{sort} = $user->sort;
168 $self->{width} = $user->width;
170 $self->{startt} = $self->{t} = $main::systime;
172 $self->{oldstate} = 0;
173 $self->{lang} = $main::lang if !$self->{lang};
175 $self->{width} ||= 80;
177 # add in all the dxcc, itu, zone info
178 my @dxcc = Prefix::extract($call);
180 $self->{dxcc} = $dxcc[1]->dxcc;
181 $self->{itu} = $dxcc[1]->itu;
182 $self->{cq} = $dxcc[1]->cq;
184 $self->{inqueue} = [];
187 dbg("DXChannel $self->{call} created ($count)") if isdbg('chan');
189 return $channels{$call} = $self;
192 # count errors and disconnect if too many
193 # this has to be here because it can come from rcmd (DXProt) as
194 # well as DXCommandmode.
199 if (++$self->{errors} > $maxerrors) {
200 $self->send($self->msg('e26'));
204 return ($self->msg($e));
208 # rebless this channel as something else
213 return $channels{$self->{call}} = bless $self, $class;
218 my ($self, $msg) = @_;
220 # queue the message and the channel object for later processing
222 push @{$self->{inqueue}}, $msg;
227 # obtain a channel object by callsign [$obj = DXChannel::get($call)]
231 return $channels{$call};
234 # obtain all the channel objects
237 return values(%channels);
241 # gimme all the ak1a nodes
247 foreach $ref (values %channels) {
248 push @out, $ref if $ref->is_node;
253 # return a list of node calls
254 sub get_all_node_calls
258 foreach $ref (values %channels) {
259 push @out, $ref->{call} if $ref->is_node;
264 # return a list of all users
269 foreach $ref (values %channels) {
270 push @out, $ref if $ref->is_user;
275 # return a list of all user callsigns
276 sub get_all_user_calls
280 foreach $ref (values %channels) {
281 push @out, $ref->{call} if $ref->is_user;
286 # obtain a channel object by searching for its connection reference
289 my ($pkg, $conn) = @_;
292 foreach $self (values(%channels)) {
293 return $self if ($self->{conn} == $conn);
298 # get rid of a channel object [$obj->del()]
303 $self->{group} = undef; # belt and braces
304 delete $channels{$self->{call}};
310 return $_[0]->{sort} eq 'B';
315 return $_[0]->{sort} =~ /^[ACRSX]$/;
317 # is it an ak1a node ?
320 return $_[0]->{sort} eq 'A';
326 return $_[0]->{sort} =~ /^[UW]$/;
332 return $_[0]->{sort} eq 'C';
335 # it is a Web connected user
338 return $_[0]->{sort} eq 'W';
341 # is it a spider node
344 return $_[0]->{sort} eq 'S';
350 return $_[0]->{sort} eq 'X';
353 # is it a ar-cluster node
356 return $_[0]->{sort} eq 'R';
361 return $_[0]->{sort} eq 'N';
366 return $_[0]->{sort} eq 'L';
369 # for perl 5.004's benefit
373 return @_ ? $self->{sort} = shift : $self->{sort} ;
376 # find out whether we are prepared to believe this callsign on this interface
382 return grep $call eq $_, $self->user->believe;
385 # handle out going messages, immediately without waiting for the select to drop
386 # this could, in theory, block
390 my $conn = $self->{conn};
393 my $call = $self->{call};
397 my @lines = split /\n/;
399 $conn->send_now("$sort$call|$_");
400 # debug log it, but not if it is a log message
401 dbg("-> $sort $call $_") if $sort ne 'L' && isdbg('chan');
408 # send later with letter (more control)
414 my $conn = $self->{conn};
417 my $call = $self->{call};
421 my @lines = split /\n/;
423 $conn->send_later("$sort$call|$_");
424 # debug log it, but not if it is a log message
425 dbg("-> $sort $call $_") if $sort ne 'L' && isdbg('chan');
432 # the normal output routine
434 sub send # this is always later and always data
437 my $conn = $self->{conn};
439 my $call = $self->{call};
442 for (ref $l ? @$l : $l) {
443 my @lines = split /\n/;
445 $conn->send_later("D$call|$_");
446 dbg("-> D $call $_") if isdbg('chan');
450 $self->{t} = $main::systime;
453 # send a file (always later)
456 my ($self, $fn) = @_;
457 my $call = $self->{call};
458 my $conn = $self->{conn};
461 open(F, $fn) or die "can't open $fn for sending file ($!)";
467 # this will implement language independence (in time)
471 return DXM::msg($self->{lang}, @_);
474 # stick a broadcast on the delayed queue (but only up to 20 items)
480 $self->{delayed} = [] unless $self->{delayed};
481 push @{$self->{delayed}}, $s;
482 if (@{$self->{delayed}} >= 20) {
483 shift @{$self->{delayed}}; # lose oldest one
487 # change the state of the channel - lots of scope for debugging here :-)
492 $self->{oldstate} = $self->{state};
493 $self->{state} = shift;
494 $self->{func} = '' unless defined $self->{func};
495 dbg("$self->{call} channel func $self->{func} state $self->{oldstate} -> $self->{state}\n") if isdbg('state');
497 # if there is any queued up broadcasts then splurge them out here
498 if ($self->{delayed} && ($self->{state} eq 'prompt' || $self->{state} eq 'talk')) {
499 $self->send (@{$self->{delayed}});
500 delete $self->{delayed};
503 return $self->{state};
506 # disconnect this channel
510 my $user = $self->{user};
512 $user->close($self->{startt}, $self->{hostname}) if defined $user;
513 $self->{conn}->disconnect if $self->{conn};
518 # just close all the socket connections down without any fiddling about, cleaning, being
519 # nice to other processes and otherwise telling them what is going on.
521 # This is for the benefit of forked processes to prepare for starting new programs, they
522 # don't want or need all this baggage.
528 foreach $ref (values %channels) {
529 $ref->{conn}->disconnect() if $ref->{conn};
534 # Tell all the users that we have come in or out (if they want to know)
538 my ($self, $m, $call) = @_;
540 $call ||= $self->{call};
542 # send info to all logged in thingies
543 my @dxchan = get_all_users();
545 foreach $dxchan (@dxchan) {
546 next if $dxchan == $self;
547 next if $dxchan->{call} eq $main::mycall;
548 $dxchan->send($dxchan->msg($m, $call)) if $dxchan->{logininfo};
553 # Tell all the users if a buddy is logged or out
557 my ($self, $m, $call, $node) = @_;
559 $call ||= $self->{call};
563 # send info to all logged in thingies
564 my @dxchan = get_all_users();
566 foreach $dxchan (@dxchan) {
567 next if $dxchan == $self;
568 next if $dxchan->{call} eq $main::mycall;
569 $dxchan->send($dxchan->msg($m, $call, $node)) if grep $_ eq $call, @{$dxchan->{user}->{buddies}} ;
573 # various access routines
576 # return a list of valid elements
585 # return a prompt for a field
590 my ($self, $ele) = @_;
594 # take a standard input message and decode it into its standard parts
599 my ($sort, $call, $line) = $data =~ /^([A-Z])(#?[A-Z0-9\/\-]{3,25})\|(.*)$/;
601 my $chcall = (ref $dxchan) ? $dxchan->call : "UN.KNOWN";
603 # the above regexp must work
604 unless (defined $sort && defined $call && defined $line) {
605 # $data =~ s/([\x00-\x1f\x7f-\xff])/uc sprintf("%%%02x",ord($1))/eg;
606 dbg("DUFF Line on $chcall: $data");
610 if(ref($dxchan) && $call ne $chcall) {
611 dbg("DUFF Line come in for $call on wrong channel $chcall");
615 return ($sort, $call, $line);
618 # broadcast a message to all clusters taking into account isolation
619 # [except those mentioned after buffer]
622 my $s = shift; # the line to be rebroadcast
623 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
624 my @dxchan = get_all_nodes();
627 # send it if it isn't the except list and isn't isolated and still has a hop count
628 foreach $dxchan (@dxchan) {
629 next if grep $dxchan == $_, @except;
630 next if $dxchan == $main::me;
632 my $routeit = $dxchan->can('adjust_hops') ? $dxchan->adjust_hops($s) : $s; # adjust its hop count by node name
634 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
638 # broadcast a message to all clusters ignoring isolation
639 # [except those mentioned after buffer]
640 sub broadcast_all_nodes
642 my $s = shift; # the line to be rebroadcast
643 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
644 my @dxchan = get_all_nodes();
647 # send it if it isn't the except list and isn't isolated and still has a hop count
648 foreach $dxchan (@dxchan) {
649 next if grep $dxchan == $_, @except;
650 next if $dxchan == $main::me;
652 my $routeit = $dxchan->can('adjust_hops') ? $dxchan->adjust_hops($s) : $s; # adjust its hop count by node name
653 $dxchan->send($routeit);
657 # broadcast to all users
658 # storing the spot or whatever until it is in a state to receive it
661 my $s = shift; # the line to be rebroadcast
662 my $sort = shift; # the type of transmission
663 my $fref = shift; # a reference to an object to filter on
664 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
665 my @dxchan = get_all_users();
669 foreach $dxchan (@dxchan) {
670 next if grep $dxchan == $_, @except;
673 broadcast_list($s, $sort, $fref, @out);
677 # broadcast to a list of users
685 foreach $dxchan (@_) {
687 next if $dxchan == $main::me;
690 next unless $dxchan->{dx};
691 ($filter) = $dxchan->{spotsfilter}->it($fref) if $dxchan->{spotsfilter} && ref $fref;
694 next if $sort eq 'ann' && !$dxchan->{ann} && $s !~ /^To\s+LOCAL\s+de\s+(?:$main::myalias|$main::mycall)/i;
695 next if $sort eq 'wwv' && !$dxchan->{wwv};
696 next if $sort eq 'wcy' && !$dxchan->{wcy};
697 next if $sort eq 'wx' && !$dxchan->{wx};
699 $s =~ s/\a//og unless $dxchan->{beep};
701 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
713 while (my $data = shift @{$self->{inqueue}}) {
714 my ($sort, $call, $line) = $self->decode_input($data);
715 next unless defined $sort;
717 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
718 dbg("<- $sort $call $line") if $sort ne 'D' && isdbg('chan');
721 my $user = $self->user;
723 die "\$user not defined for $call" unless defined $user;
726 $self->normal($line);
727 } elsif ($sort eq 'G') {
728 $self->enhanced($line);
729 } elsif ($sort eq 'A' || $sort eq 'O' || $sort eq 'W') {
730 $self->start($line, $sort);
731 } elsif ($sort eq 'C') {
732 $self->width($line); # change number of columns
733 } elsif ($sort eq 'Z') {
735 } elsif ($sort eq 'D') {
736 ; # ignored (an echo)
738 dbg atime . " DXChannel::process_one: Unknown command letter ($sort) received from $call\n";
745 foreach my $dxchan (values %channels) {
746 next if $dxchan->{disconnecting};
747 $dxchan->process_one;
756 if (DXXml::available()) {
757 $r = $self->{handle_xml} || 0;
759 delete $self->{handle_xml} if exists $self->{handle_xml};
767 my $error = shift || '';
768 dbg("$self->{call} ERROR '$error', closing") if isdbg('chan');
769 $self->{conn}->set_error(undef) if exists $self->{conn};
770 $self->disconnect(1);
778 # the sysop is registered!
779 return 1 if $self->{call} eq $main::myalias || $self->{call} eq $main::mycall;
782 return $self->{registered};
792 my $name = $AUTOLOAD;
793 return if $name =~ /::DESTROY$/;
796 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
798 # this clever line of code creates a subroutine which takes over from autoload
799 # from OO Perl - Conway
800 *$AUTOLOAD = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};