3 # This module impliments the message handling for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
10 # Notes for implementors:-
12 # PC28 field 11 is the RR required flag
13 # PC28 field 12 is a VIA routing (ie it is a node call)
18 @ISA = qw(DXProt DXChannel);
34 use vars qw(%work @msg $msgdir %valid %busy $maxage $last_clean
35 @badmsg $badmsgfn $forwardfn @forward $timeout $waittime);
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
47 $badmsgfn = "$msgdir/badmsg.pl"; # list of TO address we wont store
48 $forwardfn = "$msgdir/forward.pl"; # the forwarding table
51 fromnode => '9,From Node',
52 tonode => '9,To Node',
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',
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',
70 keep => '0,Keep this?,yesno',
71 lastt => '9,Last processed,cldatetime',
72 waitt => '9,Wait until,cldatetime',
82 # allocate a new object
83 # called fromnode, tonode, from, to, datetime, private?, subject, nolinesper
87 my $self = bless {}, $pkg;
88 $self->{msgno} = shift;
91 $self->{to} = ($to eq $main::mycall) ? $main::myalias : $to;
94 $self->{from} = uc $from;
96 $self->{private} = shift;
97 $self->{subject} = shift;
98 $self->{origin} = shift;
99 $self->{'read'} = shift;
100 $self->{rrreq} = shift;
102 $self->{lastt} = $main::systime;
110 delete $ref->{lines};
111 delete $ref->{linesreq};
112 delete $ref->{tonode};
113 delete $ref->{fromnode};
114 delete $ref->{stream};
115 delete $ref->{lines};
117 delete $ref->{count};
118 delete $ref->{lastt} if exists $ref->{lastt};
119 delete $ref->{waitt} if exists $ref->{waitt};
124 my ($self, $line) = @_;
126 # this is periodic processing
127 if (!$self || !$line) {
129 # wander down the work queue stopping any messages that have timed out
133 if (exists $ref->{lastt} && $main::systime > $ref->{lastt} + $timeout) {
134 $ref->stop_msg($node);
136 # delay any outgoing messages that fail
137 $ref->{waitt} = $main::systime + $waittime if $node ne $main::mycall;
141 # clean the message queue
142 clean_old() if $main::systime - $last_clean > 3600 ;
146 my @f = split /\^/, $line;
147 my ($pcno) = $f[0] =~ /^PC(\d\d)/; # just get the number
150 if ($pcno == 28) { # incoming message
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($self->call);
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]);
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
179 if ($pcno == 29) { # incoming text
180 my $ref = $work{"$f[2]$f[3]"};
182 push @{$ref->{lines}}, $f[4];
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");
189 $ref->{lastt} = $main::systime;
194 if ($pcno == 30) { # this is a incoming subject ack
195 my $ref = $work{$f[2]}; # note no stream at this stage
198 $ref->{stream} = $f[3];
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
205 push @{$ref->{lines}}, ($ref->read_msg_body);
206 $ref->send_tranche($self);
207 $ref->{lastt} = $main::systime;
209 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
214 if ($pcno == 31) { # acknowledge a tranche of lines
215 my $ref = $work{"$f[2]$f[3]"};
217 dbg('msg', "tranche ack stream $f[3]\n");
218 $ref->send_tranche($self);
219 $ref->{lastt} = $main::systime;
221 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
226 if ($pcno == 32) { # incoming EOM
227 dbg('msg', "stream $f[3]: EOM received\n");
228 my $ref = $work{"$f[2]$f[3]"};
230 $self->send(DXProt::pc33($f[2], $f[1], $f[3])); # acknowledge it
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
239 $ref->store($ref->{lines});
242 # does an identical message already exist?
245 if ($ref->{subject} eq $m->{subject} && $ref->{t} == $m->{t} && $ref->{from} eq $m->{from}) {
246 $ref->stop_msg($self->call);
247 my $msgno = $m->{msgno};
248 dbg('msg', "duplicate message to $msgno\n");
249 Log('msg', "duplicate message to $msgno");
254 # look for 'bad' to addresses
255 if (grep $ref->{to} eq $_, @badmsg) {
256 $ref->stop_msg($self->call);
257 dbg('msg', "'Bad' TO address $ref->{to}");
258 Log('msg', "'Bad' TO address $ref->{to}");
262 $ref->{msgno} = next_transno("Msgno");
263 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
264 $ref->store($ref->{lines});
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}");
271 $ref->stop_msg($self->call);
274 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
280 if ($pcno == 33) { # acknowledge the end of message
281 my $ref = $work{"$f[2]$f[3]"};
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");
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
291 $ref->stop_msg($self->call);
293 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
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;
307 # create any directories
308 my @part = split /\//, $f[3];
310 my $fn = "$main::root";
311 pop @part; # remove last part
312 foreach $part (@part) {
315 last SWITCH if !mkdir $fn, 0777;
316 dbg('msg', "created directory $fn\n");
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');
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
328 $work{"$f[2]$stream"} = $ref; # store in work
329 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
334 if ($pcno == 42) { # abort transfer
335 dbg('msg', "stream $f[3]: abort received\n");
336 my $ref = $work{"$f[2]$f[3]"};
338 $ref->stop_msg($self->call);
345 if ($pcno == 49) { # global delete on subject
347 if ($_->{from} eq $f[1] && $_->{subject} eq $f[2]) {
349 Log('msg', "Message $_->{msgno} from $_->{from} ($_->{subject}) fully deleted");
350 DXProt::broadcast_ak1a($line, $self);
358 # store a message away on disc or whatever
360 # NOTE the second arg is a REFERENCE not a list
366 # we only proceed if there are actually any lines in the file
367 if (!$lines || @{$lines} == 0) {
371 if ($ref->{file}) { # a file
372 dbg('msg', "To be stored in $ref->{to}\n");
374 my $fh = new IO::File "$ref->{to}", "w";
377 foreach $line (@{$lines}) {
381 dbg('msg', "file $ref->{to} stored\n");
382 Log('msg', "file $ref->{to} from $ref->{from} stored" );
384 confess "can't open file $ref->{to} $!";
386 } else { # a normal message
388 # attempt to open the message file
389 my $fn = filename($ref->{msgno});
391 dbg('msg', "To be stored in $fn\n");
393 # now save the file, overwriting what's there, YES I KNOW OK! (I will change it if it's a problem)
394 my $fh = new IO::File "$fn", "w";
396 my $rr = $ref->{rrreq} ? '1' : '0';
397 my $priv = $ref->{private} ? '1': '0';
398 print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$priv^$ref->{subject}^$ref->{origin}^$ref->{'read'}^$rr\n";
399 print $fh "=== ", join('^', @{$ref->{gotit}}), "\n";
402 foreach $line (@{$lines}) {
403 $ref->{size} += (length $line) + 1;
407 dbg('msg', "msg $ref->{msgno} stored\n");
408 Log('msg', "msg $ref->{msgno} from $ref->{from} to $ref->{to} stored" );
410 confess "can't open msg file $fn $!";
420 # remove it from the active message list
421 @msg = map { $_ != $self ? $_ : () } @msg;
423 # belt and braces (one day I will ask someone if this is REALLY necessary)
424 delete $self->{gotit};
425 delete $self->{list};
428 unlink filename($self->{msgno});
429 dbg('msg', "deleting $self->{msgno}\n");
432 # clean out old messages from the message queue
437 # mark old messages for deletion
438 foreach $ref (@msg) {
439 if (!$ref->{keep} && $ref->{t} < $main::systime - $maxage) {
440 $ref->{deleteme} = 1;
441 delete $ref->{gotit};
443 unlink filename($ref->{msgno});
444 dbg('msg', "deleting old $ref->{msgno}\n");
448 # remove them all from the active message list
449 @msg = map { $_->{deleteme} ? () : $_ } @msg;
450 $last_clean = $main::systime;
453 # read in a message header
463 $file = new IO::File;
464 if (!open($file, $fn)) {
465 print "Error reading $fn $!\n";
469 $line = <$file>; # first line
471 $size -= length $line;
472 if (! $line =~ /^===/o) {
473 print "corrupt first line in $fn ($line)\n";
477 @f = split /\^/, $line;
478 $ref = DXMsg->alloc(@f);
480 $line = <$file>; # second line
482 $size -= length $line;
483 if (! $line =~ /^===/o) {
484 print "corrupt second line in $fn ($line)\n";
489 @f = split /\^/, $line;
490 push @{$ref->{gotit}}, @f;
491 $ref->{size} = $size;
498 # read in a message header
502 my $msgno = $self->{msgno};
505 my $fn = filename($msgno);
508 $file = new IO::File;
509 if (!open($file, $fn)) {
510 print "Error reading $fn $!\n";
513 chomp (@out = <$file>);
516 shift @out if $out[0] =~ /^=== /;
517 shift @out if $out[0] =~ /^=== /;
521 # send a tranche of lines to the other end
524 my ($self, $dxchan) = @_;
526 my $to = $self->{tonode};
527 my $from = $self->{fromnode};
528 my $stream = $self->{stream};
529 my $lines = $self->{lines};
532 for ($i = 0, $c = $self->{count}; $i < $self->{linesreq} && $c < @$lines; $i++, $c++) {
533 push @out, DXProt::pc29($to, $from, $stream, $lines->[$c]);
537 push @out, DXProt::pc32($to, $from, $stream) if $i < $self->{linesreq};
542 # find a message to send out and start the ball rolling
550 my @nodelist = DXProt::get_all_ak1a();
552 # bat down the message list looking for one that needs to go off site and whose
553 # nearest node is not busy.
555 dbg('msg', "queue msg ($sort)\n");
556 foreach $ref (@msg) {
557 # firstly, is it private and unread? if so can I find the recipient
558 # in my cluster node list offsite?
560 # ignore 'delayed' messages until their waiting time has expired
561 if (exists $ref->{waitt}) {
562 next if $ref->{waitt} < $main::systime;
563 delete $ref->{waitt};
566 if ($ref->{private}) {
567 if ($ref->{'read'} == 0) {
568 $clref = DXCluster->get_exact($ref->{to});
569 unless ($clref) { # otherwise look for a homenode
570 my $uref = DXUser->get($ref->{to});
571 my $hnode = $uref->homenode if $uref;
572 $clref = DXCluster->get_exact($hnode) if $hnode;
574 if ($clref && !grep { $clref->{dxchan} == $_ } DXCommandmode::get_all) {
575 $dxchan = $clref->{dxchan};
576 $ref->start_msg($dxchan) if $dxchan && $clref && !get_busy($dxchan->call) && $dxchan->state eq 'normal';
580 # otherwise we are dealing with a bulletin, compare the gotit list with
581 # the nodelist up above, if there are sites that haven't got it yet
582 # then start sending it - what happens when we get loops is anyone's
583 # guess, use (to, from, time, subject) tuple?
585 foreach $noderef (@nodelist) {
586 next if $noderef->call eq $main::mycall;
587 next if grep { $_ eq $noderef->call } @{$ref->{gotit}};
588 next unless $ref->forward_it($noderef->call); # check the forwarding file
589 # next if $noderef->isolate; # maybe add code for stuff originated here?
590 # next if DXUser->get( ${$ref->{gotit}}[0] )->isolate; # is the origin isolated?
592 # if we are here we have a node that doesn't have this message
593 $ref->start_msg($noderef) if !get_busy($noderef->call) && $noderef->state eq 'normal';
598 # if all the available nodes are busy then stop
599 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
603 # is there a message for me?
609 foreach $ref (@msg) {
610 # is it for me, private and unread?
611 if ($ref->{to} eq $call && $ref->{private}) {
612 return 1 if !$ref->{'read'};
618 # start the message off on its travels with a PC28
621 my ($self, $dxchan) = @_;
623 dbg('msg', "start msg $self->{msgno}\n");
624 $self->{linesreq} = 5;
626 $self->{tonode} = $dxchan->call;
627 $self->{fromnode} = $main::mycall;
628 $busy{$self->{tonode}} = $self;
629 $work{$self->{tonode}} = $self;
630 $dxchan->send(DXProt::pc28($self->{tonode}, $self->{fromnode}, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $self->{origin}, $self->{rrreq}));
633 # get the ref of a busy node
646 # get the forwarding queue
652 # stop a message from continuing, clean it out, unlock interlocks etc
657 my $stream = $self->{stream} if exists $self->{stream};
660 dbg('msg', "stop msg $self->{msgno} -> node $node\n");
662 delete $work{"$node$stream"} if $stream;
667 # get a new transaction number from the file specified
671 $name =~ s/\W//og; # remove non-word characters
672 my $fn = "$msgdir/$name";
675 my $fh = new IO::File;
676 if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
678 $msgno = $fh->getline;
682 $fh->print("$msgno\n");
683 dbg('msg', "msgno $msgno allocated for $name\n");
686 confess "can't open $fn $!";
691 # initialise the message 'system', read in all the message headers
694 my $dir = new IO::File;
698 # load various control files
699 my @in = load_badmsg();
700 print "@in\n" if @in;
701 @in = load_forward();
702 print "@in\n" if @in;
704 # read in the directory
705 opendir($dir, $msgdir) or confess "can't open $msgdir $!";
706 @dir = readdir($dir);
711 next unless /^m\d+$/o;
713 $ref = read_msg_header("$msgdir/$_");
716 # delete any messages to 'badmsg.pl' places
717 if (grep $ref->{to} eq $_, @badmsg) {
718 dbg('msg', "'Bad' TO address $ref->{to}");
719 Log('msg', "'Bad' TO address $ref->{to}");
724 # add the message to the available queue
729 # add the message to the directory listing
733 confess "tried to add a non-ref to the msg directory" if !ref $ref;
737 # return all the current messages
743 # get a particular message
748 return $_ if $_->{msgno} == $msgno;
749 last if $_->{msgno} > $msgno;
754 # return the official filename for a message no
757 return sprintf "$msgdir/m%06d", shift;
761 # return a list of valid elements
770 # return a prompt for a field
775 my ($self, $ele) = @_;
780 # send a message state machine
787 if ($self->state eq 'send1') {
789 confess "local var gone missing" if !ref $self->{loc};
790 my $loc = $self->{loc};
791 $loc->{subject} = $line;
793 $self->state('sendbody');
794 #push @out, $self->msg('sendbody');
795 push @out, $self->msg('m8');
796 } elsif ($self->state eq 'sendbody') {
797 confess "local var gone missing" if !ref $self->{loc};
798 my $loc = $self->{loc};
799 if ($line eq "\032" || uc $line eq "/EX") {
802 if (@{$loc->{lines}} > 0) {
803 foreach $to (@{$loc->{to}}) {
805 my $systime = $main::systime;
806 my $mycall = $main::mycall;
807 $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
816 $ref->store($loc->{lines});
818 push @out, $self->msg('m11', $ref->{msgno}, $to);
819 #push @out, "msgno $ref->{msgno} sent to $to";
820 my $dxchan = DXChannel->get(uc $to);
822 if ($dxchan->is_user()) {
823 $dxchan->send($dxchan->msg('m9'));
828 delete $loc->{lines};
834 $self->state('prompt');
835 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
836 #push @out, $self->msg('sendabort');
837 push @out, $self->msg('m10');
838 delete $loc->{lines};
842 $self->state('prompt');
845 # i.e. it ain't and end or abort, therefore store the line
846 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
852 # return the standard directory line for this ref
856 return sprintf "%6d%s%s%5d %8.8s %8.8s %-6.6s %5.5s %-30.30s",
857 $ref->msgno, $ref->read ? '-' : ' ', $ref->private ? 'p' : ' ', $ref->size,
858 $ref->to, $ref->from, cldate($ref->t), ztime($ref->t), $ref->subject;
861 # load the forward table
865 do "$forwardfn" if -e "$forwardfn";
870 # load the bad message table
874 do "$badmsgfn" if -e "$badmsgfn";
880 # forward that message or not according to the forwarding table
881 # returns 1 for forward, 0 - to ignore
890 for ($i = 0; $i < @forward; $i += 5) {
891 my ($sort, $field, $pattern, $action, $bbs) = @forward[$i..($i+4)];
895 last if $ref->{private} && $sort ne 'P';
896 last if !$ref->{private} && $sort ne 'B';
899 $tested = $ref->{to} if $field eq 'T';
900 $tested = $ref->{from} if $field eq 'F';
901 $tested = $ref->{origin} if $field eq 'O';
902 $tested = $ref->{subject} if $field eq 'S';
904 if (!$pattern || $tested =~ m{$pattern}i) {
905 return 0 if $action eq 'I';
906 return 1 if !$bbs || grep $_ eq $call, @{$bbs};
916 my $name = $AUTOLOAD;
917 return if $name =~ /::DESTROY$/;
920 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
921 @_ ? $self->{$name} = shift : $self->{$name} ;