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
36 $queueinterval $lastq);
38 %work = (); # outstanding jobs
39 @msg = (); # messages we have
40 %busy = (); # station interlocks
41 $msgdir = "$main::root/msg"; # directory contain the msgs
42 $maxage = 30 * 86400; # the maximum age that a message shall live for if not marked
43 $last_clean = 0; # last time we did a clean
44 @forward = (); # msg forward table
45 $timeout = 30*60; # forwarding timeout
46 $waittime = 60*60; # time an aborted outgoing message waits before trying again
47 $queueinterval = 5*60; # run the queue every 5 minutes
51 $badmsgfn = "$msgdir/badmsg.pl"; # list of TO address we wont store
52 $forwardfn = "$msgdir/forward.pl"; # the forwarding table
55 fromnode => '5,From Node',
56 tonode => '5,To Node',
59 t => '0,Msg Time,cldatetime',
60 private => '5,Private',
61 subject => '0,Subject',
62 linesreq => '0,Lines per Gob',
63 rrreq => '5,Read Confirm',
66 stream => '9,Stream No',
67 count => '5,Gob Linecnt',
68 file => '5,File?,yesno',
69 gotit => '5,Got it Nodes,parray',
70 lines => '5,Lines,parray',
71 'read' => '5,Times read',
74 keep => '0,Keep this?,yesno',
75 lastt => '5,Last processed,cldatetime',
76 waitt => '5,Wait until,cldatetime',
86 # allocate a new object
87 # called fromnode, tonode, from, to, datetime, private?, subject, nolinesper
91 my $self = bless {}, $pkg;
92 $self->{msgno} = shift;
95 $self->{to} = ($to eq $main::mycall) ? $main::myalias : $to;
98 $self->{from} = uc $from;
100 $self->{private} = shift;
101 $self->{subject} = shift;
102 $self->{origin} = shift;
103 $self->{'read'} = shift;
104 $self->{rrreq} = shift;
106 $self->{lastt} = $main::systime;
114 delete $ref->{lines};
115 delete $ref->{linesreq};
116 delete $ref->{tonode};
117 delete $ref->{fromnode};
118 delete $ref->{stream};
119 delete $ref->{lines};
121 delete $ref->{count};
122 delete $ref->{lastt} if exists $ref->{lastt};
123 delete $ref->{waitt} if exists $ref->{waitt};
128 my ($self, $line) = @_;
130 # this is periodic processing
131 if (!$self || !$line) {
133 # wander down the work queue stopping any messages that have timed out
137 if (exists $ref->{lastt} && $main::systime > $ref->{lastt} + $timeout) {
138 dbg('msg', "Timeout, stopping msgno: $ref->{msgno} -> $node");
139 $ref->stop_msg($node);
141 # delay any outgoing messages that fail
142 $ref->{waitt} = $main::systime + $waittime + rand(120) if $node ne $main::mycall;
146 # queue some message if the interval timer has gone off
147 if ($main::systime > $lastq + $queueinterval) {
149 $lastq = $main::systime;
152 # clean the message queue
153 clean_old() if $main::systime - $last_clean > 3600 ;
157 my @f = split /\^/, $line;
158 my ($pcno) = $f[0] =~ /^PC(\d\d)/; # just get the number
161 if ($pcno == 28) { # incoming message
163 # first look for any messages in the busy queue
164 # and cancel them this should both resolve timed out incoming messages
165 # and crossing of message between nodes, incoming messages have priority
166 if (exists $busy{$f[2]}) {
167 my $ref = $busy{$f[2]};
168 my $tonode = $ref->{tonode};
169 dbg('msg', "Busy, stopping msgno: $ref->{msgno} -> $f[2]");
170 $ref->stop_msg($self->call);
173 my $t = cltounix($f[5], $f[6]);
174 my $stream = next_transno($f[2]);
175 my $ref = DXMsg->alloc($stream, uc $f[3], $f[4], $t, $f[7], $f[8], $f[13], '0', $f[11]);
177 # fill in various forwarding state variables
178 $ref->{fromnode} = $f[2];
179 $ref->{tonode} = $f[1];
180 $ref->{rrreq} = $f[11];
181 $ref->{linesreq} = $f[10];
182 $ref->{stream} = $stream;
183 $ref->{count} = 0; # no of lines between PC31s
184 dbg('msg', "new message from $f[4] to $f[3] '$f[8]' stream $stream\n");
185 $work{"$f[2]$stream"} = $ref; # store in work
186 $busy{$f[2]} = $ref; # set interlock
187 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
188 $ref->{lastt} = $main::systime;
192 if ($pcno == 29) { # incoming text
193 my $ref = $work{"$f[2]$f[3]"};
195 push @{$ref->{lines}}, $f[4];
197 if ($ref->{count} >= $ref->{linesreq}) {
198 $self->send(DXProt::pc31($f[2], $f[1], $f[3]));
199 dbg('msg', "stream $f[3]: $ref->{count} lines received\n");
202 $ref->{lastt} = $main::systime;
204 dbg('msg', "PC29 from unknown stream $f[3] from $f[2]" );
205 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
210 if ($pcno == 30) { # this is a incoming subject ack
211 my $ref = $work{$f[2]}; # note no stream at this stage
214 $ref->{stream} = $f[3];
216 $ref->{linesreq} = 5;
217 $work{"$f[2]$f[3]"} = $ref; # new ref
218 dbg('msg', "incoming subject ack stream $f[3]\n");
219 $busy{$f[2]} = $ref; # interlock
221 push @{$ref->{lines}}, ($ref->read_msg_body);
222 $ref->send_tranche($self);
223 $ref->{lastt} = $main::systime;
225 dbg('msg', "PC30 from unknown stream $f[3] from $f[2]" );
226 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
231 if ($pcno == 31) { # acknowledge a tranche of lines
232 my $ref = $work{"$f[2]$f[3]"};
234 dbg('msg', "tranche ack stream $f[3]\n");
235 $ref->send_tranche($self);
236 $ref->{lastt} = $main::systime;
238 dbg('msg', "PC31 from unknown stream $f[3] from $f[2]" );
239 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
244 if ($pcno == 32) { # incoming EOM
245 dbg('msg', "stream $f[3]: EOM received\n");
246 my $ref = $work{"$f[2]$f[3]"};
248 $self->send(DXProt::pc33($f[2], $f[1], $f[3])); # acknowledge it
250 # get the next msg no - note that this has NOTHING to do with the stream number in PC protocol
251 # store the file or message
252 # remove extraneous rubbish from the hash
253 # remove it from the work in progress vector
254 # stuff it on the msg queue
255 if ($ref->{lines} && @{$ref->{lines}} > 0) { # ignore messages with 0 lines
257 $ref->store($ref->{lines});
260 # does an identical message already exist?
263 if ($ref->{subject} eq $m->{subject} && $ref->{t} == $m->{t} && $ref->{from} eq $m->{from}) {
264 $ref->stop_msg($self->call);
265 my $msgno = $m->{msgno};
266 dbg('msg', "duplicate message to $msgno\n");
267 Log('msg', "duplicate message to $msgno");
272 # look for 'bad' to addresses
273 if (grep $ref->{to} eq $_, @badmsg) {
274 $ref->stop_msg($self->call);
275 dbg('msg', "'Bad' TO address $ref->{to}");
276 Log('msg', "'Bad' TO address $ref->{to}");
280 $ref->{msgno} = next_transno("Msgno");
281 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
282 $ref->store($ref->{lines});
284 my $dxchan = DXChannel->get($ref->{to});
285 $dxchan->send($dxchan->msg('m9')) if $dxchan && $dxchan->is_user;
286 Log('msg', "Message $ref->{msgno} from $ref->{from} received from $f[2] for $ref->{to}");
289 $ref->stop_msg($self->call);
291 dbg('msg', "PC32 from unknown stream $f[3] from $f[2]" );
292 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
298 if ($pcno == 33) { # acknowledge the end of message
299 my $ref = $work{"$f[2]$f[3]"};
301 if ($ref->{private}) { # remove it if it private and gone off site#
302 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2] and deleted");
305 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2]");
306 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
307 $ref->store($ref->{lines}); # re- store the file
309 $ref->stop_msg($self->call);
311 dbg('msg', "PC33 from unknown stream $f[3] from $f[2]" );
312 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
315 # send next one if present
320 if ($pcno == 40) { # this is a file request
321 $f[3] =~ s/\\/\//og; # change the slashes
322 $f[3] =~ s/\.//og; # remove dots
323 $f[3] =~ s/^\///o; # remove the leading /
324 $f[3] = lc $f[3]; # to lower case;
325 dbg('msg', "incoming file $f[3]\n");
326 $f[3] = 'packclus/' . $f[3] unless $f[3] =~ /^packclus\//o;
328 # create any directories
329 my @part = split /\//, $f[3];
331 my $fn = "$main::root";
332 pop @part; # remove last part
333 foreach $part (@part) {
336 last SWITCH if !mkdir $fn, 0777;
337 dbg('msg', "created directory $fn\n");
339 my $stream = next_transno($f[2]);
340 my $ref = DXMsg->alloc($stream, "$main::root/$f[3]", $self->call, time, !$f[4], $f[3], ' ', '0', '0');
342 # forwarding variables
343 $ref->{fromnode} = $f[1];
344 $ref->{tonode} = $f[2];
345 $ref->{linesreq} = $f[5];
346 $ref->{stream} = $stream;
347 $ref->{count} = 0; # no of lines between PC31s
349 $ref->{lastt} = $main::systime;
350 $work{"$f[2]$stream"} = $ref; # store in work
351 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
356 if ($pcno == 42) { # abort transfer
357 dbg('msg', "stream $f[3]: abort received\n");
358 my $ref = $work{"$f[2]$f[3]"};
360 $ref->stop_msg($self->call);
367 if ($pcno == 49) { # global delete on subject
369 if ($_->{from} eq $f[1] && $_->{subject} eq $f[2]) {
371 Log('msg', "Message $_->{msgno} from $_->{from} ($_->{subject}) fully deleted");
372 DXProt::broadcast_ak1a($line, $self);
380 # store a message away on disc or whatever
382 # NOTE the second arg is a REFERENCE not a list
388 # we only proceed if there are actually any lines in the file
389 if (!$lines || @{$lines} == 0) {
393 if ($ref->{file}) { # a file
394 dbg('msg', "To be stored in $ref->{to}\n");
396 my $fh = new IO::File "$ref->{to}", "w";
399 foreach $line (@{$lines}) {
403 dbg('msg', "file $ref->{to} stored\n");
404 Log('msg', "file $ref->{to} from $ref->{from} stored" );
406 confess "can't open file $ref->{to} $!";
408 } else { # a normal message
410 # attempt to open the message file
411 my $fn = filename($ref->{msgno});
413 dbg('msg', "To be stored in $fn\n");
415 # now save the file, overwriting what's there, YES I KNOW OK! (I will change it if it's a problem)
416 my $fh = new IO::File "$fn", "w";
418 my $rr = $ref->{rrreq} ? '1' : '0';
419 my $priv = $ref->{private} ? '1': '0';
420 print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$priv^$ref->{subject}^$ref->{origin}^$ref->{'read'}^$rr\n";
421 print $fh "=== ", join('^', @{$ref->{gotit}}), "\n";
424 foreach $line (@{$lines}) {
425 $ref->{size} += (length $line) + 1;
429 dbg('msg', "msg $ref->{msgno} stored\n");
430 Log('msg', "msg $ref->{msgno} from $ref->{from} to $ref->{to} stored" );
432 confess "can't open msg file $fn $!";
442 # remove it from the active message list
443 @msg = map { $_ != $self ? $_ : () } @msg;
445 # belt and braces (one day I will ask someone if this is REALLY necessary)
446 delete $self->{gotit};
447 delete $self->{list};
450 unlink filename($self->{msgno});
451 dbg('msg', "deleting $self->{msgno}\n");
454 # clean out old messages from the message queue
459 # mark old messages for deletion
460 foreach $ref (@msg) {
461 if (!$ref->{keep} && $ref->{t} < $main::systime - $maxage) {
462 $ref->{deleteme} = 1;
463 delete $ref->{gotit};
465 unlink filename($ref->{msgno});
466 dbg('msg', "deleting old $ref->{msgno}\n");
470 # remove them all from the active message list
471 @msg = map { $_->{deleteme} ? () : $_ } @msg;
472 $last_clean = $main::systime;
475 # read in a message header
485 $file = new IO::File;
486 if (!open($file, $fn)) {
487 print "Error reading $fn $!\n";
491 $line = <$file>; # first line
493 $size -= length $line;
494 if (! $line =~ /^===/o) {
495 print "corrupt first line in $fn ($line)\n";
499 @f = split /\^/, $line;
500 $ref = DXMsg->alloc(@f);
502 $line = <$file>; # second line
504 $size -= length $line;
505 if (! $line =~ /^===/o) {
506 print "corrupt second line in $fn ($line)\n";
511 @f = split /\^/, $line;
512 push @{$ref->{gotit}}, @f;
513 $ref->{size} = $size;
520 # read in a message header
524 my $msgno = $self->{msgno};
527 my $fn = filename($msgno);
530 $file = new IO::File;
531 if (!open($file, $fn)) {
532 print "Error reading $fn $!\n";
535 chomp (@out = <$file>);
538 shift @out if $out[0] =~ /^=== /;
539 shift @out if $out[0] =~ /^=== /;
543 # send a tranche of lines to the other end
546 my ($self, $dxchan) = @_;
548 my $to = $self->{tonode};
549 my $from = $self->{fromnode};
550 my $stream = $self->{stream};
551 my $lines = $self->{lines};
554 for ($i = 0, $c = $self->{count}; $i < $self->{linesreq} && $c < @$lines; $i++, $c++) {
555 push @out, DXProt::pc29($to, $from, $stream, $lines->[$c]);
559 push @out, DXProt::pc32($to, $from, $stream) if $i < $self->{linesreq};
564 # find a message to send out and start the ball rolling
572 my @nodelist = DXProt::get_all_ak1a();
574 # bat down the message list looking for one that needs to go off site and whose
575 # nearest node is not busy.
577 dbg('msg', "queue msg ($sort)\n");
578 foreach $ref (@msg) {
579 # firstly, is it private and unread? if so can I find the recipient
580 # in my cluster node list offsite?
582 # ignore 'delayed' messages until their waiting time has expired
583 if (exists $ref->{waitt}) {
584 next if $ref->{waitt} < $main::systime;
585 delete $ref->{waitt};
588 if ($ref->{private}) {
589 if ($ref->{'read'} == 0) {
590 $clref = DXCluster->get_exact($ref->{to});
591 unless ($clref) { # otherwise look for a homenode
592 my $uref = DXUser->get($ref->{to});
593 my $hnode = $uref->homenode if $uref;
594 $clref = DXCluster->get_exact($hnode) if $hnode;
596 if ($clref && !grep { $clref->{dxchan} == $_ } DXCommandmode::get_all) {
597 $dxchan = $clref->{dxchan};
598 $ref->start_msg($dxchan) if $dxchan && $clref && !get_busy($dxchan->call) && $dxchan->state eq 'normal';
602 # otherwise we are dealing with a bulletin, compare the gotit list with
603 # the nodelist up above, if there are sites that haven't got it yet
604 # then start sending it - what happens when we get loops is anyone's
605 # guess, use (to, from, time, subject) tuple?
607 foreach $noderef (@nodelist) {
608 next if $noderef->call eq $main::mycall;
609 next if grep { $_ eq $noderef->call } @{$ref->{gotit}};
610 next unless $ref->forward_it($noderef->call); # check the forwarding file
611 # next if $noderef->isolate; # maybe add code for stuff originated here?
612 # next if DXUser->get( ${$ref->{gotit}}[0] )->isolate; # is the origin isolated?
614 # if we are here we have a node that doesn't have this message
615 $ref->start_msg($noderef) if !get_busy($noderef->call) && $noderef->state eq 'normal';
620 # if all the available nodes are busy then stop
621 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
625 # is there a message for me?
631 foreach $ref (@msg) {
632 # is it for me, private and unread?
633 if ($ref->{to} eq $call && $ref->{private}) {
634 return 1 if !$ref->{'read'};
640 # start the message off on its travels with a PC28
643 my ($self, $dxchan) = @_;
645 dbg('msg', "start msg $self->{msgno}\n");
646 $self->{linesreq} = 5;
648 $self->{tonode} = $dxchan->call;
649 $self->{fromnode} = $main::mycall;
650 $busy{$self->{tonode}} = $self;
651 $work{$self->{tonode}} = $self;
652 $dxchan->send(DXProt::pc28($self->{tonode}, $self->{fromnode}, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $self->{origin}, $self->{rrreq}));
655 # get the ref of a busy node
668 # get the forwarding queue
674 # stop a message from continuing, clean it out, unlock interlocks etc
679 my $stream = $self->{stream} if exists $self->{stream};
682 dbg('msg', "stop msg $self->{msgno} -> node $node\n");
684 delete $work{"$node$stream"} if $stream;
689 # get a new transaction number from the file specified
693 $name =~ s/\W//og; # remove non-word characters
694 my $fn = "$msgdir/$name";
697 my $fh = new IO::File;
698 if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
700 $msgno = $fh->getline;
704 $fh->print("$msgno\n");
705 dbg('msg', "msgno $msgno allocated for $name\n");
708 confess "can't open $fn $!";
713 # initialise the message 'system', read in all the message headers
716 my $dir = new IO::File;
720 # load various control files
721 my @in = load_badmsg();
722 print "@in\n" if @in;
723 @in = load_forward();
724 print "@in\n" if @in;
726 # read in the directory
727 opendir($dir, $msgdir) or confess "can't open $msgdir $!";
728 @dir = readdir($dir);
733 next unless /^m\d+$/o;
735 $ref = read_msg_header("$msgdir/$_");
738 # delete any messages to 'badmsg.pl' places
739 if (grep $ref->{to} eq $_, @badmsg) {
740 dbg('msg', "'Bad' TO address $ref->{to}");
741 Log('msg', "'Bad' TO address $ref->{to}");
746 # add the message to the available queue
751 # add the message to the directory listing
755 confess "tried to add a non-ref to the msg directory" if !ref $ref;
759 # return all the current messages
765 # get a particular message
770 return $_ if $_->{msgno} == $msgno;
771 last if $_->{msgno} > $msgno;
776 # return the official filename for a message no
779 return sprintf "$msgdir/m%06d", shift;
783 # return a list of valid elements
792 # return a prompt for a field
797 my ($self, $ele) = @_;
802 # send a message state machine
809 if ($self->state eq 'send1') {
811 confess "local var gone missing" if !ref $self->{loc};
812 my $loc = $self->{loc};
813 $loc->{subject} = $line;
815 $self->state('sendbody');
816 #push @out, $self->msg('sendbody');
817 push @out, $self->msg('m8');
818 } elsif ($self->state eq 'sendbody') {
819 confess "local var gone missing" if !ref $self->{loc};
820 my $loc = $self->{loc};
821 if ($line eq "\032" || uc $line eq "/EX") {
824 if (@{$loc->{lines}} > 0) {
825 foreach $to (@{$loc->{to}}) {
827 my $systime = $main::systime;
828 my $mycall = $main::mycall;
829 $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
838 $ref->store($loc->{lines});
840 push @out, $self->msg('m11', $ref->{msgno}, $to);
841 #push @out, "msgno $ref->{msgno} sent to $to";
842 my $dxchan = DXChannel->get(uc $to);
844 if ($dxchan->is_user()) {
845 $dxchan->send($dxchan->msg('m9'));
850 delete $loc->{lines};
855 $self->state('prompt');
856 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
857 #push @out, $self->msg('sendabort');
858 push @out, $self->msg('m10');
859 delete $loc->{lines};
863 $self->state('prompt');
866 # i.e. it ain't and end or abort, therefore store the line
867 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
873 # return the standard directory line for this ref
877 return sprintf "%6d%s%s%5d %8.8s %8.8s %-6.6s %5.5s %-30.30s",
878 $ref->msgno, $ref->read ? '-' : ' ', $ref->private ? 'p' : ' ', $ref->size,
879 $ref->to, $ref->from, cldate($ref->t), ztime($ref->t), $ref->subject;
882 # load the forward table
886 do "$forwardfn" if -e "$forwardfn";
891 # load the bad message table
895 do "$badmsgfn" if -e "$badmsgfn";
901 # forward that message or not according to the forwarding table
902 # returns 1 for forward, 0 - to ignore
911 for ($i = 0; $i < @forward; $i += 5) {
912 my ($sort, $field, $pattern, $action, $bbs) = @forward[$i..($i+4)];
916 last if $ref->{private} && $sort ne 'P';
917 last if !$ref->{private} && $sort ne 'B';
920 $tested = $ref->{to} if $field eq 'T';
921 $tested = $ref->{from} if $field eq 'F';
922 $tested = $ref->{origin} if $field eq 'O';
923 $tested = $ref->{subject} if $field eq 'S';
925 if (!$pattern || $tested =~ m{$pattern}i) {
926 return 0 if $action eq 'I';
927 return 1 if !$bbs || grep $_ eq $call, @{$bbs};
937 my $name = $AUTOLOAD;
938 return if $name =~ /::DESTROY$/;
941 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
942 @_ ? $self->{$name} = shift : $self->{$name} ;