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
40 use vars qw(%channels %valid @ISA $count $maxerrors);
46 'sort' => '5,Type of Channel',
47 ann => '0,Want Announce,yesno',
48 ann_talk => '0,Suppress Talk Anns,yesno',
49 annfilter => '5,Ann Filt-out',
50 badcount => '1,Bad Word Count',
51 badip => '9,BAD IP address',
52 beep => '0,Want Beeps,yesno',
53 build => '1,Node Build',
55 cmdcount => '5,Cmds in cmdinterval',
56 cmdintstart => '5,Cmd int start,atime',
57 cluster => '5,Cluster data',
58 conf => '0,In Conference?,yesno',
59 conn => '9,Msg Conn ref',
60 consort => '5,Connection Type',
62 delayed => '5,Delayed messages,parray',
63 disconnecting => '9,Disconnecting,yesno',
64 do_pc9x => '9,Handles PC9x,yesno',
65 dx => '0,DX Spots,yesno',
66 dxcc => '0,Country Code',
67 edit => '7,Edit Function',
68 enhanced => '5,Enhanced Client,yesno',
71 group => '0,Access Group,parray',
72 gtk => '5,Using GTK,yesno',
73 handle_xml => '9,Handles XML,yesno',
74 here => '0,Here?,yesno',
75 hostname => '0,Hostname',
76 inannfilter => '5,Ann Filt-inp',
77 inpc92filter => '5,PC92 Route Filt-inp',
78 inqueue => '9,Input Queue,parray',
79 inrbnfilter => '5,RBN Filt-inp',
80 inroutefilter => '5,Route Filt-inp',
81 inscript => '9,In a script,yesno',
82 inspotsfilter => '5,Spot Filt-inp',
83 inwcyfilter => '5,WCY Filt-inp',
84 inwwvfilter => '5,WWV Filt-inp',
85 isbasic => '9,Internal Connection',
86 isolate => '5,Isolate network,yesno',
87 isslugged => '9,Still Slugged,yesno',
89 K => '9,Seen on PC92 K,yesno',
91 lastmsgpoll => '0,Last Msg Poll,atime',
92 lastping => '5,Ping last sent,atime',
93 lastread => '5,Last Msg Read',
94 list => '9,Dep Chan List',
95 loc => '9,Local Vars', # used by func to store local variables in
96 logininfo => '9,Login info req,yesno',
97 metric => '1,Route metric',
98 name => '0,User Name',
99 newroute => '1,New Style Routing,yesno',
100 next_pc92_keepalive => '9,Next PC92 KeepAlive,atime',
101 next_pc92_update => '9,Next PC92 Update,atime',
102 nopings => '5,Ping Obs Count',
103 oldstate => '5,Last State',
104 outbound => '5,outbound?,yesno',
105 pagedata => '9,Page Data Store',
106 pagelth => '0,Page Length',
107 passwd => '9,Passwd List,yesno',
108 pc50_t => '5,Last PC50 Time,atime',
109 pc92filter => '5,PC92 Route Filt-out',
110 pingave => '0,Ping ave time',
111 pingint => '5,Ping Interval ',
112 pingtime => '5,Ping totaltime,parray',
113 priv => '9,Privilege',
114 prompt => '0,Required Prompt',
115 rbnfilter => '5,RBN Filt-out',
116 rbnseeme => '0,RBN See Me,yesno',
117 redirect => '0,Redirect messages to',
118 registered => '9,Registered?,yesno',
119 remotecmd => '9,doing rcmd,yesno',
120 route => '9,Route Data',
121 routefilter => '5,Route Filt-out',
122 senddbg => '8,Sending Debug,yesno',
123 sluggedpcs => '9,Slugged PCxx Queue,parray',
124 spotsfilter => '5,Spot Filt-out',
125 startt => '0,Start Time,atime',
126 state => '0,Current State',
128 talk => '0,Want Talk,yesno',
129 talklist => '0,Talk List,parray',
130 user => '9,DXUser ref',
131 user_interval => '0,Prompt Idle Time',
132 ve7cc => '0,VE7CC program special,yesno',
133 verified => '9,Verified?,yesno',
134 version => '1,Node Version',
135 wcy => '0,Want WCY,yesno',
136 wcyfilter => '5,WCY Filt-out',
137 width => '0,Column Width',
138 wwv => '0,Want WWV,yesno',
139 wwvfilter => '5,WWV Filt-out',
140 wx => '0,Want WX,yesno',
143 $maxerrors = 10; # the maximum number of concurrent errors allowed before disconnection
150 if (ref($self->{$_})) {
154 dbg("DXChannel $self->{call} destroyed ($count)") if isdbg('chan');
158 # create a new channel object [$obj = DXChannel->new($call, $msg_conn_obj, $user_obj)]
161 my ($pkg, $call, $conn, $user) = @_;
164 die "trying to create a duplicate channel for $call" if $channels{$call};
165 $self->{call} = $call;
167 $self->{conn} = $conn if defined $conn; # if this isn't defined then it must be a list
169 $self->{user} = $user;
170 $self->{lang} = $user->lang;
171 $user->new_group unless $user->group;
172 $user->new_buddies unless $user->buddies;
173 $self->{group} = $user->group;
174 $self->{sort} = $user->sort;
175 $self->{width} = $user->width;
177 $self->{startt} = $self->{t} = $main::systime;
179 $self->{oldstate} = 0;
180 $self->{lang} = $main::lang if !$self->{lang};
182 $self->{width} ||= 80;
183 $self->{_nospawn} = 0;
185 # add in all the dxcc, itu, zone info
186 my @dxcc = Prefix::extract($call);
188 $self->{dxcc} = $dxcc[1]->dxcc;
189 $self->{itu} = $dxcc[1]->itu;
190 $self->{cq} = $dxcc[1]->cq;
192 $self->{inqueue} = [];
195 $self->{hostname} = $self->{conn}->peerhost;
196 $self->{sockhost} = $self->{conn}->sockhost;
200 dbg("DXChannel $self->{call} created ($count)") if isdbg('chan');
202 return $channels{$call} = $self;
205 # count errors and disconnect if too many
206 # this has to be here because it can come from rcmd (DXProt) as
207 # well as DXCommandmode.
212 if ($self != $main::me && ++$self->{errors} > $maxerrors) {
213 $self->send($self->msg('e26'));
214 LogDbg('err', "DXChannel $self->{call}: too many errors ($self->{errors} > $maxerrors), disconnecting");
218 return ($e ? $self->msg($e) : '');
222 # rebless this channel as something else
227 return $channels{$self->{call}} = bless $self, $class;
232 my ($self, $msg) = @_;
234 # queue the message and the channel object for later processing
236 push @{$self->{inqueue}}, $msg;
241 # obtain a channel object by callsign [$obj = DXChannel::get($call)]
245 return $channels{$call};
248 # obtain all the channel objects
251 return values(%channels);
255 # gimme all the ak1a nodes
261 foreach $ref (values %channels) {
262 push @out, $ref if $ref->is_node;
267 # return a list of node calls
268 sub get_all_node_calls
272 foreach $ref (values %channels) {
273 push @out, $ref->{call} if $ref->is_node;
278 # return a list of all users
283 foreach $ref (values %channels) {
284 push @out, $ref if $ref->is_user;
289 # return a list of all user callsigns
290 sub get_all_user_calls
294 foreach $ref (values %channels) {
295 push @out, $ref->{call} if $ref->is_user;
300 # obtain a channel object by searching for its connection reference
303 my ($pkg, $conn) = @_;
306 foreach $self (values(%channels)) {
307 return $self if ($self->{conn} == $conn);
312 # get rid of a channel object [$obj->del()]
317 $self->{group} = undef; # belt and braces
318 delete $channels{$self->{call}};
324 return $_[0]->{sort} eq 'B';
329 return $_[0]->{sort} =~ /^[ACRSX]$/;
331 # is it an ak1a node ?
334 return $_[0]->{sort} eq 'A';
340 return $_[0]->{sort} =~ /^[UW]$/;
346 return $_[0]->{sort} eq 'C';
349 # it is a Web connected user
352 return $_[0]->{sort} eq 'W';
355 # is it a spider node
358 return $_[0]->{sort} eq 'S';
364 return $_[0]->{sort} eq 'X';
367 # is it a ar-cluster node
370 return $_[0]->{sort} eq 'R';
375 return $_[0]->{sort} eq 'N';
380 return $_[0]->{sort} eq 'L';
383 # for perl 5.004's benefit
387 return @_ ? $self->{sort} = shift : $self->{sort} ;
390 # find out whether we are prepared to believe this callsign on this interface
396 return grep $call eq $_, $self->user->believe;
399 # handle out going messages, immediately without waiting for the select to drop
400 # this could, in theory, block
404 my $conn = $self->{conn};
407 my $call = $self->{call};
411 my @lines = split /\n/;
413 $conn->send_now("$sort$call|$_");
414 # debug log it, but not if it is a log message
415 dbg("-> $sort $call $_") if $sort ne 'L' && isdbg('chan');
422 # send later with letter (more control)
428 my $conn = $self->{conn};
431 my $call = $self->{call};
435 my @lines = split /\n/;
437 $conn->send_later("$sort$call|$_");
438 # debug log it, but not if it is a log message
439 dbg("-> $sort $call $_") if $sort ne 'L' && isdbg('chan');
446 # the normal output routine
448 sub send # this is always later and always data
451 my $conn = $self->{conn};
453 my $call = $self->{call};
456 for (ref $l ? @$l : $l) {
457 my @lines = split /\n/;
459 $conn->send_later("D$call|$_");
460 dbg("-> D $call $_") if isdbg('chan');
464 $self->{t} = $main::systime;
467 # send a file (always later)
470 my ($self, $fn) = @_;
471 my $call = $self->{call};
472 my $conn = $self->{conn};
475 open(F, $fn) or die "can't open $fn for sending file ($!)";
481 # this will implement language independence (in time)
485 return DXM::msg($self->{lang}, @_);
488 # stick a broadcast on the delayed queue (but only up to 20 items)
494 $self->{delayed} = [] unless $self->{delayed};
495 push @{$self->{delayed}}, $s;
496 if (@{$self->{delayed}} >= 20) {
497 shift @{$self->{delayed}}; # lose oldest one
501 # change the state of the channel - lots of scope for debugging here :-)
506 $self->{oldstate} = $self->{state};
507 $self->{state} = shift;
508 $self->{func} = '' unless defined $self->{func};
509 dbg("$self->{call} channel func $self->{func} state $self->{oldstate} -> $self->{state}\n") if isdbg('state');
511 # if there is any queued up broadcasts then splurge them out here
512 if ($self->{delayed} && ($self->{state} eq 'prompt' || $self->{state} eq 'talk')) {
513 $self->send (@{$self->{delayed}});
514 delete $self->{delayed};
517 return $self->{state};
520 # disconnect this channel
524 my $user = $self->{user};
526 $user->close($self->{startt}, $self->{hostname}) if defined $user;
527 $self->{conn}->disconnect if $self->{conn};
532 # just close all the socket connections down without any fiddling about, cleaning, being
533 # nice to other processes and otherwise telling them what is going on.
535 # This is for the benefit of forked processes to prepare for starting new programs, they
536 # don't want or need all this baggage.
542 foreach $ref (values %channels) {
543 $ref->{conn}->disconnect() if $ref->{conn};
548 # Tell all the users that we have come in or out (if they want to know)
552 my ($self, $m, $call) = @_;
554 $call ||= $self->{call};
556 # send info to all logged in thingies
557 my @dxchan = get_all_users();
559 foreach $dxchan (@dxchan) {
560 next if $dxchan == $self;
561 next if $dxchan->{call} eq $main::mycall;
562 $dxchan->send($dxchan->msg($m, $call)) if $dxchan->{logininfo};
567 # Tell all the users if a buddy is logged or out
571 my ($self, $m, $call, $node) = @_;
573 $call ||= $self->{call};
577 # send info to all logged in thingies
578 my @dxchan = get_all_users();
580 foreach $dxchan (@dxchan) {
581 next if $dxchan == $self;
582 next if $dxchan->{call} eq $main::mycall;
583 $dxchan->send($dxchan->msg($m, $call, $node)) if grep $_ eq $call, @{$dxchan->{user}->{buddies}} ;
587 # various access routines
590 # return a list of valid elements
599 # return a prompt for a field
604 my ($self, $ele) = @_;
608 # take a standard input message and decode it into its standard parts
613 my ($sort, $call, $line) = $data =~ /^([A-Z])(#?[A-Z0-9\/\-]{3,25})\|(.*)$/;
615 my $chcall = (ref $dxchan) ? $dxchan->call : "UN.KNOWN";
617 # the above regexp must work
618 unless (defined $sort && defined $call && defined $line) {
619 # $data =~ s/([\x00-\x1f\x7f-\xff])/uc sprintf("%%%02x",ord($1))/eg;
620 dbg("DUFF Line on $chcall: $data");
624 if(ref($dxchan) && $call ne $chcall) {
625 dbg("DUFF Line come in for $call on wrong channel $chcall");
629 return ($sort, $call, $line);
632 # broadcast a message to all clusters taking into account isolation
633 # [except those mentioned after buffer]
636 my $s = shift; # the line to be rebroadcast
637 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
638 my @dxchan = get_all_nodes();
641 # send it if it isn't the except list and isn't isolated and still has a hop count
642 foreach $dxchan (@dxchan) {
643 next if grep $dxchan == $_, @except;
644 next if $dxchan == $main::me;
646 my $routeit = $dxchan->can('adjust_hops') ? $dxchan->adjust_hops($s) : $s; # adjust its hop count by node name
648 $dxchan->send($routeit) unless $dxchan->{isolate} || !$routeit;
652 # broadcast a message to all clusters ignoring isolation
653 # [except those mentioned after buffer]
654 sub broadcast_all_nodes
656 my $s = shift; # the line to be rebroadcast
657 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
658 my @dxchan = get_all_nodes();
661 # send it if it isn't the except list and isn't isolated and still has a hop count
662 foreach $dxchan (@dxchan) {
663 next if grep $dxchan == $_, @except;
664 next if $dxchan == $main::me;
666 my $routeit = $dxchan->can('adjust_hops') ? $dxchan->adjust_hops($s) : $s; # adjust its hop count by node name
667 $dxchan->send($routeit);
671 # broadcast to all users
672 # storing the spot or whatever until it is in a state to receive it
675 my $s = shift; # the line to be rebroadcast
676 my $sort = shift; # the type of transmission
677 my $fref = shift; # a reference to an object to filter on
678 my @except = @_; # to all channels EXCEPT these (dxchannel refs)
679 my @dxchan = get_all_users();
683 foreach $dxchan (@dxchan) {
684 next if grep $dxchan == $_, @except;
687 broadcast_list($s, $sort, $fref, @out);
691 # broadcast to a list of users
699 foreach $dxchan (@_) {
701 next if $dxchan == $main::me;
704 next unless $dxchan->{dx};
705 ($filter) = $dxchan->{spotsfilter}->it($fref) if $dxchan->{spotsfilter} && ref $fref;
708 next if $sort eq 'ann' && !$dxchan->{ann} && $s !~ /^To\s+LOCAL\s+de\s+(?:$main::myalias|$main::mycall)/i;
709 next if $sort eq 'wwv' && !$dxchan->{wwv};
710 next if $sort eq 'wcy' && !$dxchan->{wcy};
711 next if $sort eq 'wx' && !$dxchan->{wx};
713 $s =~ s/\a//og unless $dxchan->{beep};
715 if ($dxchan->{state} eq 'prompt' || $dxchan->{state} eq 'talk') {
727 while (my $data = shift @{$self->{inqueue}}) {
728 my ($sort, $call, $line) = $self->decode_input($data);
729 next unless defined $sort;
733 if (($self->is_rbn && isdbg('rbnchan')) || !$self->is_rbn) {
734 dbg("<- $sort $call $line") if isdbg('chan'); # you may think this is tautology, but it's needed get the correct label on the debug line
740 my $user = $self->user;
742 die "\$user not defined for $call" unless defined $user;
745 $self->normal($line);
746 } elsif ($sort eq 'G') {
747 $self->enhanced($line);
748 } elsif ($sort eq 'A' || $sort eq 'O' || $sort eq 'W') {
749 $self->start($line, $sort);
750 } elsif ($sort eq 'C') {
751 $self->width($line); # change number of columns
752 } elsif ($sort eq 'Z') {
754 } elsif ($sort eq 'D') {
755 ; # ignored (an echo)
757 dbg atime . " DXChannel::process_one: Unknown command letter ($sort) received from $call\n";
764 foreach my $dxchan (values %channels) {
765 next if $dxchan->{disconnecting};
766 $dxchan->process_one;
775 if (DXXml::available()) {
776 $r = $self->{handle_xml} || 0;
778 delete $self->{handle_xml} if exists $self->{handle_xml};
786 my $error = shift || '';
787 dbg("$self->{call} ERROR '$error', closing") if isdbg('chan');
788 $self->{conn}->set_error(undef) if exists $self->{conn};
789 $self->disconnect(1);
796 return unless $call && $user && ref $user;
797 my $self = DXChannel::get($call);
798 $self->{user} = $user;
806 # the sysop is registered!
807 return 1 if $self->{call} eq $main::myalias || $self->{call} eq $main::mycall;
810 return $self->{registered};
816 # try to replace the in-core version of the dxchan with the one that has been modified with items like filters
822 $call ||= $self->{call};
823 $channels{$call} = $self;
831 my $name = $AUTOLOAD;
832 return if $name =~ /::DESTROY$/;
835 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
837 # this clever line of code creates a subroutine which takes over from autoload
838 # from OO Perl - Conway
839 *$AUTOLOAD = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};