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 @swop $swopfn $badmsgfn $forwardfn @forward $timeout $waittime
36 $queueinterval $lastq $importfn $minchunk $maxchunk);
38 %work = (); # outstanding jobs
39 @msg = (); # messages we have
40 %busy = (); # station interlocks
41 $msgdir = "$main::root/msg"; # directory contain the msgs
42 $maxage = 30 * 86400; # the maximum age that a message shall live for if not marked
43 $last_clean = 0; # last time we did a clean
44 @forward = (); # msg forward table
45 @badmsg = (); # bad message table
46 @swop = (); # swop table
47 $timeout = 30*60; # forwarding timeout
48 $waittime = 30*60; # time an aborted outgoing message waits before trying again
49 $queueinterval = 1*60; # run the queue every 1 minute
52 $minchunk = 4800; # minimum chunk size for a split message
53 $maxchunk = 6000; # maximum chunk size
55 $badmsgfn = "$msgdir/badmsg.pl"; # list of TO address we wont store
56 $forwardfn = "$msgdir/forward.pl"; # the forwarding table
57 $swopfn = "$msgdir/swop.pl"; # the swopping table
58 $importfn = "$msgdir/import"; # import directory
62 fromnode => '5,From Node',
63 tonode => '5,To Node',
66 t => '0,Msg Time,cldatetime',
67 private => '5,Private',
68 subject => '0,Subject',
69 linesreq => '0,Lines per Gob',
70 rrreq => '5,Read Confirm',
73 stream => '9,Stream No',
74 count => '5,Gob Linecnt',
75 file => '5,File?,yesno',
76 gotit => '5,Got it Nodes,parray',
77 lines => '5,Lines,parray',
78 'read' => '5,Times read',
81 keep => '0,Keep this?,yesno',
82 lastt => '5,Last processed,cldatetime',
83 waitt => '5,Wait until,cldatetime',
93 # allocate a new object
94 # called fromnode, tonode, from, to, datetime, private?, subject, nolinesper
98 my $self = bless {}, $pkg;
99 $self->{msgno} = shift;
102 $self->{to} = ($to eq $main::mycall) ? $main::myalias : $to;
105 $self->{from} = uc $from;
107 $self->{private} = shift;
108 $self->{subject} = shift;
109 $self->{origin} = shift;
110 $self->{'read'} = shift;
111 $self->{rrreq} = shift;
113 $self->{lastt} = $main::systime;
121 delete $ref->{lines};
122 delete $ref->{linesreq};
123 delete $ref->{tonode};
124 delete $ref->{fromnode};
125 delete $ref->{stream};
126 delete $ref->{lines};
128 delete $ref->{count};
129 delete $ref->{lastt} if exists $ref->{lastt};
130 delete $ref->{waitt} if exists $ref->{waitt};
135 my ($self, $line) = @_;
137 # this is periodic processing
138 if (!$self || !$line) {
140 if ($main::systime > $lastq + $queueinterval) {
142 # wander down the work queue stopping any messages that have timed out
146 if (exists $ref->{lastt} && $main::systime >= $ref->{lastt} + $timeout) {
147 dbg('msg', "Timeout, stopping msgno: $ref->{msgno} -> $node");
148 $ref->stop_msg($node);
150 # delay any outgoing messages that fail
151 $ref->{waitt} = $main::systime + $waittime + rand(120) if $node ne $main::mycall;
155 # queue some message if the interval timer has gone off
158 # import any messages in the import directory
161 $lastq = $main::systime;
164 # clean the message queue
165 clean_old() if $main::systime - $last_clean > 3600 ;
169 my @f = split /\^/, $line;
170 my ($pcno) = $f[0] =~ /^PC(\d\d)/; # just get the number
173 if ($pcno == 28) { # incoming message
175 # first look for any messages in the busy queue
176 # and cancel them this should both resolve timed out incoming messages
177 # and crossing of message between nodes, incoming messages have priority
178 if (exists $busy{$f[2]}) {
179 my $ref = $busy{$f[2]};
180 my $tonode = $ref->{tonode};
181 dbg('msg', "Busy, stopping msgno: $ref->{msgno} -> $f[2]");
182 $ref->stop_msg($self->call);
185 my $t = cltounix($f[5], $f[6]);
186 my $stream = next_transno($f[2]);
187 my $ref = DXMsg->alloc($stream, uc $f[3], $f[4], $t, $f[7], $f[8], $f[13], '0', $f[11]);
189 # fill in various forwarding state variables
190 $ref->{fromnode} = $f[2];
191 $ref->{tonode} = $f[1];
192 $ref->{rrreq} = $f[11];
193 $ref->{linesreq} = $f[10];
194 $ref->{stream} = $stream;
195 $ref->{count} = 0; # no of lines between PC31s
196 dbg('msg', "new message from $f[4] to $f[3] '$f[8]' stream $stream\n");
197 $work{"$f[2]$stream"} = $ref; # store in work
198 $busy{$f[2]} = $ref; # set interlock
199 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
200 $ref->{lastt} = $main::systime;
202 # look to see whether this is a non private message sent to a known callsign
203 my $uref = DXUser->get_current($ref->{to});
204 if (iscallsign($ref->{to}) && !$ref->{private} && $uref && $uref->homenode) {
206 dbg('msg', "set bull to $ref->{to} to private");
211 if ($pcno == 29) { # incoming text
212 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('msg', "stream $f[3]: $ref->{count} lines received\n");
221 $ref->{lastt} = $main::systime;
223 dbg('msg', "PC29 from unknown stream $f[3] from $f[2]" );
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('msg', "incoming subject ack stream $f[3]\n");
238 $busy{$f[2]} = $ref; # interlock
240 push @{$ref->{lines}}, ($ref->read_msg_body);
241 $ref->send_tranche($self);
242 $ref->{lastt} = $main::systime;
244 dbg('msg', "PC30 from unknown stream $f[3] from $f[2]" );
245 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
250 if ($pcno == 31) { # acknowledge a tranche of lines
251 my $ref = $work{"$f[2]$f[3]"};
253 dbg('msg', "tranche ack stream $f[3]\n");
254 $ref->send_tranche($self);
255 $ref->{lastt} = $main::systime;
257 dbg('msg', "PC31 from unknown stream $f[3] from $f[2]" );
258 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
263 if ($pcno == 32) { # incoming EOM
264 dbg('msg', "stream $f[3]: EOM received\n");
265 my $ref = $work{"$f[2]$f[3]"};
267 $self->send(DXProt::pc33($f[2], $f[1], $f[3])); # acknowledge it
269 # get the next msg no - note that this has NOTHING to do with the stream number in PC protocol
270 # store the file or message
271 # remove extraneous rubbish from the hash
272 # remove it from the work in progress vector
273 # stuff it on the msg queue
276 $ref->store($ref->{lines});
279 # does an identical message already exist?
282 if ($ref->{subject} eq $m->{subject} && $ref->{t} == $m->{t} && $ref->{from} eq $m->{from} && $ref->{to} eq $m->{to}) {
283 $ref->stop_msg($self->call);
284 my $msgno = $m->{msgno};
285 dbg('msg', "duplicate message to $msgno\n");
286 Log('msg', "duplicate message to $msgno");
292 $ref->swop_it($self->call);
294 # look for 'bad' to addresses
295 # if (grep $ref->{to} eq $_, @badmsg) {
296 if ($ref->dump_it($self->call)) {
297 $ref->stop_msg($self->call);
298 dbg('msg', "'Bad' message $ref->{to}");
299 Log('msg', "'Bad' message $ref->{to}");
303 $ref->{msgno} = next_transno("Msgno");
304 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
305 $ref->store($ref->{lines});
307 my $dxchan = DXChannel->get($ref->{to});
308 $dxchan->send($dxchan->msg('m9')) if $dxchan && $dxchan->is_user;
309 Log('msg', "Message $ref->{msgno} from $ref->{from} received from $f[2] for $ref->{to}");
312 $ref->stop_msg($self->call);
314 dbg('msg', "PC32 from unknown stream $f[3] from $f[2]" );
315 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
321 if ($pcno == 33) { # acknowledge the end of message
322 my $ref = $work{"$f[2]$f[3]"};
324 if ($ref->{private}) { # remove it if it private and gone off site#
325 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2] and deleted");
328 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2]");
329 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
330 $ref->store($ref->{lines}); # re- store the file
332 $ref->stop_msg($self->call);
334 dbg('msg', "PC33 from unknown stream $f[3] from $f[2]" );
335 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
338 # send next one if present
343 if ($pcno == 40) { # this is a file request
344 $f[3] =~ s/\\/\//og; # change the slashes
345 $f[3] =~ s/\.//og; # remove dots
346 $f[3] =~ s/^\///o; # remove the leading /
347 $f[3] = lc $f[3]; # to lower case;
348 dbg('msg', "incoming file $f[3]\n");
349 $f[3] = 'packclus/' . $f[3] unless $f[3] =~ /^packclus\//o;
351 # create any directories
352 my @part = split /\//, $f[3];
354 my $fn = "$main::root";
355 pop @part; # remove last part
356 foreach $part (@part) {
359 last SWITCH if !mkdir $fn, 0777;
360 dbg('msg', "created directory $fn\n");
362 my $stream = next_transno($f[2]);
363 my $ref = DXMsg->alloc($stream, "$main::root/$f[3]", $self->call, time, !$f[4], $f[3], ' ', '0', '0');
365 # forwarding variables
366 $ref->{fromnode} = $f[1];
367 $ref->{tonode} = $f[2];
368 $ref->{linesreq} = $f[5];
369 $ref->{stream} = $stream;
370 $ref->{count} = 0; # no of lines between PC31s
372 $ref->{lastt} = $main::systime;
373 $work{"$f[2]$stream"} = $ref; # store in work
374 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
379 if ($pcno == 42) { # abort transfer
380 dbg('msg', "stream $f[3]: abort received\n");
381 my $ref = $work{"$f[2]$f[3]"};
383 $ref->stop_msg($self->call);
389 if ($pcno == 49) { # global delete on subject
391 if ($_->{from} eq $f[1] && $_->{subject} eq $f[2]) {
393 Log('msg', "Message $_->{msgno} from $_->{from} ($_->{subject}) fully deleted");
394 DXProt::broadcast_ak1a($line, $self);
402 # store a message away on disc or whatever
404 # NOTE the second arg is a REFERENCE not a list
410 # we only proceed if there are actually any lines in the file
411 # if (!$lines || @{$lines} == 0) {
415 if ($ref->{file}) { # a file
416 dbg('msg', "To be stored in $ref->{to}\n");
418 my $fh = new IO::File "$ref->{to}", "w";
421 foreach $line (@{$lines}) {
425 dbg('msg', "file $ref->{to} stored\n");
426 Log('msg', "file $ref->{to} from $ref->{from} stored" );
428 confess "can't open file $ref->{to} $!";
430 } else { # a normal message
432 # attempt to open the message file
433 my $fn = filename($ref->{msgno});
435 dbg('msg', "To be stored in $fn\n");
437 # now save the file, overwriting what's there, YES I KNOW OK! (I will change it if it's a problem)
438 my $fh = new IO::File "$fn", "w";
440 my $rr = $ref->{rrreq} ? '1' : '0';
441 my $priv = $ref->{private} ? '1': '0';
442 print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$priv^$ref->{subject}^$ref->{origin}^$ref->{'read'}^$rr\n";
443 print $fh "=== ", join('^', @{$ref->{gotit}}), "\n";
446 foreach $line (@{$lines}) {
447 $ref->{size} += (length $line) + 1;
451 dbg('msg', "msg $ref->{msgno} stored\n");
452 Log('msg', "msg $ref->{msgno} from $ref->{from} to $ref->{to} stored" );
454 confess "can't open msg file $fn $!";
464 # remove it from the active message list
465 @msg = map { $_ != $self ? $_ : () } @msg;
467 # belt and braces (one day I will ask someone if this is REALLY necessary)
468 delete $self->{gotit};
469 delete $self->{list};
472 unlink filename($self->{msgno});
473 dbg('msg', "deleting $self->{msgno}\n");
476 # clean out old messages from the message queue
481 # mark old messages for deletion
482 foreach $ref (@msg) {
483 if (!$ref->{keep} && $ref->{t} < $main::systime - $maxage) {
484 $ref->{deleteme} = 1;
485 delete $ref->{gotit};
487 unlink filename($ref->{msgno});
488 dbg('msg', "deleting old $ref->{msgno}\n");
492 # remove them all from the active message list
493 @msg = map { $_->{deleteme} ? () : $_ } @msg;
494 $last_clean = $main::systime;
497 # read in a message header
507 $file = new IO::File;
508 if (!open($file, $fn)) {
509 print "Error reading $fn $!\n";
513 $line = <$file>; # first line
515 $size -= length $line;
516 if (! $line =~ /^===/o) {
517 print "corrupt first line in $fn ($line)\n";
521 @f = split /\^/, $line;
522 $ref = DXMsg->alloc(@f);
524 $line = <$file>; # second line
526 $size -= length $line;
527 if (! $line =~ /^===/o) {
528 print "corrupt second line in $fn ($line)\n";
533 @f = split /\^/, $line;
534 push @{$ref->{gotit}}, @f;
535 $ref->{size} = $size;
542 # read in a message header
546 my $msgno = $self->{msgno};
549 my $fn = filename($msgno);
552 $file = new IO::File;
553 if (!open($file, $fn)) {
554 print "Error reading $fn $!\n";
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 my @nodelist = DXChannel::get_all_ak1a();
595 # bat down the message list looking for one that needs to go off site and whose
596 # nearest node is not busy.
598 dbg('msg', "queue msg ($sort)\n");
599 foreach $ref (@msg) {
600 # firstly, is it private and unread? if so can I find the recipient
601 # in my cluster node list offsite?
603 # ignore 'delayed' messages until their waiting time has expired
604 if (exists $ref->{waitt}) {
605 next if $ref->{waitt} > $main::systime;
606 delete $ref->{waitt};
609 # deal with routed private messages
611 if ($ref->{private}) {
612 next if $ref->{'read'}; # if it is read, it is stuck here
613 $clref = DXCluster->get_exact($ref->{to});
614 unless ($clref) { # otherwise look for a homenode
615 my $uref = DXUser->get($ref->{to});
616 my $hnode = $uref->homenode if $uref;
617 $clref = DXCluster->get_exact($hnode) if $hnode;
619 if ($clref && !grep { $clref->{dxchan} == $_ } DXCommandmode::get_all()) {
620 next if $clref->call eq $main::mycall; # i.e. it lives here
621 $noderef = $clref->{dxchan};
622 $ref->start_msg($noderef) if !get_busy($noderef->call) && $noderef->state eq 'normal';
626 # otherwise we are dealing with a bulletin or forwarded private message
627 # compare the gotit list with
628 # the nodelist up above, if there are sites that haven't got it yet
629 # then start sending it - what happens when we get loops is anyone's
630 # guess, use (to, from, time, subject) tuple?
631 foreach $noderef (@nodelist) {
632 next if $noderef->call eq $main::mycall;
633 next if grep { $_ eq $noderef->call } @{$ref->{gotit}};
634 next unless $ref->forward_it($noderef->call); # check the forwarding file
636 # if we are here we have a node that doesn't have this message
637 $ref->start_msg($noderef) if !get_busy($noderef->call) && $noderef->state eq 'normal';
641 # if all the available nodes are busy then stop
642 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
646 # is there a message for me?
652 foreach $ref (@msg) {
653 # is it for me, private and unread?
654 if ($ref->{to} eq $call && $ref->{private}) {
655 return 1 if !$ref->{'read'};
661 # start the message off on its travels with a PC28
664 my ($self, $dxchan) = @_;
666 dbg('msg', "start msg $self->{msgno}\n");
667 $self->{linesreq} = 5;
669 $self->{tonode} = $dxchan->call;
670 $self->{fromnode} = $main::mycall;
671 $busy{$self->{tonode}} = $self;
672 $work{$self->{tonode}} = $self;
673 $self->{lastt} = $main::systime;
674 $dxchan->send(DXProt::pc28($self->{tonode}, $self->{fromnode}, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $self->{origin}, $self->{rrreq}));
677 # get the ref of a busy node
690 # get the forwarding queue
696 # stop a message from continuing, clean it out, unlock interlocks etc
701 my $stream = $self->{stream} if exists $self->{stream};
704 dbg('msg', "stop msg $self->{msgno} -> node $node\n");
706 delete $work{"$node$stream"} if $stream;
711 # get a new transaction number from the file specified
715 $name =~ s/\W//og; # remove non-word characters
716 my $fn = "$msgdir/$name";
719 my $fh = new IO::File;
720 if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
722 $msgno = $fh->getline;
726 $fh->print("$msgno\n");
727 dbg('msg', "msgno $msgno allocated for $name\n");
730 confess "can't open $fn $!";
735 # initialise the message 'system', read in all the message headers
738 my $dir = new IO::File;
742 # load various control files
743 print "load badmsg: ", (load_badmsg() or "Ok"), "\n";
744 print "load forward: ", (load_forward() or "Ok"), "\n";
745 print "load swop: ", (load_swop() or "Ok"), "\n";
747 # read in the directory
748 opendir($dir, $msgdir) or confess "can't open $msgdir $!";
749 @dir = readdir($dir);
754 next unless /^m\d+$/o;
756 $ref = read_msg_header("$msgdir/$_");
759 # delete any messages to 'badmsg.pl' places
760 if (grep $ref->{to} eq $_, @badmsg) {
761 dbg('msg', "'Bad' TO address $ref->{to}");
762 Log('msg', "'Bad' TO address $ref->{to}");
767 # add the message to the available queue
772 # add the message to the directory listing
776 confess "tried to add a non-ref to the msg directory" if !ref $ref;
780 # return all the current messages
786 # get a particular message
791 return $_ if $_->{msgno} == $msgno;
792 last if $_->{msgno} > $msgno;
797 # return the official filename for a message no
800 return sprintf "$msgdir/m%06d", shift;
804 # return a list of valid elements
813 # return a prompt for a field
818 my ($self, $ele) = @_;
823 # send a message state machine
830 if ($self->state eq 'send1') {
832 confess "local var gone missing" if !ref $self->{loc};
833 my $loc = $self->{loc};
834 $loc->{subject} = $line;
836 $self->state('sendbody');
837 #push @out, $self->msg('sendbody');
838 push @out, $self->msg('m8');
839 } elsif ($self->state eq 'sendbody') {
840 confess "local var gone missing" if !ref $self->{loc};
841 my $loc = $self->{loc};
842 if ($line eq "\032" || $line eq '%1A' || uc $line eq "/EX") {
845 foreach $to (@{$loc->{to}}) {
847 my $systime = $main::systime;
848 my $mycall = $main::mycall;
849 $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
851 exists $loc->{from} ? $loc->{from} : $self->call,
855 exists $loc->{origin} ? $loc->{origin} : $mycall,
858 $ref->swop_it($self->call);
859 $ref->store($loc->{lines});
861 push @out, $self->msg('m11', $ref->{msgno}, $to);
862 #push @out, "msgno $ref->{msgno} sent to $to";
863 my $dxchan = DXChannel->get(uc $to);
865 if ($dxchan->is_user()) {
866 $dxchan->send($dxchan->msg('m9'));
871 delete $loc->{lines};
876 $self->state('prompt');
877 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
878 #push @out, $self->msg('sendabort');
879 push @out, $self->msg('m10');
880 delete $loc->{lines};
884 $self->state('prompt');
887 # i.e. it ain't and end or abort, therefore store the line
888 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
894 # return the standard directory line for this ref
898 return sprintf "%6d%s%s%5d %8.8s %8.8s %-6.6s %5.5s %-30.30s",
899 $ref->msgno, $ref->read ? '-' : ' ', $ref->private ? 'p' : ' ', $ref->size,
900 $ref->to, $ref->from, cldate($ref->t), ztime($ref->t), $ref->subject;
903 # load the forward table
907 my $s = readfilestr($forwardfn);
915 # load the bad message table
919 my $s = readfilestr($badmsgfn);
927 # load the swop message table
931 my $s = readfilestr($swopfn);
940 # forward that message or not according to the forwarding table
941 # returns 1 for forward, 0 - to ignore
950 for ($i = 0; $i < @forward; $i += 5) {
951 my ($sort, $field, $pattern, $action, $bbs) = @forward[$i..($i+4)];
955 next if $ref->{private} && $sort ne 'P';
956 next if !$ref->{private} && $sort ne 'B';
959 $tested = $ref->{to} if $field eq 'T';
960 $tested = $ref->{from} if $field eq 'F';
961 $tested = $ref->{origin} if $field eq 'O';
962 $tested = $ref->{subject} if $field eq 'S';
964 if (!$pattern || $tested =~ m{$pattern}i) {
965 return 0 if $action eq 'I';
966 return 1 if !$bbs || grep $_ eq $call, @{$bbs};
978 for ($i = 0; $i < @badmsg; $i += 3) {
979 my ($sort, $field, $pattern) = @badmsg[$i..($i+2)];
983 next if $ref->{private} && $sort ne 'P';
984 next if !$ref->{private} && $sort ne 'B';
987 $tested = $ref->{to} if $field eq 'T';
988 $tested = $ref->{from} if $field eq 'F';
989 $tested = $ref->{origin} if $field eq 'O';
990 $tested = $ref->{subject} if $field eq 'S';
992 if (!$pattern || $tested =~ m{$pattern}i) {
1006 for ($i = 0; $i < @swop; $i += 5) {
1007 my ($sort, $field, $pattern, $tfield, $topattern) = @swop[$i..($i+4)];
1012 # are we interested?
1013 next if $ref->{private} && $sort ne 'P';
1014 next if !$ref->{private} && $sort ne 'B';
1017 $tested = $ref->{to} if $field eq 'T';
1018 $tested = $ref->{from} if $field eq 'F';
1019 $tested = $ref->{origin} if $field eq 'O';
1020 $tested = $ref->{subject} if $field eq 'S';
1023 $old = $swop = $ref->{to} if $tfield eq 'T';
1024 $old = $swop = $ref->{from} if $tfield eq 'F';
1025 $old = $swop = $ref->{origin} if $tfield eq 'O';
1026 $old = $swop = $ref->{subject} if $tfield eq 'S';
1028 if ($tested =~ m{$pattern}i) {
1029 if ($tested eq $swop) {
1030 $swop =~ s{$pattern}{$topattern}i;
1034 Log('msg', "Msg $ref->{msgno}: $tfield $old -> $swop");
1035 Log('dbg', "Msg $ref->{msgno}: $tfield $old -> $swop");
1036 $ref->{to} = $swop if $tfield eq 'T';
1037 $ref->{from} = $swop if $tfield eq 'F';
1038 $ref->{origin} = $swop if $tfield eq 'O';
1039 $ref->{subject} = $swop if $tfield eq 'S';
1046 # import any msgs in the import directory
1047 # the messages are in BBS format (but may have cluster extentions
1048 # so SB UK < GB7TLH is legal
1051 # are there any to do in this directory?
1052 return unless -d $importfn;
1053 unless (opendir(DIR, $importfn)) {
1054 dbg('msg', "can\'t open $importfn $!");
1055 Log('msg', "can\'t open $importfn $!");
1059 my @names = readdir(DIR);
1062 foreach $name (@names) {
1063 next if $name =~ /^\./;
1064 my $splitit = $name =~ /^split/;
1065 my $fn = "$importfn/$name";
1067 unless (open(MSG, $fn)) {
1068 dbg('msg', "can\'t open import file $fn $!");
1069 Log('msg', "can\'t open import file $fn $!");
1073 my @msg = map { chomp; $_ } <MSG>;
1076 my @out = import_one($DXProt::me, \@msg, $splitit);
1081 # import one message as a list in bbs (as extended) mode
1082 # takes a reference to an array containing the whole message
1087 my $splitit = shift;
1091 my $from = $dxchan->call;
1092 my $origin = $main::mycall;
1097 my $line = shift @$ref;
1098 my @f = split /\s+/, $line;
1099 unless (@f && $f[0] =~ /^(:?S|SP|SB|SEND)$/ ) {
1100 my $m = "invalid first line in import '$line'";
1105 my $f = uc shift @f;
1106 next if $f eq 'SEND';
1108 # private / noprivate / rr
1109 if ($notincalls && ($f eq 'B' || $f eq 'SB' || $f =~ /^NOP/oi)) {
1111 } elsif ($notincalls && ($f eq 'P' || $f eq 'SP' || $f =~ /^PRI/oi)) {
1113 } elsif ($notincalls && ($f eq 'RR')) {
1115 } elsif ($f eq '@' && @f) { # this is bbs syntax, for origin
1116 $origin = uc shift @f;
1117 } elsif ($f eq '<' && @f) { # this is bbs syntax for from call
1118 $from = uc shift @f;
1119 } elsif ($f =~ /^\$/) { # this is bbs syntax for a bid
1121 } elsif ($f =~ /^<\S+/) { # this is bbs syntax for from call
1122 ($from) = $f =~ /^<(\S+)$/;
1123 } elsif ($f =~ /^\@\S+/) { # this is bbs syntax for origin
1124 ($origin) = $f =~ /^\@(\S+)$/;
1130 # is this callsign a distro?
1131 my $fn = "$msgdir/distro/$f.pl";
1133 my $fh = new IO::File $fn;
1140 return (1, "Error in Distro $f.pl:", $@) if $@;
1148 if (grep $_ eq $f, @DXMsg::badmsg) {
1149 push @out, $dxchan->msg('m3', $f);
1156 # subject is the next line
1157 my $subject = shift @$ref;
1159 # strip off trailing lines
1160 pop @$ref while (@$ref && $$ref[-1] =~ /^\s*$/);
1162 # strip off /EX or /ABORT
1163 return ("aborted") if @$ref && $$ref[-1] =~ m{^/ABORT$}i;
1164 pop @$ref if (@$ref && $$ref[-1] =~ m{^/EX$}i);
1166 # sort out any splitting that needs to be done
1172 if ($lth >= $maxchunk || ($lth > $minchunk && /^\s*$/)) {
1173 push @chunk, $lines;
1180 push @chunk, $lines if @$lines;
1185 # write all the messages away
1187 for ( $i = 0; $i < @chunk; $i++) {
1188 my $chunk = $chunk[$i];
1191 my $num = " [" . ($i+1) . "/" . scalar @chunk . "]";
1192 $ch_subject = substr($subject, 0, 27 - length $num) . $num;
1194 $ch_subject = $subject;
1198 my $systime = $main::systime;
1199 my $mycall = $main::mycall;
1200 my $mref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
1209 $mref->swop_it($main::mycall);
1210 $mref->store($chunk);
1212 push @out, $dxchan->msg('m11', $mref->{msgno}, $to);
1213 #push @out, "msgno $ref->{msgno} sent to $to";
1214 my $todxchan = DXChannel->get(uc $to);
1216 if ($todxchan->is_user()) {
1217 $todxchan->send($todxchan->msg('m9'));
1229 my $name = $AUTOLOAD;
1230 return if $name =~ /::DESTROY$/;
1233 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
1234 @_ ? $self->{$name} = shift : $self->{$name} ;