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 = 30*60; # time an aborted outgoing message waits before trying again
47 $queueinterval = 1*60; # run the queue every 1 minute
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 if ($main::systime > $lastq + $queueinterval) {
135 # wander down the work queue stopping any messages that have timed out
139 if (exists $ref->{lastt} && $main::systime >= $ref->{lastt} + $timeout) {
140 dbg('msg', "Timeout, stopping msgno: $ref->{msgno} -> $node");
141 $ref->stop_msg($node);
143 # delay any outgoing messages that fail
144 $ref->{waitt} = $main::systime + $waittime + rand(120) if $node ne $main::mycall;
148 # queue some message if the interval timer has gone off
150 $lastq = $main::systime;
153 # clean the message queue
154 clean_old() if $main::systime - $last_clean > 3600 ;
158 my @f = split /\^/, $line;
159 my ($pcno) = $f[0] =~ /^PC(\d\d)/; # just get the number
162 if ($pcno == 28) { # incoming message
164 # first look for any messages in the busy queue
165 # and cancel them this should both resolve timed out incoming messages
166 # and crossing of message between nodes, incoming messages have priority
167 if (exists $busy{$f[2]}) {
168 my $ref = $busy{$f[2]};
169 my $tonode = $ref->{tonode};
170 dbg('msg', "Busy, stopping msgno: $ref->{msgno} -> $f[2]");
171 $ref->stop_msg($self->call);
174 my $t = cltounix($f[5], $f[6]);
175 my $stream = next_transno($f[2]);
176 my $ref = DXMsg->alloc($stream, uc $f[3], $f[4], $t, $f[7], $f[8], $f[13], '0', $f[11]);
178 # fill in various forwarding state variables
179 $ref->{fromnode} = $f[2];
180 $ref->{tonode} = $f[1];
181 $ref->{rrreq} = $f[11];
182 $ref->{linesreq} = $f[10];
183 $ref->{stream} = $stream;
184 $ref->{count} = 0; # no of lines between PC31s
185 dbg('msg', "new message from $f[4] to $f[3] '$f[8]' stream $stream\n");
186 $work{"$f[2]$stream"} = $ref; # store in work
187 $busy{$f[2]} = $ref; # set interlock
188 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
189 $ref->{lastt} = $main::systime;
191 # look to see whether this is a non private message sent to a known callsign
192 my $uref = DXUser->get_current($ref->{to});
193 if (iscallsign($ref->{to}) && !$ref->{private} && $uref && $uref->homenode) {
195 dbg('msg', "set bull to $ref->{to} to private");
200 if ($pcno == 29) { # incoming text
201 my $ref = $work{"$f[2]$f[3]"};
203 push @{$ref->{lines}}, $f[4];
205 if ($ref->{count} >= $ref->{linesreq}) {
206 $self->send(DXProt::pc31($f[2], $f[1], $f[3]));
207 dbg('msg', "stream $f[3]: $ref->{count} lines received\n");
210 $ref->{lastt} = $main::systime;
212 dbg('msg', "PC29 from unknown stream $f[3] from $f[2]" );
213 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
218 if ($pcno == 30) { # this is a incoming subject ack
219 my $ref = $work{$f[2]}; # note no stream at this stage
222 $ref->{stream} = $f[3];
224 $ref->{linesreq} = 5;
225 $work{"$f[2]$f[3]"} = $ref; # new ref
226 dbg('msg', "incoming subject ack stream $f[3]\n");
227 $busy{$f[2]} = $ref; # interlock
229 push @{$ref->{lines}}, ($ref->read_msg_body);
230 $ref->send_tranche($self);
231 $ref->{lastt} = $main::systime;
233 dbg('msg', "PC30 from unknown stream $f[3] from $f[2]" );
234 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
239 if ($pcno == 31) { # acknowledge a tranche of lines
240 my $ref = $work{"$f[2]$f[3]"};
242 dbg('msg', "tranche ack stream $f[3]\n");
243 $ref->send_tranche($self);
244 $ref->{lastt} = $main::systime;
246 dbg('msg', "PC31 from unknown stream $f[3] from $f[2]" );
247 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
252 if ($pcno == 32) { # incoming EOM
253 dbg('msg', "stream $f[3]: EOM received\n");
254 my $ref = $work{"$f[2]$f[3]"};
256 $self->send(DXProt::pc33($f[2], $f[1], $f[3])); # acknowledge it
258 # get the next msg no - note that this has NOTHING to do with the stream number in PC protocol
259 # store the file or message
260 # remove extraneous rubbish from the hash
261 # remove it from the work in progress vector
262 # stuff it on the msg queue
263 if ($ref->{lines} && @{$ref->{lines}} > 0) { # ignore messages with 0 lines
265 $ref->store($ref->{lines});
268 # does an identical message already exist?
271 if ($ref->{subject} eq $m->{subject} && $ref->{t} == $m->{t} && $ref->{from} eq $m->{from} && $ref->{to} eq $m->{to}) {
272 $ref->stop_msg($self->call);
273 my $msgno = $m->{msgno};
274 dbg('msg', "duplicate message to $msgno\n");
275 Log('msg', "duplicate message to $msgno");
280 # look for 'bad' to addresses
281 if (grep $ref->{to} eq $_, @badmsg) {
282 $ref->stop_msg($self->call);
283 dbg('msg', "'Bad' TO address $ref->{to}");
284 Log('msg', "'Bad' TO address $ref->{to}");
288 $ref->{msgno} = next_transno("Msgno");
289 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
290 $ref->store($ref->{lines});
292 my $dxchan = DXChannel->get($ref->{to});
293 $dxchan->send($dxchan->msg('m9')) if $dxchan && $dxchan->is_user;
294 Log('msg', "Message $ref->{msgno} from $ref->{from} received from $f[2] for $ref->{to}");
297 $ref->stop_msg($self->call);
299 dbg('msg', "PC32 from unknown stream $f[3] from $f[2]" );
300 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
306 if ($pcno == 33) { # acknowledge the end of message
307 my $ref = $work{"$f[2]$f[3]"};
309 if ($ref->{private}) { # remove it if it private and gone off site#
310 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2] and deleted");
313 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2]");
314 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
315 $ref->store($ref->{lines}); # re- store the file
317 $ref->stop_msg($self->call);
319 dbg('msg', "PC33 from unknown stream $f[3] from $f[2]" );
320 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
323 # send next one if present
328 if ($pcno == 40) { # this is a file request
329 $f[3] =~ s/\\/\//og; # change the slashes
330 $f[3] =~ s/\.//og; # remove dots
331 $f[3] =~ s/^\///o; # remove the leading /
332 $f[3] = lc $f[3]; # to lower case;
333 dbg('msg', "incoming file $f[3]\n");
334 $f[3] = 'packclus/' . $f[3] unless $f[3] =~ /^packclus\//o;
336 # create any directories
337 my @part = split /\//, $f[3];
339 my $fn = "$main::root";
340 pop @part; # remove last part
341 foreach $part (@part) {
344 last SWITCH if !mkdir $fn, 0777;
345 dbg('msg', "created directory $fn\n");
347 my $stream = next_transno($f[2]);
348 my $ref = DXMsg->alloc($stream, "$main::root/$f[3]", $self->call, time, !$f[4], $f[3], ' ', '0', '0');
350 # forwarding variables
351 $ref->{fromnode} = $f[1];
352 $ref->{tonode} = $f[2];
353 $ref->{linesreq} = $f[5];
354 $ref->{stream} = $stream;
355 $ref->{count} = 0; # no of lines between PC31s
357 $ref->{lastt} = $main::systime;
358 $work{"$f[2]$stream"} = $ref; # store in work
359 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
364 if ($pcno == 42) { # abort transfer
365 dbg('msg', "stream $f[3]: abort received\n");
366 my $ref = $work{"$f[2]$f[3]"};
368 $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 = DXChannel::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" || $line eq '%1A' || 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 my $s = readfilestr($forwardfn);
900 # load the bad message table
904 my $s = readfilestr($badmsgfn);
913 # forward that message or not according to the forwarding table
914 # returns 1 for forward, 0 - to ignore
923 for ($i = 0; $i < @forward; $i += 5) {
924 my ($sort, $field, $pattern, $action, $bbs) = @forward[$i..($i+4)];
928 next if $ref->{private} && $sort ne 'P';
929 next if !$ref->{private} && $sort ne 'B';
932 $tested = $ref->{to} if $field eq 'T';
933 my $at = $ref->{to} =~ /\@\s*(\S+)/;
934 $tested = $at if $field eq '\@';
935 $tested = $ref->{from} if $field eq 'F';
936 $tested = $ref->{origin} if $field eq 'O';
937 $tested = $ref->{subject} if $field eq 'S';
939 if (!$pattern || $tested =~ m{$pattern}i) {
940 return 0 if $action eq 'I';
941 return 1 if !$bbs || grep $_ eq $call, @{$bbs};
951 my $name = $AUTOLOAD;
952 return if $name =~ /::DESTROY$/;
955 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
956 @_ ? $self->{$name} = shift : $self->{$name} ;