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);
34 %work = (); # outstanding jobs
35 @msg = (); # messages we have
36 %busy = (); # station interlocks
37 $msgdir = "$main::root/msg"; # directory contain the msgs
38 $maxage = 30 * 86400; # the maximum age that a message shall live for if not marked
39 $last_clean = 0; # last time we did a clean
42 fromnode => '9,From Node',
43 tonode => '9,To Node',
46 t => '0,Msg Time,cldatetime',
47 private => '9,Private',
48 subject => '0,Subject',
49 linesreq => '0,Lines per Gob',
50 rrreq => '9,Read Confirm',
53 stream => '9,Stream No',
54 count => '9,Gob Linecnt',
55 file => '9,File?,yesno',
56 gotit => '9,Got it Nodes,parray',
57 lines => '9,Lines,parray',
58 'read' => '9,Times read',
61 keep => '0,Keep this?,yesno',
64 # allocate a new object
65 # called fromnode, tonode, from, to, datetime, private?, subject, nolinesper
69 my $self = bless {}, $pkg;
70 $self->{msgno} = shift;
76 $self->{from} = uc $from;
78 $self->{private} = shift;
79 $self->{subject} = shift;
80 $self->{origin} = shift;
81 $self->{'read'} = shift;
82 $self->{rrreq} = shift;
92 delete $ref->{linesreq};
93 delete $ref->{tonode};
94 delete $ref->{fromnode};
95 delete $ref->{stream};
103 my ($self, $line) = @_;
104 my @f = split /[\^\~]/, $line;
105 my ($pcno) = $f[0] =~ /^PC(\d\d)/; # just get the number
108 if ($pcno == 28) { # incoming message
109 my $t = cltounix($f[5], $f[6]);
110 my $stream = next_transno($f[2]);
111 my $ref = DXMsg->alloc($stream, uc $f[3], $f[4], $t, $f[7], $f[8], $f[13], '0', $f[11]);
113 # fill in various forwarding state variables
114 $ref->{fromnode} = $f[2];
115 $ref->{tonode} = $f[1];
116 $ref->{rrreq} = $f[11];
117 $ref->{linesreq} = $f[10];
118 $ref->{stream} = $stream;
119 $ref->{count} = 0; # no of lines between PC31s
120 dbg('msg', "new message from $f[4] to $f[3] '$f[8]' stream $stream\n");
121 $work{"$f[2]$stream"} = $ref; # store in work
122 $busy{$f[2]} = $ref; # set interlock
123 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
127 if ($pcno == 29) { # incoming text
128 my $ref = $work{"$f[2]$f[3]"};
130 push @{$ref->{lines}}, $f[4];
132 if ($ref->{count} >= $ref->{linesreq}) {
133 $self->send(DXProt::pc31($f[2], $f[1], $f[3]));
134 dbg('msg', "stream $f[3]: $ref->{count} lines received\n");
141 if ($pcno == 30) { # this is a incoming subject ack
142 my $ref = $work{$f[2]}; # note no stream at this stage
145 $ref->{stream} = $f[3];
147 $ref->{linesreq} = 5;
148 $work{"$f[2]$f[3]"} = $ref; # new ref
149 dbg('msg', "incoming subject ack stream $f[3]\n");
150 $busy{$f[2]} = $ref; # interlock
152 push @{$ref->{lines}}, ($ref->read_msg_body);
153 $ref->send_tranche($self);
155 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
160 if ($pcno == 31) { # acknowledge a tranche of lines
161 my $ref = $work{"$f[2]$f[3]"};
163 dbg('msg', "tranche ack stream $f[3]\n");
164 $ref->send_tranche($self);
166 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
171 if ($pcno == 32) { # incoming EOM
172 dbg('msg', "stream $f[3]: EOM received\n");
173 my $ref = $work{"$f[2]$f[3]"};
175 $self->send(DXProt::pc33($f[2], $f[1], $f[3])); # acknowledge it
177 # get the next msg no - note that this has NOTHING to do with the stream number in PC protocol
178 # store the file or message
179 # remove extraneous rubbish from the hash
180 # remove it from the work in progress vector
181 # stuff it on the msg queue
182 if ($ref->{lines} && @{$ref->{lines}} > 0) { # ignore messages with 0 lines
184 $ref->store($ref->{lines});
187 # does an identical message already exist?
190 if ($ref->{subject} eq $m->{subject} && $ref->{t} == $m->{t} && $ref->{from} eq $m->{from}) {
191 $ref->stop_msg($self);
192 my $msgno = $m->{msgno};
193 dbg('msg', "duplicate message to $msgno\n");
194 Log('msg', "duplicate message to $msgno");
199 $ref->{msgno} = next_transno("Msgno");
200 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
201 $ref->store($ref->{lines});
203 my $dxchan = DXChannel->get($ref->{to});
204 $dxchan->send("New mail has arrived for you") if $dxchan;
205 Log('msg', "Message $ref->{msgno} from $ref->{from} received from $f[2] for $ref->{to}");
208 $ref->stop_msg($self);
211 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
217 if ($pcno == 33) { # acknowledge the end of message
218 my $ref = $work{"$f[2]$f[3]"};
220 if ($ref->{private}) { # remove it if it private and gone off site#
221 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2] and deleted");
224 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2]");
225 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
226 $ref->store($ref->{lines}); # re- store the file
228 $ref->stop_msg($self);
230 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
236 if ($pcno == 40) { # this is a file request
237 $f[3] =~ s/\\/\//og; # change the slashes
238 $f[3] =~ s/\.//og; # remove dots
239 $f[3] =~ s/^\///o; # remove the leading /
240 $f[3] = lc $f[3]; # to lower case;
241 dbg('msg', "incoming file $f[3]\n");
242 last SWITCH if $f[3] =~ /^(perl|cmd|local|src|lib|include|sys|msg|connect)/; # prevent access to executables
244 # create any directories
245 my @part = split /\//, $f[3];
247 my $fn = "$main::root";
248 pop @part; # remove last part
249 foreach $part (@part) {
252 last SWITCH if !mkdir $fn, 0777;
253 dbg('msg', "created directory $fn\n");
255 my $stream = next_transno($f[2]);
256 my $ref = DXMsg->alloc($stream, "$main::root/$f[3]", $self->call, time, !$f[4], $f[3], ' ', '0', '0');
258 # forwarding variables
259 $ref->{fromnode} = $f[1];
260 $ref->{tonode} = $f[2];
261 $ref->{linesreq} = $f[5];
262 $ref->{stream} = $stream;
263 $ref->{count} = 0; # no of lines between PC31s
265 $work{"$f[2]$stream"} = $ref; # store in work
266 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
271 if ($pcno == 42) { # abort transfer
272 dbg('msg', "stream $f[3]: abort received\n");
273 my $ref = $work{"$f[2]$f[3]"};
275 $ref->stop_msg($self);
282 if ($pcno == 49) { # global delete on subject
284 if ($_->{subject} eq $f[2]) {
286 Log('msg', "Message $_->{msgno} fully deleted by $f[1]");
292 clean_old() if $main::systime - $last_clean > 3600 ; # clean the message queue
296 # store a message away on disc or whatever
298 # NOTE the second arg is a REFERENCE not a list
304 # we only proceed if there are actually any lines in the file
305 if (!$lines || @{$lines} == 0) {
309 if ($ref->{file}) { # a file
310 dbg('msg', "To be stored in $ref->{to}\n");
312 my $fh = new FileHandle "$ref->{to}", "w";
315 foreach $line (@{$lines}) {
319 dbg('msg', "file $ref->{to} stored\n");
320 Log('msg', "file $ref->{to} from $ref->{from} stored" );
322 confess "can't open file $ref->{to} $!";
324 } else { # a normal message
326 # attempt to open the message file
327 my $fn = filename($ref->{msgno});
329 dbg('msg', "To be stored in $fn\n");
331 # now save the file, overwriting what's there, YES I KNOW OK! (I will change it if it's a problem)
332 my $fh = new FileHandle "$fn", "w";
334 my $rr = $ref->{rrreq} ? '1' : '0';
335 my $priv = $ref->{private} ? '1': '0';
336 print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$priv^$ref->{subject}^$ref->{origin}^$ref->{'read'}^$rr\n";
337 print $fh "=== ", join('^', @{$ref->{gotit}}), "\n";
340 foreach $line (@{$lines}) {
341 $ref->{size} += (length $line) + 1;
345 dbg('msg', "msg $ref->{msgno} stored\n");
346 Log('msg', "msg $ref->{msgno} from $ref->{from} to $ref->{to} stored" );
348 confess "can't open msg file $fn $!";
358 # remove it from the active message list
359 @msg = map { $_ != $self ? $_ : () } @msg;
361 # belt and braces (one day I will ask someone if this is REALLY necessary)
362 delete $self->{gotit};
363 delete $self->{list};
366 unlink filename($self->{msgno});
367 dbg('msg', "deleting $self->{msgno}\n");
370 # clean out old messages from the message queue
375 # mark old messages for deletion
376 foreach $ref (@msg) {
377 if (!$ref->{keep} && $ref->{t} < $main::systime - $maxage) {
378 $ref->{deleteme} = 1;
379 delete $ref->{gotit};
381 unlink filename($ref->{msgno});
382 dbg('msg', "deleting old $ref->{msgno}\n");
386 # remove them all from the active message list
387 @msg = map { $_->{deleteme} ? () : $_ } @msg;
388 $last_clean = $main::systime;
391 # read in a message header
401 $file = new FileHandle;
402 if (!open($file, $fn)) {
403 print "Error reading $fn $!\n";
407 $line = <$file>; # first line
409 $size -= length $line;
410 if (! $line =~ /^===/o) {
411 print "corrupt first line in $fn ($line)\n";
415 @f = split /\^/, $line;
416 $ref = DXMsg->alloc(@f);
418 $line = <$file>; # second line
420 $size -= length $line;
421 if (! $line =~ /^===/o) {
422 print "corrupt second line in $fn ($line)\n";
427 @f = split /\^/, $line;
428 push @{$ref->{gotit}}, @f;
429 $ref->{size} = $size;
436 # read in a message header
440 my $msgno = $self->{msgno};
443 my $fn = filename($msgno);
446 $file = new FileHandle;
447 if (!open($file, $fn)) {
448 print "Error reading $fn $!\n";
451 chomp (@out = <$file>);
454 shift @out if $out[0] =~ /^=== /;
455 shift @out if $out[0] =~ /^=== /;
459 # send a tranche of lines to the other end
462 my ($self, $dxchan) = @_;
464 my $to = $self->{tonode};
465 my $from = $self->{fromnode};
466 my $stream = $self->{stream};
467 my $lines = $self->{lines};
470 for ($i = 0, $c = $self->{count}; $i < $self->{linesreq} && $c < @$lines; $i++, $c++) {
471 push @out, DXProt::pc29($to, $from, $stream, $lines->[$c]);
475 push @out, DXProt::pc32($to, $from, $stream) if $i < $self->{linesreq};
480 # find a message to send out and start the ball rolling
488 my @nodelist = DXProt::get_all_ak1a();
490 # bat down the message list looking for one that needs to go off site and whose
491 # nearest node is not busy.
493 dbg('msg', "queue msg ($sort)\n");
494 foreach $ref (@msg) {
495 # firstly, is it private and unread? if so can I find the recipient
496 # in my cluster node list offsite?
497 if ($ref->{private}) {
498 if ($ref->{'read'} == 0) {
499 $clref = DXCluster->get_exact($ref->{to});
500 unless ($clref) { # otherwise look for a homenode
501 my $uref = DXUser->get($ref->{to});
502 my $hnode = $uref->homenode if $uref;
503 $clref = DXCluster->get_exact($hnode) if $hnode;
505 if ($clref && !grep { $clref->{dxchan} == $_ } DXCommandmode::get_all) {
506 $dxchan = $clref->{dxchan};
507 $ref->start_msg($dxchan) if $clref && !get_busy($dxchan->call) && $dxchan->state eq 'normal';
511 # otherwise we are dealing with a bulletin, compare the gotit list with
512 # the nodelist up above, if there are sites that haven't got it yet
513 # then start sending it - what happens when we get loops is anyone's
514 # guess, use (to, from, time, subject) tuple?
516 foreach $noderef (@nodelist) {
517 next if $noderef->call eq $main::mycall;
518 next if $noderef->isolate; # maybe add code for stuff originated here?
519 next if grep { $_ eq $noderef->call } @{$ref->{gotit}};
521 # if we are here we have a node that doesn't have this message
522 $ref->start_msg($noderef) if !get_busy($noderef->call) && $noderef->state eq 'normal';
527 # if all the available nodes are busy then stop
528 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
532 # start the message off on its travels with a PC28
535 my ($self, $dxchan) = @_;
537 dbg('msg', "start msg $self->{msgno}\n");
538 $self->{linesreq} = 5;
540 $self->{tonode} = $dxchan->call;
541 $self->{fromnode} = $main::mycall;
542 $busy{$dxchan->call} = $self;
543 $work{"$self->{tonode}"} = $self;
544 $dxchan->send(DXProt::pc28($self->{tonode}, $self->{fromnode}, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $self->{origin}, $self->{rrreq}));
547 # get the ref of a busy node
560 # get the forwarding queue
566 # stop a message from continuing, clean it out, unlock interlocks etc
569 my ($self, $dxchan) = @_;
570 my $node = $dxchan->call;
572 dbg('msg', "stop msg $self->{msgno} stream $self->{stream}\n");
574 delete $work{"$node$self->{stream}"};
579 # get a new transaction number from the file specified
583 $name =~ s/\W//og; # remove non-word characters
584 my $fn = "$msgdir/$name";
587 my $fh = new FileHandle;
588 if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
590 $msgno = $fh->getline;
594 $fh->print("$msgno\n");
595 dbg('msg', "msgno $msgno allocated for $name\n");
598 confess "can't open $fn $!";
603 # initialise the message 'system', read in all the message headers
606 my $dir = new FileHandle;
610 # read in the directory
611 opendir($dir, $msgdir) or confess "can't open $msgdir $!";
612 @dir = readdir($dir);
619 $ref = read_msg_header("$msgdir/$_");
622 # add the message to the available queue
628 # add the message to the directory listing
632 confess "tried to add a non-ref to the msg directory" if !ref $ref;
636 # return all the current messages
642 # get a particular message
647 return $_ if $_->{msgno} == $msgno;
648 last if $_->{msgno} > $msgno;
653 # return the official filename for a message no
656 return sprintf "$msgdir/m%06d", shift;
660 # return a list of valid elements
669 # return a prompt for a field
674 my ($self, $ele) = @_;
679 # send a message state machine
686 if ($self->state eq 'send1') {
688 confess "local var gone missing" if !ref $self->{loc};
689 my $loc = $self->{loc};
690 $loc->{subject} = $line;
692 $self->state('sendbody');
693 #push @out, $self->msg('sendbody');
694 push @out, "Enter Message /EX (^Z) to send or /ABORT (^Y) to exit";
695 } elsif ($self->state eq 'sendbody') {
696 confess "local var gone missing" if !ref $self->{loc};
697 my $loc = $self->{loc};
698 if ($line eq "\032" || uc $line eq "/EX") {
701 if (@{$loc->{lines}} > 0) {
702 foreach $to (@{$loc->{to}}) {
704 my $systime = $main::systime;
705 my $mycall = $main::mycall;
706 $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
715 $ref->store($loc->{lines});
717 #push @out, $self->msg('sendsent', $to);
718 push @out, "msgno $ref->{msgno} sent to $to";
719 my $dxchan = DXChannel->get(uc $to);
721 if ($dxchan->is_user()) {
722 $dxchan->send("New mail has arrived for you");
727 delete $loc->{lines};
730 $self->state('prompt');
733 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
734 #push @out, $self->msg('sendabort');
735 push @out, "aborted";
736 delete $loc->{lines};
740 $self->state('prompt');
743 # i.e. it ain't and end or abort, therefore store the line
744 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
750 # return the standard directory line for this ref
754 return sprintf "%6d%s%s%5d %8.8s %8.8s %-6.6s %5.5s %-30.30s",
755 $ref->msgno, $ref->read ? '-' : ' ', $ref->private ? 'p' : ' ', $ref->size,
756 $ref->to, $ref->from, cldate($ref->t), ztime($ref->t), $ref->subject;
763 my $name = $AUTOLOAD;
764 return if $name =~ /::DESTROY$/;
767 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
768 @_ ? $self->{$name} = shift : $self->{$name} ;