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);
29 %work = (); # outstanding jobs
30 @msg = (); # messages we have
31 %busy = (); # station interlocks
32 $msgdir = "$main::root/msg"; # directory contain the msgs
35 fromnode => '9,From Node',
36 tonode => '9,To Node',
39 t => '0,Msg Time,cldatetime',
40 private => '9,Private',
41 subject => '0,Subject',
42 linesreq => '0,Lines per Gob',
43 rrreq => '9,Read Confirm',
46 stream => '9,Stream No',
47 count => '9,Gob Linecnt',
48 file => '9,File?,yesno',
49 gotit => '9,Got it Nodes,parray',
50 lines => '9,Lines,parray',
51 read => '9,Times read',
56 # allocate a new object
57 # called fromnode, tonode, from, to, datetime, private?, subject, nolinesper
61 my $self = bless {}, $pkg;
62 $self->{msgno} = shift;
64 $self->{from} = shift;
66 $self->{private} = shift;
67 $self->{subject} = shift;
68 $self->{origin} = shift;
69 $self->{read} = shift;
70 $self->{rrreq} = shift;
80 delete $ref->{linesreq};
81 delete $ref->{tonode};
82 delete $ref->{fromnode};
83 delete $ref->{stream};
91 my ($self, $line) = @_;
92 my @f = split /[\^\~]/, $line;
93 my ($pcno) = $f[0] =~ /^PC(\d\d)/; # just get the number
96 if ($pcno == 28) { # incoming message
97 my $t = cltounix($f[5], $f[6]);
98 my $stream = next_transno($f[2]);
99 my $ref = DXMsg->alloc($stream, $f[3], $f[4], $t, $f[7], $f[8], $f[13], '0', $f[11]);
101 # fill in various forwarding state variables
102 $ref->{fromnode} = $f[2];
103 $ref->{tonode} = $f[1];
104 $ref->{rrreq} = $f[11];
105 $ref->{linesreq} = $f[10];
106 $ref->{stream} = $stream;
107 $ref->{count} = 0; # no of lines between PC31s
108 dbg('msg', "new message from $f[4] to $f[3] '$f[8]' stream $stream\n");
109 $work{"$f[2]$stream"} = $ref; # store in work
110 $busy{$f[2]} = $ref; # set interlock
111 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
115 if ($pcno == 29) { # incoming text
116 my $ref = $work{"$f[2]$f[3]"};
118 push @{$ref->{lines}}, $f[4];
120 if ($ref->{count} >= $ref->{linesreq}) {
121 $self->send(DXProt::pc31($f[2], $f[1], $f[3]));
122 dbg('msg', "stream $f[3]: $ref->{linereq} lines received\n");
129 if ($pcno == 30) { # this is a incoming subject ack
130 my $ref = $work{$f[2]}; # note no stream at this stage
132 $ref->{stream} = $f[3];
134 $ref->{linesreq} = 5;
135 $work{"$f[2]$f[3]"} = $ref; # new ref
136 dbg('msg', "incoming subject ack stream $f[3]\n");
137 $busy{$f[2]} = $ref; # interlock
139 push @{$ref->{lines}}, ($ref->read_msg_body);
140 $ref->send_tranche($self);
144 if ($pcno == 31) { # acknowledge a tranche of lines
145 my $ref = $work{"$f[2]$f[3]"};
147 dbg('msg', "tranche ack stream $f[3]\n");
148 $ref->send_tranche($self);
150 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
155 if ($pcno == 32) { # incoming EOM
156 dbg('msg', "stream $f[3]: EOM received\n");
157 my $ref = $work{"$f[2]$f[3]"};
159 $self->send(DXProt::pc33($f[2], $f[1], $f[3]));# acknowledge it
161 # get the next msg no - note that this has NOTHING to do with the stream number in PC protocol
162 # store the file or message
163 # remove extraneous rubbish from the hash
164 # remove it from the work in progress vector
165 # stuff it on the msg queue
166 if ($ref->{lines} && @{$ref->{lines}} > 0) { # ignore messages with 0 lines
167 $ref->{msgno} = next_transno("Msgno") if !$ref->{file};
168 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
169 $ref->store($ref->{lines});
171 my $dxchan = DXChannel->get($ref->{to});
172 $dxchan->send("New mail has arrived for you") if $dxchan;
174 $ref->stop_msg($self);
177 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
183 if ($pcno == 33) { # acknowledge the end of message
184 my $ref = $work{"$f[2]$f[3]"};
186 if ($ref->{private}) { # remove it if it private and gone off site#
189 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
190 $ref->store($ref->{lines}); # re- store the file
192 $ref->stop_msg($self);
194 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
200 if ($pcno == 40) { # this is a file request
201 $f[3] =~ s/\\/\//og; # change the slashes
202 $f[3] =~ s/\.//og; # remove dots
203 $f[3] = lc $f[3]; # to lower case;
204 dbg('msg', "incoming file $f[3]\n");
205 last SWITCH if $f[3] =~ /^\/(perl|cmd|local_cmd|src|lib|include|sys|msg)\//; # prevent access to executables
207 # create any directories
208 my @part = split /\//, $f[3];
210 my $fn = "$main::root";
211 pop @part; # remove last part
212 foreach $part (@part) {
215 last SWITCH if !mkdir $fn, 0777;
216 dbg('msg', "created directory $fn\n");
218 my $stream = next_transno($f[2]);
219 my $ref = DXMsg->alloc($stream, "$main::root/$f[3]", $self->call, time, !$f[4], $f[3], ' ', '0', '0');
221 # forwarding variables
222 $ref->{fromnode} = $f[1];
223 $ref->{tonode} = $f[2];
224 $ref->{linesreq} = $f[5];
225 $ref->{stream} = $stream;
226 $ref->{count} = 0; # no of lines between PC31s
228 $work{"$f[2]$stream"} = $ref; # store in work
229 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
234 if ($pcno == 42) { # abort transfer
235 dbg('msg', "stream $f[3]: abort received\n");
236 my $ref = $work{"$f[2]$f[3]"};
238 $ref->stop_msg($self);
248 # store a message away on disc or whatever
250 # NOTE the second arg is a REFERENCE not a list
256 # we only proceed if there are actually any lines in the file
257 if (!$lines || @{$lines} == 0) {
261 if ($ref->{file}) { # a file
262 dbg('msg', "To be stored in $ref->{to}\n");
264 my $fh = new FileHandle "$ref->{to}", "w";
267 foreach $line (@{$lines}) {
271 dbg('msg', "file $ref->{to} stored\n");
272 Log('msg', "file $ref->{to} from $ref->{from} stored" );
274 confess "can't open file $ref->{to} $!";
276 # push @{$ref->{gotit}}, $ref->{fromnode} if $ref->{fromnode};
277 } else { # a normal message
279 # attempt to open the message file
280 my $fn = filename($ref->{msgno});
282 dbg('msg', "To be stored in $fn\n");
284 # now save the file, overwriting what's there, YES I KNOW OK! (I will change it if it's a problem)
285 my $fh = new FileHandle "$fn", "w";
287 my $rr = $ref->{rrreq} ? '1' : '0';
288 my $priv = $ref->{private} ? '1': '0';
289 print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$priv^$ref->{subject}^$ref->{origin}^$ref->{read}^$rr\n";
290 print $fh "=== ", join('^', @{$ref->{gotit}}), "\n";
293 foreach $line (@{$lines}) {
294 $ref->{size} += (length $line) + 1;
298 dbg('msg', "msg $ref->{msgno} stored\n");
299 Log('msg', "msg $ref->{msgno} from $ref->{from} to $ref->{to} stored" );
301 confess "can't open msg file $fn $!";
311 # remove it from the active message list
312 @msg = map { $_ != $self ? $_ : () } @msg;
314 # belt and braces (one day I will ask someone if this is REALLY necessary)
315 delete $self->{gotit};
316 delete $self->{list};
319 unlink filename($self->{msgno});
320 dbg('msg', "deleting $self->{msgno}\n");
323 # read in a message header
333 $file = new FileHandle;
334 if (!open($file, $fn)) {
335 print "Error reading $fn $!\n";
339 $line = <$file>; # first line
341 $size -= length $line;
342 if (! $line =~ /^===/o) {
343 print "corrupt first line in $fn ($line)\n";
347 @f = split /\^/, $line;
348 $ref = DXMsg->alloc(@f);
350 $line = <$file>; # second line
352 $size -= length $line;
353 if (! $line =~ /^===/o) {
354 print "corrupt second line in $fn ($line)\n";
359 @f = split /\^/, $line;
360 push @{$ref->{gotit}}, @f;
361 $ref->{size} = $size;
368 # read in a message header
372 my $msgno = $self->{msgno};
375 my $fn = filename($msgno);
378 $file = new FileHandle;
379 if (!open($file, $fn)) {
380 print "Error reading $fn $!\n";
383 chomp (@out = <$file>);
386 shift @out if $out[0] =~ /^=== /;
387 shift @out if $out[0] =~ /^=== /;
391 # send a tranche of lines to the other end
394 my ($self, $dxchan) = @_;
396 my $to = $self->{tonode};
397 my $from = $self->{fromnode};
398 my $stream = $self->{stream};
401 for ($i = 0; $i < $self->{linesreq} && $self->{count} < @{$self->{lines}}; $i++, $self->{count}++) {
402 push @out, DXProt::pc29($to, $from, $stream, ${$self->{lines}}[$self->{count}]);
404 push @out, DXProt::pc32($to, $from, $stream) if $i < $self->{linesreq};
409 # find a message to send out and start the ball rolling
413 my @nodelist = DXProt::get_all_ak1a();
418 # bat down the message list looking for one that needs to go off site and whose
419 # nearest node is not busy.
421 dbg('msg', "queue msg ($sort)\n");
422 foreach $ref (@msg) {
423 # firstly, is it private and unread? if so can I find the recipient
424 # in my cluster node list offsite?
425 if ($ref->{private}) {
426 if ($ref->{read} == 0) {
427 $clref = DXCluster->get($ref->{to});
428 if ($clref && !grep { $clref->{dxchan} == $_ } DXCommandmode::get_all) {
429 $dxchan = $clref->{dxchan};
430 $ref->start_msg($dxchan) if $clref && !get_busy($dxchan->call);
433 } elsif ($sort == undef) {
434 # otherwise we are dealing with a bulletin, compare the gotit list with
435 # the nodelist up above, if there are sites that haven't got it yet
436 # then start sending it - what happens when we get loops is anyone's
437 # guess, use (to, from, time, subject) tuple?
439 foreach $noderef (@nodelist) {
440 next if $noderef->call eq $main::mycall;
441 next if grep { $_ eq $noderef->call } @{$ref->{gotit}};
443 # if we are here we have a node that doesn't have this message
444 $ref->start_msg($noderef) if !get_busy($noderef->call);
449 # if all the available nodes are busy then stop
450 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
454 # start the message off on its travels with a PC28
457 my ($self, $dxchan) = @_;
459 dbg('msg', "start msg $self->{msgno}\n");
460 $self->{linesreq} = 5;
462 $self->{tonode} = $dxchan->call;
463 $self->{fromnode} = $main::mycall;
464 $busy{$dxchan->call} = $self;
465 $work{"$self->{tonode}"} = $self;
466 $dxchan->send(DXProt::pc28($self->{tonode}, $self->{fromnode}, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $self->{origin}, $self->{rrreq}));
469 # get the ref of a busy node
482 # get the forwarding queue
488 # stop a message from continuing, clean it out, unlock interlocks etc
491 my ($self, $dxchan) = @_;
492 my $node = $dxchan->call;
494 dbg('msg', "stop msg $self->{msgno} stream $self->{stream}\n");
496 delete $work{"$node$self->{stream}"};
501 # get a new transaction number from the file specified
505 $name =~ s/\W//og; # remove non-word characters
506 my $fn = "$msgdir/$name";
509 my $fh = new FileHandle;
510 if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
512 $msgno = $fh->getline;
516 $fh->print("$msgno\n");
517 dbg('msg', "msgno $msgno allocated for $name\n");
520 confess "can't open $fn $!";
525 # initialise the message 'system', read in all the message headers
528 my $dir = new FileHandle;
532 # read in the directory
533 opendir($dir, $msgdir) or confess "can't open $msgdir $!";
534 @dir = readdir($dir);
541 $ref = read_msg_header("$msgdir/$_");
544 # add the message to the available queue
550 # add the message to the directory listing
554 confess "tried to add a non-ref to the msg directory" if !ref $ref;
558 # return all the current messages
564 # get a particular message
569 return $_ if $_->{msgno} == $msgno;
570 last if $_->{msgno} > $msgno;
575 # return the official filename for a message no
578 return sprintf "$msgdir/m%06d", shift;
582 # return a list of valid elements
591 # return a prompt for a field
596 my ($self, $ele) = @_;
601 # send a message state machine
608 if ($self->state eq 'send1') {
610 confess "local var gone missing" if !ref $self->{loc};
611 my $loc = $self->{loc};
612 $loc->{subject} = $line;
614 $self->state('sendbody');
615 #push @out, $self->msg('sendbody');
616 push @out, "Enter Message /EX (^Z) to send or /ABORT (^Y) to exit";
617 } elsif ($self->state eq 'sendbody') {
618 confess "local var gone missing" if !ref $self->{loc};
619 my $loc = $self->{loc};
620 if ($line eq "\032" || uc $line eq "/EX") {
623 if (@{$loc->{lines}} > 0) {
624 foreach $to (@{$loc->{to}}) {
626 my $systime = $main::systime;
627 my $mycall = $main::mycall;
628 $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
637 $ref->store($loc->{lines});
639 #push @out, $self->msg('sendsent', $to);
640 push @out, "msgno $ref->{msgno} sent to $to";
641 my $dxchan = DXChannel->get(uc $to);
642 $dxchan->send("New mail has arrived for you") if $dxchan;
645 delete $loc->{lines};
648 $self->state('prompt');
651 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
652 #push @out, $self->msg('sendabort');
653 push @out, "aborted";
654 delete $loc->{lines};
658 $self->state('prompt');
661 # i.e. it ain't and end or abort, therefore store the line
662 push @{$loc->{lines}}, $line;
668 # return the standard directory line for this ref
672 return sprintf "%6d%s%s%5d %8.8s %8.8s %-6.6s %5.5s %-30.30s",
673 $ref->msgno, $ref->read ? '-' : ' ', $ref->private ? 'p' : ' ', $ref->size,
674 $ref->to, $ref->from, cldate($ref->t), ztime($ref->t), $ref->subject;
681 my $name = $AUTOLOAD;
682 return if $name =~ /::DESTROY$/;
685 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
686 @_ ? $self->{$name} = shift : $self->{$name} ;