3 # This module impliments the message handling for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
12 @ISA = qw(DXProt DXChannel);
27 use vars qw(%work @msg $msgdir %valid %busy $maxage $last_clean);
29 %work = (); # outstanding jobs
30 @msg = (); # messages we have
31 %busy = (); # station interlocks
32 $msgdir = "$main::root/msg"; # directory contain the msgs
33 $maxage = 30 * 86400; # the maximum age that a message shall live for if not marked
34 $last_clean = 0; # last time we did a clean
37 fromnode => '9,From Node',
38 tonode => '9,To Node',
41 t => '0,Msg Time,cldatetime',
42 private => '9,Private',
43 subject => '0,Subject',
44 linesreq => '0,Lines per Gob',
45 rrreq => '9,Read Confirm',
48 stream => '9,Stream No',
49 count => '9,Gob Linecnt',
50 file => '9,File?,yesno',
51 gotit => '9,Got it Nodes,parray',
52 lines => '9,Lines,parray',
53 read => '9,Times read',
56 keep => '0,Keep this?,yesno',
59 # allocate a new object
60 # called fromnode, tonode, from, to, datetime, private?, subject, nolinesper
64 my $self = bless {}, $pkg;
65 $self->{msgno} = shift;
67 $self->{from} = shift;
69 $self->{private} = shift;
70 $self->{subject} = shift;
71 $self->{origin} = shift;
72 $self->{read} = shift;
73 $self->{rrreq} = shift;
83 delete $ref->{linesreq};
84 delete $ref->{tonode};
85 delete $ref->{fromnode};
86 delete $ref->{stream};
94 my ($self, $line) = @_;
95 my @f = split /[\^\~]/, $line;
96 my ($pcno) = $f[0] =~ /^PC(\d\d)/; # just get the number
99 if ($pcno == 28) { # incoming message
100 my $t = cltounix($f[5], $f[6]);
101 my $stream = next_transno($f[2]);
102 my $ref = DXMsg->alloc($stream, $f[3], $f[4], $t, $f[7], $f[8], $f[13], '0', $f[11]);
104 # fill in various forwarding state variables
105 $ref->{fromnode} = $f[2];
106 $ref->{tonode} = $f[1];
107 $ref->{rrreq} = $f[11];
108 $ref->{linesreq} = $f[10];
109 $ref->{stream} = $stream;
110 $ref->{count} = 0; # no of lines between PC31s
111 dbg('msg', "new message from $f[4] to $f[3] '$f[8]' stream $stream\n");
112 $work{"$f[2]$stream"} = $ref; # store in work
113 $busy{$f[2]} = $ref; # set interlock
114 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
118 if ($pcno == 29) { # incoming text
119 my $ref = $work{"$f[2]$f[3]"};
121 push @{$ref->{lines}}, $f[4];
123 if ($ref->{count} >= $ref->{linesreq}) {
124 $self->send(DXProt::pc31($f[2], $f[1], $f[3]));
125 dbg('msg', "stream $f[3]: $ref->{linereq} lines received\n");
132 if ($pcno == 30) { # this is a incoming subject ack
133 my $ref = $work{$f[2]}; # note no stream at this stage
135 $ref->{stream} = $f[3];
137 $ref->{linesreq} = 5;
138 $work{"$f[2]$f[3]"} = $ref; # new ref
139 dbg('msg', "incoming subject ack stream $f[3]\n");
140 $busy{$f[2]} = $ref; # interlock
142 push @{$ref->{lines}}, ($ref->read_msg_body);
143 $ref->send_tranche($self);
147 if ($pcno == 31) { # acknowledge a tranche of lines
148 my $ref = $work{"$f[2]$f[3]"};
150 dbg('msg', "tranche ack stream $f[3]\n");
151 $ref->send_tranche($self);
153 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
158 if ($pcno == 32) { # incoming EOM
159 dbg('msg', "stream $f[3]: EOM received\n");
160 my $ref = $work{"$f[2]$f[3]"};
162 $self->send(DXProt::pc33($f[2], $f[1], $f[3]));# acknowledge it
164 # get the next msg no - note that this has NOTHING to do with the stream number in PC protocol
165 # store the file or message
166 # remove extraneous rubbish from the hash
167 # remove it from the work in progress vector
168 # stuff it on the msg queue
169 if ($ref->{lines} && @{$ref->{lines}} > 0) { # ignore messages with 0 lines
170 $ref->{msgno} = next_transno("Msgno") if !$ref->{file};
171 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
172 $ref->store($ref->{lines});
174 my $dxchan = DXChannel->get($ref->{to});
175 $dxchan->send("New mail has arrived for you") if $dxchan;
176 Log('msg', "Message $ref->{msgno} from $ref->{from} received from $f[2] for $ref->{to}");
178 $ref->stop_msg($self);
181 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
187 if ($pcno == 33) { # acknowledge the end of message
188 my $ref = $work{"$f[2]$f[3]"};
190 if ($ref->{private}) { # remove it if it private and gone off site#
191 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2] and deleted");
194 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2]");
195 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
196 $ref->store($ref->{lines}); # re- store the file
198 $ref->stop_msg($self);
200 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
206 if ($pcno == 40) { # this is a file request
207 $f[3] =~ s/\\/\//og; # change the slashes
208 $f[3] =~ s/\.//og; # remove dots
209 $f[3] = lc $f[3]; # to lower case;
210 dbg('msg', "incoming file $f[3]\n");
211 last SWITCH if $f[3] =~ /^\/(perl|cmd|local_cmd|src|lib|include|sys|msg)\//; # prevent access to executables
213 # create any directories
214 my @part = split /\//, $f[3];
216 my $fn = "$main::root";
217 pop @part; # remove last part
218 foreach $part (@part) {
221 last SWITCH if !mkdir $fn, 0777;
222 dbg('msg', "created directory $fn\n");
224 my $stream = next_transno($f[2]);
225 my $ref = DXMsg->alloc($stream, "$main::root/$f[3]", $self->call, time, !$f[4], $f[3], ' ', '0', '0');
227 # forwarding variables
228 $ref->{fromnode} = $f[1];
229 $ref->{tonode} = $f[2];
230 $ref->{linesreq} = $f[5];
231 $ref->{stream} = $stream;
232 $ref->{count} = 0; # no of lines between PC31s
234 $work{"$f[2]$stream"} = $ref; # store in work
235 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
240 if ($pcno == 42) { # abort transfer
241 dbg('msg', "stream $f[3]: abort received\n");
242 my $ref = $work{"$f[2]$f[3]"};
244 $ref->stop_msg($self);
252 clean_old() if $main::systime - $last_clean > 3600 ; # clean the message queue
256 # store a message away on disc or whatever
258 # NOTE the second arg is a REFERENCE not a list
264 # we only proceed if there are actually any lines in the file
265 if (!$lines || @{$lines} == 0) {
269 if ($ref->{file}) { # a file
270 dbg('msg', "To be stored in $ref->{to}\n");
272 my $fh = new FileHandle "$ref->{to}", "w";
275 foreach $line (@{$lines}) {
279 dbg('msg', "file $ref->{to} stored\n");
280 Log('msg', "file $ref->{to} from $ref->{from} stored" );
282 confess "can't open file $ref->{to} $!";
284 # push @{$ref->{gotit}}, $ref->{fromnode} if $ref->{fromnode};
285 } else { # a normal message
287 # attempt to open the message file
288 my $fn = filename($ref->{msgno});
290 dbg('msg', "To be stored in $fn\n");
292 # now save the file, overwriting what's there, YES I KNOW OK! (I will change it if it's a problem)
293 my $fh = new FileHandle "$fn", "w";
295 my $rr = $ref->{rrreq} ? '1' : '0';
296 my $priv = $ref->{private} ? '1': '0';
297 print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$priv^$ref->{subject}^$ref->{origin}^$ref->{read}^$rr\n";
298 print $fh "=== ", join('^', @{$ref->{gotit}}), "\n";
301 foreach $line (@{$lines}) {
302 $ref->{size} += (length $line) + 1;
306 dbg('msg', "msg $ref->{msgno} stored\n");
307 Log('msg', "msg $ref->{msgno} from $ref->{from} to $ref->{to} stored" );
309 confess "can't open msg file $fn $!";
319 # remove it from the active message list
320 @msg = map { $_ != $self ? $_ : () } @msg;
322 # belt and braces (one day I will ask someone if this is REALLY necessary)
323 delete $self->{gotit};
324 delete $self->{list};
327 unlink filename($self->{msgno});
328 dbg('msg', "deleting $self->{msgno}\n");
331 # clean out old messages from the message queue
336 # mark old messages for deletion
337 foreach $ref (@msg) {
338 if (!$ref->{keep} && $ref->{t} < $main::systime - $maxage) {
339 $ref->{deleteme} = 1;
340 delete $ref->{gotit};
342 unlink filename($ref->{msgno});
343 dbg('msg', "deleting old $ref->{msgno}\n");
347 # remove them all from the active message list
348 @msg = map { $_->{deleteme} ? () : $_ } @msg;
349 $last_clean = $main::systime;
352 # read in a message header
362 $file = new FileHandle;
363 if (!open($file, $fn)) {
364 print "Error reading $fn $!\n";
368 $line = <$file>; # first line
370 $size -= length $line;
371 if (! $line =~ /^===/o) {
372 print "corrupt first line in $fn ($line)\n";
376 @f = split /\^/, $line;
377 $ref = DXMsg->alloc(@f);
379 $line = <$file>; # second line
381 $size -= length $line;
382 if (! $line =~ /^===/o) {
383 print "corrupt second line in $fn ($line)\n";
388 @f = split /\^/, $line;
389 push @{$ref->{gotit}}, @f;
390 $ref->{size} = $size;
397 # read in a message header
401 my $msgno = $self->{msgno};
404 my $fn = filename($msgno);
407 $file = new FileHandle;
408 if (!open($file, $fn)) {
409 print "Error reading $fn $!\n";
412 chomp (@out = <$file>);
415 shift @out if $out[0] =~ /^=== /;
416 shift @out if $out[0] =~ /^=== /;
420 # send a tranche of lines to the other end
423 my ($self, $dxchan) = @_;
425 my $to = $self->{tonode};
426 my $from = $self->{fromnode};
427 my $stream = $self->{stream};
430 for ($i = 0; $i < $self->{linesreq} && $self->{count} < @{$self->{lines}}; $i++, $self->{count}++) {
431 push @out, DXProt::pc29($to, $from, $stream, ${$self->{lines}}[$self->{count}]);
433 push @out, DXProt::pc32($to, $from, $stream) if $i < $self->{linesreq};
438 # find a message to send out and start the ball rolling
442 my @nodelist = DXProt::get_all_ak1a();
447 # bat down the message list looking for one that needs to go off site and whose
448 # nearest node is not busy.
450 dbg('msg', "queue msg ($sort)\n");
451 foreach $ref (@msg) {
452 # firstly, is it private and unread? if so can I find the recipient
453 # in my cluster node list offsite?
454 if ($ref->{private}) {
455 if ($ref->{read} == 0) {
456 $clref = DXCluster->get($ref->{to});
457 if ($clref && !grep { $clref->{dxchan} == $_ } DXCommandmode::get_all) {
458 $dxchan = $clref->{dxchan};
459 $ref->start_msg($dxchan) if $clref && !get_busy($dxchan->call);
462 } elsif ($sort == undef) {
463 # otherwise we are dealing with a bulletin, compare the gotit list with
464 # the nodelist up above, if there are sites that haven't got it yet
465 # then start sending it - what happens when we get loops is anyone's
466 # guess, use (to, from, time, subject) tuple?
468 foreach $noderef (@nodelist) {
469 next if $noderef->call eq $main::mycall;
470 next if grep { $_ eq $noderef->call } @{$ref->{gotit}};
472 # if we are here we have a node that doesn't have this message
473 $ref->start_msg($noderef) if !get_busy($noderef->call);
478 # if all the available nodes are busy then stop
479 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
483 # start the message off on its travels with a PC28
486 my ($self, $dxchan) = @_;
488 dbg('msg', "start msg $self->{msgno}\n");
489 $self->{linesreq} = 5;
491 $self->{tonode} = $dxchan->call;
492 $self->{fromnode} = $main::mycall;
493 $busy{$dxchan->call} = $self;
494 $work{"$self->{tonode}"} = $self;
495 $dxchan->send(DXProt::pc28($self->{tonode}, $self->{fromnode}, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $self->{origin}, $self->{rrreq}));
498 # get the ref of a busy node
511 # get the forwarding queue
517 # stop a message from continuing, clean it out, unlock interlocks etc
520 my ($self, $dxchan) = @_;
521 my $node = $dxchan->call;
523 dbg('msg', "stop msg $self->{msgno} stream $self->{stream}\n");
525 delete $work{"$node$self->{stream}"};
530 # get a new transaction number from the file specified
534 $name =~ s/\W//og; # remove non-word characters
535 my $fn = "$msgdir/$name";
538 my $fh = new FileHandle;
539 if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
541 $msgno = $fh->getline;
545 $fh->print("$msgno\n");
546 dbg('msg', "msgno $msgno allocated for $name\n");
549 confess "can't open $fn $!";
554 # initialise the message 'system', read in all the message headers
557 my $dir = new FileHandle;
561 # read in the directory
562 opendir($dir, $msgdir) or confess "can't open $msgdir $!";
563 @dir = readdir($dir);
570 $ref = read_msg_header("$msgdir/$_");
573 # add the message to the available queue
579 # add the message to the directory listing
583 confess "tried to add a non-ref to the msg directory" if !ref $ref;
587 # return all the current messages
593 # get a particular message
598 return $_ if $_->{msgno} == $msgno;
599 last if $_->{msgno} > $msgno;
604 # return the official filename for a message no
607 return sprintf "$msgdir/m%06d", shift;
611 # return a list of valid elements
620 # return a prompt for a field
625 my ($self, $ele) = @_;
630 # send a message state machine
637 if ($self->state eq 'send1') {
639 confess "local var gone missing" if !ref $self->{loc};
640 my $loc = $self->{loc};
641 $loc->{subject} = $line;
643 $self->state('sendbody');
644 #push @out, $self->msg('sendbody');
645 push @out, "Enter Message /EX (^Z) to send or /ABORT (^Y) to exit";
646 } elsif ($self->state eq 'sendbody') {
647 confess "local var gone missing" if !ref $self->{loc};
648 my $loc = $self->{loc};
649 if ($line eq "\032" || uc $line eq "/EX") {
652 if (@{$loc->{lines}} > 0) {
653 foreach $to (@{$loc->{to}}) {
655 my $systime = $main::systime;
656 my $mycall = $main::mycall;
657 $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
666 $ref->store($loc->{lines});
668 #push @out, $self->msg('sendsent', $to);
669 push @out, "msgno $ref->{msgno} sent to $to";
670 my $dxchan = DXChannel->get(uc $to);
671 $dxchan->send("New mail has arrived for you") if $dxchan;
674 delete $loc->{lines};
677 $self->state('prompt');
680 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
681 #push @out, $self->msg('sendabort');
682 push @out, "aborted";
683 delete $loc->{lines};
687 $self->state('prompt');
690 # i.e. it ain't and end or abort, therefore store the line
691 push @{$loc->{lines}}, $line;
697 # return the standard directory line for this ref
701 return sprintf "%6d%s%s%5d %8.8s %8.8s %-6.6s %5.5s %-30.30s",
702 $ref->msgno, $ref->read ? '-' : ' ', $ref->private ? 'p' : ' ', $ref->size,
703 $ref->to, $ref->from, cldate($ref->t), ztime($ref->t), $ref->subject;
710 my $name = $AUTOLOAD;
711 return if $name =~ /::DESTROY$/;
714 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
715 @_ ? $self->{$name} = shift : $self->{$name} ;