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)
31 use vars qw($VERSION $BRANCH);
32 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
33 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ ) || 0;
34 $main::build += $VERSION;
35 $main::branch += $BRANCH;
37 use vars qw(%work @msg $msgdir %valid %busy $maxage $last_clean
38 @badmsg @swop $swopfn $badmsgfn $forwardfn @forward $timeout $waittime
39 $queueinterval $lastq $importfn $minchunk $maxchunk $bulltopriv);
41 %work = (); # outstanding jobs
42 @msg = (); # messages we have
43 %busy = (); # station interlocks
44 $msgdir = "$main::root/msg"; # directory contain the msgs
45 $maxage = 30 * 86400; # the maximum age that a message shall live for if not marked
46 $last_clean = 0; # last time we did a clean
47 @forward = (); # msg forward table
48 @badmsg = (); # bad message table
49 @swop = (); # swop table
50 $timeout = 30*60; # forwarding timeout
51 $waittime = 30*60; # time an aborted outgoing message waits before trying again
52 $queueinterval = 1*60; # run the queue every 1 minute
55 $minchunk = 4800; # minimum chunk size for a split message
56 $maxchunk = 6000; # maximum chunk size
57 $bulltopriv = 1; # convert msgs with callsigns to private if they are bulls
60 $badmsgfn = "$msgdir/badmsg.pl"; # list of TO address we wont store
61 $forwardfn = "$msgdir/forward.pl"; # the forwarding table
62 $swopfn = "$msgdir/swop.pl"; # the swopping table
63 $importfn = "$msgdir/import"; # import directory
67 fromnode => '5,From Node',
68 tonode => '5,To Node',
71 t => '0,Msg Time,cldatetime',
72 private => '5,Private',
73 subject => '0,Subject',
74 linesreq => '0,Lines per Gob',
75 rrreq => '5,Read Confirm',
78 stream => '9,Stream No',
79 count => '5,Gob Linecnt',
80 file => '5,File?,yesno',
81 gotit => '5,Got it Nodes,parray',
82 lines => '5,Lines,parray',
83 'read' => '5,Times read',
86 keep => '0,Keep this?,yesno',
87 lastt => '5,Last processed,cldatetime',
88 waitt => '5,Wait until,cldatetime',
91 # allocate a new object
92 # called fromnode, tonode, from, to, datetime, private?, subject, nolinesper
96 my $self = bless {}, $pkg;
97 $self->{msgno} = shift;
100 $self->{to} = ($to eq $main::mycall) ? $main::myalias : $to;
103 $self->{from} = uc $from;
105 $self->{private} = shift;
106 $self->{subject} = shift;
107 $self->{origin} = shift;
108 $self->{'read'} = shift;
109 $self->{rrreq} = shift;
111 # $self->{lastt} = $main::systime;
113 $self->{private} = 1 if $bulltopriv && DXUser->get_current($self->{to});
121 delete $ref->{lines};
122 delete $ref->{linesreq};
123 delete $ref->{tonode};
124 delete $ref->{fromnode};
125 delete $ref->{stream};
127 delete $ref->{count};
128 delete $ref->{lastt} if exists $ref->{lastt};
129 delete $ref->{waitt} if exists $ref->{waitt};
134 my ($self, $line) = @_;
136 # this is periodic processing
137 if (!$self || !$line) {
139 if ($main::systime >= $lastq + $queueinterval) {
141 # queue some message if the interval timer has gone off
144 # import any messages in the import directory
147 $lastq = $main::systime;
150 # clean the message queue
151 clean_old() if $main::systime - $last_clean > 3600 ;
155 my @f = split /\^/, $line;
156 my ($pcno) = $f[0] =~ /^PC(\d\d)/; # just get the number
159 if ($pcno == 28) { # incoming message
161 # sort out various extant protocol errors that occur
162 my ($fromnode, $origin);
163 if ($self->is_arcluster && $f[13] eq $self->call) {
170 $origin = $self->call unless $origin && $origin gt ' ';
172 # first look for any messages in the busy queue
173 # and cancel them this should both resolve timed out incoming messages
174 # and crossing of message between nodes, incoming messages have priority
176 if (exists $busy{$fromnode}) {
177 my $ref = $busy{$fromnode};
178 my $tonode = $ref->{tonode} || "unknown";
179 dbg("Busy, stopping msgno: $ref->{msgno} $fromnode->$tonode") if isdbg('msg');
180 $ref->stop_msg($self->call);
183 my $t = cltounix($f[5], $f[6]);
184 my $stream = next_transno($fromnode);
185 my $ref = DXMsg->alloc($stream, uc $f[3], $f[4], $t, $f[7], $f[8], $origin, '0', $f[11]);
187 # fill in various forwarding state variables
188 $ref->{fromnode} = $fromnode;
189 $ref->{tonode} = $f[1];
190 $ref->{rrreq} = $f[11];
191 $ref->{linesreq} = $f[10];
192 $ref->{stream} = $stream;
193 $ref->{count} = 0; # no of lines between PC31s
194 dbg("new message from $f[4] to $f[3] '$f[8]' stream $fromnode/$stream\n") if isdbg('msg');
195 Log('msg', "Incoming message $f[4] to $f[3] '$f[8]'" );
196 $work{"$fromnode$stream"} = $ref; # store in work
197 $busy{$fromnode} = $ref; # set interlock
198 $self->send(DXProt::pc30($fromnode, $f[1], $stream)); # send ack
199 $ref->{lastt} = $main::systime;
201 # look to see whether this is a non private message sent to a known callsign
202 my $uref = DXUser->get_current($ref->{to});
203 if (is_callsign($ref->{to}) && !$ref->{private} && $uref && $uref->homenode) {
205 dbg("set bull to $ref->{to} to private") if isdbg('msg');
210 if ($pcno == 29) { # incoming text
211 my $ref = $work{"$f[2]$f[3]"};
214 push @{$ref->{lines}}, $f[4];
216 if ($ref->{count} >= $ref->{linesreq}) {
217 $self->send(DXProt::pc31($f[2], $f[1], $f[3]));
218 dbg("stream $f[3]: $ref->{count} lines received\n") if isdbg('msg');
221 $ref->{lastt} = $main::systime;
223 dbg("PC29 from unknown stream $f[3] from $f[2]") if isdbg('msg');
224 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
229 if ($pcno == 30) { # this is a incoming subject ack
230 my $ref = $work{$f[2]}; # note no stream at this stage
233 $ref->{stream} = $f[3];
235 $ref->{linesreq} = 5;
236 $work{"$f[2]$f[3]"} = $ref; # new ref
237 dbg("incoming subject ack stream $f[3]\n") if isdbg('msg');
238 $busy{$f[2]} = $ref; # interlock
239 push @{$ref->{lines}}, ($ref->read_msg_body);
240 $ref->send_tranche($self);
241 $ref->{lastt} = $main::systime;
243 dbg("PC30 from unknown stream $f[3] from $f[2]") if isdbg('msg');
244 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
249 if ($pcno == 31) { # acknowledge a tranche of lines
250 my $ref = $work{"$f[2]$f[3]"};
252 dbg("tranche ack stream $f[3]\n") if isdbg('msg');
253 $ref->send_tranche($self);
254 $ref->{lastt} = $main::systime;
256 dbg("PC31 from unknown stream $f[3] from $f[2]") if isdbg('msg');
257 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
262 if ($pcno == 32) { # incoming EOM
263 dbg("stream $f[3]: EOM received\n") if isdbg('msg');
264 my $ref = $work{"$f[2]$f[3]"};
266 $self->send(DXProt::pc33($f[2], $f[1], $f[3])); # acknowledge it
268 # get the next msg no - note that this has NOTHING to do with the stream number in PC protocol
269 # store the file or message
270 # remove extraneous rubbish from the hash
271 # remove it from the work in progress vector
272 # stuff it on the msg queue
275 $ref->store($ref->{lines});
278 # does an identical message already exist?
281 if ($ref->{subject} eq $m->{subject} && $ref->{t} == $m->{t} && $ref->{from} eq $m->{from} && $ref->{to} eq $m->{to}) {
282 $ref->stop_msg($self->call);
283 my $msgno = $m->{msgno};
284 dbg("duplicate message from $ref->{from} -> $ref->{to} to msg: $msgno") if isdbg('msg');
285 Log('msg', "duplicate message from $ref->{from} -> $ref->{to} to msg: $msgno");
291 $ref->swop_it($self->call);
293 # look for 'bad' to addresses
294 if ($ref->dump_it($self->call)) {
295 $ref->stop_msg($self->call);
296 dbg("'Bad' message $ref->{to}") if isdbg('msg');
297 Log('msg', "'Bad' message $ref->{to}");
301 $ref->{msgno} = next_transno("Msgno");
302 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
303 $ref->store($ref->{lines});
305 my $dxchan = DXChannel->get($ref->{to});
306 $dxchan->send($dxchan->msg('m9')) if $dxchan && $dxchan->is_user;
307 Log('msg', "Message $ref->{msgno} from $ref->{from} received from $f[2] for $ref->{to}");
310 $ref->stop_msg($self->call);
312 dbg("PC32 from unknown stream $f[3] from $f[2]") if isdbg('msg');
313 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
319 if ($pcno == 33) { # acknowledge the end of message
320 my $ref = $work{"$f[2]$f[3]"};
322 if ($ref->{private}) { # remove it if it private and gone off site#
323 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2] and deleted");
326 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2]");
327 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
328 $ref->store($ref->{lines}); # re- store the file
330 $ref->stop_msg($self->call);
332 dbg("PC33 from unknown stream $f[3] from $f[2]") if isdbg('msg');
333 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
336 # send next one if present
341 if ($pcno == 40) { # this is a file request
342 $f[3] =~ s/\\/\//og; # change the slashes
343 $f[3] =~ s/\.//og; # remove dots
344 $f[3] =~ s/^\///o; # remove the leading /
345 $f[3] = lc $f[3]; # to lower case;
346 dbg("incoming file $f[3]\n") if isdbg('msg');
347 $f[3] = 'packclus/' . $f[3] unless $f[3] =~ /^packclus\//o;
349 # create any directories
350 my @part = split /\//, $f[3];
352 my $fn = "$main::root";
353 pop @part; # remove last part
354 foreach $part (@part) {
357 last SWITCH if !mkdir $fn, 0777;
358 dbg("created directory $fn\n") if isdbg('msg');
360 my $stream = next_transno($f[2]);
361 my $ref = DXMsg->alloc($stream, "$main::root/$f[3]", $self->call, time, !$f[4], $f[3], ' ', '0', '0');
363 # forwarding variables
364 $ref->{fromnode} = $f[1];
365 $ref->{tonode} = $f[2];
366 $ref->{linesreq} = $f[5];
367 $ref->{stream} = $stream;
368 $ref->{count} = 0; # no of lines between PC31s
370 $ref->{lastt} = $main::systime;
371 $work{"$f[2]$stream"} = $ref; # store in work
372 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
377 if ($pcno == 42) { # abort transfer
378 dbg("stream $f[3]: abort received\n") if isdbg('msg');
379 my $ref = $work{"$f[2]$f[3]"};
381 $ref->stop_msg($self->call);
387 if ($pcno == 49) { # global delete on subject
389 if ($_->{from} eq $f[1] && $_->{subject} eq $f[2]) {
391 Log('msg', "Message $_->{msgno} from $_->{from} ($_->{subject}) fully deleted");
392 DXChannel::broadcast_nodes($line, $self);
400 # store a message away on disc or whatever
402 # NOTE the second arg is a REFERENCE not a list
408 if ($ref->{file}) { # a file
409 dbg("To be stored in $ref->{to}\n") if isdbg('msg');
411 my $fh = new IO::File "$ref->{to}", "w";
414 foreach $line (@{$lines}) {
418 dbg("file $ref->{to} stored\n") if isdbg('msg');
419 Log('msg', "file $ref->{to} from $ref->{from} stored" );
421 confess "can't open file $ref->{to} $!";
423 } else { # a normal message
425 # attempt to open the message file
426 my $fn = filename($ref->{msgno});
428 dbg("To be stored in $fn\n") if isdbg('msg');
430 # now save the file, overwriting what's there, YES I KNOW OK! (I will change it if it's a problem)
431 my $fh = new IO::File "$fn", "w";
433 my $rr = $ref->{rrreq} ? '1' : '0';
434 my $priv = $ref->{private} ? '1': '0';
435 print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$priv^$ref->{subject}^$ref->{origin}^$ref->{'read'}^$rr\n";
436 print $fh "=== ", join('^', @{$ref->{gotit}}), "\n";
439 foreach $line (@{$lines}) {
440 $ref->{size} += (length $line) + 1;
444 dbg("msg $ref->{msgno} stored\n") if isdbg('msg');
445 Log('msg', "msg $ref->{msgno} from $ref->{from} to $ref->{to} stored" );
447 confess "can't open msg file $fn $!";
457 # remove it from the active message list
458 dbg("\@msg = " . scalar @msg . " before delete") if isdbg('msg');
459 @msg = grep { $_ != $self } @msg;
462 unlink filename($self->{msgno});
463 dbg("deleting $self->{msgno}\n") if isdbg('msg');
464 dbg("\@msg = " . scalar @msg . " after delete") if isdbg('msg');
467 # clean out old messages from the message queue
472 # mark old messages for deletion
473 dbg("\@msg = " . scalar @msg . " before delete") if isdbg('msg');
474 foreach $ref (@msg) {
475 if (ref($ref) && !$ref->{keep} && $ref->{t} < $main::systime - $maxage) {
476 $ref->{deleteme} = 1;
477 unlink filename($ref->{msgno});
478 dbg("deleting old $ref->{msgno}\n") if isdbg('msg');
482 # remove them all from the active message list
483 @msg = grep { !$_->{deleteme} } @msg;
484 dbg("\@msg = " . scalar @msg . " after delete") if isdbg('msg');
485 $last_clean = $main::systime;
488 # read in a message header
498 $file = new IO::File "$fn";
500 dbg("Error reading $fn $!");
501 Log('err', "Error reading $fn $!");
505 $line = <$file>; # first line
506 if ($size == 0 || !$line) {
508 Log('err', "Empty $fn $!");
512 $size -= length $line;
513 if (! $line =~ /^===/o) {
514 dbg("corrupt first line in $fn ($line)");
515 Log('err', "corrupt first line in $fn ($line)");
519 @f = split /\^/, $line;
520 $ref = DXMsg->alloc(@f);
522 $line = <$file>; # second line
524 $size -= length $line;
525 if (! $line =~ /^===/o) {
526 dbg("corrupt second line in $fn ($line)");
527 Log('err', "corrupt second line in $fn ($line)");
532 @f = split /\^/, $line;
533 push @{$ref->{gotit}}, @f;
534 $ref->{size} = $size;
541 # read in a message header
545 my $msgno = $self->{msgno};
548 my $fn = filename($msgno);
551 $file = new IO::File;
552 if (!open($file, $fn)) {
553 dbg("Error reading $fn $!");
554 Log('err' ,"Error reading $fn $!");
557 @out = map {chomp; $_} <$file>;
560 shift @out if $out[0] =~ /^=== /;
561 shift @out if $out[0] =~ /^=== /;
565 # send a tranche of lines to the other end
568 my ($self, $dxchan) = @_;
570 my $to = $self->{tonode};
571 my $from = $self->{fromnode};
572 my $stream = $self->{stream};
573 my $lines = $self->{lines};
576 for ($i = 0, $c = $self->{count}; $i < $self->{linesreq} && $c < @$lines; $i++, $c++) {
577 push @out, DXProt::pc29($to, $from, $stream, $lines->[$c]);
581 push @out, DXProt::pc32($to, $from, $stream) if $i < $self->{linesreq};
586 # find a message to send out and start the ball rolling
593 # bat down the message list looking for one that needs to go off site and whose
594 # nearest node is not busy.
596 dbg("queue msg ($sort)\n") if isdbg('msg');
597 my @nodelist = DXChannel::get_all_nodes;
598 foreach $ref (@msg) {
600 # ignore 'delayed' messages until their waiting time has expired
601 if (exists $ref->{waitt}) {
602 next if $ref->{waitt} > $main::systime;
603 delete $ref->{waitt};
607 if (exists $ref->{lastt} && $main::systime >= $ref->{lastt} + $timeout) {
608 my $node = $ref->{tonode};
609 dbg("Timeout, stopping msgno: $ref->{msgno} -> $node") if isdbg('msg');
610 Log('msg', "Timeout, stopping msgno: $ref->{msgno} -> $node");
611 $ref->stop_msg($node);
613 # delay any outgoing messages that fail
614 $ref->{waitt} = $main::systime + $waittime + rand(120) if $node ne $main::mycall;
615 delete $ref->{lastt};
619 # firstly, is it private and unread? if so can I find the recipient
620 # in my cluster node list offsite?
622 # deal with routed private messages
624 if ($ref->{private}) {
625 next if $ref->{'read'}; # if it is read, it is stuck here
626 $clref = Route::get($ref->{to});
627 # unless ($clref) { # otherwise look for a homenode
628 # my $uref = DXUser->get_current($ref->{to});
629 # my $hnode = $uref->homenode if $uref;
630 # $clref = Route::Node::get($hnode) if $hnode;
633 $dxchan = $clref->dxchan;
635 if ($dxchan->is_node) {
636 next if $clref->call eq $main::mycall; # i.e. it lives here
637 $ref->start_msg($dxchan) if !get_busy($dxchan->call) && $dxchan->state eq 'normal';
640 dbg("Route: No dxchan for $ref->{to} " . ref($clref) ) if isdbg('msg');
645 # otherwise we are dealing with a bulletin or forwarded private message
646 # compare the gotit list with
647 # the nodelist up above, if there are sites that haven't got it yet
648 # then start sending it - what happens when we get loops is anyone's
649 # guess, use (to, from, time, subject) tuple?
650 foreach $dxchan (@nodelist) {
651 my $call = $dxchan->call;
653 next if $call eq $main::mycall;
654 next if ref $ref->{gotit} && grep $_ eq $call, @{$ref->{gotit}};
655 next unless $ref->forward_it($call); # check the forwarding file
657 # if we are here we have a node that doesn't have this message
658 if (!get_busy($call) && $dxchan->state eq 'normal') {
659 $ref->start_msg($dxchan);
665 # if all the available nodes are busy then stop
666 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
670 # is there a message for me?
676 foreach $ref (@msg) {
677 # is it for me, private and unread?
678 if ($ref->{to} eq $call && $ref->{private}) {
679 return 1 if !$ref->{'read'};
685 # start the message off on its travels with a PC28
688 my ($self, $dxchan) = @_;
690 dbg("start msg $self->{msgno}\n") if isdbg('msg');
691 $self->{linesreq} = 10;
693 $self->{tonode} = $dxchan->call;
694 $self->{fromnode} = $main::mycall;
695 $busy{$self->{tonode}} = $self;
696 $work{$self->{tonode}} = $self;
697 $self->{lastt} = $main::systime;
698 my ($fromnode, $origin);
699 if ($dxchan->is_arcluster) {
700 $fromnode = $self->{origin};
701 $origin = $self->{fromnode};
703 $fromnode = $self->{fromnode};
704 $origin = $self->{origin};
706 $dxchan->send(DXProt::pc28($self->{tonode}, $fromnode, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $origin, $self->{rrreq}));
709 # get the ref of a busy node
722 # get the forwarding queue
728 # stop a message from continuing, clean it out, unlock interlocks etc
733 my $stream = $self->{stream} if exists $self->{stream};
736 dbg("stop msg $self->{msgno} -> node $node\n") if isdbg('msg');
738 delete $work{"$node$stream"} if $stream;
743 # get a new transaction number from the file specified
747 $name =~ s/\W//og; # remove non-word characters
748 my $fn = "$msgdir/$name";
751 my $fh = new IO::File;
752 if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
754 $msgno = $fh->getline || '0';
758 $fh->print("$msgno\n");
759 dbg("msgno $msgno allocated for $name\n") if isdbg('msg');
762 confess "can't open $fn $!";
767 # initialise the message 'system', read in all the message headers
770 my $dir = new IO::File;
774 # load various control files
775 dbg("load badmsg: " . (load_badmsg() or "Ok"));
776 dbg("load forward: " . (load_forward() or "Ok"));
777 dbg("load swop: " . (load_swop() or "Ok"));
779 # read in the directory
780 opendir($dir, $msgdir) or confess "can't open $msgdir $!";
781 @dir = readdir($dir);
786 next unless /^m\d\d\d\d\d\d$/;
788 $ref = read_msg_header("$msgdir/$_");
791 Log('err', "Deleting $_");
796 # delete any messages to 'badmsg.pl' places
797 if ($ref->dump_it('')) {
798 dbg("'Bad' TO address $ref->{to}") if isdbg('msg');
799 Log('msg', "'Bad' TO address $ref->{to}");
804 # add the message to the available queue
809 # add the message to the directory listing
813 confess "tried to add a non-ref to the msg directory" if !ref $ref;
817 # return all the current messages
823 # get a particular message
828 return $_ if $_->{msgno} == $msgno;
829 last if $_->{msgno} > $msgno;
834 # return the official filename for a message no
837 return sprintf "$msgdir/m%06d", shift;
841 # return a list of valid elements
850 # return a prompt for a field
855 my ($self, $ele) = @_;
860 # send a message state machine
867 if ($self->state eq 'send1') {
869 confess "local var gone missing" if !ref $self->{loc};
870 my $loc = $self->{loc};
871 $loc->{subject} = $line;
873 $self->state('sendbody');
874 #push @out, $self->msg('sendbody');
875 push @out, $self->msg('m8');
876 } elsif ($self->state eq 'sendbody') {
877 confess "local var gone missing" if !ref $self->{loc};
878 my $loc = $self->{loc};
879 if ($line eq "\032" || $line eq '%1A' || uc $line eq "/EX") {
882 foreach $to (@{$loc->{to}}) {
884 my $systime = $main::systime;
885 my $mycall = $main::mycall;
886 $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
888 exists $loc->{from} ? $loc->{from} : $self->call,
892 exists $loc->{origin} ? $loc->{origin} : $mycall,
895 $ref->swop_it($self->call);
896 $ref->store($loc->{lines});
898 push @out, $self->msg('m11', $ref->{msgno}, $to);
899 #push @out, "msgno $ref->{msgno} sent to $to";
900 my $dxchan = DXChannel->get(uc $to);
902 if ($dxchan->is_user()) {
903 $dxchan->send($dxchan->msg('m9'));
908 delete $loc->{lines};
913 $self->state('prompt');
914 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
915 #push @out, $self->msg('sendabort');
916 push @out, $self->msg('m10');
917 delete $loc->{lines};
921 $self->state('prompt');
924 # i.e. it ain't and end or abort, therefore store the line
925 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
931 # return the standard directory line for this ref
935 return sprintf "%6d%s%s%5d %8.8s %8.8s %-6.6s %5.5s %-30.30s",
936 $ref->msgno, $ref->read ? '-' : ' ', $ref->private ? 'p' : ' ', $ref->size,
937 $ref->to, $ref->from, cldate($ref->t), ztime($ref->t), $ref->subject;
940 # load the forward table
944 my $s = readfilestr($forwardfn);
952 # load the bad message table
956 my $s = readfilestr($badmsgfn);
964 # load the swop message table
968 my $s = readfilestr($swopfn);
977 # forward that message or not according to the forwarding table
978 # returns 1 for forward, 0 - to ignore
987 for ($i = 0; $i < @forward; $i += 5) {
988 my ($sort, $field, $pattern, $action, $bbs) = @forward[$i..($i+4)];
992 next if $ref->{private} && $sort ne 'P';
993 next if !$ref->{private} && $sort ne 'B';
996 $tested = $ref->{to} if $field eq 'T';
997 $tested = $ref->{from} if $field eq 'F';
998 $tested = $ref->{origin} if $field eq 'O';
999 $tested = $ref->{subject} if $field eq 'S';
1001 if (!$pattern || $tested =~ m{$pattern}i) {
1002 return 0 if $action eq 'I';
1003 return 1 if !$bbs || grep $_ eq $call, @{$bbs};
1015 for ($i = 0; $i < @badmsg; $i += 3) {
1016 my ($sort, $field, $pattern) = @badmsg[$i..($i+2)];
1019 # are we interested?
1020 next if $ref->{private} && $sort ne 'P';
1021 next if !$ref->{private} && $sort ne 'B';
1024 $tested = $ref->{to} if $field eq 'T';
1025 $tested = $ref->{from} if $field eq 'F';
1026 $tested = $ref->{origin} if $field eq 'O';
1027 $tested = $ref->{subject} if $field eq 'S';
1028 $tested = $call if $field eq 'I';
1030 if (!$pattern || $tested =~ m{$pattern}i) {
1044 for ($i = 0; $i < @swop; $i += 5) {
1045 my ($sort, $field, $pattern, $tfield, $topattern) = @swop[$i..($i+4)];
1050 # are we interested?
1051 next if $ref->{private} && $sort ne 'P';
1052 next if !$ref->{private} && $sort ne 'B';
1055 $tested = $ref->{to} if $field eq 'T';
1056 $tested = $ref->{from} if $field eq 'F';
1057 $tested = $ref->{origin} if $field eq 'O';
1058 $tested = $ref->{subject} if $field eq 'S';
1061 $old = $swop = $ref->{to} if $tfield eq 'T';
1062 $old = $swop = $ref->{from} if $tfield eq 'F';
1063 $old = $swop = $ref->{origin} if $tfield eq 'O';
1064 $old = $swop = $ref->{subject} if $tfield eq 'S';
1066 if ($tested =~ m{$pattern}i) {
1067 if ($tested eq $swop) {
1068 $swop =~ s{$pattern}{$topattern}i;
1072 Log('msg', "Msg $ref->{msgno}: $tfield $old -> $swop");
1073 Log('dbg', "Msg $ref->{msgno}: $tfield $old -> $swop");
1074 $ref->{to} = $swop if $tfield eq 'T';
1075 $ref->{from} = $swop if $tfield eq 'F';
1076 $ref->{origin} = $swop if $tfield eq 'O';
1077 $ref->{subject} = $swop if $tfield eq 'S';
1084 # import any msgs in the import directory
1085 # the messages are in BBS format (but may have cluster extentions
1086 # so SB UK < GB7TLH is legal
1089 # are there any to do in this directory?
1090 return unless -d $importfn;
1091 unless (opendir(DIR, $importfn)) {
1092 dbg("can\'t open $importfn $!") if isdbg('msg');
1093 Log('msg', "can\'t open $importfn $!");
1097 my @names = readdir(DIR);
1100 foreach $name (@names) {
1101 next if $name =~ /^\./;
1102 my $splitit = $name =~ /^split/;
1103 my $fn = "$importfn/$name";
1105 unless (open(MSG, $fn)) {
1106 dbg("can\'t open import file $fn $!") if isdbg('msg');
1107 Log('msg', "can\'t open import file $fn $!");
1111 my @msg = map { chomp; $_ } <MSG>;
1114 my @out = import_one($main::me, \@msg, $splitit);
1119 # import one message as a list in bbs (as extended) mode
1120 # takes a reference to an array containing the whole message
1125 my $splitit = shift;
1129 my $from = $dxchan->call;
1130 my $origin = $main::mycall;
1135 my $line = shift @$ref;
1136 my @f = split /\s+/, $line;
1137 unless (@f && $f[0] =~ /^(:?S|SP|SB|SEND)$/ ) {
1138 my $m = "invalid first line in import '$line'";
1139 dbg($m) if isdbg('msg');
1143 my $f = uc shift @f;
1144 next if $f eq 'SEND';
1146 # private / noprivate / rr
1147 if ($notincalls && ($f eq 'B' || $f eq 'SB' || $f =~ /^NOP/oi)) {
1149 } elsif ($notincalls && ($f eq 'P' || $f eq 'SP' || $f =~ /^PRI/oi)) {
1151 } elsif ($notincalls && ($f eq 'RR')) {
1153 } elsif ($f eq '@' && @f) { # this is bbs syntax, for origin
1154 $origin = uc shift @f;
1155 } elsif ($f eq '<' && @f) { # this is bbs syntax for from call
1156 $from = uc shift @f;
1157 } elsif ($f =~ /^\$/) { # this is bbs syntax for a bid
1159 } elsif ($f =~ /^<\S+/) { # this is bbs syntax for from call
1160 ($from) = $f =~ /^<(\S+)$/;
1161 } elsif ($f =~ /^\@\S+/) { # this is bbs syntax for origin
1162 ($origin) = $f =~ /^\@(\S+)$/;
1168 # is this callsign a distro?
1169 my $fn = "$msgdir/distro/$f.pl";
1171 my $fh = new IO::File $fn;
1178 return (1, "Error in Distro $f.pl:", $@) if $@;
1186 if (grep $_ eq $f, @DXMsg::badmsg) {
1187 push @out, $dxchan->msg('m3', $f);
1194 # subject is the next line
1195 my $subject = shift @$ref;
1197 # strip off trailing lines
1198 pop @$ref while (@$ref && $$ref[-1] =~ /^\s*$/);
1200 # strip off /EX or /ABORT
1201 return ("aborted") if @$ref && $$ref[-1] =~ m{^/ABORT$}i;
1202 pop @$ref if (@$ref && $$ref[-1] =~ m{^/EX$}i);
1204 # sort out any splitting that needs to be done
1210 if ($lth >= $maxchunk || ($lth > $minchunk && /^\s*$/)) {
1211 push @chunk, $lines;
1218 push @chunk, $lines if @$lines;
1223 # write all the messages away
1225 for ( $i = 0; $i < @chunk; $i++) {
1226 my $chunk = $chunk[$i];
1229 my $num = " [" . ($i+1) . "/" . scalar @chunk . "]";
1230 $ch_subject = substr($subject, 0, 27 - length $num) . $num;
1232 $ch_subject = $subject;
1236 my $systime = $main::systime;
1237 my $mycall = $main::mycall;
1238 my $mref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
1247 $mref->swop_it($main::mycall);
1248 $mref->store($chunk);
1250 push @out, $dxchan->msg('m11', $mref->{msgno}, $to);
1251 #push @out, "msgno $ref->{msgno} sent to $to";
1252 my $todxchan = DXChannel->get(uc $to);
1254 if ($todxchan->is_user()) {
1255 $todxchan->send($todxchan->msg('m9'));
1267 my $name = $AUTOLOAD;
1268 return if $name =~ /::DESTROY$/;
1271 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
1272 # this clever line of code creates a subroutine which takes over from autoload
1273 # from OO Perl - Conway
1274 *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
1275 @_ ? $self->{$name} = shift : $self->{$name} ;