1. added the start of script files on login/startup. You can now add
[spider.git] / perl / DXChannel.pm
1 #
2 # module to manage channel lists & data
3 #
4 # This is the base class for all channel operations, which is everything to do 
5 # with input and output really.
6 #
7 # The instance variable in the outside world will be generally be called $dxchan
8 #
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.
13 #
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)
17 #
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). 
21 #
22 # Copyright (c) 1998-2000 - Dirk Koopman G1TLH
23 #
24 # $Id$
25 #
26 package DXChannel;
27
28 use Msg;
29 use DXM;
30 use DXUtil;
31 use DXVars;
32 use DXDebug;
33 use Filter;
34 use Prefix;
35
36 use strict;
37 use vars qw(%channels %valid @ISA $count);
38
39 %channels = ();
40 $count = 0;
41
42 %valid = (
43                   call => '0,Callsign',
44                   conn => '9,Msg Conn ref',
45                   user => '9,DXUser ref',
46                   startt => '0,Start Time,atime',
47                   t => '9,Time,atime',
48                   pc50_t => '5,Last PC50 Time,atime',
49                   priv => '9,Privilege',
50                   state => '0,Current State',
51                   oldstate => '5,Last State',
52                   list => '9,Dep Chan List',
53                   name => '0,User Name',
54                   consort => '5,Connection Type',
55                   'sort' => '5,Type of Channel',
56                   wwv => '0,Want WWV,yesno',
57                   wcy => '0,Want WCY,yesno',
58                   wx => '0,Want WX,yesno',
59                   talk => '0,Want Talk,yesno',
60                   ann => '0,Want Announce,yesno',
61                   here => '0,Here?,yesno',
62                   conf => '0,In Conference?,yesno',
63                   dx => '0,DX Spots,yesno',
64                   redirect => '0,Redirect messages to',
65                   lang => '0,Language',
66                   func => '5,Function',
67                   loc => '9,Local Vars', # used by func to store local variables in
68                   beep => '0,Want Beeps,yesno',
69                   lastread => '5,Last Msg Read',
70                   outbound => '5,outbound?,yesno',
71                   remotecmd => '9,doing rcmd,yesno',
72                   pagelth => '0,Page Length',
73                   pagedata => '9,Page Data Store',
74                   group => '0,Access Group,parray',     # used to create a group of users/nodes for some purpose or other
75                   isolate => '5,Isolate network,yesno',
76                   delayed => '5,Delayed messages,parray',
77                   annfilter => '5,Ann Filt-out',
78                   wwvfilter => '5,WWV Filt-out',
79                   wcyfilter => '5,WCY Filt-out',
80                   spotsfilter => '5,Spot Filt-out',
81                   routefilter => '5,Route Filt-out',
82                   inannfilter => '5,Ann Filt-inp',
83                   inwwvfilter => '5,WWV Filt-inp',
84                   inwcyfilter => '5,WCY Filt-inp',
85                   inspotsfilter => '5,Spot Filt-inp',
86                   inroutefilter => '5,Route Filt-inp',
87                   passwd => '9,Passwd List,parray',
88                   pingint => '5,Ping Interval ',
89                   nopings => '5,Ping Obs Count',
90                   lastping => '5,Ping last sent,atime',
91                   pingtime => '5,Ping totaltime,parray',
92                   pingave => '0,Ping ave time',
93                   logininfo => '9,Login info req,yesno',
94                   talklist => '0,Talk List,parray',
95                   cluster => '5,Cluster data',
96                   isbasic => '9,Internal Connection', 
97                   errors => '9,Errors',
98                   route => '9,Route Data',
99                   dxcc => '0,Country Code',
100                   itu => '0,ITU Zone',
101                   cq => '0,CQ Zone',
102                   enhanced => '5,Enhanced Client,yesno',
103                   senddbg => '8,Sending Debug,yesno',
104                   width => '0,Column Width',
105                   disconnecting => '9,Disconnecting,yesno',
106                   ann_talk => '0,Suppress Talk Anns,yesno',
107                  );
108
109 use vars qw($VERSION $BRANCH);
110 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
111 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ ) || 0;
112 $main::build += $VERSION;
113 $main::branch += $BRANCH;
114
115 # object destruction
116 sub DESTROY
117 {
118         my $self = shift;
119         for (keys %$self) {
120                 if (ref($self->{$_})) {
121                         delete $self->{$_};
122                 }
123         }
124         dbg("DXChannel $self->{call} destroyed ($count)") if isdbg('chan');
125         $count--;
126 }
127
128 # create a new channel object [$obj = DXChannel->new($call, $msg_conn_obj, $user_obj)]
129 sub alloc
130 {
131         my ($pkg, $call, $conn, $user) = @_;
132         my $self = {};
133   
134         die "trying to create a duplicate channel for $call" if $channels{$call};
135         $self->{call} = $call;
136         $self->{priv} = 0;
137         $self->{conn} = $conn if defined $conn; # if this isn't defined then it must be a list
138         if (defined $user) {
139                 $self->{user} = $user;
140                 $self->{lang} = $user->lang;
141                 $user->new_group() if !$user->group;
142                 $self->{group} = $user->group;
143                 $self->{sort} = $user->sort;
144         }
145         $self->{startt} = $self->{t} = time;
146         $self->{state} = 0;
147         $self->{oldstate} = 0;
148         $self->{lang} = $main::lang if !$self->{lang};
149         $self->{func} = "";
150
151         # add in all the dxcc, itu, zone info
152         my @dxcc = Prefix::extract($call);
153         if (@dxcc > 0) {
154                 $self->{dxcc} = $dxcc[1]->dxcc;
155                 $self->{itu} = $dxcc[1]->itu;
156                 $self->{cq} = $dxcc[1]->cq;                                             
157         }
158
159         $count++;
160         dbg("DXChannel $self->{call} created ($count)") if isdbg('chan');
161         bless $self, $pkg; 
162         return $channels{$call} = $self;
163 }
164
165 # obtain a channel object by callsign [$obj = DXChannel->get($call)]
166 sub get
167 {
168         my ($pkg, $call) = @_;
169         return $channels{$call};
170 }
171
172 # obtain all the channel objects
173 sub get_all
174 {
175         my ($pkg) = @_;
176         return values(%channels);
177 }
178
179 #
180 # gimme all the ak1a nodes
181 #
182 sub get_all_nodes
183 {
184         my $ref;
185         my @out;
186         foreach $ref (values %channels) {
187                 push @out, $ref if $ref->is_node;
188         }
189         return @out;
190 }
191
192 # return a list of all users
193 sub get_all_users
194 {
195         my $ref;
196         my @out;
197         foreach $ref (values %channels) {
198                 push @out, $ref if $ref->is_user;
199         }
200         return @out;
201 }
202
203 # return a list of all user callsigns
204 sub get_all_user_calls
205 {
206         my $ref;
207         my @out;
208         foreach $ref (values %channels) {
209                 push @out, $ref->{call} if $ref->is_user;
210         }
211         return @out;
212 }
213
214 # obtain a channel object by searching for its connection reference
215 sub get_by_cnum
216 {
217         my ($pkg, $conn) = @_;
218         my $self;
219   
220         foreach $self (values(%channels)) {
221                 return $self if ($self->{conn} == $conn);
222         }
223         return undef;
224 }
225
226 # get rid of a channel object [$obj->del()]
227 sub del
228 {
229         my $self = shift;
230
231         $self->{group} = undef;         # belt and braces
232         delete $channels{$self->{call}};
233 }
234
235 # is it a bbs
236 sub is_bbs
237 {
238         my $self = shift;
239         return $self->{'sort'} eq 'B';
240 }
241
242 sub is_node
243 {
244         my $self = shift;
245         return $self->{'sort'} =~ /[ACRSX]/;
246 }
247 # is it an ak1a node ?
248 sub is_ak1a
249 {
250         my $self = shift;
251         return $self->{'sort'} eq 'A';
252 }
253
254 # is it a user?
255 sub is_user
256 {
257         my $self = shift;
258         return $self->{'sort'} eq 'U';
259 }
260
261 # is it a clx node
262 sub is_clx
263 {
264         my $self = shift;
265         return $self->{'sort'} eq 'C';
266 }
267
268 # is it a spider node
269 sub is_spider
270 {
271         my $self = shift;
272         return $self->{'sort'} eq 'S';
273 }
274
275 # is it a DXNet node
276 sub is_dxnet
277 {
278         my $self = shift;
279         return $self->{'sort'} eq 'X';
280 }
281
282 # is it a ar-cluster node
283 sub is_arcluster
284 {
285         my $self = shift;
286         return $self->{'sort'} eq 'R';
287 }
288
289 # for perl 5.004's benefit
290 sub sort
291 {
292         my $self = shift;
293         return @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
294 }
295
296 # handle out going messages, immediately without waiting for the select to drop
297 # this could, in theory, block
298 sub send_now
299 {
300         my $self = shift;
301         my $conn = $self->{conn};
302         return unless $conn;
303         my $sort = shift;
304         my $call = $self->{call};
305         
306         for (@_) {
307 #               chomp;
308         my @lines = split /\n/;
309                 for (@lines) {
310                         $conn->send_now("$sort$call|$_");
311                         dbg("-> $sort $call $_") if isdbg('chan');
312                 }
313         }
314         $self->{t} = time;
315 }
316
317 #
318 # send later with letter (more control)
319 #
320
321 sub send_later
322 {
323         my $self = shift;
324         my $conn = $self->{conn};
325         return unless $conn;
326         my $sort = shift;
327         my $call = $self->{call};
328         
329         for (@_) {
330 #               chomp;
331         my @lines = split /\n/;
332                 for (@lines) {
333                         $conn->send_later("$sort$call|$_");
334                         dbg("-> $sort $call $_") if isdbg('chan');
335                 }
336         }
337         $self->{t} = time;
338 }
339
340 #
341 # the normal output routine
342 #
343 sub send                                                # this is always later and always data
344 {
345         my $self = shift;
346         my $conn = $self->{conn};
347         return unless $conn;
348         my $call = $self->{call};
349
350         for (@_) {
351 #               chomp;
352         my @lines = split /\n/;
353                 for (@lines) {
354                         $conn->send_later("D$call|$_");
355                         dbg("-> D $call $_") if isdbg('chan');
356                 }
357         }
358         $self->{t} = time;
359 }
360
361 # send a file (always later)
362 sub send_file
363 {
364         my ($self, $fn) = @_;
365         my $call = $self->{call};
366         my $conn = $self->{conn};
367         my @buf;
368   
369         open(F, $fn) or die "can't open $fn for sending file ($!)";
370         @buf = <F>;
371         close(F);
372         $self->send(@buf);
373 }
374
375 # this will implement language independence (in time)
376 sub msg
377 {
378         my $self = shift;
379         return DXM::msg($self->{lang}, @_);
380 }
381
382 # stick a broadcast on the delayed queue (but only up to 20 items)
383 sub delay
384 {
385         my $self = shift;
386         my $s = shift;
387         
388         $self->{delayed} = [] unless $self->{delayed};
389         push @{$self->{delayed}}, $s;
390         if (@{$self->{delayed}} >= 20) {
391                 shift @{$self->{delayed}};   # lose oldest one
392         }
393 }
394
395 # change the state of the channel - lots of scope for debugging here :-)
396 sub state
397 {
398         my $self = shift;
399         if (@_) {
400                 $self->{oldstate} = $self->{state};
401                 $self->{state} = shift;
402                 $self->{func} = '' unless defined $self->{func};
403                 dbg("$self->{call} channel func $self->{func} state $self->{oldstate} -> $self->{state}\n") if isdbg('state');
404
405                 # if there is any queued up broadcasts then splurge them out here
406                 if ($self->{delayed} && ($self->{state} eq 'prompt' || $self->{state} eq 'talk')) {
407                         $self->send (@{$self->{delayed}});
408                         delete $self->{delayed};
409                 }
410         }
411         return $self->{state};
412 }
413
414 # disconnect this channel
415 sub disconnect
416 {
417         my $self = shift;
418         my $user = $self->{user};
419         
420         $user->close() if defined $user;
421         $self->{conn}->disconnect;
422         $self->del();
423 }
424
425 #
426 # just close all the socket connections down without any fiddling about, cleaning, being
427 # nice to other processes and otherwise telling them what is going on.
428 #
429 # This is for the benefit of forked processes to prepare for starting new programs, they
430 # don't want or need all this baggage.
431 #
432
433 sub closeall
434 {
435         my $ref;
436         foreach $ref (values %channels) {
437                 $ref->{conn}->disconnect() if $ref->{conn};
438         }
439 }
440
441 #
442 # Tell all the users that we have come in or out (if they want to know)
443 #
444 sub tell_login
445 {
446         my ($self, $m) = @_;
447         
448         # send info to all logged in thingies
449         my @dxchan = get_all_users();
450         my $dxchan;
451         foreach $dxchan (@dxchan) {
452                 next if $dxchan == $self;
453                 next if $dxchan->{call} eq $main::mycall;
454                 $dxchan->send($dxchan->msg($m, $self->{call})) if $dxchan->{logininfo};
455         }
456 }
457
458 # various access routines
459
460 #
461 # return a list of valid elements 
462
463
464 sub fields
465 {
466         return keys(%valid);
467 }
468
469 #
470 # return a prompt for a field
471 #
472
473 sub field_prompt
474
475         my ($self, $ele) = @_;
476         return $valid{$ele};
477 }
478
479 # take a standard input message and decode it into its standard parts
480 sub decode_input
481 {
482         my $dxchan = shift;
483         my $data = shift;
484         my ($sort, $call, $line) = $data =~ /^([A-Z])([A-Z0-9\-]{3,9})\|(.*)$/;
485
486         my $chcall = (ref $dxchan) ? $dxchan->call : "UN.KNOWN";
487         
488         # the above regexp must work
489         unless (defined $sort && defined $call && defined $line) {
490 #               $data =~ s/([\x00-\x1f\x7f-\xff])/uc sprintf("%%%02x",ord($1))/eg;
491                 dbg("DUFF Line on $chcall: $data") if isdbg('err');
492                 return ();
493         }
494
495         if(ref($dxchan) && $call ne $chcall) {
496                 dbg("DUFF Line come in for $call on wrong channel $chcall") if isdbg('err');
497                 return();
498         }
499         
500         return ($sort, $call, $line);
501 }
502
503 no strict;
504 sub AUTOLOAD
505 {
506         my $self = shift;
507         my $name = $AUTOLOAD;
508         return if $name =~ /::DESTROY$/;
509         $name =~ s/.*:://o;
510   
511         confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
512
513         # this clever line of code creates a subroutine which takes over from autoload
514         # from OO Perl - Conway
515         *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
516     @_ ? $self->{$name} = shift : $self->{$name} ;
517 }
518
519
520 1;
521 __END__;