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);
33 use vars qw(%work @msg $msgdir %valid %busy $maxage $last_clean
34 @badmsg $badmsgfn $forwardfn @forward);
36 %work = (); # outstanding jobs
37 @msg = (); # messages we have
38 %busy = (); # station interlocks
39 $msgdir = "$main::root/msg"; # directory contain the msgs
40 $maxage = 30 * 86400; # the maximum age that a message shall live for if not marked
41 $last_clean = 0; # last time we did a clean
42 @forward = (); # msg forward table
44 $badmsgfn = "$msgdir/badmsg.pl"; # list of TO address we wont store
45 $forwardfn = "$msgdir/forward.pl"; # the forwarding table
48 fromnode => '9,From Node',
49 tonode => '9,To Node',
52 t => '0,Msg Time,cldatetime',
53 private => '9,Private',
54 subject => '0,Subject',
55 linesreq => '0,Lines per Gob',
56 rrreq => '9,Read Confirm',
59 stream => '9,Stream No',
60 count => '9,Gob Linecnt',
61 file => '9,File?,yesno',
62 gotit => '9,Got it Nodes,parray',
63 lines => '9,Lines,parray',
64 'read' => '9,Times read',
67 keep => '0,Keep this?,yesno',
77 # allocate a new object
78 # called fromnode, tonode, from, to, datetime, private?, subject, nolinesper
82 my $self = bless {}, $pkg;
83 $self->{msgno} = shift;
86 $self->{to} = ($to eq $main::mycall) ? $main::myalias : $to;
89 $self->{from} = uc $from;
91 $self->{private} = shift;
92 $self->{subject} = shift;
93 $self->{origin} = shift;
94 $self->{'read'} = shift;
95 $self->{rrreq} = shift;
104 delete $ref->{lines};
105 delete $ref->{linesreq};
106 delete $ref->{tonode};
107 delete $ref->{fromnode};
108 delete $ref->{stream};
109 delete $ref->{lines};
111 delete $ref->{count};
116 my ($self, $line) = @_;
117 my @f = split /\^/, $line;
118 my ($pcno) = $f[0] =~ /^PC(\d\d)/; # just get the number
121 if ($pcno == 28) { # incoming message
122 my $t = cltounix($f[5], $f[6]);
123 my $stream = next_transno($f[2]);
124 my $ref = DXMsg->alloc($stream, uc $f[3], $f[4], $t, $f[7], $f[8], $f[13], '0', $f[11]);
126 # fill in various forwarding state variables
127 $ref->{fromnode} = $f[2];
128 $ref->{tonode} = $f[1];
129 $ref->{rrreq} = $f[11];
130 $ref->{linesreq} = $f[10];
131 $ref->{stream} = $stream;
132 $ref->{count} = 0; # no of lines between PC31s
133 dbg('msg', "new message from $f[4] to $f[3] '$f[8]' stream $stream\n");
134 $work{"$f[2]$stream"} = $ref; # store in work
135 $busy{$f[2]} = $ref; # set interlock
136 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
140 if ($pcno == 29) { # incoming text
141 my $ref = $work{"$f[2]$f[3]"};
143 push @{$ref->{lines}}, $f[4];
145 if ($ref->{count} >= $ref->{linesreq}) {
146 $self->send(DXProt::pc31($f[2], $f[1], $f[3]));
147 dbg('msg', "stream $f[3]: $ref->{count} lines received\n");
154 if ($pcno == 30) { # this is a incoming subject ack
155 my $ref = $work{$f[2]}; # note no stream at this stage
158 $ref->{stream} = $f[3];
160 $ref->{linesreq} = 5;
161 $work{"$f[2]$f[3]"} = $ref; # new ref
162 dbg('msg', "incoming subject ack stream $f[3]\n");
163 $busy{$f[2]} = $ref; # interlock
165 push @{$ref->{lines}}, ($ref->read_msg_body);
166 $ref->send_tranche($self);
168 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
173 if ($pcno == 31) { # acknowledge a tranche of lines
174 my $ref = $work{"$f[2]$f[3]"};
176 dbg('msg', "tranche ack stream $f[3]\n");
177 $ref->send_tranche($self);
179 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
184 if ($pcno == 32) { # incoming EOM
185 dbg('msg', "stream $f[3]: EOM received\n");
186 my $ref = $work{"$f[2]$f[3]"};
188 $self->send(DXProt::pc33($f[2], $f[1], $f[3])); # acknowledge it
190 # get the next msg no - note that this has NOTHING to do with the stream number in PC protocol
191 # store the file or message
192 # remove extraneous rubbish from the hash
193 # remove it from the work in progress vector
194 # stuff it on the msg queue
195 if ($ref->{lines} && @{$ref->{lines}} > 0) { # ignore messages with 0 lines
197 $ref->store($ref->{lines});
200 # does an identical message already exist?
203 if ($ref->{subject} eq $m->{subject} && $ref->{t} == $m->{t} && $ref->{from} eq $m->{from}) {
204 $ref->stop_msg($self);
205 my $msgno = $m->{msgno};
206 dbg('msg', "duplicate message to $msgno\n");
207 Log('msg', "duplicate message to $msgno");
212 # look for 'bad' to addresses
213 if (grep $ref->{to} eq $_, @badmsg) {
214 $ref->stop_msg($self);
215 dbg('msg', "'Bad' TO address $ref->{to}");
216 Log('msg', "'Bad' TO address $ref->{to}");
220 $ref->{msgno} = next_transno("Msgno");
221 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
222 $ref->store($ref->{lines});
224 my $dxchan = DXChannel->get($ref->{to});
225 $dxchan->send($dxchan->msg('msgnew')) if $dxchan;
226 Log('msg', "Message $ref->{msgno} from $ref->{from} received from $f[2] for $ref->{to}");
229 $ref->stop_msg($self);
232 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
238 if ($pcno == 33) { # acknowledge the end of message
239 my $ref = $work{"$f[2]$f[3]"};
241 if ($ref->{private}) { # remove it if it private and gone off site#
242 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2] and deleted");
245 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2]");
246 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
247 $ref->store($ref->{lines}); # re- store the file
249 $ref->stop_msg($self);
251 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
257 if ($pcno == 40) { # this is a file request
258 $f[3] =~ s/\\/\//og; # change the slashes
259 $f[3] =~ s/\.//og; # remove dots
260 $f[3] =~ s/^\///o; # remove the leading /
261 $f[3] = lc $f[3]; # to lower case;
262 dbg('msg', "incoming file $f[3]\n");
263 $f[3] = 'packclus/' . $f[3] unless $f[3] =~ /^packclus\//o;
265 # create any directories
266 my @part = split /\//, $f[3];
268 my $fn = "$main::root";
269 pop @part; # remove last part
270 foreach $part (@part) {
273 last SWITCH if !mkdir $fn, 0777;
274 dbg('msg', "created directory $fn\n");
276 my $stream = next_transno($f[2]);
277 my $ref = DXMsg->alloc($stream, "$main::root/$f[3]", $self->call, time, !$f[4], $f[3], ' ', '0', '0');
279 # forwarding variables
280 $ref->{fromnode} = $f[1];
281 $ref->{tonode} = $f[2];
282 $ref->{linesreq} = $f[5];
283 $ref->{stream} = $stream;
284 $ref->{count} = 0; # no of lines between PC31s
286 $work{"$f[2]$stream"} = $ref; # store in work
287 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
292 if ($pcno == 42) { # abort transfer
293 dbg('msg', "stream $f[3]: abort received\n");
294 my $ref = $work{"$f[2]$f[3]"};
296 $ref->stop_msg($self);
303 if ($pcno == 49) { # global delete on subject
305 if ($_->{subject} eq $f[2]) {
307 Log('msg', "Message $_->{msgno} fully deleted by $f[1]");
313 clean_old() if $main::systime - $last_clean > 3600 ; # clean the message queue
317 # store a message away on disc or whatever
319 # NOTE the second arg is a REFERENCE not a list
325 # we only proceed if there are actually any lines in the file
326 if (!$lines || @{$lines} == 0) {
330 if ($ref->{file}) { # a file
331 dbg('msg', "To be stored in $ref->{to}\n");
333 my $fh = new FileHandle "$ref->{to}", "w";
336 foreach $line (@{$lines}) {
340 dbg('msg', "file $ref->{to} stored\n");
341 Log('msg', "file $ref->{to} from $ref->{from} stored" );
343 confess "can't open file $ref->{to} $!";
345 } else { # a normal message
347 # attempt to open the message file
348 my $fn = filename($ref->{msgno});
350 dbg('msg', "To be stored in $fn\n");
352 # now save the file, overwriting what's there, YES I KNOW OK! (I will change it if it's a problem)
353 my $fh = new FileHandle "$fn", "w";
355 my $rr = $ref->{rrreq} ? '1' : '0';
356 my $priv = $ref->{private} ? '1': '0';
357 print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$priv^$ref->{subject}^$ref->{origin}^$ref->{'read'}^$rr\n";
358 print $fh "=== ", join('^', @{$ref->{gotit}}), "\n";
361 foreach $line (@{$lines}) {
362 $ref->{size} += (length $line) + 1;
366 dbg('msg', "msg $ref->{msgno} stored\n");
367 Log('msg', "msg $ref->{msgno} from $ref->{from} to $ref->{to} stored" );
369 confess "can't open msg file $fn $!";
379 # remove it from the active message list
380 @msg = map { $_ != $self ? $_ : () } @msg;
382 # belt and braces (one day I will ask someone if this is REALLY necessary)
383 delete $self->{gotit};
384 delete $self->{list};
387 unlink filename($self->{msgno});
388 dbg('msg', "deleting $self->{msgno}\n");
391 # clean out old messages from the message queue
396 # mark old messages for deletion
397 foreach $ref (@msg) {
398 if (!$ref->{keep} && $ref->{t} < $main::systime - $maxage) {
399 $ref->{deleteme} = 1;
400 delete $ref->{gotit};
402 unlink filename($ref->{msgno});
403 dbg('msg', "deleting old $ref->{msgno}\n");
407 # remove them all from the active message list
408 @msg = map { $_->{deleteme} ? () : $_ } @msg;
409 $last_clean = $main::systime;
412 # read in a message header
422 $file = new FileHandle;
423 if (!open($file, $fn)) {
424 print "Error reading $fn $!\n";
428 $line = <$file>; # first line
430 $size -= length $line;
431 if (! $line =~ /^===/o) {
432 print "corrupt first line in $fn ($line)\n";
436 @f = split /\^/, $line;
437 $ref = DXMsg->alloc(@f);
439 $line = <$file>; # second line
441 $size -= length $line;
442 if (! $line =~ /^===/o) {
443 print "corrupt second line in $fn ($line)\n";
448 @f = split /\^/, $line;
449 push @{$ref->{gotit}}, @f;
450 $ref->{size} = $size;
457 # read in a message header
461 my $msgno = $self->{msgno};
464 my $fn = filename($msgno);
467 $file = new FileHandle;
468 if (!open($file, $fn)) {
469 print "Error reading $fn $!\n";
472 chomp (@out = <$file>);
475 shift @out if $out[0] =~ /^=== /;
476 shift @out if $out[0] =~ /^=== /;
480 # send a tranche of lines to the other end
483 my ($self, $dxchan) = @_;
485 my $to = $self->{tonode};
486 my $from = $self->{fromnode};
487 my $stream = $self->{stream};
488 my $lines = $self->{lines};
491 for ($i = 0, $c = $self->{count}; $i < $self->{linesreq} && $c < @$lines; $i++, $c++) {
492 push @out, DXProt::pc29($to, $from, $stream, $lines->[$c]);
496 push @out, DXProt::pc32($to, $from, $stream) if $i < $self->{linesreq};
501 # find a message to send out and start the ball rolling
509 my @nodelist = DXProt::get_all_ak1a();
511 # bat down the message list looking for one that needs to go off site and whose
512 # nearest node is not busy.
514 dbg('msg', "queue msg ($sort)\n");
515 foreach $ref (@msg) {
516 # firstly, is it private and unread? if so can I find the recipient
517 # in my cluster node list offsite?
518 if ($ref->{private}) {
519 if ($ref->{'read'} == 0) {
520 $clref = DXCluster->get_exact($ref->{to});
521 unless ($clref) { # otherwise look for a homenode
522 my $uref = DXUser->get($ref->{to});
523 my $hnode = $uref->homenode if $uref;
524 $clref = DXCluster->get_exact($hnode) if $hnode;
526 if ($clref && !grep { $clref->{dxchan} == $_ } DXCommandmode::get_all) {
527 $dxchan = $clref->{dxchan};
528 $ref->start_msg($dxchan) if $dxchan && $clref && !get_busy($dxchan->call) && $dxchan->state eq 'normal';
532 # otherwise we are dealing with a bulletin, compare the gotit list with
533 # the nodelist up above, if there are sites that haven't got it yet
534 # then start sending it - what happens when we get loops is anyone's
535 # guess, use (to, from, time, subject) tuple?
537 foreach $noderef (@nodelist) {
538 next if $noderef->call eq $main::mycall;
539 next if grep { $_ eq $noderef->call } @{$ref->{gotit}};
540 next unless $ref->forward_it($noderef->call); # check the forwarding file
541 # next if $noderef->isolate; # maybe add code for stuff originated here?
542 # next if DXUser->get( ${$ref->{gotit}}[0] )->isolate; # is the origin isolated?
544 # if we are here we have a node that doesn't have this message
545 $ref->start_msg($noderef) if !get_busy($noderef->call) && $noderef->state eq 'normal';
550 # if all the available nodes are busy then stop
551 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
555 # is there a message for me?
561 foreach $ref (@msg) {
562 # is it for me, private and unread?
563 if ($ref->{to} eq $call && $ref->{private}) {
564 return 1 if !$ref->{'read'};
570 # start the message off on its travels with a PC28
573 my ($self, $dxchan) = @_;
575 dbg('msg', "start msg $self->{msgno}\n");
576 $self->{linesreq} = 5;
578 $self->{tonode} = $dxchan->call;
579 $self->{fromnode} = $main::mycall;
580 $busy{$dxchan->call} = $self;
581 $work{"$self->{tonode}"} = $self;
582 $dxchan->send(DXProt::pc28($self->{tonode}, $self->{fromnode}, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $self->{origin}, $self->{rrreq}));
585 # get the ref of a busy node
598 # get the forwarding queue
604 # stop a message from continuing, clean it out, unlock interlocks etc
607 my ($self, $dxchan) = @_;
608 my $node = $dxchan->call;
610 dbg('msg', "stop msg $self->{msgno} stream $self->{stream}\n");
612 delete $work{"$node$self->{stream}"};
617 # get a new transaction number from the file specified
621 $name =~ s/\W//og; # remove non-word characters
622 my $fn = "$msgdir/$name";
625 my $fh = new FileHandle;
626 if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
628 $msgno = $fh->getline;
632 $fh->print("$msgno\n");
633 dbg('msg', "msgno $msgno allocated for $name\n");
636 confess "can't open $fn $!";
641 # initialise the message 'system', read in all the message headers
644 my $dir = new FileHandle;
648 # load various control files
649 my @in = load_badmsg();
650 print "@in\n" if @in;
651 @in = load_forward();
652 print "@in\n" if @in;
654 # read in the directory
655 opendir($dir, $msgdir) or confess "can't open $msgdir $!";
656 @dir = readdir($dir);
661 next unless /^m\d+$/o;
663 $ref = read_msg_header("$msgdir/$_");
666 # delete any messages to 'badmsg.pl' places
667 if (grep $ref->{to} eq $_, @badmsg) {
668 dbg('msg', "'Bad' TO address $ref->{to}");
669 Log('msg', "'Bad' TO address $ref->{to}");
674 # add the message to the available queue
679 # add the message to the directory listing
683 confess "tried to add a non-ref to the msg directory" if !ref $ref;
687 # return all the current messages
693 # get a particular message
698 return $_ if $_->{msgno} == $msgno;
699 last if $_->{msgno} > $msgno;
704 # return the official filename for a message no
707 return sprintf "$msgdir/m%06d", shift;
711 # return a list of valid elements
720 # return a prompt for a field
725 my ($self, $ele) = @_;
730 # send a message state machine
737 if ($self->state eq 'send1') {
739 confess "local var gone missing" if !ref $self->{loc};
740 my $loc = $self->{loc};
741 $loc->{subject} = $line;
743 $self->state('sendbody');
744 #push @out, $self->msg('sendbody');
745 push @out, "Enter Message /EX (^Z) to send or /ABORT (^Y) to exit";
746 } elsif ($self->state eq 'sendbody') {
747 confess "local var gone missing" if !ref $self->{loc};
748 my $loc = $self->{loc};
749 if ($line eq "\032" || uc $line eq "/EX") {
752 if (@{$loc->{lines}} > 0) {
753 foreach $to (@{$loc->{to}}) {
755 my $systime = $main::systime;
756 my $mycall = $main::mycall;
757 $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
766 $ref->store($loc->{lines});
768 #push @out, $self->msg('sendsent', $to);
769 push @out, "msgno $ref->{msgno} sent to $to";
770 my $dxchan = DXChannel->get(uc $to);
772 if ($dxchan->is_user()) {
773 $dxchan->send("New mail has arrived for you");
778 delete $loc->{lines};
783 $self->state('prompt');
784 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
785 #push @out, $self->msg('sendabort');
786 push @out, "aborted";
787 delete $loc->{lines};
791 $self->state('prompt');
794 # i.e. it ain't and end or abort, therefore store the line
795 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
801 # return the standard directory line for this ref
805 return sprintf "%6d%s%s%5d %8.8s %8.8s %-6.6s %5.5s %-30.30s",
806 $ref->msgno, $ref->read ? '-' : ' ', $ref->private ? 'p' : ' ', $ref->size,
807 $ref->to, $ref->from, cldate($ref->t), ztime($ref->t), $ref->subject;
810 # load the forward table
814 do "$forwardfn" if -e "$forwardfn";
819 # load the bad message table
823 do "$badmsgfn" if -e "$badmsgfn";
829 # forward that message or not according to the forwarding table
830 # returns 1 for forward, 0 - to ignore
839 for ($i = 0; $i < @forward; $i += 5) {
840 my ($sort, $field, $pattern, $action, $bbs) = @forward[$i..($i+4)];
844 last if $ref->{private} && $sort ne 'P';
845 last if !$ref->{private} && $sort ne 'B';
848 $tested = $ref->{to} if $field eq 'T';
849 $tested = $ref->{from} if $field eq 'F';
850 $tested = $ref->{origin} if $field eq 'O';
851 $tested = $ref->{subject} if $field eq 'S';
853 if (!$pattern || $tested =~ m{$pattern}i) {
854 return 0 if $action eq 'I';
855 return 1 if !$bbs || grep $_ eq $call, @{$bbs};
865 my $name = $AUTOLOAD;
866 return if $name =~ /::DESTROY$/;
869 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
870 @_ ? $self->{$name} = shift : $self->{$name} ;