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)
18 @ISA = qw(DXProt DXChannel);
34 use vars qw(%work @msg $msgdir %valid %busy $maxage $last_clean
35 @badmsg $badmsgfn $forwardfn @forward);
37 %work = (); # outstanding jobs
38 @msg = (); # messages we have
39 %busy = (); # station interlocks
40 $msgdir = "$main::root/msg"; # directory contain the msgs
41 $maxage = 30 * 86400; # the maximum age that a message shall live for if not marked
42 $last_clean = 0; # last time we did a clean
43 @forward = (); # msg forward table
45 $badmsgfn = "$msgdir/badmsg.pl"; # list of TO address we wont store
46 $forwardfn = "$msgdir/forward.pl"; # the forwarding table
49 fromnode => '9,From Node',
50 tonode => '9,To Node',
53 t => '0,Msg Time,cldatetime',
54 private => '9,Private',
55 subject => '0,Subject',
56 linesreq => '0,Lines per Gob',
57 rrreq => '9,Read Confirm',
60 stream => '9,Stream No',
61 count => '9,Gob Linecnt',
62 file => '9,File?,yesno',
63 gotit => '9,Got it Nodes,parray',
64 lines => '9,Lines,parray',
65 'read' => '9,Times read',
68 keep => '0,Keep this?,yesno',
78 # allocate a new object
79 # called fromnode, tonode, from, to, datetime, private?, subject, nolinesper
83 my $self = bless {}, $pkg;
84 $self->{msgno} = shift;
87 $self->{to} = ($to eq $main::mycall) ? $main::myalias : $to;
90 $self->{from} = uc $from;
92 $self->{private} = shift;
93 $self->{subject} = shift;
94 $self->{origin} = shift;
95 $self->{'read'} = shift;
96 $self->{rrreq} = shift;
105 delete $ref->{lines};
106 delete $ref->{linesreq};
107 delete $ref->{tonode};
108 delete $ref->{fromnode};
109 delete $ref->{stream};
110 delete $ref->{lines};
112 delete $ref->{count};
117 my ($self, $line) = @_;
118 my @f = split /\^/, $line;
119 my ($pcno) = $f[0] =~ /^PC(\d\d)/; # just get the number
122 if ($pcno == 28) { # incoming message
123 my $t = cltounix($f[5], $f[6]);
124 my $stream = next_transno($f[2]);
125 my $ref = DXMsg->alloc($stream, uc $f[3], $f[4], $t, $f[7], $f[8], $f[13], '0', $f[11]);
127 # fill in various forwarding state variables
128 $ref->{fromnode} = $f[2];
129 $ref->{tonode} = $f[1];
130 $ref->{rrreq} = $f[11];
131 $ref->{linesreq} = $f[10];
132 $ref->{stream} = $stream;
133 $ref->{count} = 0; # no of lines between PC31s
134 dbg('msg', "new message from $f[4] to $f[3] '$f[8]' stream $stream\n");
135 $work{"$f[2]$stream"} = $ref; # store in work
136 $busy{$f[2]} = $ref; # set interlock
137 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
141 if ($pcno == 29) { # incoming text
142 my $ref = $work{"$f[2]$f[3]"};
144 push @{$ref->{lines}}, $f[4];
146 if ($ref->{count} >= $ref->{linesreq}) {
147 $self->send(DXProt::pc31($f[2], $f[1], $f[3]));
148 dbg('msg', "stream $f[3]: $ref->{count} lines received\n");
155 if ($pcno == 30) { # this is a incoming subject ack
156 my $ref = $work{$f[2]}; # note no stream at this stage
159 $ref->{stream} = $f[3];
161 $ref->{linesreq} = 5;
162 $work{"$f[2]$f[3]"} = $ref; # new ref
163 dbg('msg', "incoming subject ack stream $f[3]\n");
164 $busy{$f[2]} = $ref; # interlock
166 push @{$ref->{lines}}, ($ref->read_msg_body);
167 $ref->send_tranche($self);
169 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
174 if ($pcno == 31) { # acknowledge a tranche of lines
175 my $ref = $work{"$f[2]$f[3]"};
177 dbg('msg', "tranche ack stream $f[3]\n");
178 $ref->send_tranche($self);
180 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
185 if ($pcno == 32) { # incoming EOM
186 dbg('msg', "stream $f[3]: EOM received\n");
187 my $ref = $work{"$f[2]$f[3]"};
189 $self->send(DXProt::pc33($f[2], $f[1], $f[3])); # acknowledge it
191 # get the next msg no - note that this has NOTHING to do with the stream number in PC protocol
192 # store the file or message
193 # remove extraneous rubbish from the hash
194 # remove it from the work in progress vector
195 # stuff it on the msg queue
196 if ($ref->{lines} && @{$ref->{lines}} > 0) { # ignore messages with 0 lines
198 $ref->store($ref->{lines});
201 # does an identical message already exist?
204 if ($ref->{subject} eq $m->{subject} && $ref->{t} == $m->{t} && $ref->{from} eq $m->{from}) {
205 $ref->stop_msg($self);
206 my $msgno = $m->{msgno};
207 dbg('msg', "duplicate message to $msgno\n");
208 Log('msg', "duplicate message to $msgno");
213 # look for 'bad' to addresses
214 if (grep $ref->{to} eq $_, @badmsg) {
215 $ref->stop_msg($self);
216 dbg('msg', "'Bad' TO address $ref->{to}");
217 Log('msg', "'Bad' TO address $ref->{to}");
221 $ref->{msgno} = next_transno("Msgno");
222 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
223 $ref->store($ref->{lines});
225 my $dxchan = DXChannel->get($ref->{to});
226 $dxchan->send($dxchan->msg('msgnew')) if $dxchan;
227 Log('msg', "Message $ref->{msgno} from $ref->{from} received from $f[2] for $ref->{to}");
230 $ref->stop_msg($self);
233 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
239 if ($pcno == 33) { # acknowledge the end of message
240 my $ref = $work{"$f[2]$f[3]"};
242 if ($ref->{private}) { # remove it if it private and gone off site#
243 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2] and deleted");
246 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2]");
247 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
248 $ref->store($ref->{lines}); # re- store the file
250 $ref->stop_msg($self);
252 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
258 if ($pcno == 40) { # this is a file request
259 $f[3] =~ s/\\/\//og; # change the slashes
260 $f[3] =~ s/\.//og; # remove dots
261 $f[3] =~ s/^\///o; # remove the leading /
262 $f[3] = lc $f[3]; # to lower case;
263 dbg('msg', "incoming file $f[3]\n");
264 $f[3] = 'packclus/' . $f[3] unless $f[3] =~ /^packclus\//o;
266 # create any directories
267 my @part = split /\//, $f[3];
269 my $fn = "$main::root";
270 pop @part; # remove last part
271 foreach $part (@part) {
274 last SWITCH if !mkdir $fn, 0777;
275 dbg('msg', "created directory $fn\n");
277 my $stream = next_transno($f[2]);
278 my $ref = DXMsg->alloc($stream, "$main::root/$f[3]", $self->call, time, !$f[4], $f[3], ' ', '0', '0');
280 # forwarding variables
281 $ref->{fromnode} = $f[1];
282 $ref->{tonode} = $f[2];
283 $ref->{linesreq} = $f[5];
284 $ref->{stream} = $stream;
285 $ref->{count} = 0; # no of lines between PC31s
287 $work{"$f[2]$stream"} = $ref; # store in work
288 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
293 if ($pcno == 42) { # abort transfer
294 dbg('msg', "stream $f[3]: abort received\n");
295 my $ref = $work{"$f[2]$f[3]"};
297 $ref->stop_msg($self);
304 if ($pcno == 49) { # global delete on subject
306 if ($_->{subject} eq $f[2]) {
308 Log('msg', "Message $_->{msgno} fully deleted by $f[1]");
314 clean_old() if $main::systime - $last_clean > 3600 ; # clean the message queue
318 # store a message away on disc or whatever
320 # NOTE the second arg is a REFERENCE not a list
326 # we only proceed if there are actually any lines in the file
327 if (!$lines || @{$lines} == 0) {
331 if ($ref->{file}) { # a file
332 dbg('msg', "To be stored in $ref->{to}\n");
334 my $fh = new IO::File "$ref->{to}", "w";
337 foreach $line (@{$lines}) {
341 dbg('msg', "file $ref->{to} stored\n");
342 Log('msg', "file $ref->{to} from $ref->{from} stored" );
344 confess "can't open file $ref->{to} $!";
346 } else { # a normal message
348 # attempt to open the message file
349 my $fn = filename($ref->{msgno});
351 dbg('msg', "To be stored in $fn\n");
353 # now save the file, overwriting what's there, YES I KNOW OK! (I will change it if it's a problem)
354 my $fh = new IO::File "$fn", "w";
356 my $rr = $ref->{rrreq} ? '1' : '0';
357 my $priv = $ref->{private} ? '1': '0';
358 print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$priv^$ref->{subject}^$ref->{origin}^$ref->{'read'}^$rr\n";
359 print $fh "=== ", join('^', @{$ref->{gotit}}), "\n";
362 foreach $line (@{$lines}) {
363 $ref->{size} += (length $line) + 1;
367 dbg('msg', "msg $ref->{msgno} stored\n");
368 Log('msg', "msg $ref->{msgno} from $ref->{from} to $ref->{to} stored" );
370 confess "can't open msg file $fn $!";
380 # remove it from the active message list
381 @msg = map { $_ != $self ? $_ : () } @msg;
383 # belt and braces (one day I will ask someone if this is REALLY necessary)
384 delete $self->{gotit};
385 delete $self->{list};
388 unlink filename($self->{msgno});
389 dbg('msg', "deleting $self->{msgno}\n");
392 # clean out old messages from the message queue
397 # mark old messages for deletion
398 foreach $ref (@msg) {
399 if (!$ref->{keep} && $ref->{t} < $main::systime - $maxage) {
400 $ref->{deleteme} = 1;
401 delete $ref->{gotit};
403 unlink filename($ref->{msgno});
404 dbg('msg', "deleting old $ref->{msgno}\n");
408 # remove them all from the active message list
409 @msg = map { $_->{deleteme} ? () : $_ } @msg;
410 $last_clean = $main::systime;
413 # read in a message header
423 $file = new IO::File;
424 if (!open($file, $fn)) {
425 print "Error reading $fn $!\n";
429 $line = <$file>; # first line
431 $size -= length $line;
432 if (! $line =~ /^===/o) {
433 print "corrupt first line in $fn ($line)\n";
437 @f = split /\^/, $line;
438 $ref = DXMsg->alloc(@f);
440 $line = <$file>; # second line
442 $size -= length $line;
443 if (! $line =~ /^===/o) {
444 print "corrupt second line in $fn ($line)\n";
449 @f = split /\^/, $line;
450 push @{$ref->{gotit}}, @f;
451 $ref->{size} = $size;
458 # read in a message header
462 my $msgno = $self->{msgno};
465 my $fn = filename($msgno);
468 $file = new IO::File;
469 if (!open($file, $fn)) {
470 print "Error reading $fn $!\n";
473 chomp (@out = <$file>);
476 shift @out if $out[0] =~ /^=== /;
477 shift @out if $out[0] =~ /^=== /;
481 # send a tranche of lines to the other end
484 my ($self, $dxchan) = @_;
486 my $to = $self->{tonode};
487 my $from = $self->{fromnode};
488 my $stream = $self->{stream};
489 my $lines = $self->{lines};
492 for ($i = 0, $c = $self->{count}; $i < $self->{linesreq} && $c < @$lines; $i++, $c++) {
493 push @out, DXProt::pc29($to, $from, $stream, $lines->[$c]);
497 push @out, DXProt::pc32($to, $from, $stream) if $i < $self->{linesreq};
502 # find a message to send out and start the ball rolling
510 my @nodelist = DXProt::get_all_ak1a();
512 # bat down the message list looking for one that needs to go off site and whose
513 # nearest node is not busy.
515 dbg('msg', "queue msg ($sort)\n");
516 foreach $ref (@msg) {
517 # firstly, is it private and unread? if so can I find the recipient
518 # in my cluster node list offsite?
519 if ($ref->{private}) {
520 if ($ref->{'read'} == 0) {
521 $clref = DXCluster->get_exact($ref->{to});
522 unless ($clref) { # otherwise look for a homenode
523 my $uref = DXUser->get($ref->{to});
524 my $hnode = $uref->homenode if $uref;
525 $clref = DXCluster->get_exact($hnode) if $hnode;
527 if ($clref && !grep { $clref->{dxchan} == $_ } DXCommandmode::get_all) {
528 $dxchan = $clref->{dxchan};
529 $ref->start_msg($dxchan) if $dxchan && $clref && !get_busy($dxchan->call) && $dxchan->state eq 'normal';
533 # otherwise we are dealing with a bulletin, compare the gotit list with
534 # the nodelist up above, if there are sites that haven't got it yet
535 # then start sending it - what happens when we get loops is anyone's
536 # guess, use (to, from, time, subject) tuple?
538 foreach $noderef (@nodelist) {
539 next if $noderef->call eq $main::mycall;
540 next if grep { $_ eq $noderef->call } @{$ref->{gotit}};
541 next unless $ref->forward_it($noderef->call); # check the forwarding file
542 # next if $noderef->isolate; # maybe add code for stuff originated here?
543 # next if DXUser->get( ${$ref->{gotit}}[0] )->isolate; # is the origin isolated?
545 # if we are here we have a node that doesn't have this message
546 $ref->start_msg($noderef) if !get_busy($noderef->call) && $noderef->state eq 'normal';
551 # if all the available nodes are busy then stop
552 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
556 # is there a message for me?
562 foreach $ref (@msg) {
563 # is it for me, private and unread?
564 if ($ref->{to} eq $call && $ref->{private}) {
565 return 1 if !$ref->{'read'};
571 # start the message off on its travels with a PC28
574 my ($self, $dxchan) = @_;
576 dbg('msg', "start msg $self->{msgno}\n");
577 $self->{linesreq} = 5;
579 $self->{tonode} = $dxchan->call;
580 $self->{fromnode} = $main::mycall;
581 $busy{$dxchan->call} = $self;
582 $work{"$self->{tonode}"} = $self;
583 $dxchan->send(DXProt::pc28($self->{tonode}, $self->{fromnode}, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $self->{origin}, $self->{rrreq}));
586 # get the ref of a busy node
599 # get the forwarding queue
605 # stop a message from continuing, clean it out, unlock interlocks etc
608 my ($self, $dxchan) = @_;
609 my $node = $dxchan->call;
611 dbg('msg', "stop msg $self->{msgno} stream $self->{stream}\n");
613 delete $work{"$node$self->{stream}"};
618 # get a new transaction number from the file specified
622 $name =~ s/\W//og; # remove non-word characters
623 my $fn = "$msgdir/$name";
626 my $fh = new IO::File;
627 if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
629 $msgno = $fh->getline;
633 $fh->print("$msgno\n");
634 dbg('msg', "msgno $msgno allocated for $name\n");
637 confess "can't open $fn $!";
642 # initialise the message 'system', read in all the message headers
645 my $dir = new IO::File;
649 # load various control files
650 my @in = load_badmsg();
651 print "@in\n" if @in;
652 @in = load_forward();
653 print "@in\n" if @in;
655 # read in the directory
656 opendir($dir, $msgdir) or confess "can't open $msgdir $!";
657 @dir = readdir($dir);
662 next unless /^m\d+$/o;
664 $ref = read_msg_header("$msgdir/$_");
667 # delete any messages to 'badmsg.pl' places
668 if (grep $ref->{to} eq $_, @badmsg) {
669 dbg('msg', "'Bad' TO address $ref->{to}");
670 Log('msg', "'Bad' TO address $ref->{to}");
675 # add the message to the available queue
680 # add the message to the directory listing
684 confess "tried to add a non-ref to the msg directory" if !ref $ref;
688 # return all the current messages
694 # get a particular message
699 return $_ if $_->{msgno} == $msgno;
700 last if $_->{msgno} > $msgno;
705 # return the official filename for a message no
708 return sprintf "$msgdir/m%06d", shift;
712 # return a list of valid elements
721 # return a prompt for a field
726 my ($self, $ele) = @_;
731 # send a message state machine
738 if ($self->state eq 'send1') {
740 confess "local var gone missing" if !ref $self->{loc};
741 my $loc = $self->{loc};
742 $loc->{subject} = $line;
744 $self->state('sendbody');
745 #push @out, $self->msg('sendbody');
746 push @out, "Enter Message /EX (^Z) to send or /ABORT (^Y) to exit";
747 } elsif ($self->state eq 'sendbody') {
748 confess "local var gone missing" if !ref $self->{loc};
749 my $loc = $self->{loc};
750 if ($line eq "\032" || uc $line eq "/EX") {
753 if (@{$loc->{lines}} > 0) {
754 foreach $to (@{$loc->{to}}) {
756 my $systime = $main::systime;
757 my $mycall = $main::mycall;
758 $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
767 $ref->store($loc->{lines});
769 #push @out, $self->msg('sendsent', $to);
770 push @out, "msgno $ref->{msgno} sent to $to";
771 my $dxchan = DXChannel->get(uc $to);
773 if ($dxchan->is_user()) {
774 $dxchan->send("New mail has arrived for you");
779 delete $loc->{lines};
784 $self->state('prompt');
785 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
786 #push @out, $self->msg('sendabort');
787 push @out, "aborted";
788 delete $loc->{lines};
792 $self->state('prompt');
795 # i.e. it ain't and end or abort, therefore store the line
796 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
802 # return the standard directory line for this ref
806 return sprintf "%6d%s%s%5d %8.8s %8.8s %-6.6s %5.5s %-30.30s",
807 $ref->msgno, $ref->read ? '-' : ' ', $ref->private ? 'p' : ' ', $ref->size,
808 $ref->to, $ref->from, cldate($ref->t), ztime($ref->t), $ref->subject;
811 # load the forward table
815 do "$forwardfn" if -e "$forwardfn";
820 # load the bad message table
824 do "$badmsgfn" if -e "$badmsgfn";
830 # forward that message or not according to the forwarding table
831 # returns 1 for forward, 0 - to ignore
840 for ($i = 0; $i < @forward; $i += 5) {
841 my ($sort, $field, $pattern, $action, $bbs) = @forward[$i..($i+4)];
845 last if $ref->{private} && $sort ne 'P';
846 last if !$ref->{private} && $sort ne 'B';
849 $tested = $ref->{to} if $field eq 'T';
850 $tested = $ref->{from} if $field eq 'F';
851 $tested = $ref->{origin} if $field eq 'O';
852 $tested = $ref->{subject} if $field eq 'S';
854 if (!$pattern || $tested =~ m{$pattern}i) {
855 return 0 if $action eq 'I';
856 return 1 if !$bbs || grep $_ eq $call, @{$bbs};
866 my $name = $AUTOLOAD;
867 return if $name =~ /::DESTROY$/;
870 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
871 @_ ? $self->{$name} = shift : $self->{$name} ;