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',
85 K => '9,Seen on PC92 K,yesno',
87 lastmsgpoll => '0,Last Msg Poll,atime',
88 lastping => '5,Ping last sent,atime',
89 lastread => '5,Last Msg Read',
90 list => '9,Dep Chan List',
91 loc => '9,Local Vars', # used by func to store local variables in
92 logininfo => '9,Login info req,yesno',
93 metric => '1,Route metric',
94 name => '0,User Name',
95 newroute => '1,New Style Routing,yesno',
96 next_pc92_keepalive => '9,Next PC92 KeepAlive,atime',
97 next_pc92_update => '9,Next PC92 Update,atime',
98 nopings => '5,Ping Obs Count',
99 oldstate => '5,Last State',
100 outbound => '5,outbound?,yesno',
101 pagedata => '9,Page Data Store',
102 pagelth => '0,Page Length',
103 passwd => '9,Passwd List,yesno',
104 pc50_t => '5,Last PC50 Time,atime',
105 pc92filter => '5,PC92 Route Filt-out',
106 pingave => '0,Ping ave time',
107 pingint => '5,Ping Interval ',
108 pingtime => '5,Ping totaltime,parray',
109 priv => '9,Privilege',
110 prompt => '0,Required Prompt',
111 rbnfilter => '5,RBN Filt-out',
112 redirect => '0,Redirect messages to',
113 registered => '9,Registered?,yesno',
114 remotecmd => '9,doing rcmd,yesno',
115 route => '9,Route Data',
116 routefilter => '5,Route Filt-out',
117 senddbg => '8,Sending Debug,yesno',
118 sluggedpcs => '9,Slugged PCxx Queue,parray',
119 spotsfilter => '5,Spot Filt-out',
120 startt => '0,Start Time,atime',
121 state => '0,Current State',
123 talk => '0,Want Talk,yesno',
124 talklist => '0,Talk List,parray',
125 user => '9,DXUser ref',
126 ve7cc => '0,VE7CC program special,yesno',
127 verified => '9,Verified?,yesno',
128 version => '1,Node Version',
129 wcy => '0,Want WCY,yesno',
130 wcyfilter => '5,WCY Filt-out',
131 width => '0,Column Width',
132 wwv => '0,Want WWV,yesno',
133 wwvfilter => '5,WWV Filt-out',
134 wx => '0,Want WX,yesno',
137 $maxerrors = 20; # the maximum number of concurrent errors allowed before disconnection
144 if (ref($self->{$_})) {
148 dbg("DXChannel $self->{call} destroyed ($count)") if isdbg('chan');
152 # create a new channel object [$obj = DXChannel->new($call, $msg_conn_obj, $user_obj)]
155 my ($pkg, $call, $conn, $user) = @_;
158 die "trying to create a duplicate channel for $call" if $channels{$call};
159 $self->{call} = $call;
161 $self->{conn} = $conn if defined $conn; # if this isn't defined then it must be a list
163 $self->{user} = $user;
164 $self->{lang} = $user->lang;
165 $user->new_group unless $user->group;
166 $user->new_buddies unless $user->buddies;
167 $self->{group} = $user->group;
168 $self->{sort} = $user->sort;
169 $self->{width} = $user->width;
171 $self->{startt} = $self->{t} = $main::systime;
173 $self->{oldstate} = 0;
174 $self->{lang} = $main::lang if !$self->{lang};
176 $self->{width} ||= 80;
178 # add in all the dxcc, itu, zone info
179 my @dxcc = Prefix::extract($call);
181 $self->{dxcc} = $dxcc[1]->dxcc;
182 $self->{itu} = $dxcc[1]->itu;
183 $self->{cq} = $dxcc[1]->cq;
185 $self->{inqueue} = [];
188 dbg("DXChannel $self->{call} created ($count)") if isdbg('chan');
190 return $channels{$call} = $self;
193 # count errors and disconnect if too many
194 # this has to be here because it can come from rcmd (DXProt) as
195 # well as DXCommandmode.
200 if (++$self->{errors} > $maxerrors) {
201 $self->send($self->msg('e26'));
205 return ($self->msg($e));
209 # rebless this channel as something else
214 return $channels{$self->{call}} = bless $self, $class;
219 my ($self, $msg) = @_;
221 # queue the message and the channel object for later processing
223 push @{$self->{inqueue}}, $msg;
228 # obtain a channel object by callsign [$obj = DXChannel::get($call)]
232 return $channels{$call};
235 # obtain all the channel objects
238 return values(%channels);
242 # gimme all the ak1a nodes
248 foreach $ref (values %channels) {
249 push @out, $ref if $ref->is_node;
254 # return a list of node calls
255 sub get_all_node_calls
259 foreach $ref (values %channels) {
260 push @out, $ref->{call} if $ref->is_node;
265 # return a list of all users
270 foreach $ref (values %channels) {
271 push @out, $ref if $ref->is_user;
276 # return a list of all user callsigns
277 sub get_all_user_calls
281 foreach $ref (values %channels) {
282 push @out, $ref->{call} if $ref->is_user;
287 # obtain a channel object by searching for its connection reference
290 my ($pkg, $conn) = @_;
293 foreach $self (values(%channels)) {
294 return $self if ($self->{conn} == $conn);
299 # get rid of a channel object [$obj->del()]
304 $self->{group} = undef; # belt and braces
305 delete $channels{$self->{call}};
311 return $_[0]->{sort} eq 'B';
316 return $_[0]->{sort} =~ /^[ACRSX]$/;
318 # is it an ak1a node ?
321 return $_[0]->{sort} eq 'A';
327 return $_[0]->{sort} =~ /^[UW]$/;
333 return $_[0]->{sort} eq 'C';
336 # it is a Web connected user
339 return $_[0]->{sort} eq 'W';
342 # is it a spider node
345 return $_[0]->{sort} eq 'S';
351 return $_[0]->{sort} eq 'X';
354 # is it a ar-cluster node
357 return $_[0]->{sort} eq 'R';
362 return $_[0]->{sort} eq 'N';
367 return $_[0]->{sort} eq 'L';
370 # for perl 5.004's benefit
374 return @_ ? $self->{sort} = shift : $self->{sort} ;
377 # find out whether we are prepared to believe this callsign on this interface
383 return grep $call eq $_, $self->user->believe;
386 # handle out going messages, immediately without waiting for the select to drop
387 # this could, in theory, block
391 my $conn = $self->{conn};
394 my $call = $self->{call};
398 my @lines = split /\n/;
400 $conn->send_now("$sort$call|$_");
401 # debug log it, but not if it is a log message
402 dbg("-> $sort $call $_") if $sort ne 'L' && isdbg('chan');
409 # send later with letter (more control)
415 my $conn = $self->{conn};
418 my $call = $self->{call};
422 my @lines = split /\n/;
424 $conn->send_later("$sort$call|$_");
425 # debug log it, but not if it is a log message
426 dbg("-> $sort $call $_") if $sort ne 'L' && isdbg('chan');
433 # the normal output routine
435 sub send # this is always later and always data
438 my $conn = $self->{conn};
440 my $call = $self->{call};
443 for (ref $l ? @$l : $l) {
444 my @lines = split /\n/;
446 $conn->send_later("D$call|$_");
447 dbg("-> D $call $_") if isdbg('chan');
451 $self->{t} = $main::systime;
454 # send a file (always later)
457 my ($self, $fn) = @_;
458 my $call = $self->{call};
459 my $conn = $self->{conn};
462 open(F, $fn) or die "can't open $fn for sending file ($!)";
468 # this will implement language independence (in time)
472 return DXM::msg($self->{lang}, @_);
475 # stick a broadcast on the delayed queue (but only up to 20 items)
481 $self->{delayed} = [] unless $self->{delayed};
482 push @{$self->{delayed}}, $s;
483 if (@{$self->{delayed}} >= 20) {
484 shift @{$self->{delayed}}; # lose oldest one
488 # change the state of the channel - lots of scope for debugging here :-)
493 $self->{oldstate} = $self->{state};
494 $self->{state} = shift;
495 $self->{func} = '' unless defined $self->{func};
496 dbg("$self->{call} channel func $self->{func} state $self->{oldstate} -> $self->{state}\n") if isdbg('state');
498 # if there is any queued up broadcasts then splurge them out here
499 if ($self->{delayed} && ($self->{state} eq 'prompt' || $self->{state} eq 'talk')) {
500 $self->send (@{$self->{delayed}});
501 delete $self->{delayed};
504 return $self->{state};
507 # disconnect this channel
511 my $user = $self->{user};
513 $user->close($self->{startt}, $self->{hostname}) if defined $user;
514 $self->{conn}->disconnect if $self->{conn};
519 # just close all the socket connections down without any fiddling about, cleaning, being
520 # nice to other processes and otherwise telling them what is going on.
522 # This is for the benefit of forked processes to prepare for starting new programs, they
523 # don't want or need all this baggage.
529 foreach $ref (values %channels) {
530 $ref->{conn}->disconnect() if $ref->{conn};
535 # Tell all the users that we have come in or out (if they want to know)
539 my ($self, $m, $call) = @_;
541 $call ||= $self->{call};
543 # send info to all logged in thingies
544 my @dxchan = get_all_users();
546 foreach $dxchan (@dxchan) {
547 next if $dxchan == $self;
548 next if $dxchan->{call} eq $main::mycall;
549 $dxchan->send($dxchan->msg($m, $call)) if $dxchan->{logininfo};
554 # Tell all the users if a buddy is logged or out
558 my ($self, $m, $call, $node) = @_;
560 $call ||= $self->{call};
564 # send info to all logged in thingies
565 my @dxchan = get_all_users();
567 foreach $dxchan (@dxchan) {
568 next if $dxchan == $self;
569 next if $dxchan->{call} eq $main::mycall;
570 $dxchan->send($dxchan->msg($m, $call, $node)) if grep $_ eq $call, @{$dxchan->{user}->{buddies}} ;
574 # various access routines
577 # return a list of valid elements
586 # return a prompt for a field
591 my ($self, $ele) = @_;
595 # take a standard input message and decode it into its standard parts
600 my ($sort, $call, $line) = $data =~ /^([A-Z])(#?[A-Z0-9\/\-]{3,25})\|(.*)$/;
602 my $chcall = (ref $dxchan) ? $dxchan->call : "UN.KNOWN";
604 # the above regexp must work
605 unless (defined $sort && defined $call && defined $line) {
606 # $data =~ s/([\x00-\x1f\x7f-\xff])/uc sprintf("%%%02x",ord($1))/eg;
607 dbg("DUFF Line on $chcall: $data");
611 if(ref($dxchan) && $call ne $chcall) {
612 dbg("DUFF Line come in for $call on wrong channel $chcall");
616 return ($sort, $call, $line);
619 # broadcast a message to all clusters taking into account isolation
620 # [except those mentioned after buffer]
623 my $s = shift; # the line to be rebroadcast
624 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
625 my @dxchan = get_all_nodes();
628 # send it if it isn't the except list and isn't isolated and still has a hop count
629 foreach $dxchan (@dxchan) {
630 next if grep $dxchan == $_, @except;
631 next if $dxchan == $main::me;
633 my $routeit = $dxchan->can('adjust_hops') ? $dxchan->adjust_hops($s) : $s; # adjust its hop count by node name
635 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
639 # broadcast a message to all clusters ignoring isolation
640 # [except those mentioned after buffer]
641 sub broadcast_all_nodes
643 my $s = shift; # the line to be rebroadcast
644 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
645 my @dxchan = get_all_nodes();
648 # send it if it isn't the except list and isn't isolated and still has a hop count
649 foreach $dxchan (@dxchan) {
650 next if grep $dxchan == $_, @except;
651 next if $dxchan == $main::me;
653 my $routeit = $dxchan->can('adjust_hops') ? $dxchan->adjust_hops($s) : $s; # adjust its hop count by node name
654 $dxchan->send($routeit);
658 # broadcast to all users
659 # storing the spot or whatever until it is in a state to receive it
662 my $s = shift; # the line to be rebroadcast
663 my $sort = shift; # the type of transmission
664 my $fref = shift; # a reference to an object to filter on
665 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
666 my @dxchan = get_all_users();
670 foreach $dxchan (@dxchan) {
671 next if grep $dxchan == $_, @except;
674 broadcast_list($s, $sort, $fref, @out);
678 # broadcast to a list of users
686 foreach $dxchan (@_) {
688 next if $dxchan == $main::me;
691 next unless $dxchan->{dx};
692 ($filter) = $dxchan->{spotsfilter}->it($fref) if $dxchan->{spotsfilter} && ref $fref;
695 next if $sort eq 'ann' && !$dxchan->{ann} && $s !~ /^To\s+LOCAL\s+de\s+(?:$main::myalias|$main::mycall)/i;
696 next if $sort eq 'wwv' && !$dxchan->{wwv};
697 next if $sort eq 'wcy' && !$dxchan->{wcy};
698 next if $sort eq 'wx' && !$dxchan->{wx};
700 $s =~ s/\a//og unless $dxchan->{beep};
702 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
714 while (my $data = shift @{$self->{inqueue}}) {
715 my ($sort, $call, $line) = $self->decode_input($data);
716 next unless defined $sort;
718 # do the really sexy console interface bit! (Who is going to do the TK interface then?)
719 dbg("<- $sort $call $line") if $sort ne 'D' && isdbg('chan');
722 my $user = $self->user;
724 die "\$user not defined for $call" unless defined $user;
727 $self->normal($line);
728 } elsif ($sort eq 'G') {
729 $self->enhanced($line);
730 } elsif ($sort eq 'A' || $sort eq 'O' || $sort eq 'W') {
731 $self->start($line, $sort);
732 } elsif ($sort eq 'C') {
733 $self->width($line); # change number of columns
734 } elsif ($sort eq 'Z') {
736 } elsif ($sort eq 'D') {
737 ; # ignored (an echo)
739 dbg atime . " DXChannel::process_one: Unknown command letter ($sort) received from $call\n";
746 foreach my $dxchan (values %channels) {
747 next if $dxchan->{disconnecting};
748 $dxchan->process_one;
757 if (DXXml::available()) {
758 $r = $self->{handle_xml} || 0;
760 delete $self->{handle_xml} if exists $self->{handle_xml};
768 my $error = shift || '';
769 dbg("$self->{call} ERROR '$error', closing") if isdbg('chan');
770 $self->{conn}->set_error(undef) if exists $self->{conn};
771 $self->disconnect(1);
779 # the sysop is registered!
780 return 1 if $self->{call} eq $main::myalias || $self->{call} eq $main::mycall;
783 return $self->{registered};
793 my $name = $AUTOLOAD;
794 return if $name =~ /::DESTROY$/;
797 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
799 # this clever line of code creates a subroutine which takes over from autoload
800 # from OO Perl - Conway
801 *$AUTOLOAD = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};