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;
71 $self->{from} = uc $from;
73 $self->{private} = shift;
74 $self->{subject} = shift;
75 $self->{origin} = shift;
76 $self->{read} = shift;
77 $self->{rrreq} = shift;
87 delete $ref->{linesreq};
88 delete $ref->{tonode};
89 delete $ref->{fromnode};
90 delete $ref->{stream};
98 my ($self, $line) = @_;
99 my @f = split /[\^\~]/, $line;
100 my ($pcno) = $f[0] =~ /^PC(\d\d)/; # just get the number
103 if ($pcno == 28) { # incoming message
104 my $t = cltounix($f[5], $f[6]);
105 my $stream = next_transno($f[2]);
106 my $ref = DXMsg->alloc($stream, $f[3], $f[4], $t, $f[7], $f[8], $f[13], '0', $f[11]);
108 # fill in various forwarding state variables
109 $ref->{fromnode} = $f[2];
110 $ref->{tonode} = $f[1];
111 $ref->{rrreq} = $f[11];
112 $ref->{linesreq} = $f[10];
113 $ref->{stream} = $stream;
114 $ref->{count} = 0; # no of lines between PC31s
115 dbg('msg', "new message from $f[4] to $f[3] '$f[8]' stream $stream\n");
116 $work{"$f[2]$stream"} = $ref; # store in work
117 $busy{$f[2]} = $ref; # set interlock
118 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
122 if ($pcno == 29) { # incoming text
123 my $ref = $work{"$f[2]$f[3]"};
125 push @{$ref->{lines}}, $f[4];
127 if ($ref->{count} >= $ref->{linesreq}) {
128 $self->send(DXProt::pc31($f[2], $f[1], $f[3]));
129 dbg('msg', "stream $f[3]: $ref->{linereq} lines received\n");
136 if ($pcno == 30) { # this is a incoming subject ack
137 my $ref = $work{$f[2]}; # note no stream at this stage
139 $ref->{stream} = $f[3];
141 $ref->{linesreq} = 5;
142 $work{"$f[2]$f[3]"} = $ref; # new ref
143 dbg('msg', "incoming subject ack stream $f[3]\n");
144 $busy{$f[2]} = $ref; # interlock
146 push @{$ref->{lines}}, ($ref->read_msg_body);
147 $ref->send_tranche($self);
151 if ($pcno == 31) { # acknowledge a tranche of lines
152 my $ref = $work{"$f[2]$f[3]"};
154 dbg('msg', "tranche ack stream $f[3]\n");
155 $ref->send_tranche($self);
157 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
162 if ($pcno == 32) { # incoming EOM
163 dbg('msg', "stream $f[3]: EOM received\n");
164 my $ref = $work{"$f[2]$f[3]"};
166 $self->send(DXProt::pc33($f[2], $f[1], $f[3])); # acknowledge it
168 # get the next msg no - note that this has NOTHING to do with the stream number in PC protocol
169 # store the file or message
170 # remove extraneous rubbish from the hash
171 # remove it from the work in progress vector
172 # stuff it on the msg queue
173 if ($ref->{lines} && @{$ref->{lines}} > 0) { # ignore messages with 0 lines
174 $ref->{msgno} = next_transno("Msgno") if !$ref->{file};
175 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
176 $ref->store($ref->{lines});
178 my $dxchan = DXChannel->get($ref->{to});
179 $dxchan->send("New mail has arrived for you") if $dxchan;
180 Log('msg', "Message $ref->{msgno} from $ref->{from} received from $f[2] for $ref->{to}");
182 $ref->stop_msg($self);
185 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
191 if ($pcno == 33) { # acknowledge the end of message
192 my $ref = $work{"$f[2]$f[3]"};
194 if ($ref->{private}) { # remove it if it private and gone off site#
195 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2] and deleted");
198 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2]");
199 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
200 $ref->store($ref->{lines}); # re- store the file
202 $ref->stop_msg($self);
204 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
210 if ($pcno == 40) { # this is a file request
211 $f[3] =~ s/\\/\//og; # change the slashes
212 $f[3] =~ s/\.//og; # remove dots
213 $f[3] = lc $f[3]; # to lower case;
214 dbg('msg', "incoming file $f[3]\n");
215 last SWITCH if $f[3] =~ /^\/(perl|cmd|local_cmd|src|lib|include|sys|msg)\//; # prevent access to executables
217 # create any directories
218 my @part = split /\//, $f[3];
220 my $fn = "$main::root";
221 pop @part; # remove last part
222 foreach $part (@part) {
225 last SWITCH if !mkdir $fn, 0777;
226 dbg('msg', "created directory $fn\n");
228 my $stream = next_transno($f[2]);
229 my $ref = DXMsg->alloc($stream, "$main::root/$f[3]", $self->call, time, !$f[4], $f[3], ' ', '0', '0');
231 # forwarding variables
232 $ref->{fromnode} = $f[1];
233 $ref->{tonode} = $f[2];
234 $ref->{linesreq} = $f[5];
235 $ref->{stream} = $stream;
236 $ref->{count} = 0; # no of lines between PC31s
238 $work{"$f[2]$stream"} = $ref; # store in work
239 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
244 if ($pcno == 42) { # abort transfer
245 dbg('msg', "stream $f[3]: abort received\n");
246 my $ref = $work{"$f[2]$f[3]"};
248 $ref->stop_msg($self);
256 clean_old() if $main::systime - $last_clean > 3600 ; # clean the message queue
260 # store a message away on disc or whatever
262 # NOTE the second arg is a REFERENCE not a list
268 # we only proceed if there are actually any lines in the file
269 if (!$lines || @{$lines} == 0) {
273 if ($ref->{file}) { # a file
274 dbg('msg', "To be stored in $ref->{to}\n");
276 my $fh = new FileHandle "$ref->{to}", "w";
279 foreach $line (@{$lines}) {
283 dbg('msg', "file $ref->{to} stored\n");
284 Log('msg', "file $ref->{to} from $ref->{from} stored" );
286 confess "can't open file $ref->{to} $!";
288 # push @{$ref->{gotit}}, $ref->{fromnode} if $ref->{fromnode};
289 } else { # a normal message
291 # attempt to open the message file
292 my $fn = filename($ref->{msgno});
294 dbg('msg', "To be stored in $fn\n");
296 # now save the file, overwriting what's there, YES I KNOW OK! (I will change it if it's a problem)
297 my $fh = new FileHandle "$fn", "w";
299 my $rr = $ref->{rrreq} ? '1' : '0';
300 my $priv = $ref->{private} ? '1': '0';
301 print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$priv^$ref->{subject}^$ref->{origin}^$ref->{read}^$rr\n";
302 print $fh "=== ", join('^', @{$ref->{gotit}}), "\n";
305 foreach $line (@{$lines}) {
306 $ref->{size} += (length $line) + 1;
310 dbg('msg', "msg $ref->{msgno} stored\n");
311 Log('msg', "msg $ref->{msgno} from $ref->{from} to $ref->{to} stored" );
313 confess "can't open msg file $fn $!";
323 # remove it from the active message list
324 @msg = map { $_ != $self ? $_ : () } @msg;
326 # belt and braces (one day I will ask someone if this is REALLY necessary)
327 delete $self->{gotit};
328 delete $self->{list};
331 unlink filename($self->{msgno});
332 dbg('msg', "deleting $self->{msgno}\n");
335 # clean out old messages from the message queue
340 # mark old messages for deletion
341 foreach $ref (@msg) {
342 if (!$ref->{keep} && $ref->{t} < $main::systime - $maxage) {
343 $ref->{deleteme} = 1;
344 delete $ref->{gotit};
346 unlink filename($ref->{msgno});
347 dbg('msg', "deleting old $ref->{msgno}\n");
351 # remove them all from the active message list
352 @msg = map { $_->{deleteme} ? () : $_ } @msg;
353 $last_clean = $main::systime;
356 # read in a message header
366 $file = new FileHandle;
367 if (!open($file, $fn)) {
368 print "Error reading $fn $!\n";
372 $line = <$file>; # first line
374 $size -= length $line;
375 if (! $line =~ /^===/o) {
376 print "corrupt first line in $fn ($line)\n";
380 @f = split /\^/, $line;
381 $ref = DXMsg->alloc(@f);
383 $line = <$file>; # second line
385 $size -= length $line;
386 if (! $line =~ /^===/o) {
387 print "corrupt second line in $fn ($line)\n";
392 @f = split /\^/, $line;
393 push @{$ref->{gotit}}, @f;
394 $ref->{size} = $size;
401 # read in a message header
405 my $msgno = $self->{msgno};
408 my $fn = filename($msgno);
411 $file = new FileHandle;
412 if (!open($file, $fn)) {
413 print "Error reading $fn $!\n";
416 chomp (@out = <$file>);
419 shift @out if $out[0] =~ /^=== /;
420 shift @out if $out[0] =~ /^=== /;
424 # send a tranche of lines to the other end
427 my ($self, $dxchan) = @_;
429 my $to = $self->{tonode};
430 my $from = $self->{fromnode};
431 my $stream = $self->{stream};
434 for ($i = 0; $i < $self->{linesreq} && $self->{count} < @{$self->{lines}}; $i++, $self->{count}++) {
435 push @out, DXProt::pc29($to, $from, $stream, ${$self->{lines}}[$self->{count}]);
437 push @out, DXProt::pc32($to, $from, $stream) if $i < $self->{linesreq};
442 # find a message to send out and start the ball rolling
446 my @nodelist = DXProt::get_all_ak1a();
451 # bat down the message list looking for one that needs to go off site and whose
452 # nearest node is not busy.
454 dbg('msg', "queue msg ($sort)\n");
455 foreach $ref (@msg) {
456 # firstly, is it private and unread? if so can I find the recipient
457 # in my cluster node list offsite?
458 if ($ref->{private}) {
459 if ($ref->{read} == 0) {
460 $clref = DXCluster->get($ref->{to});
461 if ($clref && !grep { $clref->{dxchan} == $_ } DXCommandmode::get_all) {
462 $dxchan = $clref->{dxchan};
463 $ref->start_msg($dxchan) if $clref && !get_busy($dxchan->call);
466 } elsif ($sort == undef) {
467 # otherwise we are dealing with a bulletin, compare the gotit list with
468 # the nodelist up above, if there are sites that haven't got it yet
469 # then start sending it - what happens when we get loops is anyone's
470 # guess, use (to, from, time, subject) tuple?
472 foreach $noderef (@nodelist) {
473 next if $noderef->call eq $main::mycall;
474 next if grep { $_ eq $noderef->call } @{$ref->{gotit}};
476 # if we are here we have a node that doesn't have this message
477 $ref->start_msg($noderef) if !get_busy($noderef->call);
482 # if all the available nodes are busy then stop
483 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
487 # start the message off on its travels with a PC28
490 my ($self, $dxchan) = @_;
492 dbg('msg', "start msg $self->{msgno}\n");
493 $self->{linesreq} = 5;
495 $self->{tonode} = $dxchan->call;
496 $self->{fromnode} = $main::mycall;
497 $busy{$dxchan->call} = $self;
498 $work{"$self->{tonode}"} = $self;
499 $dxchan->send(DXProt::pc28($self->{tonode}, $self->{fromnode}, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $self->{origin}, $self->{rrreq}));
502 # get the ref of a busy node
515 # get the forwarding queue
521 # stop a message from continuing, clean it out, unlock interlocks etc
524 my ($self, $dxchan) = @_;
525 my $node = $dxchan->call;
527 dbg('msg', "stop msg $self->{msgno} stream $self->{stream}\n");
529 delete $work{"$node$self->{stream}"};
534 # get a new transaction number from the file specified
538 $name =~ s/\W//og; # remove non-word characters
539 my $fn = "$msgdir/$name";
542 my $fh = new FileHandle;
543 if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
545 $msgno = $fh->getline;
549 $fh->print("$msgno\n");
550 dbg('msg', "msgno $msgno allocated for $name\n");
553 confess "can't open $fn $!";
558 # initialise the message 'system', read in all the message headers
561 my $dir = new FileHandle;
565 # read in the directory
566 opendir($dir, $msgdir) or confess "can't open $msgdir $!";
567 @dir = readdir($dir);
574 $ref = read_msg_header("$msgdir/$_");
577 # add the message to the available queue
583 # add the message to the directory listing
587 confess "tried to add a non-ref to the msg directory" if !ref $ref;
591 # return all the current messages
597 # get a particular message
602 return $_ if $_->{msgno} == $msgno;
603 last if $_->{msgno} > $msgno;
608 # return the official filename for a message no
611 return sprintf "$msgdir/m%06d", shift;
615 # return a list of valid elements
624 # return a prompt for a field
629 my ($self, $ele) = @_;
634 # send a message state machine
641 if ($self->state eq 'send1') {
643 confess "local var gone missing" if !ref $self->{loc};
644 my $loc = $self->{loc};
645 $loc->{subject} = $line;
647 $self->state('sendbody');
648 #push @out, $self->msg('sendbody');
649 push @out, "Enter Message /EX (^Z) to send or /ABORT (^Y) to exit";
650 } elsif ($self->state eq 'sendbody') {
651 confess "local var gone missing" if !ref $self->{loc};
652 my $loc = $self->{loc};
653 if ($line eq "\032" || uc $line eq "/EX") {
656 if (@{$loc->{lines}} > 0) {
657 foreach $to (@{$loc->{to}}) {
659 my $systime = $main::systime;
660 my $mycall = $main::mycall;
661 $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
670 $ref->store($loc->{lines});
672 #push @out, $self->msg('sendsent', $to);
673 push @out, "msgno $ref->{msgno} sent to $to";
674 my $dxchan = DXChannel->get(uc $to);
676 if ($dxchan->is_user()) {
677 $dxchan->send("New mail has arrived for you");
682 delete $loc->{lines};
685 $self->state('prompt');
688 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
689 #push @out, $self->msg('sendabort');
690 push @out, "aborted";
691 delete $loc->{lines};
695 $self->state('prompt');
698 # i.e. it ain't and end or abort, therefore store the line
699 push @{$loc->{lines}}, $line;
705 # return the standard directory line for this ref
709 return sprintf "%6d%s%s%5d %8.8s %8.8s %-6.6s %5.5s %-30.30s",
710 $ref->msgno, $ref->read ? '-' : ' ', $ref->private ? 'p' : ' ', $ref->size,
711 $ref->to, $ref->from, cldate($ref->t), ztime($ref->t), $ref->subject;
718 my $name = $AUTOLOAD;
719 return if $name =~ /::DESTROY$/;
722 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
723 @_ ? $self->{$name} = shift : $self->{$name} ;