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
140 $ref->{stream} = $f[3];
142 $ref->{linesreq} = 5;
143 $work{"$f[2]$f[3]"} = $ref; # new ref
144 dbg('msg', "incoming subject ack stream $f[3]\n");
145 $busy{$f[2]} = $ref; # interlock
147 push @{$ref->{lines}}, ($ref->read_msg_body);
148 $ref->send_tranche($self);
150 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
155 if ($pcno == 31) { # acknowledge a tranche of lines
156 my $ref = $work{"$f[2]$f[3]"};
158 dbg('msg', "tranche ack stream $f[3]\n");
159 $ref->send_tranche($self);
161 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
166 if ($pcno == 32) { # incoming EOM
167 dbg('msg', "stream $f[3]: EOM received\n");
168 my $ref = $work{"$f[2]$f[3]"};
170 $self->send(DXProt::pc33($f[2], $f[1], $f[3])); # acknowledge it
172 # get the next msg no - note that this has NOTHING to do with the stream number in PC protocol
173 # store the file or message
174 # remove extraneous rubbish from the hash
175 # remove it from the work in progress vector
176 # stuff it on the msg queue
177 if ($ref->{lines} && @{$ref->{lines}} > 0) { # ignore messages with 0 lines
179 $ref->store($ref->{lines});
182 # does an identical message already exist?
185 if ($ref->{subject} eq $m->{subject} && $ref->{t} == $m->{t} && $ref->{from} eq $m->{from}) {
186 $ref->stop_msg($self);
187 my $msgno = $m->{msgno};
188 dbg('msg', "duplicate message to $msgno\n");
189 Log('msg', "duplicate message to $msgno");
194 $ref->{msgno} = next_transno("Msgno");
195 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
196 $ref->store($ref->{lines});
198 my $dxchan = DXChannel->get($ref->{to});
199 $dxchan->send("New mail has arrived for you") if $dxchan;
200 Log('msg', "Message $ref->{msgno} from $ref->{from} received from $f[2] for $ref->{to}");
203 $ref->stop_msg($self);
206 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
212 if ($pcno == 33) { # acknowledge the end of message
213 my $ref = $work{"$f[2]$f[3]"};
215 if ($ref->{private}) { # remove it if it private and gone off site#
216 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2] and deleted");
219 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2]");
220 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
221 $ref->store($ref->{lines}); # re- store the file
223 $ref->stop_msg($self);
225 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
231 if ($pcno == 40) { # this is a file request
232 $f[3] =~ s/\\/\//og; # change the slashes
233 $f[3] =~ s/\.//og; # remove dots
234 $f[3] =~ s/^\///o; # remove the leading /
235 $f[3] = lc $f[3]; # to lower case;
236 dbg('msg', "incoming file $f[3]\n");
237 last SWITCH if $f[3] =~ /^(perl|cmd|local|src|lib|include|sys|msg|connect)/; # prevent access to executables
239 # create any directories
240 my @part = split /\//, $f[3];
242 my $fn = "$main::root";
243 pop @part; # remove last part
244 foreach $part (@part) {
247 last SWITCH if !mkdir $fn, 0777;
248 dbg('msg', "created directory $fn\n");
250 my $stream = next_transno($f[2]);
251 my $ref = DXMsg->alloc($stream, "$main::root/$f[3]", $self->call, time, !$f[4], $f[3], ' ', '0', '0');
253 # forwarding variables
254 $ref->{fromnode} = $f[1];
255 $ref->{tonode} = $f[2];
256 $ref->{linesreq} = $f[5];
257 $ref->{stream} = $stream;
258 $ref->{count} = 0; # no of lines between PC31s
260 $work{"$f[2]$stream"} = $ref; # store in work
261 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
266 if ($pcno == 42) { # abort transfer
267 dbg('msg', "stream $f[3]: abort received\n");
268 my $ref = $work{"$f[2]$f[3]"};
270 $ref->stop_msg($self);
277 if ($pcno == 49) { # global delete on subject
279 if ($_->{subject} eq $f[2]) {
281 Log('msg', "Message $_->{msgno} fully deleted by $f[1]");
287 clean_old() if $main::systime - $last_clean > 3600 ; # clean the message queue
291 # store a message away on disc or whatever
293 # NOTE the second arg is a REFERENCE not a list
299 # we only proceed if there are actually any lines in the file
300 if (!$lines || @{$lines} == 0) {
304 if ($ref->{file}) { # a file
305 dbg('msg', "To be stored in $ref->{to}\n");
307 my $fh = new FileHandle "$ref->{to}", "w";
310 foreach $line (@{$lines}) {
314 dbg('msg', "file $ref->{to} stored\n");
315 Log('msg', "file $ref->{to} from $ref->{from} stored" );
317 confess "can't open file $ref->{to} $!";
319 } else { # a normal message
321 # attempt to open the message file
322 my $fn = filename($ref->{msgno});
324 dbg('msg', "To be stored in $fn\n");
326 # now save the file, overwriting what's there, YES I KNOW OK! (I will change it if it's a problem)
327 my $fh = new FileHandle "$fn", "w";
329 my $rr = $ref->{rrreq} ? '1' : '0';
330 my $priv = $ref->{private} ? '1': '0';
331 print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$priv^$ref->{subject}^$ref->{origin}^$ref->{read}^$rr\n";
332 print $fh "=== ", join('^', @{$ref->{gotit}}), "\n";
335 foreach $line (@{$lines}) {
336 $ref->{size} += (length $line) + 1;
340 dbg('msg', "msg $ref->{msgno} stored\n");
341 Log('msg', "msg $ref->{msgno} from $ref->{from} to $ref->{to} stored" );
343 confess "can't open msg file $fn $!";
353 # remove it from the active message list
354 @msg = map { $_ != $self ? $_ : () } @msg;
356 # belt and braces (one day I will ask someone if this is REALLY necessary)
357 delete $self->{gotit};
358 delete $self->{list};
361 unlink filename($self->{msgno});
362 dbg('msg', "deleting $self->{msgno}\n");
365 # clean out old messages from the message queue
370 # mark old messages for deletion
371 foreach $ref (@msg) {
372 if (!$ref->{keep} && $ref->{t} < $main::systime - $maxage) {
373 $ref->{deleteme} = 1;
374 delete $ref->{gotit};
376 unlink filename($ref->{msgno});
377 dbg('msg', "deleting old $ref->{msgno}\n");
381 # remove them all from the active message list
382 @msg = map { $_->{deleteme} ? () : $_ } @msg;
383 $last_clean = $main::systime;
386 # read in a message header
396 $file = new FileHandle;
397 if (!open($file, $fn)) {
398 print "Error reading $fn $!\n";
402 $line = <$file>; # first line
404 $size -= length $line;
405 if (! $line =~ /^===/o) {
406 print "corrupt first line in $fn ($line)\n";
410 @f = split /\^/, $line;
411 $ref = DXMsg->alloc(@f);
413 $line = <$file>; # second line
415 $size -= length $line;
416 if (! $line =~ /^===/o) {
417 print "corrupt second line in $fn ($line)\n";
422 @f = split /\^/, $line;
423 push @{$ref->{gotit}}, @f;
424 $ref->{size} = $size;
431 # read in a message header
435 my $msgno = $self->{msgno};
438 my $fn = filename($msgno);
441 $file = new FileHandle;
442 if (!open($file, $fn)) {
443 print "Error reading $fn $!\n";
446 chomp (@out = <$file>);
449 shift @out if $out[0] =~ /^=== /;
450 shift @out if $out[0] =~ /^=== /;
454 # send a tranche of lines to the other end
457 my ($self, $dxchan) = @_;
459 my $to = $self->{tonode};
460 my $from = $self->{fromnode};
461 my $stream = $self->{stream};
462 my $lines = $self->{lines};
465 for ($i = 0, $c = $self->{count}; $i < $self->{linesreq} && $c < @$lines; $i++, $c++) {
466 push @out, DXProt::pc29($to, $from, $stream, $lines->[$c]);
470 push @out, DXProt::pc32($to, $from, $stream) if $i < $self->{linesreq};
475 # find a message to send out and start the ball rolling
480 my @nodelist = DXProt::get_all_ak1a();
485 # bat down the message list looking for one that needs to go off site and whose
486 # nearest node is not busy.
488 dbg('msg', "queue msg ($sort)\n");
489 foreach $ref (@msg) {
490 # firstly, is it private and unread? if so can I find the recipient
491 # in my cluster node list offsite?
492 if ($ref->{private}) {
493 if ($ref->{read} == 0) {
494 $clref = DXCluster->get($ref->{to});
495 if ($clref && !grep { $clref->{dxchan} == $_ } DXCommandmode::get_all) {
496 $dxchan = $clref->{dxchan};
497 $ref->start_msg($dxchan) if $clref && !get_busy($dxchan->call) && $dxchan->state eq 'normal';
500 } elsif ($sort == undef) {
501 # otherwise we are dealing with a bulletin, compare the gotit list with
502 # the nodelist up above, if there are sites that haven't got it yet
503 # then start sending it - what happens when we get loops is anyone's
504 # guess, use (to, from, time, subject) tuple?
506 foreach $noderef (@nodelist) {
507 next if $noderef->call eq $main::mycall;
508 next if grep { $_ eq $noderef->call } @{$ref->{gotit}};
510 # if we are here we have a node that doesn't have this message
511 $ref->start_msg($noderef) if !get_busy($noderef->call) && $noderef->state eq 'normal';
516 # if all the available nodes are busy then stop
517 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
521 # start the message off on its travels with a PC28
524 my ($self, $dxchan) = @_;
526 dbg('msg', "start msg $self->{msgno}\n");
527 $self->{linesreq} = 5;
529 $self->{tonode} = $dxchan->call;
530 $self->{fromnode} = $main::mycall;
531 $busy{$dxchan->call} = $self;
532 $work{"$self->{tonode}"} = $self;
533 $dxchan->send(DXProt::pc28($self->{tonode}, $self->{fromnode}, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $self->{origin}, $self->{rrreq}));
536 # get the ref of a busy node
549 # get the forwarding queue
555 # stop a message from continuing, clean it out, unlock interlocks etc
558 my ($self, $dxchan) = @_;
559 my $node = $dxchan->call;
561 dbg('msg', "stop msg $self->{msgno} stream $self->{stream}\n");
563 delete $work{"$node$self->{stream}"};
568 # get a new transaction number from the file specified
572 $name =~ s/\W//og; # remove non-word characters
573 my $fn = "$msgdir/$name";
576 my $fh = new FileHandle;
577 if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
579 $msgno = $fh->getline;
583 $fh->print("$msgno\n");
584 dbg('msg', "msgno $msgno allocated for $name\n");
587 confess "can't open $fn $!";
592 # initialise the message 'system', read in all the message headers
595 my $dir = new FileHandle;
599 # read in the directory
600 opendir($dir, $msgdir) or confess "can't open $msgdir $!";
601 @dir = readdir($dir);
608 $ref = read_msg_header("$msgdir/$_");
611 # add the message to the available queue
617 # add the message to the directory listing
621 confess "tried to add a non-ref to the msg directory" if !ref $ref;
625 # return all the current messages
631 # get a particular message
636 return $_ if $_->{msgno} == $msgno;
637 last if $_->{msgno} > $msgno;
642 # return the official filename for a message no
645 return sprintf "$msgdir/m%06d", shift;
649 # return a list of valid elements
658 # return a prompt for a field
663 my ($self, $ele) = @_;
668 # send a message state machine
675 if ($self->state eq 'send1') {
677 confess "local var gone missing" if !ref $self->{loc};
678 my $loc = $self->{loc};
679 $loc->{subject} = $line;
681 $self->state('sendbody');
682 #push @out, $self->msg('sendbody');
683 push @out, "Enter Message /EX (^Z) to send or /ABORT (^Y) to exit";
684 } elsif ($self->state eq 'sendbody') {
685 confess "local var gone missing" if !ref $self->{loc};
686 my $loc = $self->{loc};
687 if ($line eq "\032" || uc $line eq "/EX") {
690 if (@{$loc->{lines}} > 0) {
691 foreach $to (@{$loc->{to}}) {
693 my $systime = $main::systime;
694 my $mycall = $main::mycall;
695 $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
704 $ref->store($loc->{lines});
706 #push @out, $self->msg('sendsent', $to);
707 push @out, "msgno $ref->{msgno} sent to $to";
708 my $dxchan = DXChannel->get(uc $to);
710 if ($dxchan->is_user()) {
711 $dxchan->send("New mail has arrived for you");
716 delete $loc->{lines};
719 $self->state('prompt');
722 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
723 #push @out, $self->msg('sendabort');
724 push @out, "aborted";
725 delete $loc->{lines};
729 $self->state('prompt');
732 # i.e. it ain't and end or abort, therefore store the line
733 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
739 # return the standard directory line for this ref
743 return sprintf "%6d%s%s%5d %8.8s %8.8s %-6.6s %5.5s %-30.30s",
744 $ref->msgno, $ref->read ? '-' : ' ', $ref->private ? 'p' : ' ', $ref->size,
745 $ref->to, $ref->from, cldate($ref->t), ztime($ref->t), $ref->subject;
752 my $name = $AUTOLOAD;
753 return if $name =~ /::DESTROY$/;
756 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
757 @_ ? $self->{$name} = shift : $self->{$name} ;