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
35 %work = (); # outstanding jobs
36 @msg = (); # messages we have
37 %busy = (); # station interlocks
38 $msgdir = "$main::root/msg"; # directory contain the msgs
39 $maxage = 30 * 86400; # the maximum age that a message shall live for if not marked
40 $last_clean = 0; # last time we did a clean
42 $badmsgfn = "$main::data/badmsg.pl"; # list of TO address we wont store
45 fromnode => '9,From Node',
46 tonode => '9,To Node',
49 t => '0,Msg Time,cldatetime',
50 private => '9,Private',
51 subject => '0,Subject',
52 linesreq => '0,Lines per Gob',
53 rrreq => '9,Read Confirm',
56 stream => '9,Stream No',
57 count => '9,Gob Linecnt',
58 file => '9,File?,yesno',
59 gotit => '9,Got it Nodes,parray',
60 lines => '9,Lines,parray',
61 'read' => '9,Times read',
64 keep => '0,Keep this?,yesno',
67 # allocate a new object
68 # called fromnode, tonode, from, to, datetime, private?, subject, nolinesper
72 my $self = bless {}, $pkg;
73 $self->{msgno} = shift;
79 $self->{from} = uc $from;
81 $self->{private} = shift;
82 $self->{subject} = shift;
83 $self->{origin} = shift;
84 $self->{'read'} = shift;
85 $self->{rrreq} = shift;
95 delete $ref->{linesreq};
96 delete $ref->{tonode};
97 delete $ref->{fromnode};
98 delete $ref->{stream};
101 delete $ref->{count};
106 my ($self, $line) = @_;
107 my @f = split /[\^\~]/, $line;
108 my ($pcno) = $f[0] =~ /^PC(\d\d)/; # just get the number
111 if ($pcno == 28) { # incoming message
112 my $t = cltounix($f[5], $f[6]);
113 my $stream = next_transno($f[2]);
114 my $ref = DXMsg->alloc($stream, uc $f[3], $f[4], $t, $f[7], $f[8], $f[13], '0', $f[11]);
116 # fill in various forwarding state variables
117 $ref->{fromnode} = $f[2];
118 $ref->{tonode} = $f[1];
119 $ref->{rrreq} = $f[11];
120 $ref->{linesreq} = $f[10];
121 $ref->{stream} = $stream;
122 $ref->{count} = 0; # no of lines between PC31s
123 dbg('msg', "new message from $f[4] to $f[3] '$f[8]' stream $stream\n");
124 $work{"$f[2]$stream"} = $ref; # store in work
125 $busy{$f[2]} = $ref; # set interlock
126 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
130 if ($pcno == 29) { # incoming text
131 my $ref = $work{"$f[2]$f[3]"};
133 push @{$ref->{lines}}, $f[4];
135 if ($ref->{count} >= $ref->{linesreq}) {
136 $self->send(DXProt::pc31($f[2], $f[1], $f[3]));
137 dbg('msg', "stream $f[3]: $ref->{count} lines received\n");
144 if ($pcno == 30) { # this is a incoming subject ack
145 my $ref = $work{$f[2]}; # note no stream at this stage
148 $ref->{stream} = $f[3];
150 $ref->{linesreq} = 5;
151 $work{"$f[2]$f[3]"} = $ref; # new ref
152 dbg('msg', "incoming subject ack stream $f[3]\n");
153 $busy{$f[2]} = $ref; # interlock
155 push @{$ref->{lines}}, ($ref->read_msg_body);
156 $ref->send_tranche($self);
158 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
163 if ($pcno == 31) { # acknowledge a tranche of lines
164 my $ref = $work{"$f[2]$f[3]"};
166 dbg('msg', "tranche ack stream $f[3]\n");
167 $ref->send_tranche($self);
169 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
174 if ($pcno == 32) { # incoming EOM
175 dbg('msg', "stream $f[3]: EOM received\n");
176 my $ref = $work{"$f[2]$f[3]"};
178 $self->send(DXProt::pc33($f[2], $f[1], $f[3])); # acknowledge it
180 # get the next msg no - note that this has NOTHING to do with the stream number in PC protocol
181 # store the file or message
182 # remove extraneous rubbish from the hash
183 # remove it from the work in progress vector
184 # stuff it on the msg queue
185 if ($ref->{lines} && @{$ref->{lines}} > 0) { # ignore messages with 0 lines
187 $ref->store($ref->{lines});
190 # does an identical message already exist?
193 if ($ref->{subject} eq $m->{subject} && $ref->{t} == $m->{t} && $ref->{from} eq $m->{from}) {
194 $ref->stop_msg($self);
195 my $msgno = $m->{msgno};
196 dbg('msg', "duplicate message to $msgno\n");
197 Log('msg', "duplicate message to $msgno");
202 # look for 'bad' to addresses
203 if (grep $ref->{to} eq $_, @badmsg) {
204 $ref->stop_msg($self);
205 dbg('msg', "'Bad' TO address $ref->{to}");
206 Log('msg', "'Bad' TO address $ref->{to}");
210 $ref->{msgno} = next_transno("Msgno");
211 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
212 $ref->store($ref->{lines});
214 my $dxchan = DXChannel->get($ref->{to});
215 $dxchan->send($dxchan->msg('msgnew')) if $dxchan;
216 Log('msg', "Message $ref->{msgno} from $ref->{from} received from $f[2] for $ref->{to}");
219 $ref->stop_msg($self);
222 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
228 if ($pcno == 33) { # acknowledge the end of message
229 my $ref = $work{"$f[2]$f[3]"};
231 if ($ref->{private}) { # remove it if it private and gone off site#
232 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2] and deleted");
235 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2]");
236 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
237 $ref->store($ref->{lines}); # re- store the file
239 $ref->stop_msg($self);
241 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
247 if ($pcno == 40) { # this is a file request
248 $f[3] =~ s/\\/\//og; # change the slashes
249 $f[3] =~ s/\.//og; # remove dots
250 $f[3] =~ s/^\///o; # remove the leading /
251 $f[3] = lc $f[3]; # to lower case;
252 dbg('msg', "incoming file $f[3]\n");
253 last SWITCH if $f[3] =~ /^(perl|cmd|local|src|lib|include|sys|msg|connect)/; # prevent access to executables
255 # create any directories
256 my @part = split /\//, $f[3];
258 my $fn = "$main::root";
259 pop @part; # remove last part
260 foreach $part (@part) {
263 last SWITCH if !mkdir $fn, 0777;
264 dbg('msg', "created directory $fn\n");
266 my $stream = next_transno($f[2]);
267 my $ref = DXMsg->alloc($stream, "$main::root/$f[3]", $self->call, time, !$f[4], $f[3], ' ', '0', '0');
269 # forwarding variables
270 $ref->{fromnode} = $f[1];
271 $ref->{tonode} = $f[2];
272 $ref->{linesreq} = $f[5];
273 $ref->{stream} = $stream;
274 $ref->{count} = 0; # no of lines between PC31s
276 $work{"$f[2]$stream"} = $ref; # store in work
277 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
282 if ($pcno == 42) { # abort transfer
283 dbg('msg', "stream $f[3]: abort received\n");
284 my $ref = $work{"$f[2]$f[3]"};
286 $ref->stop_msg($self);
293 if ($pcno == 49) { # global delete on subject
295 if ($_->{subject} eq $f[2]) {
297 Log('msg', "Message $_->{msgno} fully deleted by $f[1]");
303 clean_old() if $main::systime - $last_clean > 3600 ; # clean the message queue
307 # store a message away on disc or whatever
309 # NOTE the second arg is a REFERENCE not a list
315 # we only proceed if there are actually any lines in the file
316 if (!$lines || @{$lines} == 0) {
320 if ($ref->{file}) { # a file
321 dbg('msg', "To be stored in $ref->{to}\n");
323 my $fh = new FileHandle "$ref->{to}", "w";
326 foreach $line (@{$lines}) {
330 dbg('msg', "file $ref->{to} stored\n");
331 Log('msg', "file $ref->{to} from $ref->{from} stored" );
333 confess "can't open file $ref->{to} $!";
335 } else { # a normal message
337 # attempt to open the message file
338 my $fn = filename($ref->{msgno});
340 dbg('msg', "To be stored in $fn\n");
342 # now save the file, overwriting what's there, YES I KNOW OK! (I will change it if it's a problem)
343 my $fh = new FileHandle "$fn", "w";
345 my $rr = $ref->{rrreq} ? '1' : '0';
346 my $priv = $ref->{private} ? '1': '0';
347 print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$priv^$ref->{subject}^$ref->{origin}^$ref->{'read'}^$rr\n";
348 print $fh "=== ", join('^', @{$ref->{gotit}}), "\n";
351 foreach $line (@{$lines}) {
352 $ref->{size} += (length $line) + 1;
356 dbg('msg', "msg $ref->{msgno} stored\n");
357 Log('msg', "msg $ref->{msgno} from $ref->{from} to $ref->{to} stored" );
359 confess "can't open msg file $fn $!";
369 # remove it from the active message list
370 @msg = map { $_ != $self ? $_ : () } @msg;
372 # belt and braces (one day I will ask someone if this is REALLY necessary)
373 delete $self->{gotit};
374 delete $self->{list};
377 unlink filename($self->{msgno});
378 dbg('msg', "deleting $self->{msgno}\n");
381 # clean out old messages from the message queue
386 # mark old messages for deletion
387 foreach $ref (@msg) {
388 if (!$ref->{keep} && $ref->{t} < $main::systime - $maxage) {
389 $ref->{deleteme} = 1;
390 delete $ref->{gotit};
392 unlink filename($ref->{msgno});
393 dbg('msg', "deleting old $ref->{msgno}\n");
397 # remove them all from the active message list
398 @msg = map { $_->{deleteme} ? () : $_ } @msg;
399 $last_clean = $main::systime;
402 # read in a message header
412 $file = new FileHandle;
413 if (!open($file, $fn)) {
414 print "Error reading $fn $!\n";
418 $line = <$file>; # first line
420 $size -= length $line;
421 if (! $line =~ /^===/o) {
422 print "corrupt first line in $fn ($line)\n";
426 @f = split /\^/, $line;
427 $ref = DXMsg->alloc(@f);
429 $line = <$file>; # second line
431 $size -= length $line;
432 if (! $line =~ /^===/o) {
433 print "corrupt second line in $fn ($line)\n";
438 @f = split /\^/, $line;
439 push @{$ref->{gotit}}, @f;
440 $ref->{size} = $size;
447 # read in a message header
451 my $msgno = $self->{msgno};
454 my $fn = filename($msgno);
457 $file = new FileHandle;
458 if (!open($file, $fn)) {
459 print "Error reading $fn $!\n";
462 chomp (@out = <$file>);
465 shift @out if $out[0] =~ /^=== /;
466 shift @out if $out[0] =~ /^=== /;
470 # send a tranche of lines to the other end
473 my ($self, $dxchan) = @_;
475 my $to = $self->{tonode};
476 my $from = $self->{fromnode};
477 my $stream = $self->{stream};
478 my $lines = $self->{lines};
481 for ($i = 0, $c = $self->{count}; $i < $self->{linesreq} && $c < @$lines; $i++, $c++) {
482 push @out, DXProt::pc29($to, $from, $stream, $lines->[$c]);
486 push @out, DXProt::pc32($to, $from, $stream) if $i < $self->{linesreq};
491 # find a message to send out and start the ball rolling
499 my @nodelist = DXProt::get_all_ak1a();
501 # bat down the message list looking for one that needs to go off site and whose
502 # nearest node is not busy.
504 dbg('msg', "queue msg ($sort)\n");
505 foreach $ref (@msg) {
506 # firstly, is it private and unread? if so can I find the recipient
507 # in my cluster node list offsite?
508 if ($ref->{private}) {
509 if ($ref->{'read'} == 0) {
510 $clref = DXCluster->get_exact($ref->{to});
511 unless ($clref) { # otherwise look for a homenode
512 my $uref = DXUser->get($ref->{to});
513 my $hnode = $uref->homenode if $uref;
514 $clref = DXCluster->get_exact($hnode) if $hnode;
516 if ($clref && !grep { $clref->{dxchan} == $_ } DXCommandmode::get_all) {
517 $dxchan = $clref->{dxchan};
518 $ref->start_msg($dxchan) if $dxchan && $clref && !get_busy($dxchan->call) && $dxchan->state eq 'normal';
522 # otherwise we are dealing with a bulletin, compare the gotit list with
523 # the nodelist up above, if there are sites that haven't got it yet
524 # then start sending it - what happens when we get loops is anyone's
525 # guess, use (to, from, time, subject) tuple?
527 foreach $noderef (@nodelist) {
528 next if $noderef->call eq $main::mycall;
529 next if $noderef->isolate; # maybe add code for stuff originated here?
530 next if grep { $_ eq $noderef->call } @{$ref->{gotit}};
532 # if we are here we have a node that doesn't have this message
533 $ref->start_msg($noderef) if !get_busy($noderef->call) && $noderef->state eq 'normal';
538 # if all the available nodes are busy then stop
539 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
543 # is there a message for me?
549 foreach $ref (@msg) {
550 # is it for me, private and unread?
551 if ($ref->{to} eq $call && $ref->{private}) {
552 return 1 if !$ref->{'read'};
558 # start the message off on its travels with a PC28
561 my ($self, $dxchan) = @_;
563 dbg('msg', "start msg $self->{msgno}\n");
564 $self->{linesreq} = 5;
566 $self->{tonode} = $dxchan->call;
567 $self->{fromnode} = $main::mycall;
568 $busy{$dxchan->call} = $self;
569 $work{"$self->{tonode}"} = $self;
570 $dxchan->send(DXProt::pc28($self->{tonode}, $self->{fromnode}, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $self->{origin}, $self->{rrreq}));
573 # get the ref of a busy node
586 # get the forwarding queue
592 # stop a message from continuing, clean it out, unlock interlocks etc
595 my ($self, $dxchan) = @_;
596 my $node = $dxchan->call;
598 dbg('msg', "stop msg $self->{msgno} stream $self->{stream}\n");
600 delete $work{"$node$self->{stream}"};
605 # get a new transaction number from the file specified
609 $name =~ s/\W//og; # remove non-word characters
610 my $fn = "$msgdir/$name";
613 my $fh = new FileHandle;
614 if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
616 $msgno = $fh->getline;
620 $fh->print("$msgno\n");
621 dbg('msg', "msgno $msgno allocated for $name\n");
624 confess "can't open $fn $!";
629 # initialise the message 'system', read in all the message headers
632 my $dir = new FileHandle;
636 do "$badmsgfn" if -e "$badmsgfn";
639 # read in the directory
640 opendir($dir, $msgdir) or confess "can't open $msgdir $!";
641 @dir = readdir($dir);
646 next unless /^m\d+/o;
648 $ref = read_msg_header("$msgdir/$_");
651 # delete any messages to 'badmsg.pl' places
652 if (grep $ref->{to} eq $_, @badmsg) {
653 dbg('msg', "'Bad' TO address $ref->{to}");
654 Log('msg', "'Bad' TO address $ref->{to}");
659 # add the message to the available queue
664 # add the message to the directory listing
668 confess "tried to add a non-ref to the msg directory" if !ref $ref;
672 # return all the current messages
678 # get a particular message
683 return $_ if $_->{msgno} == $msgno;
684 last if $_->{msgno} > $msgno;
689 # return the official filename for a message no
692 return sprintf "$msgdir/m%06d", shift;
696 # return a list of valid elements
705 # return a prompt for a field
710 my ($self, $ele) = @_;
715 # send a message state machine
722 if ($self->state eq 'send1') {
724 confess "local var gone missing" if !ref $self->{loc};
725 my $loc = $self->{loc};
726 $loc->{subject} = $line;
728 $self->state('sendbody');
729 #push @out, $self->msg('sendbody');
730 push @out, "Enter Message /EX (^Z) to send or /ABORT (^Y) to exit";
731 } elsif ($self->state eq 'sendbody') {
732 confess "local var gone missing" if !ref $self->{loc};
733 my $loc = $self->{loc};
734 if ($line eq "\032" || uc $line eq "/EX") {
737 if (@{$loc->{lines}} > 0) {
738 foreach $to (@{$loc->{to}}) {
740 my $systime = $main::systime;
741 my $mycall = $main::mycall;
742 $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
751 $ref->store($loc->{lines});
753 #push @out, $self->msg('sendsent', $to);
754 push @out, "msgno $ref->{msgno} sent to $to";
755 my $dxchan = DXChannel->get(uc $to);
757 if ($dxchan->is_user()) {
758 $dxchan->send("New mail has arrived for you");
763 delete $loc->{lines};
768 $self->state('prompt');
769 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
770 #push @out, $self->msg('sendabort');
771 push @out, "aborted";
772 delete $loc->{lines};
776 $self->state('prompt');
779 # i.e. it ain't and end or abort, therefore store the line
780 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
786 # return the standard directory line for this ref
790 return sprintf "%6d%s%s%5d %8.8s %8.8s %-6.6s %5.5s %-30.30s",
791 $ref->msgno, $ref->read ? '-' : ' ', $ref->private ? 'p' : ' ', $ref->size,
792 $ref->to, $ref->from, cldate($ref->t), ztime($ref->t), $ref->subject;
799 my $name = $AUTOLOAD;
800 return if $name =~ /::DESTROY$/;
803 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
804 @_ ? $self->{$name} = shift : $self->{$name} ;