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 $ref->stop_msg($node);
140 # delay any outgoing messages that fail
141 $ref->{waitt} = $main::systime + $waittime + rand(120) if $node ne $main::mycall;
145 # queue some message if the interval timer has gone off
146 if ($main::systime > $lastq + $queueinterval) {
148 $lastq = $main::systime;
151 # clean the message queue
152 clean_old() if $main::systime - $last_clean > 3600 ;
156 my @f = split /\^/, $line;
157 my ($pcno) = $f[0] =~ /^PC(\d\d)/; # just get the number
160 if ($pcno == 28) { # incoming message
162 # first look for any messages in the busy queue
163 # and cancel them this should both resolve timed out incoming messages
164 # and crossing of message between nodes, incoming messages have priority
165 if (exists $busy{$f[2]}) {
166 my $ref = $busy{$f[2]};
167 my $tonode = $ref->{tonode};
168 $ref->stop_msg($self->call);
171 my $t = cltounix($f[5], $f[6]);
172 my $stream = next_transno($f[2]);
173 my $ref = DXMsg->alloc($stream, uc $f[3], $f[4], $t, $f[7], $f[8], $f[13], '0', $f[11]);
175 # fill in various forwarding state variables
176 $ref->{fromnode} = $f[2];
177 $ref->{tonode} = $f[1];
178 $ref->{rrreq} = $f[11];
179 $ref->{linesreq} = $f[10];
180 $ref->{stream} = $stream;
181 $ref->{count} = 0; # no of lines between PC31s
182 dbg('msg', "new message from $f[4] to $f[3] '$f[8]' stream $stream\n");
183 $work{"$f[2]$stream"} = $ref; # store in work
184 $busy{$f[2]} = $ref; # set interlock
185 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
186 $ref->{lastt} = $main::systime;
190 if ($pcno == 29) { # incoming text
191 my $ref = $work{"$f[2]$f[3]"};
193 push @{$ref->{lines}}, $f[4];
195 if ($ref->{count} >= $ref->{linesreq}) {
196 $self->send(DXProt::pc31($f[2], $f[1], $f[3]));
197 dbg('msg', "stream $f[3]: $ref->{count} lines received\n");
200 $ref->{lastt} = $main::systime;
205 if ($pcno == 30) { # this is a incoming subject ack
206 my $ref = $work{$f[2]}; # note no stream at this stage
209 $ref->{stream} = $f[3];
211 $ref->{linesreq} = 5;
212 $work{"$f[2]$f[3]"} = $ref; # new ref
213 dbg('msg', "incoming subject ack stream $f[3]\n");
214 $busy{$f[2]} = $ref; # interlock
216 push @{$ref->{lines}}, ($ref->read_msg_body);
217 $ref->send_tranche($self);
218 $ref->{lastt} = $main::systime;
220 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
225 if ($pcno == 31) { # acknowledge a tranche of lines
226 my $ref = $work{"$f[2]$f[3]"};
228 dbg('msg', "tranche ack stream $f[3]\n");
229 $ref->send_tranche($self);
230 $ref->{lastt} = $main::systime;
232 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
237 if ($pcno == 32) { # incoming EOM
238 dbg('msg', "stream $f[3]: EOM received\n");
239 my $ref = $work{"$f[2]$f[3]"};
241 $self->send(DXProt::pc33($f[2], $f[1], $f[3])); # acknowledge it
243 # get the next msg no - note that this has NOTHING to do with the stream number in PC protocol
244 # store the file or message
245 # remove extraneous rubbish from the hash
246 # remove it from the work in progress vector
247 # stuff it on the msg queue
248 if ($ref->{lines} && @{$ref->{lines}} > 0) { # ignore messages with 0 lines
250 $ref->store($ref->{lines});
253 # does an identical message already exist?
256 if ($ref->{subject} eq $m->{subject} && $ref->{t} == $m->{t} && $ref->{from} eq $m->{from}) {
257 $ref->stop_msg($self->call);
258 my $msgno = $m->{msgno};
259 dbg('msg', "duplicate message to $msgno\n");
260 Log('msg', "duplicate message to $msgno");
265 # look for 'bad' to addresses
266 if (grep $ref->{to} eq $_, @badmsg) {
267 $ref->stop_msg($self->call);
268 dbg('msg', "'Bad' TO address $ref->{to}");
269 Log('msg', "'Bad' TO address $ref->{to}");
273 $ref->{msgno} = next_transno("Msgno");
274 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
275 $ref->store($ref->{lines});
277 my $dxchan = DXChannel->get($ref->{to});
278 $dxchan->send($dxchan->msg('m9')) if $dxchan && $dxchan->is_user;
279 Log('msg', "Message $ref->{msgno} from $ref->{from} received from $f[2] for $ref->{to}");
282 $ref->stop_msg($self->call);
284 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
290 if ($pcno == 33) { # acknowledge the end of message
291 my $ref = $work{"$f[2]$f[3]"};
293 if ($ref->{private}) { # remove it if it private and gone off site#
294 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2] and deleted");
297 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2]");
298 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
299 $ref->store($ref->{lines}); # re- store the file
301 $ref->stop_msg($self->call);
303 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
306 # send next one if present
311 if ($pcno == 40) { # this is a file request
312 $f[3] =~ s/\\/\//og; # change the slashes
313 $f[3] =~ s/\.//og; # remove dots
314 $f[3] =~ s/^\///o; # remove the leading /
315 $f[3] = lc $f[3]; # to lower case;
316 dbg('msg', "incoming file $f[3]\n");
317 $f[3] = 'packclus/' . $f[3] unless $f[3] =~ /^packclus\//o;
319 # create any directories
320 my @part = split /\//, $f[3];
322 my $fn = "$main::root";
323 pop @part; # remove last part
324 foreach $part (@part) {
327 last SWITCH if !mkdir $fn, 0777;
328 dbg('msg', "created directory $fn\n");
330 my $stream = next_transno($f[2]);
331 my $ref = DXMsg->alloc($stream, "$main::root/$f[3]", $self->call, time, !$f[4], $f[3], ' ', '0', '0');
333 # forwarding variables
334 $ref->{fromnode} = $f[1];
335 $ref->{tonode} = $f[2];
336 $ref->{linesreq} = $f[5];
337 $ref->{stream} = $stream;
338 $ref->{count} = 0; # no of lines between PC31s
340 $work{"$f[2]$stream"} = $ref; # store in work
341 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
346 if ($pcno == 42) { # abort transfer
347 dbg('msg', "stream $f[3]: abort received\n");
348 my $ref = $work{"$f[2]$f[3]"};
350 $ref->stop_msg($self->call);
357 if ($pcno == 49) { # global delete on subject
359 if ($_->{from} eq $f[1] && $_->{subject} eq $f[2]) {
361 Log('msg', "Message $_->{msgno} from $_->{from} ($_->{subject}) fully deleted");
362 DXProt::broadcast_ak1a($line, $self);
370 # store a message away on disc or whatever
372 # NOTE the second arg is a REFERENCE not a list
378 # we only proceed if there are actually any lines in the file
379 if (!$lines || @{$lines} == 0) {
383 if ($ref->{file}) { # a file
384 dbg('msg', "To be stored in $ref->{to}\n");
386 my $fh = new IO::File "$ref->{to}", "w";
389 foreach $line (@{$lines}) {
393 dbg('msg', "file $ref->{to} stored\n");
394 Log('msg', "file $ref->{to} from $ref->{from} stored" );
396 confess "can't open file $ref->{to} $!";
398 } else { # a normal message
400 # attempt to open the message file
401 my $fn = filename($ref->{msgno});
403 dbg('msg', "To be stored in $fn\n");
405 # now save the file, overwriting what's there, YES I KNOW OK! (I will change it if it's a problem)
406 my $fh = new IO::File "$fn", "w";
408 my $rr = $ref->{rrreq} ? '1' : '0';
409 my $priv = $ref->{private} ? '1': '0';
410 print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$priv^$ref->{subject}^$ref->{origin}^$ref->{'read'}^$rr\n";
411 print $fh "=== ", join('^', @{$ref->{gotit}}), "\n";
414 foreach $line (@{$lines}) {
415 $ref->{size} += (length $line) + 1;
419 dbg('msg', "msg $ref->{msgno} stored\n");
420 Log('msg', "msg $ref->{msgno} from $ref->{from} to $ref->{to} stored" );
422 confess "can't open msg file $fn $!";
432 # remove it from the active message list
433 @msg = map { $_ != $self ? $_ : () } @msg;
435 # belt and braces (one day I will ask someone if this is REALLY necessary)
436 delete $self->{gotit};
437 delete $self->{list};
440 unlink filename($self->{msgno});
441 dbg('msg', "deleting $self->{msgno}\n");
444 # clean out old messages from the message queue
449 # mark old messages for deletion
450 foreach $ref (@msg) {
451 if (!$ref->{keep} && $ref->{t} < $main::systime - $maxage) {
452 $ref->{deleteme} = 1;
453 delete $ref->{gotit};
455 unlink filename($ref->{msgno});
456 dbg('msg', "deleting old $ref->{msgno}\n");
460 # remove them all from the active message list
461 @msg = map { $_->{deleteme} ? () : $_ } @msg;
462 $last_clean = $main::systime;
465 # read in a message header
475 $file = new IO::File;
476 if (!open($file, $fn)) {
477 print "Error reading $fn $!\n";
481 $line = <$file>; # first line
483 $size -= length $line;
484 if (! $line =~ /^===/o) {
485 print "corrupt first line in $fn ($line)\n";
489 @f = split /\^/, $line;
490 $ref = DXMsg->alloc(@f);
492 $line = <$file>; # second line
494 $size -= length $line;
495 if (! $line =~ /^===/o) {
496 print "corrupt second line in $fn ($line)\n";
501 @f = split /\^/, $line;
502 push @{$ref->{gotit}}, @f;
503 $ref->{size} = $size;
510 # read in a message header
514 my $msgno = $self->{msgno};
517 my $fn = filename($msgno);
520 $file = new IO::File;
521 if (!open($file, $fn)) {
522 print "Error reading $fn $!\n";
525 chomp (@out = <$file>);
528 shift @out if $out[0] =~ /^=== /;
529 shift @out if $out[0] =~ /^=== /;
533 # send a tranche of lines to the other end
536 my ($self, $dxchan) = @_;
538 my $to = $self->{tonode};
539 my $from = $self->{fromnode};
540 my $stream = $self->{stream};
541 my $lines = $self->{lines};
544 for ($i = 0, $c = $self->{count}; $i < $self->{linesreq} && $c < @$lines; $i++, $c++) {
545 push @out, DXProt::pc29($to, $from, $stream, $lines->[$c]);
549 push @out, DXProt::pc32($to, $from, $stream) if $i < $self->{linesreq};
554 # find a message to send out and start the ball rolling
562 my @nodelist = DXProt::get_all_ak1a();
564 # bat down the message list looking for one that needs to go off site and whose
565 # nearest node is not busy.
567 dbg('msg', "queue msg ($sort)\n");
568 foreach $ref (@msg) {
569 # firstly, is it private and unread? if so can I find the recipient
570 # in my cluster node list offsite?
572 # ignore 'delayed' messages until their waiting time has expired
573 if (exists $ref->{waitt}) {
574 next if $ref->{waitt} < $main::systime;
575 delete $ref->{waitt};
578 if ($ref->{private}) {
579 if ($ref->{'read'} == 0) {
580 $clref = DXCluster->get_exact($ref->{to});
581 unless ($clref) { # otherwise look for a homenode
582 my $uref = DXUser->get($ref->{to});
583 my $hnode = $uref->homenode if $uref;
584 $clref = DXCluster->get_exact($hnode) if $hnode;
586 if ($clref && !grep { $clref->{dxchan} == $_ } DXCommandmode::get_all) {
587 $dxchan = $clref->{dxchan};
588 $ref->start_msg($dxchan) if $dxchan && $clref && !get_busy($dxchan->call) && $dxchan->state eq 'normal';
592 # otherwise we are dealing with a bulletin, compare the gotit list with
593 # the nodelist up above, if there are sites that haven't got it yet
594 # then start sending it - what happens when we get loops is anyone's
595 # guess, use (to, from, time, subject) tuple?
597 foreach $noderef (@nodelist) {
598 next if $noderef->call eq $main::mycall;
599 next if grep { $_ eq $noderef->call } @{$ref->{gotit}};
600 next unless $ref->forward_it($noderef->call); # check the forwarding file
601 # next if $noderef->isolate; # maybe add code for stuff originated here?
602 # next if DXUser->get( ${$ref->{gotit}}[0] )->isolate; # is the origin isolated?
604 # if we are here we have a node that doesn't have this message
605 $ref->start_msg($noderef) if !get_busy($noderef->call) && $noderef->state eq 'normal';
610 # if all the available nodes are busy then stop
611 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
615 # is there a message for me?
621 foreach $ref (@msg) {
622 # is it for me, private and unread?
623 if ($ref->{to} eq $call && $ref->{private}) {
624 return 1 if !$ref->{'read'};
630 # start the message off on its travels with a PC28
633 my ($self, $dxchan) = @_;
635 dbg('msg', "start msg $self->{msgno}\n");
636 $self->{linesreq} = 5;
638 $self->{tonode} = $dxchan->call;
639 $self->{fromnode} = $main::mycall;
640 $busy{$self->{tonode}} = $self;
641 $work{$self->{tonode}} = $self;
642 $dxchan->send(DXProt::pc28($self->{tonode}, $self->{fromnode}, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $self->{origin}, $self->{rrreq}));
645 # get the ref of a busy node
658 # get the forwarding queue
664 # stop a message from continuing, clean it out, unlock interlocks etc
669 my $stream = $self->{stream} if exists $self->{stream};
672 dbg('msg', "stop msg $self->{msgno} -> node $node\n");
674 delete $work{"$node$stream"} if $stream;
679 # get a new transaction number from the file specified
683 $name =~ s/\W//og; # remove non-word characters
684 my $fn = "$msgdir/$name";
687 my $fh = new IO::File;
688 if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
690 $msgno = $fh->getline;
694 $fh->print("$msgno\n");
695 dbg('msg', "msgno $msgno allocated for $name\n");
698 confess "can't open $fn $!";
703 # initialise the message 'system', read in all the message headers
706 my $dir = new IO::File;
710 # load various control files
711 my @in = load_badmsg();
712 print "@in\n" if @in;
713 @in = load_forward();
714 print "@in\n" if @in;
716 # read in the directory
717 opendir($dir, $msgdir) or confess "can't open $msgdir $!";
718 @dir = readdir($dir);
723 next unless /^m\d+$/o;
725 $ref = read_msg_header("$msgdir/$_");
728 # delete any messages to 'badmsg.pl' places
729 if (grep $ref->{to} eq $_, @badmsg) {
730 dbg('msg', "'Bad' TO address $ref->{to}");
731 Log('msg', "'Bad' TO address $ref->{to}");
736 # add the message to the available queue
741 # add the message to the directory listing
745 confess "tried to add a non-ref to the msg directory" if !ref $ref;
749 # return all the current messages
755 # get a particular message
760 return $_ if $_->{msgno} == $msgno;
761 last if $_->{msgno} > $msgno;
766 # return the official filename for a message no
769 return sprintf "$msgdir/m%06d", shift;
773 # return a list of valid elements
782 # return a prompt for a field
787 my ($self, $ele) = @_;
792 # send a message state machine
799 if ($self->state eq 'send1') {
801 confess "local var gone missing" if !ref $self->{loc};
802 my $loc = $self->{loc};
803 $loc->{subject} = $line;
805 $self->state('sendbody');
806 #push @out, $self->msg('sendbody');
807 push @out, $self->msg('m8');
808 } elsif ($self->state eq 'sendbody') {
809 confess "local var gone missing" if !ref $self->{loc};
810 my $loc = $self->{loc};
811 if ($line eq "\032" || uc $line eq "/EX") {
814 if (@{$loc->{lines}} > 0) {
815 foreach $to (@{$loc->{to}}) {
817 my $systime = $main::systime;
818 my $mycall = $main::mycall;
819 $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
828 $ref->store($loc->{lines});
830 push @out, $self->msg('m11', $ref->{msgno}, $to);
831 #push @out, "msgno $ref->{msgno} sent to $to";
832 my $dxchan = DXChannel->get(uc $to);
834 if ($dxchan->is_user()) {
835 $dxchan->send($dxchan->msg('m9'));
840 delete $loc->{lines};
845 $self->state('prompt');
846 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
847 #push @out, $self->msg('sendabort');
848 push @out, $self->msg('m10');
849 delete $loc->{lines};
853 $self->state('prompt');
856 # i.e. it ain't and end or abort, therefore store the line
857 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
863 # return the standard directory line for this ref
867 return sprintf "%6d%s%s%5d %8.8s %8.8s %-6.6s %5.5s %-30.30s",
868 $ref->msgno, $ref->read ? '-' : ' ', $ref->private ? 'p' : ' ', $ref->size,
869 $ref->to, $ref->from, cldate($ref->t), ztime($ref->t), $ref->subject;
872 # load the forward table
876 do "$forwardfn" if -e "$forwardfn";
881 # load the bad message table
885 do "$badmsgfn" if -e "$badmsgfn";
891 # forward that message or not according to the forwarding table
892 # returns 1 for forward, 0 - to ignore
901 for ($i = 0; $i < @forward; $i += 5) {
902 my ($sort, $field, $pattern, $action, $bbs) = @forward[$i..($i+4)];
906 last if $ref->{private} && $sort ne 'P';
907 last if !$ref->{private} && $sort ne 'B';
910 $tested = $ref->{to} if $field eq 'T';
911 $tested = $ref->{from} if $field eq 'F';
912 $tested = $ref->{origin} if $field eq 'O';
913 $tested = $ref->{subject} if $field eq 'S';
915 if (!$pattern || $tested =~ m{$pattern}i) {
916 return 0 if $action eq 'I';
917 return 1 if !$bbs || grep $_ eq $call, @{$bbs};
927 my $name = $AUTOLOAD;
928 return if $name =~ /::DESTROY$/;
931 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
932 @_ ? $self->{$name} = shift : $self->{$name} ;