1. Added RCMD for clx
[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 $dxchann
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 by make it smaller and more efficient, then tough). 
21 #
22 # Copyright (c) 1998 - Dirk Koopman G1TLH
23 #
24 # $Id$
25 #
26 package DXChannel;
27
28 use Msg;
29 use DXM;
30 use DXUtil;
31 use DXDebug;
32 use Filter;
33
34 use strict;
35 use vars qw(%channels %valid);
36
37 %channels = ();
38
39 %valid = (
40                   call => '0,Callsign',
41                   conn => '9,Msg Conn ref',
42                   user => '9,DXUser ref',
43                   startt => '0,Start Time,atime',
44                   t => '9,Time,atime',
45                   pc50_t => '5,Last PC50 Time,atime',
46                   priv => '9,Privilege',
47                   state => '0,Current State',
48                   oldstate => '5,Last State',
49                   list => '9,Dep Chan List',
50                   name => '0,User Name',
51                   consort => '5,Connection Type',
52                   'sort' => '5,Type of Channel',
53                   wwv => '0,Want WWV,yesno',
54                   wcy => '0,Want WCY,yesno',
55                   wx => '0,Want WX,yesno',
56                   talk => '0,Want Talk,yesno',
57                   ann => '0,Want Announce,yesno',
58                   here => '0,Here?,yesno',
59                   confmode => '0,In Conference?,yesno',
60                   dx => '0,DX Spots,yesno',
61                   redirect => '0,Redirect messages to',
62                   lang => '0,Language',
63                   func => '5,Function',
64                   loc => '9,Local Vars', # used by func to store local variables in
65                   beep => '0,Want Beeps,yesno',
66                   lastread => '5,Last Msg Read',
67                   outbound => '5,outbound?,yesno',
68                   remotecmd => '9,doing rcmd,yesno',
69                   pagelth => '0,Page Length',
70                   pagedata => '9,Page Data Store',
71                   group => '0,Access Group,parray',     # used to create a group of users/nodes for some purpose or other
72                   isolate => '5,Isolate network,yesno',
73                   delayed => '5,Delayed messages,parray',
74                   annfilter => '5,Announce Filter',
75                   wwvfilter => '5,WWV Filter',
76                   wcyfilter => '5,WCY Filter',
77                   spotfilter => '5,Spot Filter',
78                   inannfilter => '5,Input Ann Filter',
79                   inwwvfilter => '5,Input WWV Filter',
80                   inspotfilter => '5,Input Spot Filter',
81                   passwd => '9,Passwd List,parray',
82                   pingint => '5,Ping Interval ',
83                   nopings => '5,Ping Obs Count',
84                   lastping => '5,Ping last sent,atime',
85                   pingtime => '5,Ping totaltime,parray',
86                   pingave => '0,Ping ave time',
87                   logininfo => '9,Login info req,yesno',
88                  );
89
90 # object destruction
91 sub DESTROY
92 {
93         my $self = shift;
94         undef $self->{user};
95         undef $self->{conn};
96         undef $self->{loc};
97         undef $self->{pagedata};
98         undef $self->{group};
99         undef $self->{delayed};
100         undef $self->{annfilter};
101         undef $self->{wwvfilter};
102         undef $self->{spotfilter};
103         undef $self->{inannfilter};
104         undef $self->{inwwvfilter};
105         undef $self->{inspotfilter};
106         undef $self->{passwd};
107 }
108
109 # create a new channel object [$obj = DXChannel->new($call, $msg_conn_obj, $user_obj)]
110 sub alloc
111 {
112         my ($pkg, $call, $conn, $user) = @_;
113         my $self = {};
114   
115         die "trying to create a duplicate channel for $call" if $channels{$call};
116         $self->{call} = $call;
117         $self->{priv} = 0;
118         $self->{conn} = $conn if defined $conn; # if this isn't defined then it must be a list
119         if (defined $user) {
120                 $self->{user} = $user;
121                 $self->{lang} = $user->lang;
122                 $user->new_group() if !$user->group;
123                 $self->{group} = $user->group;
124                 $self->{sort} = $user->sort;
125         }
126         $self->{startt} = $self->{t} = time;
127         $self->{state} = 0;
128         $self->{oldstate} = 0;
129         $self->{lang} = $main::lang if !$self->{lang};
130         $self->{func} = "";
131
132         # get the filters
133         $self->{spotfilter} = Filter::read_in('spots', $call, 0);
134         $self->{wwvfilter} = Filter::read_in('wwv', $call, 0);
135         $self->{annfilter} = Filter::read_in('ann', $call, 0);
136
137         bless $self, $pkg; 
138         return $channels{$call} = $self;
139 }
140
141 # obtain a channel object by callsign [$obj = DXChannel->get($call)]
142 sub get
143 {
144         my ($pkg, $call) = @_;
145         return $channels{$call};
146 }
147
148 # obtain all the channel objects
149 sub get_all
150 {
151         my ($pkg) = @_;
152         return values(%channels);
153 }
154
155 #
156 # gimme all the ak1a nodes
157 #
158 sub get_all_ak1a
159 {
160         my @list = DXChannel->get_all();
161         my $ref;
162         my @out;
163         foreach $ref (@list) {
164                 push @out, $ref if $ref->is_node;
165         }
166         return @out;
167 }
168
169 # return a list of all users
170 sub get_all_users
171 {
172         my @list = DXChannel->get_all();
173         my $ref;
174         my @out;
175         foreach $ref (@list) {
176                 push @out, $ref if $ref->is_user;
177         }
178         return @out;
179 }
180
181 # return a list of all user callsigns
182 sub get_all_user_calls
183 {
184         my @list = DXChannel->get_all();
185         my $ref;
186         my @out;
187         foreach $ref (@list) {
188                 push @out, $ref->call if $ref->is_user;
189         }
190         return @out;
191 }
192
193 # obtain a channel object by searching for its connection reference
194 sub get_by_cnum
195 {
196         my ($pkg, $conn) = @_;
197         my $self;
198   
199         foreach $self (values(%channels)) {
200                 return $self if ($self->{conn} == $conn);
201         }
202         return undef;
203 }
204
205 # get rid of a channel object [$obj->del()]
206 sub del
207 {
208         my $self = shift;
209
210         $self->{group} = undef;         # belt and braces
211         delete $channels{$self->{call}};
212 }
213
214 # is it a bbs
215 sub is_bbs
216 {
217         my $self = shift;
218         return $self->{'sort'} eq 'B';
219 }
220
221 sub is_node
222 {
223         my $self = shift;
224         return $self->{'sort'} =~ /[ACRSX]/;
225 }
226 # is it an ak1a node ?
227 sub is_ak1a
228 {
229         my $self = shift;
230         return $self->{'sort'} eq 'A';
231 }
232
233 # is it a user?
234 sub is_user
235 {
236         my $self = shift;
237         return $self->{'sort'} eq 'U';
238 }
239
240 # is it a clx node
241 sub is_clx
242 {
243         my $self = shift;
244         return $self->{'sort'} eq 'C';
245 }
246
247 # is it a spider node
248 sub is_spider
249 {
250         my $self = shift;
251         return $self->{'sort'} eq 'S';
252 }
253
254 # is it a DXNet node
255 sub is_dxnet
256 {
257         my $self = shift;
258         return $self->{'sort'} eq 'X';
259 }
260
261 # is it a ar-cluster node
262 sub is_arcluster
263 {
264         my $self = shift;
265         return $self->{'sort'} eq 'R';
266 }
267
268 # for perl 5.004's benefit
269 sub sort
270 {
271         my $self = shift;
272         return @_ ? $self->{'sort'} = shift : $self->{'sort'} ;
273 }
274
275 # handle out going messages, immediately without waiting for the select to drop
276 # this could, in theory, block
277 sub send_now
278 {
279         my $self = shift;
280         my $conn = $self->{conn};
281         return unless $conn;
282         my $sort = shift;
283         my $call = $self->{call};
284         
285         for (@_) {
286                 chomp;
287         my @lines = split /\n/;
288                 for (@lines) {
289                         $conn->send_now("$sort$call|$_");
290                         dbg('chan', "-> $sort $call $_");
291                 }
292         }
293         $self->{t} = time;
294 }
295
296 #
297 # the normal output routine
298 #
299 sub send                                                # this is always later and always data
300 {
301         my $self = shift;
302         my $conn = $self->{conn};
303         return unless $conn;
304         my $call = $self->{call};
305
306         for (@_) {
307                 chomp;
308         my @lines = split /\n/;
309                 for (@lines) {
310                         $conn->send_later("D$call|$_");
311                         dbg('chan', "-> D $call $_");
312                 }
313         }
314         $self->{t} = time;
315 }
316
317 # send a file (always later)
318 sub send_file
319 {
320         my ($self, $fn) = @_;
321         my $call = $self->{call};
322         my $conn = $self->{conn};
323         my @buf;
324   
325         open(F, $fn) or die "can't open $fn for sending file ($!)";
326         @buf = <F>;
327         close(F);
328         $self->send(@buf);
329 }
330
331 # this will implement language independence (in time)
332 sub msg
333 {
334         my $self = shift;
335         return DXM::msg($self->{lang}, @_);
336 }
337
338 # stick a broadcast on the delayed queue (but only up to 20 items)
339 sub delay
340 {
341         my $self = shift;
342         my $s = shift;
343         
344         $self->{delayed} = [] unless $self->{delayed};
345         push @{$self->{delayed}}, $s;
346         if (@{$self->{delayed}} >= 20) {
347                 shift @{$self->{delayed}};   # lose oldest one
348         }
349 }
350
351 # change the state of the channel - lots of scope for debugging here :-)
352 sub state
353 {
354         my $self = shift;
355         if (@_) {
356                 $self->{oldstate} = $self->{state};
357                 $self->{state} = shift;
358                 $self->{func} = '' unless defined $self->{func};
359                 dbg('state', "$self->{call} channel func $self->{func} state $self->{oldstate} -> $self->{state}\n");
360
361                 # if there is any queued up broadcasts then splurge them out here
362                 if ($self->{delayed} && ($self->{state} eq 'prompt' || $self->{state} eq 'convers')) {
363                         $self->send (@{$self->{delayed}});
364                         delete $self->{delayed};
365                 }
366         }
367         return $self->{state};
368 }
369
370 # disconnect this channel
371 sub disconnect
372 {
373         my $self = shift;
374         my $user = $self->{user};
375         my $conn = $self->{conn};
376         my $call = $self->{call};
377     my $nopc39 = shift || 0;
378         
379         $self->finish($nopc39);
380         $conn->send_now("Z$call|bye") if $conn; # this will cause 'client' to disconnect
381         $user->close() if defined $user;
382         $conn->disconnect() if $conn;
383         $self->del();
384 }
385
386 #
387 # just close all the socket connections down without any fiddling about, cleaning, being
388 # nice to other processes and otherwise telling them what is going on.
389 #
390 # This is for the benefit of forked processes to prepare for starting new programs, they
391 # don't want or need all this baggage.
392 #
393
394 sub closeall
395 {
396         my $ref;
397         foreach $ref (values %channels) {
398                 $ref->{conn}->disconnect() if $ref->{conn};
399         }
400 }
401
402 #
403 # Tell all the users that we have come in or out (if they want to know)
404 #
405 sub tell_login
406 {
407         my ($self, $m) = @_;
408         
409         # send info to all logged in thingies
410         my @dxchan = get_all_users();
411         my $dxchan;
412         foreach $dxchan (@dxchan) {
413                 next if $dxchan == $self;
414                 $dxchan->send($dxchan->msg($m, $self->{call})) if $dxchan->{logininfo};
415         }
416 }
417
418 # various access routines
419
420 #
421 # return a list of valid elements 
422
423
424 sub fields
425 {
426         return keys(%valid);
427 }
428
429 #
430 # return a prompt for a field
431 #
432
433 sub field_prompt
434
435         my ($self, $ele) = @_;
436         return $valid{$ele};
437 }
438
439 no strict;
440 sub AUTOLOAD
441 {
442         my $self = shift;
443         my $name = $AUTOLOAD;
444         return if $name =~ /::DESTROY$/;
445         $name =~ s/.*:://o;
446   
447         confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
448         @_ ? $self->{$name} = shift : $self->{$name} ;
449 }
450
451 1;
452 __END__;