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 = 2*60; # run the queue every 2 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;
190 # look to see whether this is a non private message sent to a known callsign
191 my $uref = DXUser->get_current($ref->{to});
192 if (iscallsign($ref->{to}) && !$ref->{private} && $uref && $uref->homenode) {
194 dbg('msg', "set bull to $ref->{to} to private");
199 if ($pcno == 29) { # incoming text
200 my $ref = $work{"$f[2]$f[3]"};
202 push @{$ref->{lines}}, $f[4];
204 if ($ref->{count} >= $ref->{linesreq}) {
205 $self->send(DXProt::pc31($f[2], $f[1], $f[3]));
206 dbg('msg', "stream $f[3]: $ref->{count} lines received\n");
209 $ref->{lastt} = $main::systime;
211 dbg('msg', "PC29 from unknown stream $f[3] from $f[2]" );
212 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
217 if ($pcno == 30) { # this is a incoming subject ack
218 my $ref = $work{$f[2]}; # note no stream at this stage
221 $ref->{stream} = $f[3];
223 $ref->{linesreq} = 5;
224 $work{"$f[2]$f[3]"} = $ref; # new ref
225 dbg('msg', "incoming subject ack stream $f[3]\n");
226 $busy{$f[2]} = $ref; # interlock
228 push @{$ref->{lines}}, ($ref->read_msg_body);
229 $ref->send_tranche($self);
230 $ref->{lastt} = $main::systime;
232 dbg('msg', "PC30 from unknown stream $f[3] from $f[2]" );
233 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
238 if ($pcno == 31) { # acknowledge a tranche of lines
239 my $ref = $work{"$f[2]$f[3]"};
241 dbg('msg', "tranche ack stream $f[3]\n");
242 $ref->send_tranche($self);
243 $ref->{lastt} = $main::systime;
245 dbg('msg', "PC31 from unknown stream $f[3] from $f[2]" );
246 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
251 if ($pcno == 32) { # incoming EOM
252 dbg('msg', "stream $f[3]: EOM received\n");
253 my $ref = $work{"$f[2]$f[3]"};
255 $self->send(DXProt::pc33($f[2], $f[1], $f[3])); # acknowledge it
257 # get the next msg no - note that this has NOTHING to do with the stream number in PC protocol
258 # store the file or message
259 # remove extraneous rubbish from the hash
260 # remove it from the work in progress vector
261 # stuff it on the msg queue
262 if ($ref->{lines} && @{$ref->{lines}} > 0) { # ignore messages with 0 lines
264 $ref->store($ref->{lines});
267 # does an identical message already exist?
270 if ($ref->{subject} eq $m->{subject} && $ref->{t} == $m->{t} && $ref->{from} eq $m->{from} && $ref->{to} eq $m->{to}) {
271 $ref->stop_msg($self->call);
272 my $msgno = $m->{msgno};
273 dbg('msg', "duplicate message to $msgno\n");
274 Log('msg', "duplicate message to $msgno");
279 # look for 'bad' to addresses
280 if (grep $ref->{to} eq $_, @badmsg) {
281 $ref->stop_msg($self->call);
282 dbg('msg', "'Bad' TO address $ref->{to}");
283 Log('msg', "'Bad' TO address $ref->{to}");
287 $ref->{msgno} = next_transno("Msgno");
288 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
289 $ref->store($ref->{lines});
291 my $dxchan = DXChannel->get($ref->{to});
292 $dxchan->send($dxchan->msg('m9')) if $dxchan && $dxchan->is_user;
293 Log('msg', "Message $ref->{msgno} from $ref->{from} received from $f[2] for $ref->{to}");
296 $ref->stop_msg($self->call);
298 dbg('msg', "PC32 from unknown stream $f[3] from $f[2]" );
299 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
305 if ($pcno == 33) { # acknowledge the end of message
306 my $ref = $work{"$f[2]$f[3]"};
308 if ($ref->{private}) { # remove it if it private and gone off site#
309 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2] and deleted");
312 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2]");
313 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
314 $ref->store($ref->{lines}); # re- store the file
316 $ref->stop_msg($self->call);
318 dbg('msg', "PC33 from unknown stream $f[3] from $f[2]" );
319 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
322 # send next one if present
327 if ($pcno == 40) { # this is a file request
328 $f[3] =~ s/\\/\//og; # change the slashes
329 $f[3] =~ s/\.//og; # remove dots
330 $f[3] =~ s/^\///o; # remove the leading /
331 $f[3] = lc $f[3]; # to lower case;
332 dbg('msg', "incoming file $f[3]\n");
333 $f[3] = 'packclus/' . $f[3] unless $f[3] =~ /^packclus\//o;
335 # create any directories
336 my @part = split /\//, $f[3];
338 my $fn = "$main::root";
339 pop @part; # remove last part
340 foreach $part (@part) {
343 last SWITCH if !mkdir $fn, 0777;
344 dbg('msg', "created directory $fn\n");
346 my $stream = next_transno($f[2]);
347 my $ref = DXMsg->alloc($stream, "$main::root/$f[3]", $self->call, time, !$f[4], $f[3], ' ', '0', '0');
349 # forwarding variables
350 $ref->{fromnode} = $f[1];
351 $ref->{tonode} = $f[2];
352 $ref->{linesreq} = $f[5];
353 $ref->{stream} = $stream;
354 $ref->{count} = 0; # no of lines between PC31s
356 $ref->{lastt} = $main::systime;
357 $work{"$f[2]$stream"} = $ref; # store in work
358 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
363 if ($pcno == 42) { # abort transfer
364 dbg('msg', "stream $f[3]: abort received\n");
365 my $ref = $work{"$f[2]$f[3]"};
367 $ref->stop_msg($self->call);
374 if ($pcno == 49) { # global delete on subject
376 if ($_->{from} eq $f[1] && $_->{subject} eq $f[2]) {
378 Log('msg', "Message $_->{msgno} from $_->{from} ($_->{subject}) fully deleted");
379 DXProt::broadcast_ak1a($line, $self);
387 # store a message away on disc or whatever
389 # NOTE the second arg is a REFERENCE not a list
395 # we only proceed if there are actually any lines in the file
396 # if (!$lines || @{$lines} == 0) {
400 if ($ref->{file}) { # a file
401 dbg('msg', "To be stored in $ref->{to}\n");
403 my $fh = new IO::File "$ref->{to}", "w";
406 foreach $line (@{$lines}) {
410 dbg('msg', "file $ref->{to} stored\n");
411 Log('msg', "file $ref->{to} from $ref->{from} stored" );
413 confess "can't open file $ref->{to} $!";
415 } else { # a normal message
417 # attempt to open the message file
418 my $fn = filename($ref->{msgno});
420 dbg('msg', "To be stored in $fn\n");
422 # now save the file, overwriting what's there, YES I KNOW OK! (I will change it if it's a problem)
423 my $fh = new IO::File "$fn", "w";
425 my $rr = $ref->{rrreq} ? '1' : '0';
426 my $priv = $ref->{private} ? '1': '0';
427 print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$priv^$ref->{subject}^$ref->{origin}^$ref->{'read'}^$rr\n";
428 print $fh "=== ", join('^', @{$ref->{gotit}}), "\n";
431 foreach $line (@{$lines}) {
432 $ref->{size} += (length $line) + 1;
436 dbg('msg', "msg $ref->{msgno} stored\n");
437 Log('msg', "msg $ref->{msgno} from $ref->{from} to $ref->{to} stored" );
439 confess "can't open msg file $fn $!";
449 # remove it from the active message list
450 @msg = map { $_ != $self ? $_ : () } @msg;
452 # belt and braces (one day I will ask someone if this is REALLY necessary)
453 delete $self->{gotit};
454 delete $self->{list};
457 unlink filename($self->{msgno});
458 dbg('msg', "deleting $self->{msgno}\n");
461 # clean out old messages from the message queue
466 # mark old messages for deletion
467 foreach $ref (@msg) {
468 if (!$ref->{keep} && $ref->{t} < $main::systime - $maxage) {
469 $ref->{deleteme} = 1;
470 delete $ref->{gotit};
472 unlink filename($ref->{msgno});
473 dbg('msg', "deleting old $ref->{msgno}\n");
477 # remove them all from the active message list
478 @msg = map { $_->{deleteme} ? () : $_ } @msg;
479 $last_clean = $main::systime;
482 # read in a message header
492 $file = new IO::File;
493 if (!open($file, $fn)) {
494 print "Error reading $fn $!\n";
498 $line = <$file>; # first line
500 $size -= length $line;
501 if (! $line =~ /^===/o) {
502 print "corrupt first line in $fn ($line)\n";
506 @f = split /\^/, $line;
507 $ref = DXMsg->alloc(@f);
509 $line = <$file>; # second line
511 $size -= length $line;
512 if (! $line =~ /^===/o) {
513 print "corrupt second line in $fn ($line)\n";
518 @f = split /\^/, $line;
519 push @{$ref->{gotit}}, @f;
520 $ref->{size} = $size;
527 # read in a message header
531 my $msgno = $self->{msgno};
534 my $fn = filename($msgno);
537 $file = new IO::File;
538 if (!open($file, $fn)) {
539 print "Error reading $fn $!\n";
542 chomp (@out = <$file>);
545 shift @out if $out[0] =~ /^=== /;
546 shift @out if $out[0] =~ /^=== /;
550 # send a tranche of lines to the other end
553 my ($self, $dxchan) = @_;
555 my $to = $self->{tonode};
556 my $from = $self->{fromnode};
557 my $stream = $self->{stream};
558 my $lines = $self->{lines};
561 for ($i = 0, $c = $self->{count}; $i < $self->{linesreq} && $c < @$lines; $i++, $c++) {
562 push @out, DXProt::pc29($to, $from, $stream, $lines->[$c]);
566 push @out, DXProt::pc32($to, $from, $stream) if $i < $self->{linesreq};
571 # find a message to send out and start the ball rolling
578 my @nodelist = DXProt::get_all_ak1a();
580 # bat down the message list looking for one that needs to go off site and whose
581 # nearest node is not busy.
583 dbg('msg', "queue msg ($sort)\n");
584 foreach $ref (@msg) {
585 # firstly, is it private and unread? if so can I find the recipient
586 # in my cluster node list offsite?
588 # ignore 'delayed' messages until their waiting time has expired
589 if (exists $ref->{waitt}) {
590 next if $ref->{waitt} > $main::systime;
591 delete $ref->{waitt};
594 # deal with routed private messages
596 if ($ref->{private}) {
597 $clref = DXCluster->get_exact($ref->{to});
598 unless ($clref) { # otherwise look for a homenode
599 my $uref = DXUser->get($ref->{to});
600 my $hnode = $uref->homenode if $uref;
601 $clref = DXCluster->get_exact($hnode) if $hnode;
603 if ($clref && !grep { $clref->{dxchan} == $_ } DXCommandmode::get_all) {
604 next if $clref->call eq $main::mycall; # i.e. it lives here
605 $noderef = $clref->{dxchan};
606 $ref->start_msg($noderef) if !get_busy($noderef->call) && $noderef->state eq 'normal';
610 # otherwise we are dealing with a bulletin or forwarded private message
611 # compare the gotit list with
612 # the nodelist up above, if there are sites that haven't got it yet
613 # then start sending it - what happens when we get loops is anyone's
614 # guess, use (to, from, time, subject) tuple?
615 foreach $noderef (@nodelist) {
616 next if $noderef->call eq $main::mycall;
617 next if grep { $_ eq $noderef->call } @{$ref->{gotit}};
618 next unless $ref->forward_it($noderef->call); # check the forwarding file
620 # if we are here we have a node that doesn't have this message
621 $ref->start_msg($noderef) if !get_busy($noderef->call) && $noderef->state eq 'normal';
625 # if all the available nodes are busy then stop
626 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
630 # is there a message for me?
636 foreach $ref (@msg) {
637 # is it for me, private and unread?
638 if ($ref->{to} eq $call && $ref->{private}) {
639 return 1 if !$ref->{'read'};
645 # start the message off on its travels with a PC28
648 my ($self, $dxchan) = @_;
650 dbg('msg', "start msg $self->{msgno}\n");
651 $self->{linesreq} = 5;
653 $self->{tonode} = $dxchan->call;
654 $self->{fromnode} = $main::mycall;
655 $busy{$self->{tonode}} = $self;
656 $work{$self->{tonode}} = $self;
657 $self->{lastt} = $main::systime;
658 $dxchan->send(DXProt::pc28($self->{tonode}, $self->{fromnode}, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $self->{origin}, $self->{rrreq}));
661 # get the ref of a busy node
674 # get the forwarding queue
680 # stop a message from continuing, clean it out, unlock interlocks etc
685 my $stream = $self->{stream} if exists $self->{stream};
688 dbg('msg', "stop msg $self->{msgno} -> node $node\n");
690 delete $work{"$node$stream"} if $stream;
695 # get a new transaction number from the file specified
699 $name =~ s/\W//og; # remove non-word characters
700 my $fn = "$msgdir/$name";
703 my $fh = new IO::File;
704 if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
706 $msgno = $fh->getline;
710 $fh->print("$msgno\n");
711 dbg('msg', "msgno $msgno allocated for $name\n");
714 confess "can't open $fn $!";
719 # initialise the message 'system', read in all the message headers
722 my $dir = new IO::File;
726 # load various control files
727 my @in = load_badmsg();
728 print "@in\n" if @in;
729 @in = load_forward();
730 print "@in\n" if @in;
732 # read in the directory
733 opendir($dir, $msgdir) or confess "can't open $msgdir $!";
734 @dir = readdir($dir);
739 next unless /^m\d+$/o;
741 $ref = read_msg_header("$msgdir/$_");
744 # delete any messages to 'badmsg.pl' places
745 if (grep $ref->{to} eq $_, @badmsg) {
746 dbg('msg', "'Bad' TO address $ref->{to}");
747 Log('msg', "'Bad' TO address $ref->{to}");
752 # add the message to the available queue
757 # add the message to the directory listing
761 confess "tried to add a non-ref to the msg directory" if !ref $ref;
765 # return all the current messages
771 # get a particular message
776 return $_ if $_->{msgno} == $msgno;
777 last if $_->{msgno} > $msgno;
782 # return the official filename for a message no
785 return sprintf "$msgdir/m%06d", shift;
789 # return a list of valid elements
798 # return a prompt for a field
803 my ($self, $ele) = @_;
808 # send a message state machine
815 if ($self->state eq 'send1') {
817 confess "local var gone missing" if !ref $self->{loc};
818 my $loc = $self->{loc};
819 $loc->{subject} = $line;
821 $self->state('sendbody');
822 #push @out, $self->msg('sendbody');
823 push @out, $self->msg('m8');
824 } elsif ($self->state eq 'sendbody') {
825 confess "local var gone missing" if !ref $self->{loc};
826 my $loc = $self->{loc};
827 if ($line eq "\032" || uc $line eq "/EX") {
830 if (@{$loc->{lines}} > 0) {
831 foreach $to (@{$loc->{to}}) {
833 my $systime = $main::systime;
834 my $mycall = $main::mycall;
835 $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
844 $ref->store($loc->{lines});
846 push @out, $self->msg('m11', $ref->{msgno}, $to);
847 #push @out, "msgno $ref->{msgno} sent to $to";
848 my $dxchan = DXChannel->get(uc $to);
850 if ($dxchan->is_user()) {
851 $dxchan->send($dxchan->msg('m9'));
856 delete $loc->{lines};
861 $self->state('prompt');
862 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
863 #push @out, $self->msg('sendabort');
864 push @out, $self->msg('m10');
865 delete $loc->{lines};
869 $self->state('prompt');
872 # i.e. it ain't and end or abort, therefore store the line
873 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
879 # return the standard directory line for this ref
883 return sprintf "%6d%s%s%5d %8.8s %8.8s %-6.6s %5.5s %-30.30s",
884 $ref->msgno, $ref->read ? '-' : ' ', $ref->private ? 'p' : ' ', $ref->size,
885 $ref->to, $ref->from, cldate($ref->t), ztime($ref->t), $ref->subject;
888 # load the forward table
892 do "$forwardfn" if -e "$forwardfn";
897 # load the bad message table
901 do "$badmsgfn" if -e "$badmsgfn";
907 # forward that message or not according to the forwarding table
908 # returns 1 for forward, 0 - to ignore
917 for ($i = 0; $i < @forward; $i += 5) {
918 my ($sort, $field, $pattern, $action, $bbs) = @forward[$i..($i+4)];
922 next if $ref->{private} && $sort ne 'P';
923 next if !$ref->{private} && $sort ne 'B';
926 $tested = $ref->{to} if $field eq 'T';
927 my $at = $ref->{to} =~ /\@\s*(\S+)/;
928 $tested = $at if $field eq '\@';
929 $tested = $ref->{from} if $field eq 'F';
930 $tested = $ref->{origin} if $field eq 'O';
931 $tested = $ref->{subject} if $field eq 'S';
933 if (!$pattern || $tested =~ m{$pattern}i) {
934 return 0 if $action eq 'I';
935 return 1 if !$bbs || grep $_ eq $call, @{$bbs};
945 my $name = $AUTOLOAD;
946 return if $name =~ /::DESTROY$/;
949 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
950 @_ ? $self->{$name} = shift : $self->{$name} ;