3 # This module impliments the message handling for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
12 @ISA = qw(DXProt DXChannel);
26 use vars qw(%work @msg $msgdir %valid %busy);
28 %work = (); # outstanding jobs
29 @msg = (); # messages we have
30 %busy = (); # station interlocks
31 $msgdir = "$main::root/msg"; # directory contain the msgs
34 fromnode => '9,From Node',
35 tonode => '9,To Node',
38 t => '0,Msg Time,cldatetime',
39 private => '9,Private',
40 subject => '0,Subject',
41 linesreq => '0,Lines per Gob',
42 rrreq => '9,Read Confirm',
45 stream => '9,Stream No',
46 count => '9,Gob Linecnt',
47 file => '9,File?,yesno',
48 gotit => '9,Got it Nodes,parray',
49 lines => '9,Lines,parray',
50 read => '9,Times read',
55 # allocate a new object
56 # called fromnode, tonode, from, to, datetime, private?, subject, nolinesper
60 my $self = bless {}, $pkg;
61 $self->{msgno} = shift;
63 $self->{from} = shift;
65 $self->{private} = shift;
66 $self->{subject} = shift;
67 $self->{origin} = shift;
68 $self->{read} = shift;
69 $self->{rrreq} = shift;
79 delete $ref->{linesreq};
80 delete $ref->{tonode};
81 delete $ref->{fromnode};
82 delete $ref->{stream};
90 my ($self, $line) = @_;
91 my @f = split /[\^\~]/, $line;
92 my ($pcno) = $f[0] =~ /^PC(\d\d)/; # just get the number
95 if ($pcno == 28) { # incoming message
96 my $t = cltounix($f[5], $f[6]);
97 my $stream = next_transno($f[2]);
98 my $ref = DXMsg->alloc($stream, $f[3], $f[4], $t, $f[7], $f[8], $f[13], '0', $f[11]);
100 # fill in various forwarding state variables
101 $ref->{fromnode} = $f[2];
102 $ref->{tonode} = $f[1];
103 $ref->{rrreq} = $f[11];
104 $ref->{linesreq} = $f[10];
105 $ref->{stream} = $stream;
106 $ref->{count} = 0; # no of lines between PC31s
107 dbg('msg', "new message from $f[4] to $f[3] '$f[8]' stream $stream\n");
108 $work{"$f[2]$stream"} = $ref; # store in work
109 $busy{$f[2]} = $ref; # set interlock
110 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
114 if ($pcno == 29) { # incoming text
115 my $ref = $work{"$f[2]$f[3]"};
117 push @{$ref->{lines}}, $f[4];
119 if ($ref->{count} >= $ref->{linesreq}) {
120 $self->send(DXProt::pc31($f[2], $f[1], $f[3]));
121 dbg('msg', "stream $f[3]: $ref->{linereq} lines received\n");
128 if ($pcno == 30) { # this is a incoming subject ack
129 my $ref = $work{$f[2]}; # note no stream at this stage
131 $ref->{stream} = $f[3];
133 $ref->{linesreq} = 5;
134 $work{"$f[2]$f[3]"} = $ref; # new ref
135 dbg('msg', "incoming subject ack stream $f[3]\n");
136 $busy{$f[2]} = $ref; # interlock
138 push @{$ref->{lines}}, ($ref->read_msg_body);
139 $ref->send_tranche($self);
143 if ($pcno == 31) { # acknowledge a tranche of lines
144 my $ref = $work{"$f[2]$f[3]"};
146 dbg('msg', "tranche ack stream $f[3]\n");
147 $ref->send_tranche($self);
149 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
154 if ($pcno == 32) { # incoming EOM
155 dbg('msg', "stream $f[3]: EOM received\n");
156 my $ref = $work{"$f[2]$f[3]"};
158 $self->send(DXProt::pc33($f[2], $f[1], $f[3]));# acknowledge it
160 # get the next msg no - note that this has NOTHING to do with the stream number in PC protocol
161 # store the file or message
162 # remove extraneous rubbish from the hash
163 # remove it from the work in progress vector
164 # stuff it on the msg queue
165 if ($ref->{lines} && @{$ref->{lines}} > 0) { # ignore messages with 0 lines
166 $ref->{msgno} = next_transno("Msgno") if !$ref->{file};
167 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
168 $ref->store($ref->{lines});
170 my $dxchan = DXChannel->get($ref->{to});
171 $dxchan->send("New mail has arrived for you") if $dxchan;
173 $ref->stop_msg($self);
176 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
182 if ($pcno == 33) { # acknowledge the end of message
183 my $ref = $work{"$f[2]$f[3]"};
185 if ($ref->{private}) { # remove it if it private and gone off site#
188 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
189 $ref->store($ref->{lines}); # re- store the file
191 $ref->stop_msg($self);
193 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
199 if ($pcno == 40) { # this is a file request
200 $f[3] =~ s/\\/\//og; # change the slashes
201 $f[3] =~ s/\.//og; # remove dots
202 $f[3] = lc $f[3]; # to lower case;
203 dbg('msg', "incoming file $f[3]\n");
204 last SWITCH if $f[3] =~ /^\/(perl|cmd|local_cmd|src|lib|include|sys|msg)\//; # prevent access to executables
206 # create any directories
207 my @part = split /\//, $f[3];
209 my $fn = "$main::root";
210 pop @part; # remove last part
211 foreach $part (@part) {
214 last SWITCH if !mkdir $fn, 0777;
215 dbg('msg', "created directory $fn\n");
217 my $stream = next_transno($f[2]);
218 my $ref = DXMsg->alloc($stream, "$main::root/$f[3]", $self->call, time, !$f[4], $f[3], ' ', '0', '0');
220 # forwarding variables
221 $ref->{fromnode} = $f[1];
222 $ref->{tonode} = $f[2];
223 $ref->{linesreq} = $f[5];
224 $ref->{stream} = $stream;
225 $ref->{count} = 0; # no of lines between PC31s
227 $work{"$f[2]$stream"} = $ref; # store in work
228 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
233 if ($pcno == 42) { # abort transfer
234 dbg('msg', "stream $f[3]: abort received\n");
235 my $ref = $work{"$f[2]$f[3]"};
237 $ref->stop_msg($self);
247 # store a message away on disc or whatever
249 # NOTE the second arg is a REFERENCE not a list
255 # we only proceed if there are actually any lines in the file
256 if (!$lines || @{$lines} == 0) {
260 if ($ref->{file}) { # a file
261 dbg('msg', "To be stored in $ref->{to}\n");
263 my $fh = new FileHandle "$ref->{to}", "w";
266 foreach $line (@{$lines}) {
270 dbg('msg', "file $ref->{to} stored\n");
272 confess "can't open file $ref->{to} $!";
274 # push @{$ref->{gotit}}, $ref->{fromnode} if $ref->{fromnode};
275 } else { # a normal message
277 # attempt to open the message file
278 my $fn = filename($ref->{msgno});
280 dbg('msg', "To be stored in $fn\n");
282 # now save the file, overwriting what's there, YES I KNOW OK! (I will change it if it's a problem)
283 my $fh = new FileHandle "$fn", "w";
285 my $rr = $ref->{rrreq} ? '1' : '0';
286 my $priv = $ref->{private} ? '1': '0';
287 print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$priv^$ref->{subject}^$ref->{origin}^$ref->{read}^$rr\n";
288 print $fh "=== ", join('^', @{$ref->{gotit}}), "\n";
291 foreach $line (@{$lines}) {
292 $ref->{size} += (length $line) + 1;
296 dbg('msg', "msg $ref->{msgno} stored\n");
298 confess "can't open msg file $fn $!";
308 # remove it from the active message list
309 @msg = map { $_ != $self ? $_ : () } @msg;
311 # belt and braces (one day I will ask someone if this is REALLY necessary)
312 delete $self->{gotit};
313 delete $self->{list};
316 unlink filename($self->{msgno});
317 dbg('msg', "deleting $self->{msgno}\n");
320 # read in a message header
330 $file = new FileHandle;
331 if (!open($file, $fn)) {
332 print "Error reading $fn $!\n";
336 $line = <$file>; # first line
338 $size -= length $line;
339 if (! $line =~ /^===/o) {
340 print "corrupt first line in $fn ($line)\n";
344 @f = split /\^/, $line;
345 $ref = DXMsg->alloc(@f);
347 $line = <$file>; # second line
349 $size -= length $line;
350 if (! $line =~ /^===/o) {
351 print "corrupt second line in $fn ($line)\n";
356 @f = split /\^/, $line;
357 push @{$ref->{gotit}}, @f;
358 $ref->{size} = $size;
365 # read in a message header
369 my $msgno = $self->{msgno};
372 my $fn = filename($msgno);
375 $file = new FileHandle;
376 if (!open($file, $fn)) {
377 print "Error reading $fn $!\n";
380 chomp (@out = <$file>);
383 shift @out if $out[0] =~ /^=== /;
384 shift @out if $out[0] =~ /^=== /;
388 # send a tranche of lines to the other end
391 my ($self, $dxchan) = @_;
393 my $to = $self->{tonode};
394 my $from = $self->{fromnode};
395 my $stream = $self->{stream};
398 for ($i = 0; $i < $self->{linesreq} && $self->{count} < @{$self->{lines}}; $i++, $self->{count}++) {
399 push @out, DXProt::pc29($to, $from, $stream, ${$self->{lines}}[$self->{count}]);
401 push @out, DXProt::pc32($to, $from, $stream) if $i < $self->{linesreq};
406 # find a message to send out and start the ball rolling
410 my @nodelist = DXProt::get_all_ak1a();
415 # bat down the message list looking for one that needs to go off site and whose
416 # nearest node is not busy.
418 dbg('msg', "queue msg ($sort)\n");
419 foreach $ref (@msg) {
420 # firstly, is it private and unread? if so can I find the recipient
421 # in my cluster node list offsite?
422 if ($ref->{private}) {
423 if ($ref->{read} == 0) {
424 $clref = DXCluster->get($ref->{to});
425 if ($clref && !grep { $clref->{dxchan} == $_ } DXCommandmode::get_all) {
426 $dxchan = $clref->{dxchan};
427 $ref->start_msg($dxchan) if $clref && !get_busy($dxchan->call);
430 } elsif ($sort == undef) {
431 # otherwise we are dealing with a bulletin, compare the gotit list with
432 # the nodelist up above, if there are sites that haven't got it yet
433 # then start sending it - what happens when we get loops is anyone's
434 # guess, use (to, from, time, subject) tuple?
436 foreach $noderef (@nodelist) {
437 next if $noderef->call eq $main::mycall;
438 next if grep { $_ eq $noderef->call } @{$ref->{gotit}};
440 # if we are here we have a node that doesn't have this message
441 $ref->start_msg($noderef) if !get_busy($noderef->call);
446 # if all the available nodes are busy then stop
447 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
451 # start the message off on its travels with a PC28
454 my ($self, $dxchan) = @_;
456 dbg('msg', "start msg $self->{msgno}\n");
457 $self->{linesreq} = 5;
459 $self->{tonode} = $dxchan->call;
460 $self->{fromnode} = $main::mycall;
461 $busy{$dxchan->call} = $self;
462 $work{"$self->{tonode}"} = $self;
463 $dxchan->send(DXProt::pc28($self->{tonode}, $self->{fromnode}, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $self->{origin}, $self->{rrreq}));
466 # get the ref of a busy node
479 # get the forwarding queue
485 # stop a message from continuing, clean it out, unlock interlocks etc
488 my ($self, $dxchan) = @_;
489 my $node = $dxchan->call;
491 dbg('msg', "stop msg $self->{msgno} stream $self->{stream}\n");
493 delete $work{"$node$self->{stream}"};
498 # get a new transaction number from the file specified
502 $name =~ s/\W//og; # remove non-word characters
503 my $fn = "$msgdir/$name";
506 my $fh = new FileHandle;
507 if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
509 $msgno = $fh->getline;
513 $fh->print("$msgno\n");
514 dbg('msg', "msgno $msgno allocated for $name\n");
517 confess "can't open $fn $!";
522 # initialise the message 'system', read in all the message headers
525 my $dir = new FileHandle;
529 # read in the directory
530 opendir($dir, $msgdir) or confess "can't open $msgdir $!";
531 @dir = readdir($dir);
538 $ref = read_msg_header("$msgdir/$_");
541 # add the message to the available queue
547 # add the message to the directory listing
551 confess "tried to add a non-ref to the msg directory" if !ref $ref;
555 # return all the current messages
561 # get a particular message
566 return $_ if $_->{msgno} == $msgno;
567 last if $_->{msgno} > $msgno;
572 # return the official filename for a message no
575 return sprintf "$msgdir/m%06d", shift;
579 # return a list of valid elements
588 # return a prompt for a field
593 my ($self, $ele) = @_;
598 # send a message state machine
605 if ($self->state eq 'send1') {
607 confess "local var gone missing" if !ref $self->{loc};
608 my $loc = $self->{loc};
609 $loc->{subject} = $line;
611 $self->state('sendbody');
612 #push @out, $self->msg('sendbody');
613 push @out, "Enter Message /EX (^Z) to send or /ABORT (^Y) to exit";
614 } elsif ($self->state eq 'sendbody') {
615 confess "local var gone missing" if !ref $self->{loc};
616 my $loc = $self->{loc};
617 if ($line eq "\032" || uc $line eq "/EX") {
620 if (@{$loc->{lines}} > 0) {
621 foreach $to (@{$loc->{to}}) {
623 my $systime = $main::systime;
624 my $mycall = $main::mycall;
625 $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
634 $ref->store($loc->{lines});
636 #push @out, $self->msg('sendsent', $to);
637 push @out, "msgno $ref->{msgno} sent to $to";
638 my $dxchan = DXChannel->get(uc $to);
639 $dxchan->send("New mail has arrived for you") if $dxchan;
642 delete $loc->{lines};
645 $self->state('prompt');
648 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
649 #push @out, $self->msg('sendabort');
650 push @out, "aborted";
651 delete $loc->{lines};
655 $self->state('prompt');
658 # i.e. it ain't and end or abort, therefore store the line
659 push @{$loc->{lines}}, $line;
665 # return the standard directory line for this ref
669 return sprintf "%6d%s%s%5d %8.8s %8.8s %-6.6s %5.5s %-30.30s",
670 $ref->msgno, $ref->read ? '-' : ' ', $ref->private ? 'p' : ' ', $ref->size,
671 $ref->to, $ref->from, cldate($ref->t), ztime($ref->t), $ref->subject;
678 my $name = $AUTOLOAD;
679 return if $name =~ /::DESTROY$/;
682 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
683 @_ ? $self->{$name} = shift : $self->{$name} ;