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)
17 @ISA = qw(DXProt DXChannel);
32 use vars qw(%work @msg $msgdir %valid %busy $maxage $last_clean
33 @badmsg $badmsgfn $forwardfn @forward);
35 %work = (); # outstanding jobs
36 @msg = (); # messages we have
37 %busy = (); # station interlocks
38 $msgdir = "$main::root/msg"; # directory contain the msgs
39 $maxage = 30 * 86400; # the maximum age that a message shall live for if not marked
40 $last_clean = 0; # last time we did a clean
41 @forward = (); # msg forward table
43 $badmsgfn = "$msgdir/badmsg.pl"; # list of TO address we wont store
44 $forwardfn = "$msgdir/forward.pl"; # the forwarding table
47 fromnode => '9,From Node',
48 tonode => '9,To Node',
51 t => '0,Msg Time,cldatetime',
52 private => '9,Private',
53 subject => '0,Subject',
54 linesreq => '0,Lines per Gob',
55 rrreq => '9,Read Confirm',
58 stream => '9,Stream No',
59 count => '9,Gob Linecnt',
60 file => '9,File?,yesno',
61 gotit => '9,Got it Nodes,parray',
62 lines => '9,Lines,parray',
63 'read' => '9,Times read',
66 keep => '0,Keep this?,yesno',
69 # allocate a new object
70 # called fromnode, tonode, from, to, datetime, private?, subject, nolinesper
74 my $self = bless {}, $pkg;
75 $self->{msgno} = shift;
81 $self->{from} = uc $from;
83 $self->{private} = shift;
84 $self->{subject} = shift;
85 $self->{origin} = shift;
86 $self->{'read'} = shift;
87 $self->{rrreq} = shift;
97 delete $ref->{linesreq};
98 delete $ref->{tonode};
99 delete $ref->{fromnode};
100 delete $ref->{stream};
101 delete $ref->{lines};
103 delete $ref->{count};
108 my ($self, $line) = @_;
109 my @f = split /\^/, $line;
110 my ($pcno) = $f[0] =~ /^PC(\d\d)/; # just get the number
113 if ($pcno == 28) { # incoming message
114 my $t = cltounix($f[5], $f[6]);
115 my $stream = next_transno($f[2]);
116 my $ref = DXMsg->alloc($stream, uc $f[3], $f[4], $t, $f[7], $f[8], $f[13], '0', $f[11]);
118 # fill in various forwarding state variables
119 $ref->{fromnode} = $f[2];
120 $ref->{tonode} = $f[1];
121 $ref->{rrreq} = $f[11];
122 $ref->{linesreq} = $f[10];
123 $ref->{stream} = $stream;
124 $ref->{count} = 0; # no of lines between PC31s
125 dbg('msg', "new message from $f[4] to $f[3] '$f[8]' stream $stream\n");
126 $work{"$f[2]$stream"} = $ref; # store in work
127 $busy{$f[2]} = $ref; # set interlock
128 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
132 if ($pcno == 29) { # incoming text
133 my $ref = $work{"$f[2]$f[3]"};
135 push @{$ref->{lines}}, $f[4];
137 if ($ref->{count} >= $ref->{linesreq}) {
138 $self->send(DXProt::pc31($f[2], $f[1], $f[3]));
139 dbg('msg', "stream $f[3]: $ref->{count} lines received\n");
146 if ($pcno == 30) { # this is a incoming subject ack
147 my $ref = $work{$f[2]}; # note no stream at this stage
150 $ref->{stream} = $f[3];
152 $ref->{linesreq} = 5;
153 $work{"$f[2]$f[3]"} = $ref; # new ref
154 dbg('msg', "incoming subject ack stream $f[3]\n");
155 $busy{$f[2]} = $ref; # interlock
157 push @{$ref->{lines}}, ($ref->read_msg_body);
158 $ref->send_tranche($self);
160 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
165 if ($pcno == 31) { # acknowledge a tranche of lines
166 my $ref = $work{"$f[2]$f[3]"};
168 dbg('msg', "tranche ack stream $f[3]\n");
169 $ref->send_tranche($self);
171 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
176 if ($pcno == 32) { # incoming EOM
177 dbg('msg', "stream $f[3]: EOM received\n");
178 my $ref = $work{"$f[2]$f[3]"};
180 $self->send(DXProt::pc33($f[2], $f[1], $f[3])); # acknowledge it
182 # get the next msg no - note that this has NOTHING to do with the stream number in PC protocol
183 # store the file or message
184 # remove extraneous rubbish from the hash
185 # remove it from the work in progress vector
186 # stuff it on the msg queue
187 if ($ref->{lines} && @{$ref->{lines}} > 0) { # ignore messages with 0 lines
189 $ref->store($ref->{lines});
192 # does an identical message already exist?
195 if ($ref->{subject} eq $m->{subject} && $ref->{t} == $m->{t} && $ref->{from} eq $m->{from}) {
196 $ref->stop_msg($self);
197 my $msgno = $m->{msgno};
198 dbg('msg', "duplicate message to $msgno\n");
199 Log('msg', "duplicate message to $msgno");
204 # look for 'bad' to addresses
205 if (grep $ref->{to} eq $_, @badmsg) {
206 $ref->stop_msg($self);
207 dbg('msg', "'Bad' TO address $ref->{to}");
208 Log('msg', "'Bad' TO address $ref->{to}");
212 $ref->{msgno} = next_transno("Msgno");
213 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
214 $ref->store($ref->{lines});
216 my $dxchan = DXChannel->get($ref->{to});
217 $dxchan->send($dxchan->msg('msgnew')) if $dxchan;
218 Log('msg', "Message $ref->{msgno} from $ref->{from} received from $f[2] for $ref->{to}");
221 $ref->stop_msg($self);
224 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
230 if ($pcno == 33) { # acknowledge the end of message
231 my $ref = $work{"$f[2]$f[3]"};
233 if ($ref->{private}) { # remove it if it private and gone off site#
234 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2] and deleted");
237 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2]");
238 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
239 $ref->store($ref->{lines}); # re- store the file
241 $ref->stop_msg($self);
243 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
249 if ($pcno == 40) { # this is a file request
250 $f[3] =~ s/\\/\//og; # change the slashes
251 $f[3] =~ s/\.//og; # remove dots
252 $f[3] =~ s/^\///o; # remove the leading /
253 $f[3] = lc $f[3]; # to lower case;
254 dbg('msg', "incoming file $f[3]\n");
255 last SWITCH if $f[3] =~ /^(perl|cmd|local|src|lib|include|sys|msg|connect)/; # prevent access to executables
257 # create any directories
258 my @part = split /\//, $f[3];
260 my $fn = "$main::root";
261 pop @part; # remove last part
262 foreach $part (@part) {
265 last SWITCH if !mkdir $fn, 0777;
266 dbg('msg', "created directory $fn\n");
268 my $stream = next_transno($f[2]);
269 my $ref = DXMsg->alloc($stream, "$main::root/$f[3]", $self->call, time, !$f[4], $f[3], ' ', '0', '0');
271 # forwarding variables
272 $ref->{fromnode} = $f[1];
273 $ref->{tonode} = $f[2];
274 $ref->{linesreq} = $f[5];
275 $ref->{stream} = $stream;
276 $ref->{count} = 0; # no of lines between PC31s
278 $work{"$f[2]$stream"} = $ref; # store in work
279 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
284 if ($pcno == 42) { # abort transfer
285 dbg('msg', "stream $f[3]: abort received\n");
286 my $ref = $work{"$f[2]$f[3]"};
288 $ref->stop_msg($self);
295 if ($pcno == 49) { # global delete on subject
297 if ($_->{subject} eq $f[2]) {
299 Log('msg', "Message $_->{msgno} fully deleted by $f[1]");
305 clean_old() if $main::systime - $last_clean > 3600 ; # clean the message queue
309 # store a message away on disc or whatever
311 # NOTE the second arg is a REFERENCE not a list
317 # we only proceed if there are actually any lines in the file
318 if (!$lines || @{$lines} == 0) {
322 if ($ref->{file}) { # a file
323 dbg('msg', "To be stored in $ref->{to}\n");
325 my $fh = new FileHandle "$ref->{to}", "w";
328 foreach $line (@{$lines}) {
332 dbg('msg', "file $ref->{to} stored\n");
333 Log('msg', "file $ref->{to} from $ref->{from} stored" );
335 confess "can't open file $ref->{to} $!";
337 } else { # a normal message
339 # attempt to open the message file
340 my $fn = filename($ref->{msgno});
342 dbg('msg', "To be stored in $fn\n");
344 # now save the file, overwriting what's there, YES I KNOW OK! (I will change it if it's a problem)
345 my $fh = new FileHandle "$fn", "w";
347 my $rr = $ref->{rrreq} ? '1' : '0';
348 my $priv = $ref->{private} ? '1': '0';
349 print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$priv^$ref->{subject}^$ref->{origin}^$ref->{'read'}^$rr\n";
350 print $fh "=== ", join('^', @{$ref->{gotit}}), "\n";
353 foreach $line (@{$lines}) {
354 $ref->{size} += (length $line) + 1;
358 dbg('msg', "msg $ref->{msgno} stored\n");
359 Log('msg', "msg $ref->{msgno} from $ref->{from} to $ref->{to} stored" );
361 confess "can't open msg file $fn $!";
371 # remove it from the active message list
372 @msg = map { $_ != $self ? $_ : () } @msg;
374 # belt and braces (one day I will ask someone if this is REALLY necessary)
375 delete $self->{gotit};
376 delete $self->{list};
379 unlink filename($self->{msgno});
380 dbg('msg', "deleting $self->{msgno}\n");
383 # clean out old messages from the message queue
388 # mark old messages for deletion
389 foreach $ref (@msg) {
390 if (!$ref->{keep} && $ref->{t} < $main::systime - $maxage) {
391 $ref->{deleteme} = 1;
392 delete $ref->{gotit};
394 unlink filename($ref->{msgno});
395 dbg('msg', "deleting old $ref->{msgno}\n");
399 # remove them all from the active message list
400 @msg = map { $_->{deleteme} ? () : $_ } @msg;
401 $last_clean = $main::systime;
404 # read in a message header
414 $file = new FileHandle;
415 if (!open($file, $fn)) {
416 print "Error reading $fn $!\n";
420 $line = <$file>; # first line
422 $size -= length $line;
423 if (! $line =~ /^===/o) {
424 print "corrupt first line in $fn ($line)\n";
428 @f = split /\^/, $line;
429 $ref = DXMsg->alloc(@f);
431 $line = <$file>; # second line
433 $size -= length $line;
434 if (! $line =~ /^===/o) {
435 print "corrupt second line in $fn ($line)\n";
440 @f = split /\^/, $line;
441 push @{$ref->{gotit}}, @f;
442 $ref->{size} = $size;
449 # read in a message header
453 my $msgno = $self->{msgno};
456 my $fn = filename($msgno);
459 $file = new FileHandle;
460 if (!open($file, $fn)) {
461 print "Error reading $fn $!\n";
464 chomp (@out = <$file>);
467 shift @out if $out[0] =~ /^=== /;
468 shift @out if $out[0] =~ /^=== /;
472 # send a tranche of lines to the other end
475 my ($self, $dxchan) = @_;
477 my $to = $self->{tonode};
478 my $from = $self->{fromnode};
479 my $stream = $self->{stream};
480 my $lines = $self->{lines};
483 for ($i = 0, $c = $self->{count}; $i < $self->{linesreq} && $c < @$lines; $i++, $c++) {
484 push @out, DXProt::pc29($to, $from, $stream, $lines->[$c]);
488 push @out, DXProt::pc32($to, $from, $stream) if $i < $self->{linesreq};
493 # find a message to send out and start the ball rolling
501 my @nodelist = DXProt::get_all_ak1a();
503 # bat down the message list looking for one that needs to go off site and whose
504 # nearest node is not busy.
506 dbg('msg', "queue msg ($sort)\n");
507 foreach $ref (@msg) {
508 # firstly, is it private and unread? if so can I find the recipient
509 # in my cluster node list offsite?
510 if ($ref->{private}) {
511 if ($ref->{'read'} == 0) {
512 $clref = DXCluster->get_exact($ref->{to});
513 unless ($clref) { # otherwise look for a homenode
514 my $uref = DXUser->get($ref->{to});
515 my $hnode = $uref->homenode if $uref;
516 $clref = DXCluster->get_exact($hnode) if $hnode;
518 if ($clref && !grep { $clref->{dxchan} == $_ } DXCommandmode::get_all) {
519 $dxchan = $clref->{dxchan};
520 $ref->start_msg($dxchan) if $dxchan && $clref && !get_busy($dxchan->call) && $dxchan->state eq 'normal';
524 # otherwise we are dealing with a bulletin, compare the gotit list with
525 # the nodelist up above, if there are sites that haven't got it yet
526 # then start sending it - what happens when we get loops is anyone's
527 # guess, use (to, from, time, subject) tuple?
529 foreach $noderef (@nodelist) {
530 next if $noderef->call eq $main::mycall;
531 next if grep { $_ eq $noderef->call } @{$ref->{gotit}};
532 next unless $ref->forward_it($noderef->call); # check the forwarding file
533 # next if $noderef->isolate; # maybe add code for stuff originated here?
534 # next if DXUser->get( ${$ref->{gotit}}[0] )->isolate; # is the origin isolated?
536 # if we are here we have a node that doesn't have this message
537 $ref->start_msg($noderef) if !get_busy($noderef->call) && $noderef->state eq 'normal';
542 # if all the available nodes are busy then stop
543 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
547 # is there a message for me?
553 foreach $ref (@msg) {
554 # is it for me, private and unread?
555 if ($ref->{to} eq $call && $ref->{private}) {
556 return 1 if !$ref->{'read'};
562 # start the message off on its travels with a PC28
565 my ($self, $dxchan) = @_;
567 dbg('msg', "start msg $self->{msgno}\n");
568 $self->{linesreq} = 5;
570 $self->{tonode} = $dxchan->call;
571 $self->{fromnode} = $main::mycall;
572 $busy{$dxchan->call} = $self;
573 $work{"$self->{tonode}"} = $self;
574 $dxchan->send(DXProt::pc28($self->{tonode}, $self->{fromnode}, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $self->{origin}, $self->{rrreq}));
577 # get the ref of a busy node
590 # get the forwarding queue
596 # stop a message from continuing, clean it out, unlock interlocks etc
599 my ($self, $dxchan) = @_;
600 my $node = $dxchan->call;
602 dbg('msg', "stop msg $self->{msgno} stream $self->{stream}\n");
604 delete $work{"$node$self->{stream}"};
609 # get a new transaction number from the file specified
613 $name =~ s/\W//og; # remove non-word characters
614 my $fn = "$msgdir/$name";
617 my $fh = new FileHandle;
618 if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
620 $msgno = $fh->getline;
624 $fh->print("$msgno\n");
625 dbg('msg', "msgno $msgno allocated for $name\n");
628 confess "can't open $fn $!";
633 # initialise the message 'system', read in all the message headers
636 my $dir = new FileHandle;
640 # load various control files
641 my @in = load_badmsg();
642 print "@in\n" if @in;
643 @in = load_forward();
644 print "@in\n" if @in;
646 # read in the directory
647 opendir($dir, $msgdir) or confess "can't open $msgdir $!";
648 @dir = readdir($dir);
653 next unless /^m\d+$/o;
655 $ref = read_msg_header("$msgdir/$_");
658 # delete any messages to 'badmsg.pl' places
659 if (grep $ref->{to} eq $_, @badmsg) {
660 dbg('msg', "'Bad' TO address $ref->{to}");
661 Log('msg', "'Bad' TO address $ref->{to}");
666 # add the message to the available queue
671 # add the message to the directory listing
675 confess "tried to add a non-ref to the msg directory" if !ref $ref;
679 # return all the current messages
685 # get a particular message
690 return $_ if $_->{msgno} == $msgno;
691 last if $_->{msgno} > $msgno;
696 # return the official filename for a message no
699 return sprintf "$msgdir/m%06d", shift;
703 # return a list of valid elements
712 # return a prompt for a field
717 my ($self, $ele) = @_;
722 # send a message state machine
729 if ($self->state eq 'send1') {
731 confess "local var gone missing" if !ref $self->{loc};
732 my $loc = $self->{loc};
733 $loc->{subject} = $line;
735 $self->state('sendbody');
736 #push @out, $self->msg('sendbody');
737 push @out, "Enter Message /EX (^Z) to send or /ABORT (^Y) to exit";
738 } elsif ($self->state eq 'sendbody') {
739 confess "local var gone missing" if !ref $self->{loc};
740 my $loc = $self->{loc};
741 if ($line eq "\032" || uc $line eq "/EX") {
744 if (@{$loc->{lines}} > 0) {
745 foreach $to (@{$loc->{to}}) {
747 my $systime = $main::systime;
748 my $mycall = $main::mycall;
749 $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
758 $ref->store($loc->{lines});
760 #push @out, $self->msg('sendsent', $to);
761 push @out, "msgno $ref->{msgno} sent to $to";
762 my $dxchan = DXChannel->get(uc $to);
764 if ($dxchan->is_user()) {
765 $dxchan->send("New mail has arrived for you");
770 delete $loc->{lines};
775 $self->state('prompt');
776 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
777 #push @out, $self->msg('sendabort');
778 push @out, "aborted";
779 delete $loc->{lines};
783 $self->state('prompt');
786 # i.e. it ain't and end or abort, therefore store the line
787 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
793 # return the standard directory line for this ref
797 return sprintf "%6d%s%s%5d %8.8s %8.8s %-6.6s %5.5s %-30.30s",
798 $ref->msgno, $ref->read ? '-' : ' ', $ref->private ? 'p' : ' ', $ref->size,
799 $ref->to, $ref->from, cldate($ref->t), ztime($ref->t), $ref->subject;
802 # load the forward table
806 do "$forwardfn" if -e "$forwardfn";
811 # load the bad message table
815 do "$badmsgfn" if -e "$badmsgfn";
821 # forward that message or not according to the forwarding table
822 # returns 1 for forward, 0 - to ignore
831 for ($i = 0; $i < @forward; $i += 5) {
832 my ($sort, $field, $pattern, $action, $bbs) = @forward[$i..($i+4)];
836 last if $ref->{private} && $sort ne 'P';
837 last if !$ref->{private} && $sort ne 'B';
840 $tested = $ref->{to} if $field eq 'T';
841 $tested = $ref->{from} if $field eq 'F';
842 $tested = $ref->{origin} if $field eq 'O';
843 $tested = $ref->{subject} if $field eq 'S';
845 if (!$pattern || $tested =~ m{$pattern}i) {
846 return 0 if $action eq 'I';
847 return 1 if !$bbs || grep $_ eq $call, @{$bbs};
857 my $name = $AUTOLOAD;
858 return if $name =~ /::DESTROY$/;
861 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
862 @_ ? $self->{$name} = shift : $self->{$name} ;