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, uc $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->{count} 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
175 $ref->store($ref->{lines});
177 $ref->{msgno} = next_transno("Msgno");
178 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
179 $ref->store($ref->{lines});
181 my $dxchan = DXChannel->get($ref->{to});
182 $dxchan->send("New mail has arrived for you") if $dxchan;
183 Log('msg', "Message $ref->{msgno} from $ref->{from} received from $f[2] for $ref->{to}");
186 $ref->stop_msg($self);
189 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
195 if ($pcno == 33) { # acknowledge the end of message
196 my $ref = $work{"$f[2]$f[3]"};
198 if ($ref->{private}) { # remove it if it private and gone off site#
199 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2] and deleted");
202 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2]");
203 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
204 $ref->store($ref->{lines}); # re- store the file
206 $ref->stop_msg($self);
208 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
214 if ($pcno == 40) { # this is a file request
215 $f[3] =~ s/\\/\//og; # change the slashes
216 $f[3] =~ s/\.//og; # remove dots
217 $f[3] =~ s/^\///o; # remove the leading /
218 $f[3] = lc $f[3]; # to lower case;
219 dbg('msg', "incoming file $f[3]\n");
220 last SWITCH if $f[3] =~ /^(perl|cmd|local|src|lib|include|sys|msg|connect)/; # prevent access to executables
222 # create any directories
223 my @part = split /\//, $f[3];
225 my $fn = "$main::root";
226 pop @part; # remove last part
227 foreach $part (@part) {
230 last SWITCH if !mkdir $fn, 0777;
231 dbg('msg', "created directory $fn\n");
233 my $stream = next_transno($f[2]);
234 my $ref = DXMsg->alloc($stream, "$main::root/$f[3]", $self->call, time, !$f[4], $f[3], ' ', '0', '0');
236 # forwarding variables
237 $ref->{fromnode} = $f[1];
238 $ref->{tonode} = $f[2];
239 $ref->{linesreq} = $f[5];
240 $ref->{stream} = $stream;
241 $ref->{count} = 0; # no of lines between PC31s
243 $work{"$f[2]$stream"} = $ref; # store in work
244 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
249 if ($pcno == 42) { # abort transfer
250 dbg('msg', "stream $f[3]: abort received\n");
251 my $ref = $work{"$f[2]$f[3]"};
253 $ref->stop_msg($self);
261 clean_old() if $main::systime - $last_clean > 3600 ; # clean the message queue
265 # store a message away on disc or whatever
267 # NOTE the second arg is a REFERENCE not a list
273 # we only proceed if there are actually any lines in the file
274 if (!$lines || @{$lines} == 0) {
278 if ($ref->{file}) { # a file
279 dbg('msg', "To be stored in $ref->{to}\n");
281 my $fh = new FileHandle "$ref->{to}", "w";
284 foreach $line (@{$lines}) {
288 dbg('msg', "file $ref->{to} stored\n");
289 Log('msg', "file $ref->{to} from $ref->{from} stored" );
291 confess "can't open file $ref->{to} $!";
293 } else { # a normal message
295 # attempt to open the message file
296 my $fn = filename($ref->{msgno});
298 dbg('msg', "To be stored in $fn\n");
300 # now save the file, overwriting what's there, YES I KNOW OK! (I will change it if it's a problem)
301 my $fh = new FileHandle "$fn", "w";
303 my $rr = $ref->{rrreq} ? '1' : '0';
304 my $priv = $ref->{private} ? '1': '0';
305 print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$priv^$ref->{subject}^$ref->{origin}^$ref->{read}^$rr\n";
306 print $fh "=== ", join('^', @{$ref->{gotit}}), "\n";
309 foreach $line (@{$lines}) {
310 $ref->{size} += (length $line) + 1;
314 dbg('msg', "msg $ref->{msgno} stored\n");
315 Log('msg', "msg $ref->{msgno} from $ref->{from} to $ref->{to} stored" );
317 confess "can't open msg file $fn $!";
327 # remove it from the active message list
328 @msg = map { $_ != $self ? $_ : () } @msg;
330 # belt and braces (one day I will ask someone if this is REALLY necessary)
331 delete $self->{gotit};
332 delete $self->{list};
335 unlink filename($self->{msgno});
336 dbg('msg', "deleting $self->{msgno}\n");
339 # clean out old messages from the message queue
344 # mark old messages for deletion
345 foreach $ref (@msg) {
346 if (!$ref->{keep} && $ref->{t} < $main::systime - $maxage) {
347 $ref->{deleteme} = 1;
348 delete $ref->{gotit};
350 unlink filename($ref->{msgno});
351 dbg('msg', "deleting old $ref->{msgno}\n");
355 # remove them all from the active message list
356 @msg = map { $_->{deleteme} ? () : $_ } @msg;
357 $last_clean = $main::systime;
360 # read in a message header
370 $file = new FileHandle;
371 if (!open($file, $fn)) {
372 print "Error reading $fn $!\n";
376 $line = <$file>; # first line
378 $size -= length $line;
379 if (! $line =~ /^===/o) {
380 print "corrupt first line in $fn ($line)\n";
384 @f = split /\^/, $line;
385 $ref = DXMsg->alloc(@f);
387 $line = <$file>; # second line
389 $size -= length $line;
390 if (! $line =~ /^===/o) {
391 print "corrupt second line in $fn ($line)\n";
396 @f = split /\^/, $line;
397 push @{$ref->{gotit}}, @f;
398 $ref->{size} = $size;
405 # read in a message header
409 my $msgno = $self->{msgno};
412 my $fn = filename($msgno);
415 $file = new FileHandle;
416 if (!open($file, $fn)) {
417 print "Error reading $fn $!\n";
420 chomp (@out = <$file>);
423 shift @out if $out[0] =~ /^=== /;
424 shift @out if $out[0] =~ /^=== /;
428 # send a tranche of lines to the other end
431 my ($self, $dxchan) = @_;
433 my $to = $self->{tonode};
434 my $from = $self->{fromnode};
435 my $stream = $self->{stream};
438 for ($i = 0; $i < $self->{linesreq} && $self->{count} < @{$self->{lines}}; $i++, $self->{count}++) {
439 push @out, DXProt::pc29($to, $from, $stream, ${$self->{lines}}[$self->{count}]);
441 push @out, DXProt::pc32($to, $from, $stream) if $i < $self->{linesreq};
446 # find a message to send out and start the ball rolling
450 my @nodelist = DXProt::get_all_ak1a();
455 # bat down the message list looking for one that needs to go off site and whose
456 # nearest node is not busy.
458 dbg('msg', "queue msg ($sort)\n");
459 foreach $ref (@msg) {
460 # firstly, is it private and unread? if so can I find the recipient
461 # in my cluster node list offsite?
462 if ($ref->{private}) {
463 if ($ref->{read} == 0) {
464 $clref = DXCluster->get($ref->{to});
465 if ($clref && !grep { $clref->{dxchan} == $_ } DXCommandmode::get_all) {
466 $dxchan = $clref->{dxchan};
467 $ref->start_msg($dxchan) if $clref && !get_busy($dxchan->call);
470 } elsif ($sort == undef) {
471 # otherwise we are dealing with a bulletin, compare the gotit list with
472 # the nodelist up above, if there are sites that haven't got it yet
473 # then start sending it - what happens when we get loops is anyone's
474 # guess, use (to, from, time, subject) tuple?
476 foreach $noderef (@nodelist) {
477 next if $noderef->call eq $main::mycall;
478 next if grep { $_ eq $noderef->call } @{$ref->{gotit}};
480 # if we are here we have a node that doesn't have this message
481 $ref->start_msg($noderef) if !get_busy($noderef->call);
486 # if all the available nodes are busy then stop
487 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
491 # start the message off on its travels with a PC28
494 my ($self, $dxchan) = @_;
496 dbg('msg', "start msg $self->{msgno}\n");
497 $self->{linesreq} = 5;
499 $self->{tonode} = $dxchan->call;
500 $self->{fromnode} = $main::mycall;
501 $busy{$dxchan->call} = $self;
502 $work{"$self->{tonode}"} = $self;
503 $dxchan->send(DXProt::pc28($self->{tonode}, $self->{fromnode}, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $self->{origin}, $self->{rrreq}));
506 # get the ref of a busy node
519 # get the forwarding queue
525 # stop a message from continuing, clean it out, unlock interlocks etc
528 my ($self, $dxchan) = @_;
529 my $node = $dxchan->call;
531 dbg('msg', "stop msg $self->{msgno} stream $self->{stream}\n");
533 delete $work{"$node$self->{stream}"};
538 # get a new transaction number from the file specified
542 $name =~ s/\W//og; # remove non-word characters
543 my $fn = "$msgdir/$name";
546 my $fh = new FileHandle;
547 if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
549 $msgno = $fh->getline;
553 $fh->print("$msgno\n");
554 dbg('msg', "msgno $msgno allocated for $name\n");
557 confess "can't open $fn $!";
562 # initialise the message 'system', read in all the message headers
565 my $dir = new FileHandle;
569 # read in the directory
570 opendir($dir, $msgdir) or confess "can't open $msgdir $!";
571 @dir = readdir($dir);
578 $ref = read_msg_header("$msgdir/$_");
581 # add the message to the available queue
587 # add the message to the directory listing
591 confess "tried to add a non-ref to the msg directory" if !ref $ref;
595 # return all the current messages
601 # get a particular message
606 return $_ if $_->{msgno} == $msgno;
607 last if $_->{msgno} > $msgno;
612 # return the official filename for a message no
615 return sprintf "$msgdir/m%06d", shift;
619 # return a list of valid elements
628 # return a prompt for a field
633 my ($self, $ele) = @_;
638 # send a message state machine
645 if ($self->state eq 'send1') {
647 confess "local var gone missing" if !ref $self->{loc};
648 my $loc = $self->{loc};
649 $loc->{subject} = $line;
651 $self->state('sendbody');
652 #push @out, $self->msg('sendbody');
653 push @out, "Enter Message /EX (^Z) to send or /ABORT (^Y) to exit";
654 } elsif ($self->state eq 'sendbody') {
655 confess "local var gone missing" if !ref $self->{loc};
656 my $loc = $self->{loc};
657 if ($line eq "\032" || uc $line eq "/EX") {
660 if (@{$loc->{lines}} > 0) {
661 foreach $to (@{$loc->{to}}) {
663 my $systime = $main::systime;
664 my $mycall = $main::mycall;
665 $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
674 $ref->store($loc->{lines});
676 #push @out, $self->msg('sendsent', $to);
677 push @out, "msgno $ref->{msgno} sent to $to";
678 my $dxchan = DXChannel->get(uc $to);
680 if ($dxchan->is_user()) {
681 $dxchan->send("New mail has arrived for you");
686 delete $loc->{lines};
689 $self->state('prompt');
692 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
693 #push @out, $self->msg('sendabort');
694 push @out, "aborted";
695 delete $loc->{lines};
699 $self->state('prompt');
702 # i.e. it ain't and end or abort, therefore store the line
703 push @{$loc->{lines}}, $line;
709 # return the standard directory line for this ref
713 return sprintf "%6d%s%s%5d %8.8s %8.8s %-6.6s %5.5s %-30.30s",
714 $ref->msgno, $ref->read ? '-' : ' ', $ref->private ? 'p' : ' ', $ref->size,
715 $ref->to, $ref->from, cldate($ref->t), ztime($ref->t), $ref->subject;
722 my $name = $AUTOLOAD;
723 return if $name =~ /::DESTROY$/;
726 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
727 @_ ? $self->{$name} = shift : $self->{$name} ;