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 @swop $swopfn $badmsgfn $forwardfn @forward $timeout $waittime
35 $queueinterval $lastq $importfn $minchunk $maxchunk);
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
44 @badmsg = (); # bad message table
45 @swop = (); # swop table
46 $timeout = 30*60; # forwarding timeout
47 $waittime = 30*60; # time an aborted outgoing message waits before trying again
48 $queueinterval = 1*60; # run the queue every 1 minute
51 $minchunk = 4800; # minimum chunk size for a split message
52 $maxchunk = 6000; # maximum chunk size
54 $badmsgfn = "$msgdir/badmsg.pl"; # list of TO address we wont store
55 $forwardfn = "$msgdir/forward.pl"; # the forwarding table
56 $swopfn = "$msgdir/swop.pl"; # the swopping table
57 $importfn = "$msgdir/import"; # import directory
61 fromnode => '5,From Node',
62 tonode => '5,To Node',
65 t => '0,Msg Time,cldatetime',
66 private => '5,Private',
67 subject => '0,Subject',
68 linesreq => '0,Lines per Gob',
69 rrreq => '5,Read Confirm',
72 stream => '9,Stream No',
73 count => '5,Gob Linecnt',
74 file => '5,File?,yesno',
75 gotit => '5,Got it Nodes,parray',
76 lines => '5,Lines,parray',
77 'read' => '5,Times read',
80 keep => '0,Keep this?,yesno',
81 lastt => '5,Last processed,cldatetime',
82 waitt => '5,Wait until,cldatetime',
92 # allocate a new object
93 # called fromnode, tonode, from, to, datetime, private?, subject, nolinesper
97 my $self = bless {}, $pkg;
98 $self->{msgno} = shift;
101 $self->{to} = ($to eq $main::mycall) ? $main::myalias : $to;
104 $self->{from} = uc $from;
106 $self->{private} = shift;
107 $self->{subject} = shift;
108 $self->{origin} = shift;
109 $self->{'read'} = shift;
110 $self->{rrreq} = shift;
112 $self->{lastt} = $main::systime;
120 delete $ref->{lines};
121 delete $ref->{linesreq};
122 delete $ref->{tonode};
123 delete $ref->{fromnode};
124 delete $ref->{stream};
125 delete $ref->{lines};
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 # wander down the work queue stopping any messages that have timed out
145 if (exists $ref->{lastt} && $main::systime >= $ref->{lastt} + $timeout) {
146 dbg('msg', "Timeout, stopping msgno: $ref->{msgno} -> $node");
147 $ref->stop_msg($node);
149 # delay any outgoing messages that fail
150 $ref->{waitt} = $main::systime + $waittime + rand(120) if $node ne $main::mycall;
154 # queue some message if the interval timer has gone off
157 # import any messages in the import directory
160 $lastq = $main::systime;
163 # clean the message queue
164 clean_old() if $main::systime - $last_clean > 3600 ;
168 my @f = split /\^/, $line;
169 my ($pcno) = $f[0] =~ /^PC(\d\d)/; # just get the number
172 if ($pcno == 28) { # incoming message
174 # first look for any messages in the busy queue
175 # and cancel them this should both resolve timed out incoming messages
176 # and crossing of message between nodes, incoming messages have priority
177 if (exists $busy{$f[2]}) {
178 my $ref = $busy{$f[2]};
179 my $tonode = $ref->{tonode};
180 dbg('msg', "Busy, stopping msgno: $ref->{msgno} -> $f[2]");
181 $ref->stop_msg($self->call);
184 my $t = cltounix($f[5], $f[6]);
185 my $stream = next_transno($f[2]);
186 $f[13] = $self->call unless $f[13] && $f[13] gt ' ';
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 Log('msg', "Incoming message $f[4] to $f[3] '$f[8]'" );
198 $work{"$f[2]$stream"} = $ref; # store in work
199 $busy{$f[2]} = $ref; # set interlock
200 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
201 $ref->{lastt} = $main::systime;
203 # look to see whether this is a non private message sent to a known callsign
204 my $uref = DXUser->get_current($ref->{to});
205 if (iscallsign($ref->{to}) && !$ref->{private} && $uref && $uref->homenode) {
207 dbg('msg', "set bull to $ref->{to} to private");
212 if ($pcno == 29) { # incoming text
213 my $ref = $work{"$f[2]$f[3]"};
215 push @{$ref->{lines}}, $f[4];
217 if ($ref->{count} >= $ref->{linesreq}) {
218 $self->send(DXProt::pc31($f[2], $f[1], $f[3]));
219 dbg('msg', "stream $f[3]: $ref->{count} lines received\n");
222 $ref->{lastt} = $main::systime;
224 dbg('msg', "PC29 from unknown stream $f[3] from $f[2]" );
225 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
230 if ($pcno == 30) { # this is a incoming subject ack
231 my $ref = $work{$f[2]}; # note no stream at this stage
234 $ref->{stream} = $f[3];
236 $ref->{linesreq} = 5;
237 $work{"$f[2]$f[3]"} = $ref; # new ref
238 dbg('msg', "incoming subject ack stream $f[3]\n");
239 $busy{$f[2]} = $ref; # interlock
241 push @{$ref->{lines}}, ($ref->read_msg_body);
242 $ref->send_tranche($self);
243 $ref->{lastt} = $main::systime;
245 dbg('msg', "PC30 from unknown stream $f[3] from $f[2]" );
246 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
251 if ($pcno == 31) { # acknowledge a tranche of lines
252 my $ref = $work{"$f[2]$f[3]"};
254 dbg('msg', "tranche ack stream $f[3]\n");
255 $ref->send_tranche($self);
256 $ref->{lastt} = $main::systime;
258 dbg('msg', "PC31 from unknown stream $f[3] from $f[2]" );
259 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
264 if ($pcno == 32) { # incoming EOM
265 dbg('msg', "stream $f[3]: EOM received\n");
266 my $ref = $work{"$f[2]$f[3]"};
268 $self->send(DXProt::pc33($f[2], $f[1], $f[3])); # acknowledge it
270 # get the next msg no - note that this has NOTHING to do with the stream number in PC protocol
271 # store the file or message
272 # remove extraneous rubbish from the hash
273 # remove it from the work in progress vector
274 # stuff it on the msg queue
277 $ref->store($ref->{lines});
280 # does an identical message already exist?
283 if ($ref->{subject} eq $m->{subject} && $ref->{t} == $m->{t} && $ref->{from} eq $m->{from} && $ref->{to} eq $m->{to}) {
284 $ref->stop_msg($self->call);
285 my $msgno = $m->{msgno};
286 dbg('msg', "duplicate message from $ref->{from} -> $ref->{to} to $msgno");
287 Log('msg', "duplicate message from $ref->{from} -> $ref->{to} to $msgno");
293 $ref->swop_it($self->call);
295 # look for 'bad' to addresses
296 # if (grep $ref->{to} eq $_, @badmsg) {
297 if ($ref->dump_it($self->call)) {
298 $ref->stop_msg($self->call);
299 dbg('msg', "'Bad' message $ref->{to}");
300 Log('msg', "'Bad' message $ref->{to}");
304 $ref->{msgno} = next_transno("Msgno");
305 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
306 $ref->store($ref->{lines});
308 my $dxchan = DXChannel->get($ref->{to});
309 $dxchan->send($dxchan->msg('m9')) if $dxchan && $dxchan->is_user;
310 Log('msg', "Message $ref->{msgno} from $ref->{from} received from $f[2] for $ref->{to}");
313 $ref->stop_msg($self->call);
315 dbg('msg', "PC32 from unknown stream $f[3] from $f[2]" );
316 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
322 if ($pcno == 33) { # acknowledge the end of message
323 my $ref = $work{"$f[2]$f[3]"};
325 if ($ref->{private}) { # remove it if it private and gone off site#
326 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2] and deleted");
329 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2]");
330 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
331 $ref->store($ref->{lines}); # re- store the file
333 $ref->stop_msg($self->call);
335 dbg('msg', "PC33 from unknown stream $f[3] from $f[2]" );
336 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
339 # send next one if present
344 if ($pcno == 40) { # this is a file request
345 $f[3] =~ s/\\/\//og; # change the slashes
346 $f[3] =~ s/\.//og; # remove dots
347 $f[3] =~ s/^\///o; # remove the leading /
348 $f[3] = lc $f[3]; # to lower case;
349 dbg('msg', "incoming file $f[3]\n");
350 $f[3] = 'packclus/' . $f[3] unless $f[3] =~ /^packclus\//o;
352 # create any directories
353 my @part = split /\//, $f[3];
355 my $fn = "$main::root";
356 pop @part; # remove last part
357 foreach $part (@part) {
360 last SWITCH if !mkdir $fn, 0777;
361 dbg('msg', "created directory $fn\n");
363 my $stream = next_transno($f[2]);
364 my $ref = DXMsg->alloc($stream, "$main::root/$f[3]", $self->call, time, !$f[4], $f[3], ' ', '0', '0');
366 # forwarding variables
367 $ref->{fromnode} = $f[1];
368 $ref->{tonode} = $f[2];
369 $ref->{linesreq} = $f[5];
370 $ref->{stream} = $stream;
371 $ref->{count} = 0; # no of lines between PC31s
373 $ref->{lastt} = $main::systime;
374 $work{"$f[2]$stream"} = $ref; # store in work
375 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
380 if ($pcno == 42) { # abort transfer
381 dbg('msg', "stream $f[3]: abort received\n");
382 my $ref = $work{"$f[2]$f[3]"};
384 $ref->stop_msg($self->call);
390 if ($pcno == 49) { # global delete on subject
392 if ($_->{from} eq $f[1] && $_->{subject} eq $f[2]) {
394 Log('msg', "Message $_->{msgno} from $_->{from} ($_->{subject}) fully deleted");
395 DXProt::broadcast_ak1a($line, $self);
403 # store a message away on disc or whatever
405 # NOTE the second arg is a REFERENCE not a list
411 # we only proceed if there are actually any lines in the file
412 # if (!$lines || @{$lines} == 0) {
416 if ($ref->{file}) { # a file
417 dbg('msg', "To be stored in $ref->{to}\n");
419 my $fh = new IO::File "$ref->{to}", "w";
422 foreach $line (@{$lines}) {
426 dbg('msg', "file $ref->{to} stored\n");
427 Log('msg', "file $ref->{to} from $ref->{from} stored" );
429 confess "can't open file $ref->{to} $!";
431 } else { # a normal message
433 # attempt to open the message file
434 my $fn = filename($ref->{msgno});
436 dbg('msg', "To be stored in $fn\n");
438 # now save the file, overwriting what's there, YES I KNOW OK! (I will change it if it's a problem)
439 my $fh = new IO::File "$fn", "w";
441 my $rr = $ref->{rrreq} ? '1' : '0';
442 my $priv = $ref->{private} ? '1': '0';
443 print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$priv^$ref->{subject}^$ref->{origin}^$ref->{'read'}^$rr\n";
444 print $fh "=== ", join('^', @{$ref->{gotit}}), "\n";
447 foreach $line (@{$lines}) {
448 $ref->{size} += (length $line) + 1;
452 dbg('msg', "msg $ref->{msgno} stored\n");
453 Log('msg', "msg $ref->{msgno} from $ref->{from} to $ref->{to} stored" );
455 confess "can't open msg file $fn $!";
465 # remove it from the active message list
466 @msg = map { $_ != $self ? $_ : () } @msg;
468 # belt and braces (one day I will ask someone if this is REALLY necessary)
469 delete $self->{gotit};
470 delete $self->{list};
473 unlink filename($self->{msgno});
474 dbg('msg', "deleting $self->{msgno}\n");
477 # clean out old messages from the message queue
482 # mark old messages for deletion
483 foreach $ref (@msg) {
484 if (!$ref->{keep} && $ref->{t} < $main::systime - $maxage) {
485 $ref->{deleteme} = 1;
486 delete $ref->{gotit};
488 unlink filename($ref->{msgno});
489 dbg('msg', "deleting old $ref->{msgno}\n");
493 # remove them all from the active message list
494 @msg = map { $_->{deleteme} ? () : $_ } @msg;
495 $last_clean = $main::systime;
498 # read in a message header
508 $file = new IO::File "$fn";
510 dbg('err', "Error reading $fn $!");
511 Log('err', "Error reading $fn $!");
515 $line = <$file>; # first line
516 if ($size == 0 || !$line) {
517 dbg('err', "Empty $fn $!");
518 Log('err', "Empty $fn $!");
522 $size -= length $line;
523 if (! $line =~ /^===/o) {
524 dbg('err', "corrupt first line in $fn ($line)");
525 Log('err', "corrupt first line in $fn ($line)");
529 @f = split /\^/, $line;
530 $ref = DXMsg->alloc(@f);
532 $line = <$file>; # second line
534 $size -= length $line;
535 if (! $line =~ /^===/o) {
536 dbg('err', "corrupt second line in $fn ($line)");
537 Log('err', "corrupt second line in $fn ($line)");
542 @f = split /\^/, $line;
543 push @{$ref->{gotit}}, @f;
544 $ref->{size} = $size;
551 # read in a message header
555 my $msgno = $self->{msgno};
558 my $fn = filename($msgno);
561 $file = new IO::File;
562 if (!open($file, $fn)) {
563 dbg('err' ,"Error reading $fn $!");
564 Log('err' ,"Error reading $fn $!");
567 @out = map {chomp; $_} <$file>;
570 shift @out if $out[0] =~ /^=== /;
571 shift @out if $out[0] =~ /^=== /;
575 # send a tranche of lines to the other end
578 my ($self, $dxchan) = @_;
580 my $to = $self->{tonode};
581 my $from = $self->{fromnode};
582 my $stream = $self->{stream};
583 my $lines = $self->{lines};
586 for ($i = 0, $c = $self->{count}; $i < $self->{linesreq} && $c < @$lines; $i++, $c++) {
587 push @out, DXProt::pc29($to, $from, $stream, $lines->[$c]);
591 push @out, DXProt::pc32($to, $from, $stream) if $i < $self->{linesreq};
596 # find a message to send out and start the ball rolling
603 my @nodelist = DXChannel::get_all_ak1a();
605 # bat down the message list looking for one that needs to go off site and whose
606 # nearest node is not busy.
608 dbg('msg', "queue msg ($sort)\n");
609 foreach $ref (@msg) {
610 # firstly, is it private and unread? if so can I find the recipient
611 # in my cluster node list offsite?
613 # ignore 'delayed' messages until their waiting time has expired
614 if (exists $ref->{waitt}) {
615 next if $ref->{waitt} > $main::systime;
616 delete $ref->{waitt};
619 # deal with routed private messages
621 if ($ref->{private}) {
622 next if $ref->{'read'}; # if it is read, it is stuck here
623 $clref = DXCluster->get_exact($ref->{to});
624 unless ($clref) { # otherwise look for a homenode
625 my $uref = DXUser->get_current($ref->{to});
626 my $hnode = $uref->homenode if $uref;
627 $clref = DXCluster->get_exact($hnode) if $hnode;
629 if ($clref && !grep { $clref->{dxchan} == $_ } DXCommandmode::get_all()) {
630 next if $clref->call eq $main::mycall; # i.e. it lives here
631 $noderef = $clref->{dxchan};
632 $ref->start_msg($noderef) if !get_busy($noderef->call) && $noderef->state eq 'normal';
636 # otherwise we are dealing with a bulletin or forwarded private message
637 # compare the gotit list with
638 # the nodelist up above, if there are sites that haven't got it yet
639 # then start sending it - what happens when we get loops is anyone's
640 # guess, use (to, from, time, subject) tuple?
641 foreach $noderef (@nodelist) {
642 next if $noderef->call eq $main::mycall;
643 next if grep { $_ eq $noderef->call } @{$ref->{gotit}};
644 next unless $ref->forward_it($noderef->call); # check the forwarding file
646 # if we are here we have a node that doesn't have this message
647 $ref->start_msg($noderef) if !get_busy($noderef->call) && $noderef->state eq 'normal';
651 # if all the available nodes are busy then stop
652 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
656 # is there a message for me?
662 foreach $ref (@msg) {
663 # is it for me, private and unread?
664 if ($ref->{to} eq $call && $ref->{private}) {
665 return 1 if !$ref->{'read'};
671 # start the message off on its travels with a PC28
674 my ($self, $dxchan) = @_;
676 dbg('msg', "start msg $self->{msgno}\n");
677 $self->{linesreq} = 5;
679 $self->{tonode} = $dxchan->call;
680 $self->{fromnode} = $main::mycall;
681 $busy{$self->{tonode}} = $self;
682 $work{$self->{tonode}} = $self;
683 $self->{lastt} = $main::systime;
684 $dxchan->send(DXProt::pc28($self->{tonode}, $self->{fromnode}, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $self->{origin}, $self->{rrreq}));
687 # get the ref of a busy node
700 # get the forwarding queue
706 # stop a message from continuing, clean it out, unlock interlocks etc
711 my $stream = $self->{stream} if exists $self->{stream};
714 dbg('msg', "stop msg $self->{msgno} -> node $node\n");
716 delete $work{"$node$stream"} if $stream;
721 # get a new transaction number from the file specified
725 $name =~ s/\W//og; # remove non-word characters
726 my $fn = "$msgdir/$name";
729 my $fh = new IO::File;
730 if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
732 $msgno = $fh->getline;
736 $fh->print("$msgno\n");
737 dbg('msg', "msgno $msgno allocated for $name\n");
740 confess "can't open $fn $!";
745 # initialise the message 'system', read in all the message headers
748 my $dir = new IO::File;
752 # load various control files
753 dbg('err', "load badmsg: " . (load_badmsg() or "Ok"));
754 dbg('err', "load forward: " . (load_forward() or "Ok"));
755 dbg('err', "load swop: " . (load_swop() or "Ok"));
757 # read in the directory
758 opendir($dir, $msgdir) or confess "can't open $msgdir $!";
759 @dir = readdir($dir);
764 next unless /^m\d\d\d\d\d\d$/;
766 $ref = read_msg_header("$msgdir/$_");
768 dbg('err', "Deleting $_");
769 Log('err', "Deleting $_");
774 # delete any messages to 'badmsg.pl' places
775 if (grep $ref->{to} eq $_, @badmsg) {
776 dbg('msg', "'Bad' TO address $ref->{to}");
777 Log('msg', "'Bad' TO address $ref->{to}");
782 # add the message to the available queue
787 # add the message to the directory listing
791 confess "tried to add a non-ref to the msg directory" if !ref $ref;
795 # return all the current messages
801 # get a particular message
806 return $_ if $_->{msgno} == $msgno;
807 last if $_->{msgno} > $msgno;
812 # return the official filename for a message no
815 return sprintf "$msgdir/m%06d", shift;
819 # return a list of valid elements
828 # return a prompt for a field
833 my ($self, $ele) = @_;
838 # send a message state machine
845 if ($self->state eq 'send1') {
847 confess "local var gone missing" if !ref $self->{loc};
848 my $loc = $self->{loc};
849 $loc->{subject} = $line;
851 $self->state('sendbody');
852 #push @out, $self->msg('sendbody');
853 push @out, $self->msg('m8');
854 } elsif ($self->state eq 'sendbody') {
855 confess "local var gone missing" if !ref $self->{loc};
856 my $loc = $self->{loc};
857 if ($line eq "\032" || $line eq '%1A' || uc $line eq "/EX") {
860 foreach $to (@{$loc->{to}}) {
862 my $systime = $main::systime;
863 my $mycall = $main::mycall;
864 $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
866 exists $loc->{from} ? $loc->{from} : $self->call,
870 exists $loc->{origin} ? $loc->{origin} : $mycall,
873 $ref->swop_it($self->call);
874 $ref->store($loc->{lines});
876 push @out, $self->msg('m11', $ref->{msgno}, $to);
877 #push @out, "msgno $ref->{msgno} sent to $to";
878 my $dxchan = DXChannel->get(uc $to);
880 if ($dxchan->is_user()) {
881 $dxchan->send($dxchan->msg('m9'));
886 delete $loc->{lines};
891 $self->state('prompt');
892 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
893 #push @out, $self->msg('sendabort');
894 push @out, $self->msg('m10');
895 delete $loc->{lines};
899 $self->state('prompt');
902 # i.e. it ain't and end or abort, therefore store the line
903 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
909 # return the standard directory line for this ref
913 return sprintf "%6d%s%s%5d %8.8s %8.8s %-6.6s %5.5s %-30.30s",
914 $ref->msgno, $ref->read ? '-' : ' ', $ref->private ? 'p' : ' ', $ref->size,
915 $ref->to, $ref->from, cldate($ref->t), ztime($ref->t), $ref->subject;
918 # load the forward table
922 my $s = readfilestr($forwardfn);
930 # load the bad message table
934 my $s = readfilestr($badmsgfn);
942 # load the swop message table
946 my $s = readfilestr($swopfn);
955 # forward that message or not according to the forwarding table
956 # returns 1 for forward, 0 - to ignore
965 for ($i = 0; $i < @forward; $i += 5) {
966 my ($sort, $field, $pattern, $action, $bbs) = @forward[$i..($i+4)];
970 next if $ref->{private} && $sort ne 'P';
971 next if !$ref->{private} && $sort ne 'B';
974 $tested = $ref->{to} if $field eq 'T';
975 $tested = $ref->{from} if $field eq 'F';
976 $tested = $ref->{origin} if $field eq 'O';
977 $tested = $ref->{subject} if $field eq 'S';
979 if (!$pattern || $tested =~ m{$pattern}i) {
980 return 0 if $action eq 'I';
981 return 1 if !$bbs || grep $_ eq $call, @{$bbs};
993 for ($i = 0; $i < @badmsg; $i += 3) {
994 my ($sort, $field, $pattern) = @badmsg[$i..($i+2)];
998 next if $ref->{private} && $sort ne 'P';
999 next if !$ref->{private} && $sort ne 'B';
1002 $tested = $ref->{to} if $field eq 'T';
1003 $tested = $ref->{from} if $field eq 'F';
1004 $tested = $ref->{origin} if $field eq 'O';
1005 $tested = $ref->{subject} if $field eq 'S';
1007 if (!$pattern || $tested =~ m{$pattern}i) {
1021 for ($i = 0; $i < @swop; $i += 5) {
1022 my ($sort, $field, $pattern, $tfield, $topattern) = @swop[$i..($i+4)];
1027 # are we interested?
1028 next if $ref->{private} && $sort ne 'P';
1029 next if !$ref->{private} && $sort ne 'B';
1032 $tested = $ref->{to} if $field eq 'T';
1033 $tested = $ref->{from} if $field eq 'F';
1034 $tested = $ref->{origin} if $field eq 'O';
1035 $tested = $ref->{subject} if $field eq 'S';
1038 $old = $swop = $ref->{to} if $tfield eq 'T';
1039 $old = $swop = $ref->{from} if $tfield eq 'F';
1040 $old = $swop = $ref->{origin} if $tfield eq 'O';
1041 $old = $swop = $ref->{subject} if $tfield eq 'S';
1043 if ($tested =~ m{$pattern}i) {
1044 if ($tested eq $swop) {
1045 $swop =~ s{$pattern}{$topattern}i;
1049 Log('msg', "Msg $ref->{msgno}: $tfield $old -> $swop");
1050 Log('dbg', "Msg $ref->{msgno}: $tfield $old -> $swop");
1051 $ref->{to} = $swop if $tfield eq 'T';
1052 $ref->{from} = $swop if $tfield eq 'F';
1053 $ref->{origin} = $swop if $tfield eq 'O';
1054 $ref->{subject} = $swop if $tfield eq 'S';
1061 # import any msgs in the import directory
1062 # the messages are in BBS format (but may have cluster extentions
1063 # so SB UK < GB7TLH is legal
1066 # are there any to do in this directory?
1067 return unless -d $importfn;
1068 unless (opendir(DIR, $importfn)) {
1069 dbg('msg', "can\'t open $importfn $!");
1070 Log('msg', "can\'t open $importfn $!");
1074 my @names = readdir(DIR);
1077 foreach $name (@names) {
1078 next if $name =~ /^\./;
1079 my $splitit = $name =~ /^split/;
1080 my $fn = "$importfn/$name";
1082 unless (open(MSG, $fn)) {
1083 dbg('msg', "can\'t open import file $fn $!");
1084 Log('msg', "can\'t open import file $fn $!");
1088 my @msg = map { chomp; $_ } <MSG>;
1091 my @out = import_one($DXProt::me, \@msg, $splitit);
1096 # import one message as a list in bbs (as extended) mode
1097 # takes a reference to an array containing the whole message
1102 my $splitit = shift;
1106 my $from = $dxchan->call;
1107 my $origin = $main::mycall;
1112 my $line = shift @$ref;
1113 my @f = split /\s+/, $line;
1114 unless (@f && $f[0] =~ /^(:?S|SP|SB|SEND)$/ ) {
1115 my $m = "invalid first line in import '$line'";
1120 my $f = uc shift @f;
1121 next if $f eq 'SEND';
1123 # private / noprivate / rr
1124 if ($notincalls && ($f eq 'B' || $f eq 'SB' || $f =~ /^NOP/oi)) {
1126 } elsif ($notincalls && ($f eq 'P' || $f eq 'SP' || $f =~ /^PRI/oi)) {
1128 } elsif ($notincalls && ($f eq 'RR')) {
1130 } elsif ($f eq '@' && @f) { # this is bbs syntax, for origin
1131 $origin = uc shift @f;
1132 } elsif ($f eq '<' && @f) { # this is bbs syntax for from call
1133 $from = uc shift @f;
1134 } elsif ($f =~ /^\$/) { # this is bbs syntax for a bid
1136 } elsif ($f =~ /^<\S+/) { # this is bbs syntax for from call
1137 ($from) = $f =~ /^<(\S+)$/;
1138 } elsif ($f =~ /^\@\S+/) { # this is bbs syntax for origin
1139 ($origin) = $f =~ /^\@(\S+)$/;
1145 # is this callsign a distro?
1146 my $fn = "$msgdir/distro/$f.pl";
1148 my $fh = new IO::File $fn;
1155 return (1, "Error in Distro $f.pl:", $@) if $@;
1163 if (grep $_ eq $f, @DXMsg::badmsg) {
1164 push @out, $dxchan->msg('m3', $f);
1171 # subject is the next line
1172 my $subject = shift @$ref;
1174 # strip off trailing lines
1175 pop @$ref while (@$ref && $$ref[-1] =~ /^\s*$/);
1177 # strip off /EX or /ABORT
1178 return ("aborted") if @$ref && $$ref[-1] =~ m{^/ABORT$}i;
1179 pop @$ref if (@$ref && $$ref[-1] =~ m{^/EX$}i);
1181 # sort out any splitting that needs to be done
1187 if ($lth >= $maxchunk || ($lth > $minchunk && /^\s*$/)) {
1188 push @chunk, $lines;
1195 push @chunk, $lines if @$lines;
1200 # write all the messages away
1202 for ( $i = 0; $i < @chunk; $i++) {
1203 my $chunk = $chunk[$i];
1206 my $num = " [" . ($i+1) . "/" . scalar @chunk . "]";
1207 $ch_subject = substr($subject, 0, 27 - length $num) . $num;
1209 $ch_subject = $subject;
1213 my $systime = $main::systime;
1214 my $mycall = $main::mycall;
1215 my $mref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
1224 $mref->swop_it($main::mycall);
1225 $mref->store($chunk);
1227 push @out, $dxchan->msg('m11', $mref->{msgno}, $to);
1228 #push @out, "msgno $ref->{msgno} sent to $to";
1229 my $todxchan = DXChannel->get(uc $to);
1231 if ($todxchan->is_user()) {
1232 $todxchan->send($todxchan->msg('m9'));
1244 my $name = $AUTOLOAD;
1245 return if $name =~ /::DESTROY$/;
1248 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
1249 # this clever line of code creates a subroutine which takes over from autoload
1250 # from OO Perl - Conway
1251 *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
1252 @_ ? $self->{$name} = shift : $self->{$name} ;