1. added WWV filtering
[spider.git] / perl / DXMsg.pm
1 #!/usr/bin/perl
2 #
3 # This module impliments the message handling for a dx cluster
4 #
5 # Copyright (c) 1998 Dirk Koopman G1TLH
6 #
7 # $Id$
8 #
9 #
10 # Notes for implementors:-
11 #
12 # PC28 field 11 is the RR required flag
13 # PC28 field 12 is a VIA routing (ie it is a node call) 
14 #
15
16 package DXMsg;
17
18 @ISA = qw(DXProt DXChannel);
19
20 use DXUtil;
21 use DXChannel;
22 use DXUser;
23 use DXM;
24 use DXCluster;
25 use DXProtVars;
26 use DXProtout;
27 use DXDebug;
28 use DXLog;
29 use IO::File;
30 use Fcntl;
31 use Carp;
32
33 use strict;
34 use vars qw(%work @msg $msgdir %valid %busy $maxage $last_clean
35                         @badmsg $badmsgfn $forwardfn @forward $timeout $waittime);
36
37 %work = ();                                             # outstanding jobs
38 @msg = ();                                              # messages we have
39 %busy = ();                                             # station interlocks
40 $msgdir = "$main::root/msg";    # directory contain the msgs
41 $maxage = 30 * 86400;                   # the maximum age that a message shall live for if not marked 
42 $last_clean = 0;                                # last time we did a clean
43 @forward = ();                  # msg forward table
44 $timeout = 30*60;               # forwarding timeout
45 $waittime = 60*60;              # time an aborted outgoing message waits before trying again
46
47 $badmsgfn = "$msgdir/badmsg.pl";  # list of TO address we wont store
48 $forwardfn = "$msgdir/forward.pl";  # the forwarding table
49
50 %valid = (
51                   fromnode => '9,From Node',
52                   tonode => '9,To Node',
53                   to => '0,To',
54                   from => '0,From',
55                   t => '0,Msg Time,cldatetime',
56                   private => '9,Private',
57                   subject => '0,Subject',
58                   linesreq => '0,Lines per Gob',
59                   rrreq => '9,Read Confirm',
60                   origin => '0,Origin',
61                   lines => '5,Data',
62                   stream => '9,Stream No',
63                   count => '9,Gob Linecnt',
64                   file => '9,File?,yesno',
65                   gotit => '9,Got it Nodes,parray',
66                   lines => '9,Lines,parray',
67                   'read' => '9,Times read',
68                   size => '0,Size',
69                   msgno => '0,Msgno',
70                   keep => '0,Keep this?,yesno',
71                   lastt => '9,Last processed,cldatetime',
72                   waitt => '9,Wait until,cldatetime',
73                  );
74
75 sub DESTROY
76 {
77         my $self = shift;
78         undef $self->{lines};
79         undef $self->{gotit};
80 }
81
82 # allocate a new object
83 # called fromnode, tonode, from, to, datetime, private?, subject, nolinesper  
84 sub alloc                  
85 {
86         my $pkg = shift;
87         my $self = bless {}, $pkg;
88         $self->{msgno} = shift;
89         my $to = shift;
90         #  $to =~ s/-\d+$//o;
91         $self->{to} = ($to eq $main::mycall) ? $main::myalias : $to;
92         my $from = shift;
93         $from =~ s/-\d+$//o;
94         $self->{from} = uc $from;
95         $self->{t} = shift;
96         $self->{private} = shift;
97         $self->{subject} = shift;
98         $self->{origin} = shift;
99         $self->{'read'} = shift;
100         $self->{rrreq} = shift;
101         $self->{gotit} = [];
102         $self->{lastt} = $main::systime;
103     
104         return $self;
105 }
106
107 sub workclean
108 {
109         my $ref = shift;
110         delete $ref->{lines};
111         delete $ref->{linesreq};
112         delete $ref->{tonode};
113         delete $ref->{fromnode};
114         delete $ref->{stream};
115         delete $ref->{lines};
116         delete $ref->{file};
117         delete $ref->{count};
118         delete $ref->{lastt} if exists $ref->{lastt};
119         delete $ref->{waitt} if exists $ref->{waitt};
120 }
121
122 sub process
123 {
124         my ($self, $line) = @_;
125
126         # this is periodic processing
127         if (undef $self || undef $line) {
128
129                 # wander down the work queue stopping any messages that have timed out
130                 for (keys %work) {
131                         my $ref = $work{$_};
132                         if ($main::systime > $ref->{lastt} + $timeout) {
133                                 my $tonode = $ref->{tonode};
134                                 $ref->stop_msg();
135
136                                 # delay any outgoing messages that fail
137                                 $ref->{waitt} = $main::systime + $waittime if $tonode ne $main::mycall;
138                         }
139                 }
140                 
141                 # clean the message queue
142                 clean_old() if $main::systime - $last_clean > 3600 ;
143                 return;
144         }
145
146         my @f = split /\^/, $line;
147         my ($pcno) = $f[0] =~ /^PC(\d\d)/; # just get the number
148
149  SWITCH: {
150                 if ($pcno == 28) {              # incoming message
151
152                         # first look for any messages in the busy queue 
153                         # and cancel them this should both resolve timed out incoming messages
154                         # and crossing of message between nodes, incoming messages have priority
155                         if (exists $busy{$f[2]}) {
156                                 my $ref = $busy{$f[2]};
157                                 my $tonode = $ref->{tonode};
158                                 $ref->stop_msg();
159                         }
160
161                         my $t = cltounix($f[5], $f[6]);
162                         my $stream = next_transno($f[2]);
163                         my $ref = DXMsg->alloc($stream, uc $f[3], $f[4], $t, $f[7], $f[8], $f[13], '0', $f[11]);
164                         
165                         # fill in various forwarding state variables
166                         $ref->{fromnode} = $f[2];
167                         $ref->{tonode} = $f[1];
168                         $ref->{rrreq} = $f[11];
169                         $ref->{linesreq} = $f[10];
170                         $ref->{stream} = $stream;
171                         $ref->{count} = 0;      # no of lines between PC31s
172                         dbg('msg', "new message from $f[4] to $f[3] '$f[8]' stream $stream\n");
173                         $work{"$f[2]$stream"} = $ref; # store in work
174                         $busy{$f[2]} = $ref; # set interlock
175                         $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
176                         last SWITCH;
177                 }
178                 
179                 if ($pcno == 29) {              # incoming text
180                         my $ref = $work{"$f[2]$f[3]"};
181                         if ($ref) {
182                                 push @{$ref->{lines}}, $f[4];
183                                 $ref->{count}++;
184                                 if ($ref->{count} >= $ref->{linesreq}) {
185                                         $self->send(DXProt::pc31($f[2], $f[1], $f[3]));
186                                         dbg('msg', "stream $f[3]: $ref->{count} lines received\n");
187                                         $ref->{count} = 0;
188                                 }
189                                 $ref->{lastt} = $main::systime;
190                         }
191                         last SWITCH;
192                 }
193                 
194                 if ($pcno == 30) {              # this is a incoming subject ack
195                         my $ref = $work{$f[2]}; # note no stream at this stage
196                         if ($ref) {
197                                 delete $work{$f[2]};
198                                 $ref->{stream} = $f[3];
199                                 $ref->{count} = 0;
200                                 $ref->{linesreq} = 5;
201                                 $work{"$f[2]$f[3]"} = $ref;     # new ref
202                                 dbg('msg', "incoming subject ack stream $f[3]\n");
203                                 $busy{$f[2]} = $ref; # interlock
204                                 $ref->{lines} = [];
205                                 push @{$ref->{lines}}, ($ref->read_msg_body);
206                                 $ref->send_tranche($self);
207                                 $ref->{lastt} = $main::systime;
208                         } else {
209                                 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
210                         } 
211                         last SWITCH;
212                 }
213                 
214                 if ($pcno == 31) {              # acknowledge a tranche of lines
215                         my $ref = $work{"$f[2]$f[3]"};
216                         if ($ref) {
217                                 dbg('msg', "tranche ack stream $f[3]\n");
218                                 $ref->send_tranche($self);
219                                 $ref->{lastt} = $main::systime;
220                         } else {
221                                 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
222                         } 
223                         last SWITCH;
224                 }
225                 
226                 if ($pcno == 32) {              # incoming EOM
227                         dbg('msg', "stream $f[3]: EOM received\n");
228                         my $ref = $work{"$f[2]$f[3]"};
229                         if ($ref) {
230                                 $self->send(DXProt::pc33($f[2], $f[1], $f[3])); # acknowledge it
231                                 
232                                 # get the next msg no - note that this has NOTHING to do with the stream number in PC protocol
233                                 # store the file or message
234                                 # remove extraneous rubbish from the hash
235                                 # remove it from the work in progress vector
236                                 # stuff it on the msg queue
237                                 if ($ref->{lines} && @{$ref->{lines}} > 0) { # ignore messages with 0 lines
238                                         if ($ref->{file}) {
239                                                 $ref->store($ref->{lines});
240                                         } else {
241
242                                                 # does an identical message already exist?
243                                                 my $m;
244                                                 for $m (@msg) {
245                                                         if ($ref->{subject} eq $m->{subject} && $ref->{t} == $m->{t} && $ref->{from} eq $m->{from}) {
246                                                                 $ref->stop_msg();
247                                                                 my $msgno = $m->{msgno};
248                                                                 dbg('msg', "duplicate message to $msgno\n");
249                                                                 Log('msg', "duplicate message to $msgno");
250                                                                 return;
251                                                         }
252                                                 }
253                                                         
254                                                 # look for 'bad' to addresses 
255                                                 if (grep $ref->{to} eq $_, @badmsg) {
256                                                         $ref->stop_msg();
257                                                         dbg('msg', "'Bad' TO address $ref->{to}");
258                                                         Log('msg', "'Bad' TO address $ref->{to}");
259                                                         return;
260                                                 }
261
262                                                 $ref->{msgno} = next_transno("Msgno");
263                                                 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
264                                                 $ref->store($ref->{lines});
265                                                 add_dir($ref);
266                                                 my $dxchan = DXChannel->get($ref->{to});
267                                                 $dxchan->send($dxchan->msg('m9')) if $dxchan;
268                                                 Log('msg', "Message $ref->{msgno} from $ref->{from} received from $f[2] for $ref->{to}");
269                                         }
270                                 }
271                                 $ref->stop_msg();
272                                 queue_msg(0);
273                         } else {
274                                 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
275                         }
276                         queue_msg(0);
277                         last SWITCH;
278                 }
279                 
280                 if ($pcno == 33) {              # acknowledge the end of message
281                         my $ref = $work{"$f[2]$f[3]"};
282                         if ($ref) {
283                                 if ($ref->{private}) { # remove it if it private and gone off site#
284                                         Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2] and deleted");
285                                         $ref->del_msg;
286                                 } else {
287                                         Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2]");
288                                         push @{$ref->{gotit}}, $f[2]; # mark this up as being received
289                                         $ref->store($ref->{lines});     # re- store the file
290                                 }
291                                 $ref->stop_msg();
292                         } else {
293                                 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
294                         } 
295                         queue_msg(0);
296                         last SWITCH;
297                 }
298                 
299                 if ($pcno == 40) {              # this is a file request
300                         $f[3] =~ s/\\/\//og; # change the slashes
301                         $f[3] =~ s/\.//og;      # remove dots
302                         $f[3] =~ s/^\///o;   # remove the leading /
303                         $f[3] = lc $f[3];       # to lower case;
304                         dbg('msg', "incoming file $f[3]\n");
305                         $f[3] = 'packclus/' . $f[3] unless $f[3] =~ /^packclus\//o;
306                         
307                         # create any directories
308                         my @part = split /\//, $f[3];
309                         my $part;
310                         my $fn = "$main::root";
311                         pop @part;                      # remove last part
312                         foreach $part (@part) {
313                                 $fn .= "/$part";
314                                 next if -e $fn;
315                                 last SWITCH if !mkdir $fn, 0777;
316                                 dbg('msg', "created directory $fn\n");
317                         }
318                         my $stream = next_transno($f[2]);
319                         my $ref = DXMsg->alloc($stream, "$main::root/$f[3]", $self->call, time, !$f[4], $f[3], ' ', '0', '0');
320                         
321                         # forwarding variables
322                         $ref->{fromnode} = $f[1];
323                         $ref->{tonode} = $f[2];
324                         $ref->{linesreq} = $f[5];
325                         $ref->{stream} = $stream;
326                         $ref->{count} = 0;      # no of lines between PC31s
327                         $ref->{file} = 1;
328                         $work{"$f[2]$stream"} = $ref; # store in work
329                         $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack 
330                         
331                         last SWITCH;
332                 }
333                 
334                 if ($pcno == 42) {              # abort transfer
335                         dbg('msg', "stream $f[3]: abort received\n");
336                         my $ref = $work{"$f[2]$f[3]"};
337                         if ($ref) {
338                                 $ref->stop_msg();
339                                 $ref = undef;
340                         }
341                         
342                         last SWITCH;
343                 }
344
345                 if ($pcno == 49) {      # global delete on subject
346                         for (@msg) {
347                                 if ($_->{subject} eq $f[2]) {
348                                         $_->del_msg();
349                                         Log('msg', "Message $_->{msgno} fully deleted by $f[1]");
350                                 }
351                         }
352                 }
353         }
354 }
355
356
357 # store a message away on disc or whatever
358 #
359 # NOTE the second arg is a REFERENCE not a list
360 sub store
361 {
362         my $ref = shift;
363         my $lines = shift;
364         
365         # we only proceed if there are actually any lines in the file
366         if (!$lines || @{$lines} == 0) {
367                 return;
368         }
369         
370         if ($ref->{file}) {                     # a file
371                 dbg('msg', "To be stored in $ref->{to}\n");
372                 
373                 my $fh = new IO::File "$ref->{to}", "w";
374                 if (defined $fh) {
375                         my $line;
376                         foreach $line (@{$lines}) {
377                                 print $fh "$line\n";
378                         }
379                         $fh->close;
380                         dbg('msg', "file $ref->{to} stored\n");
381                         Log('msg', "file $ref->{to} from $ref->{from} stored" );
382                 } else {
383                         confess "can't open file $ref->{to} $!";  
384                 }
385         } else {                                        # a normal message
386
387                 # attempt to open the message file
388                 my $fn = filename($ref->{msgno});
389                 
390                 dbg('msg', "To be stored in $fn\n");
391                 
392                 # now save the file, overwriting what's there, YES I KNOW OK! (I will change it if it's a problem)
393                 my $fh = new IO::File "$fn", "w";
394                 if (defined $fh) {
395                         my $rr = $ref->{rrreq} ? '1' : '0';
396                         my $priv = $ref->{private} ? '1': '0';
397                         print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$priv^$ref->{subject}^$ref->{origin}^$ref->{'read'}^$rr\n";
398                         print $fh "=== ", join('^', @{$ref->{gotit}}), "\n";
399                         my $line;
400                         $ref->{size} = 0;
401                         foreach $line (@{$lines}) {
402                                 $ref->{size} += (length $line) + 1;
403                                 print $fh "$line\n";
404                         }
405                         $fh->close;
406                         dbg('msg', "msg $ref->{msgno} stored\n");
407                         Log('msg', "msg $ref->{msgno} from $ref->{from} to $ref->{to} stored" );
408                 } else {
409                         confess "can't open msg file $fn $!";  
410                 }
411         }
412 }
413
414 # delete a message
415 sub del_msg
416 {
417         my $self = shift;
418         
419         # remove it from the active message list
420         @msg = map { $_ != $self ? $_ : () } @msg;
421         
422         # belt and braces (one day I will ask someone if this is REALLY necessary)
423         delete $self->{gotit};
424         delete $self->{list};
425         
426         # remove the file
427         unlink filename($self->{msgno});
428         dbg('msg', "deleting $self->{msgno}\n");
429 }
430
431 # clean out old messages from the message queue
432 sub clean_old
433 {
434         my $ref;
435         
436         # mark old messages for deletion
437         foreach $ref (@msg) {
438                 if (!$ref->{keep} && $ref->{t} < $main::systime - $maxage) {
439                         $ref->{deleteme} = 1;
440                         delete $ref->{gotit};
441                         delete $ref->{list};
442                         unlink filename($ref->{msgno});
443                         dbg('msg', "deleting old $ref->{msgno}\n");
444                 }
445         }
446         
447         # remove them all from the active message list
448         @msg = map { $_->{deleteme} ? () : $_ } @msg;
449         $last_clean = $main::systime;
450 }
451
452 # read in a message header
453 sub read_msg_header
454
455         my $fn = shift;
456         my $file;
457         my $line;
458         my $ref;
459         my @f;
460         my $size;
461         
462         $file = new IO::File;
463         if (!open($file, $fn)) {
464                 print "Error reading $fn $!\n";
465                 return undef;
466         }
467         $size = -s $fn;
468         $line = <$file>;                        # first line
469         chomp $line;
470         $size -= length $line;
471         if (! $line =~ /^===/o) {
472                 print "corrupt first line in $fn ($line)\n";
473                 return undef;
474         }
475         $line =~ s/^=== //o;
476         @f = split /\^/, $line;
477         $ref = DXMsg->alloc(@f);
478         
479         $line = <$file>;                        # second line
480         chomp $line;
481         $size -= length $line;
482         if (! $line =~ /^===/o) {
483                 print "corrupt second line in $fn ($line)\n";
484                 return undef;
485         }
486         $line =~ s/^=== //o;
487         $ref->{gotit} = [];
488         @f = split /\^/, $line;
489         push @{$ref->{gotit}}, @f;
490         $ref->{size} = $size;
491         
492         close($file);
493         
494         return $ref;
495 }
496
497 # read in a message header
498 sub read_msg_body
499 {
500         my $self = shift;
501         my $msgno = $self->{msgno};
502         my $file;
503         my $line;
504         my $fn = filename($msgno);
505         my @out;
506         
507         $file = new IO::File;
508         if (!open($file, $fn)) {
509                 print "Error reading $fn $!\n";
510                 return undef;
511         }
512         chomp (@out = <$file>);
513         close($file);
514         
515         shift @out if $out[0] =~ /^=== /;
516         shift @out if $out[0] =~ /^=== /;
517         return @out;
518 }
519
520 # send a tranche of lines to the other end
521 sub send_tranche
522 {
523         my ($self, $dxchan) = @_;
524         my @out;
525         my $to = $self->{tonode};
526         my $from = $self->{fromnode};
527         my $stream = $self->{stream};
528         my $lines = $self->{lines};
529         my ($c, $i);
530         
531         for ($i = 0, $c = $self->{count}; $i < $self->{linesreq} && $c < @$lines; $i++, $c++) {
532                 push @out, DXProt::pc29($to, $from, $stream, $lines->[$c]);
533     }
534     $self->{count} = $c;
535
536     push @out, DXProt::pc32($to, $from, $stream) if $i < $self->{linesreq};
537         $dxchan->send(@out);
538 }
539
540         
541 # find a message to send out and start the ball rolling
542 sub queue_msg
543 {
544         my $sort = shift;
545         my $call = shift;
546         my $ref;
547         my $clref;
548         my $dxchan;
549         my @nodelist = DXProt::get_all_ak1a();
550         
551         # bat down the message list looking for one that needs to go off site and whose
552         # nearest node is not busy.
553
554         dbg('msg', "queue msg ($sort)\n");
555         foreach $ref (@msg) {
556                 # firstly, is it private and unread? if so can I find the recipient
557                 # in my cluster node list offsite?
558
559                 # ignore 'delayed' messages until their waiting time has expired
560                 if (exists $ref->{waitt}) {
561                         next if $ref->{waitt} < $main::systime;
562                         delete $ref->{waitt};
563                 } 
564                 
565                 if ($ref->{private}) {
566                         if ($ref->{'read'} == 0) {
567                                 $clref = DXCluster->get_exact($ref->{to});
568                                 unless ($clref) {             # otherwise look for a homenode
569                                         my $uref = DXUser->get($ref->{to});
570                                         my $hnode =  $uref->homenode if $uref;
571                                         $clref = DXCluster->get_exact($hnode) if $hnode;
572                                 }
573                                 if ($clref && !grep { $clref->{dxchan} == $_ } DXCommandmode::get_all) {
574                                         $dxchan = $clref->{dxchan};
575                                         $ref->start_msg($dxchan) if $dxchan && $clref && !get_busy($dxchan->call) && $dxchan->state eq 'normal';
576                                 }
577                         }
578                 } elsif (!$sort) {
579                         # otherwise we are dealing with a bulletin, compare the gotit list with
580                         # the nodelist up above, if there are sites that haven't got it yet
581                         # then start sending it - what happens when we get loops is anyone's
582                         # guess, use (to, from, time, subject) tuple?
583                         my $noderef;
584                         foreach $noderef (@nodelist) {
585                                 next if $noderef->call eq $main::mycall;
586                                 next if grep { $_ eq $noderef->call } @{$ref->{gotit}};
587                                 next unless $ref->forward_it($noderef->call);           # check the forwarding file
588                                 # next if $noderef->isolate;               # maybe add code for stuff originated here?
589                                 # next if DXUser->get( ${$ref->{gotit}}[0] )->isolate;  # is the origin isolated?
590                                 
591                                 # if we are here we have a node that doesn't have this message
592                                 $ref->start_msg($noderef) if !get_busy($noderef->call)  && $noderef->state eq 'normal';
593                                 last;
594                         }
595                 }
596                 
597                 # if all the available nodes are busy then stop
598                 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
599         }
600 }
601
602 # is there a message for me?
603 sub for_me
604 {
605         my $call = uc shift;
606         my $ref;
607         
608         foreach $ref (@msg) {
609                 # is it for me, private and unread? 
610                 if ($ref->{to} eq $call && $ref->{private}) {
611                         return 1 if !$ref->{'read'};
612                 }
613         }
614         return 0;
615 }
616
617 # start the message off on its travels with a PC28
618 sub start_msg
619 {
620         my ($self, $dxchan) = @_;
621         
622         dbg('msg', "start msg $self->{msgno}\n");
623         $self->{linesreq} = 5;
624         $self->{count} = 0;
625         $self->{tonode} = $dxchan->call;
626         $self->{fromnode} = $main::mycall;
627         $busy{$dxchan->call} = $self;
628         $work{"$self->{tonode}"} = $self;
629         $dxchan->send(DXProt::pc28($self->{tonode}, $self->{fromnode}, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $self->{origin}, $self->{rrreq}));
630 }
631
632 # get the ref of a busy node
633 sub get_busy
634 {
635         my $call = shift;
636         return $busy{$call};
637 }
638
639 # get the busy queue
640 sub get_all_busy
641 {
642         return values %busy;
643 }
644
645 # get the forwarding queue
646 sub get_fwq
647 {
648         return values %work;
649 }
650
651 # stop a message from continuing, clean it out, unlock interlocks etc
652 sub stop_msg
653 {
654         my ($self, $dxchan) = @_;
655         my $node = $self->{tonode}
656         my $stream = $self->{stream} if exists $self->{stream};
657         
658         
659         dbg('msg', "stop msg $self->{msgno} -> node $node\n");
660         delete $work{$node};
661         delete $work{"$node$stream"} if $stream;
662         $self->workclean;
663         delete $busy{$node};
664 }
665
666 # get a new transaction number from the file specified
667 sub next_transno
668 {
669         my $name = shift;
670         $name =~ s/\W//og;                      # remove non-word characters
671         my $fn = "$msgdir/$name";
672         my $msgno;
673         
674         my $fh = new IO::File;
675         if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
676                 $fh->autoflush(1);
677                 $msgno = $fh->getline;
678                 chomp $msgno;
679                 $msgno++;
680                 seek $fh, 0, 0;
681                 $fh->print("$msgno\n");
682                 dbg('msg', "msgno $msgno allocated for $name\n");
683                 $fh->close;
684         } else {
685                 confess "can't open $fn $!";
686         }
687         return $msgno;
688 }
689
690 # initialise the message 'system', read in all the message headers
691 sub init
692 {
693         my $dir = new IO::File;
694         my @dir;
695         my $ref;
696
697         # load various control files
698         my @in = load_badmsg();
699         print "@in\n" if @in;
700         @in = load_forward();
701         print "@in\n" if @in;
702
703         # read in the directory
704         opendir($dir, $msgdir) or confess "can't open $msgdir $!";
705         @dir = readdir($dir);
706         closedir($dir);
707
708         @msg = ();
709         for (sort @dir) {
710                 next unless /^m\d+$/o;
711                 
712                 $ref = read_msg_header("$msgdir/$_");
713                 next unless $ref;
714                 
715                 # delete any messages to 'badmsg.pl' places
716                 if (grep $ref->{to} eq $_, @badmsg) {
717                         dbg('msg', "'Bad' TO address $ref->{to}");
718                         Log('msg', "'Bad' TO address $ref->{to}");
719                         $ref->del_msg;
720                         next;
721                 }
722
723                 # add the message to the available queue
724                 add_dir($ref); 
725         }
726 }
727
728 # add the message to the directory listing
729 sub add_dir
730 {
731         my $ref = shift;
732         confess "tried to add a non-ref to the msg directory" if !ref $ref;
733         push @msg, $ref;
734 }
735
736 # return all the current messages
737 sub get_all
738 {
739         return @msg;
740 }
741
742 # get a particular message
743 sub get
744 {
745         my $msgno = shift;
746         for (@msg) {
747                 return $_ if $_->{msgno} == $msgno;
748                 last if $_->{msgno} > $msgno;
749         }
750         return undef;
751 }
752
753 # return the official filename for a message no
754 sub filename
755 {
756         return sprintf "$msgdir/m%06d", shift;
757 }
758
759 #
760 # return a list of valid elements 
761
762
763 sub fields
764 {
765         return keys(%valid);
766 }
767
768 #
769 # return a prompt for a field
770 #
771
772 sub field_prompt
773
774         my ($self, $ele) = @_;
775         return $valid{$ele};
776 }
777
778 #
779 # send a message state machine
780 sub do_send_stuff
781 {
782         my $self = shift;
783         my $line = shift;
784         my @out;
785         
786         if ($self->state eq 'send1') {
787                 #  $DB::single = 1;
788                 confess "local var gone missing" if !ref $self->{loc};
789                 my $loc = $self->{loc};
790                 $loc->{subject} = $line;
791                 $loc->{lines} = [];
792                 $self->state('sendbody');
793                 #push @out, $self->msg('sendbody');
794                 push @out, $self->msg('m8');)
795         } elsif ($self->state eq 'sendbody') {
796                 confess "local var gone missing" if !ref $self->{loc};
797                 my $loc = $self->{loc};
798                 if ($line eq "\032" || uc $line eq "/EX") {
799                         my $to;
800                         
801                         if (@{$loc->{lines}} > 0) {
802                                 foreach $to (@{$loc->{to}}) {
803                                         my $ref;
804                                         my $systime = $main::systime;
805                                         my $mycall = $main::mycall;
806                                         $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
807                                                                                 uc $to,
808                                                                                 $self->call, 
809                                                                                 $systime,
810                                                                                 $loc->{private}, 
811                                                                                 $loc->{subject}, 
812                                                                                 $mycall,
813                                                                                 '0',
814                                                                                 $loc->{rrreq});
815                                         $ref->store($loc->{lines});
816                                         $ref->add_dir();
817                                         push @out, $self->msg('m11', $ref->{msgno}, $to);
818                                         #push @out, "msgno $ref->{msgno} sent to $to";
819                                         my $dxchan = DXChannel->get(uc $to);
820                                         if ($dxchan) {
821                                                 if ($dxchan->is_user()) {
822                                                         $dxchan->send($dxchan->msg('m9'));
823                                                 }
824                                         }
825                                 }
826                         }
827                         delete $loc->{lines};
828                         delete $loc->{to};
829                         delete $self->{loc};
830                         $self->func(undef);
831                         
832                         DXMsg::queue_msg(0);
833                         $self->state('prompt');
834                 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
835                         #push @out, $self->msg('sendabort');
836                         push @out, $self->msg('m10');
837                         delete $loc->{lines};
838                         delete $loc->{to};
839                         delete $self->{loc};
840                         $self->func(undef);
841                         $self->state('prompt');
842                 } else {
843                         
844                         # i.e. it ain't and end or abort, therefore store the line
845                         push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
846                 }
847         }
848         return (1, @out);
849 }
850
851 # return the standard directory line for this ref 
852 sub dir
853 {
854         my $ref = shift;
855         return sprintf "%6d%s%s%5d %8.8s %8.8s %-6.6s %5.5s %-30.30s", 
856                 $ref->msgno, $ref->read ? '-' : ' ', $ref->private ? 'p' : ' ', $ref->size,
857                         $ref->to, $ref->from, cldate($ref->t), ztime($ref->t), $ref->subject;
858 }
859
860 # load the forward table
861 sub load_forward
862 {
863         my @out;
864         do "$forwardfn" if -e "$forwardfn";
865         push @out, $@ if $@;
866         return @out;
867 }
868
869 # load the bad message table
870 sub load_badmsg
871 {
872         my @out;
873         do "$badmsgfn" if -e "$badmsgfn";
874         push @out, $@ if $@;
875         return @out;
876 }
877
878 #
879 # forward that message or not according to the forwarding table
880 # returns 1 for forward, 0 - to ignore
881 #
882
883 sub forward_it
884 {
885         my $ref = shift;
886         my $call = shift;
887         my $i;
888         
889         for ($i = 0; $i < @forward; $i += 5) {
890                 my ($sort, $field, $pattern, $action, $bbs) = @forward[$i..($i+4)]; 
891                 my $tested;
892                 
893                 # are we interested?
894                 last if $ref->{private} && $sort ne 'P';
895                 last if !$ref->{private} && $sort ne 'B';
896                 
897                 # select field
898                 $tested = $ref->{to} if $field eq 'T';
899                 $tested = $ref->{from} if $field eq 'F';
900                 $tested = $ref->{origin} if $field eq 'O';
901                 $tested = $ref->{subject} if $field eq 'S';
902
903                 if (!$pattern || $tested =~ m{$pattern}i) {
904                         return 0 if $action eq 'I';
905                         return 1 if !$bbs || grep $_ eq $call, @{$bbs};
906                 }
907         }
908         return 0;
909 }
910
911 no strict;
912 sub AUTOLOAD
913 {
914         my $self = shift;
915         my $name = $AUTOLOAD;
916         return if $name =~ /::DESTROY$/;
917         $name =~ s/.*:://o;
918         
919         confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
920         @_ ? $self->{$name} = shift : $self->{$name} ;
921 }
922
923 1;
924
925 __END__