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)
30 use vars qw(%work @msg $msgdir %valid %busy $maxage $last_clean
31 @badmsg @swop $swopfn $badmsgfn $forwardfn @forward $timeout $waittime
32 $queueinterval $lastq $importfn $minchunk $maxchunk $bulltopriv);
34 %work = (); # outstanding jobs
35 @msg = (); # messages we have
36 %busy = (); # station interlocks
37 $msgdir = "$main::root/msg"; # directory contain the msgs
38 $maxage = 30 * 86400; # the maximum age that a message shall live for if not marked
39 $last_clean = 0; # last time we did a clean
40 @forward = (); # msg forward table
41 @badmsg = (); # bad message table
42 @swop = (); # swop table
43 $timeout = 30*60; # forwarding timeout
44 $waittime = 30*60; # time an aborted outgoing message waits before trying again
45 $queueinterval = 1*60; # run the queue every 1 minute
48 $minchunk = 4800; # minimum chunk size for a split message
49 $maxchunk = 6000; # maximum chunk size
50 $bulltopriv = 1; # convert msgs with callsigns to private if they are bulls
53 $badmsgfn = "$msgdir/badmsg.pl"; # list of TO address we wont store
54 $forwardfn = "$msgdir/forward.pl"; # the forwarding table
55 $swopfn = "$msgdir/swop.pl"; # the swopping table
56 $importfn = "$msgdir/import"; # import directory
60 fromnode => '5,From Node',
61 tonode => '5,To Node',
64 t => '0,Msg Time,cldatetime',
65 private => '5,Private',
66 subject => '0,Subject',
67 linesreq => '0,Lines per Gob',
68 rrreq => '5,Read Confirm',
71 stream => '9,Stream No',
72 count => '5,Gob Linecnt',
73 file => '5,File?,yesno',
74 gotit => '5,Got it Nodes,parray',
75 lines => '5,Lines,parray',
76 'read' => '5,Times read',
79 keep => '0,Keep this?,yesno',
80 lastt => '5,Last processed,cldatetime',
81 waitt => '5,Wait until,cldatetime',
84 # allocate a new object
85 # called fromnode, tonode, from, to, datetime, private?, subject, nolinesper
89 my $self = bless {}, $pkg;
90 $self->{msgno} = shift;
93 $self->{to} = ($to eq $main::mycall) ? $main::myalias : $to;
96 $self->{from} = uc $from;
98 $self->{private} = shift;
99 $self->{subject} = shift;
100 $self->{origin} = shift;
101 $self->{'read'} = shift;
102 $self->{rrreq} = shift;
104 # $self->{lastt} = $main::systime;
106 $self->{private} = 1 if $bulltopriv && DXUser->get_current($self->{to});
114 delete $ref->{lines};
115 delete $ref->{linesreq};
116 delete $ref->{tonode};
117 delete $ref->{fromnode};
118 delete $ref->{stream};
120 delete $ref->{count};
121 delete $ref->{lastt} if exists $ref->{lastt};
122 delete $ref->{waitt} if exists $ref->{waitt};
127 my ($self, $line) = @_;
129 # this is periodic processing
130 if (!$self || !$line) {
132 if ($main::systime >= $lastq + $queueinterval) {
134 # queue some message if the interval timer has gone off
137 # import any messages in the import directory
140 $lastq = $main::systime;
143 # clean the message queue
144 clean_old() if $main::systime - $last_clean > 3600 ;
148 my @f = split /\^/, $line;
149 my ($pcno) = $f[0] =~ /^PC(\d\d)/; # just get the number
152 if ($pcno == 28) { # incoming message
154 # first look for any messages in the busy queue
155 # and cancel them this should both resolve timed out incoming messages
156 # and crossing of message between nodes, incoming messages have priority
157 if (exists $busy{$f[2]}) {
158 my $ref = $busy{$f[2]};
159 my $tonode = $ref->{tonode};
160 dbg('msg', "Busy, stopping msgno: $ref->{msgno} -> $f[2]");
161 $ref->stop_msg($self->call);
164 my $t = cltounix($f[5], $f[6]);
165 my $stream = next_transno($f[2]);
166 $f[13] = $self->call unless $f[13] && $f[13] gt ' ';
167 my $ref = DXMsg->alloc($stream, uc $f[3], $f[4], $t, $f[7], $f[8], $f[13], '0', $f[11]);
169 # fill in various forwarding state variables
170 $ref->{fromnode} = $f[2];
171 $ref->{tonode} = $f[1];
172 $ref->{rrreq} = $f[11];
173 $ref->{linesreq} = $f[10];
174 $ref->{stream} = $stream;
175 $ref->{count} = 0; # no of lines between PC31s
176 dbg('msg', "new message from $f[4] to $f[3] '$f[8]' stream $stream\n");
177 Log('msg', "Incoming message $f[4] to $f[3] '$f[8]'" );
178 $work{"$f[2]$stream"} = $ref; # store in work
179 $busy{$f[2]} = $ref; # set interlock
180 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
181 $ref->{lastt} = $main::systime;
183 # look to see whether this is a non private message sent to a known callsign
184 my $uref = DXUser->get_current($ref->{to});
185 if (is_callsign($ref->{to}) && !$ref->{private} && $uref && $uref->homenode) {
187 dbg('msg', "set bull to $ref->{to} to private");
192 if ($pcno == 29) { # incoming text
193 my $ref = $work{"$f[2]$f[3]"};
196 push @{$ref->{lines}}, $f[4];
198 if ($ref->{count} >= $ref->{linesreq}) {
199 $self->send(DXProt::pc31($f[2], $f[1], $f[3]));
200 dbg('msg', "stream $f[3]: $ref->{count} lines received\n");
203 $ref->{lastt} = $main::systime;
205 dbg('msg', "PC29 from unknown stream $f[3] from $f[2]" );
206 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
211 if ($pcno == 30) { # this is a incoming subject ack
212 my $ref = $work{$f[2]}; # note no stream at this stage
215 $ref->{stream} = $f[3];
217 $ref->{linesreq} = 5;
218 $work{"$f[2]$f[3]"} = $ref; # new ref
219 dbg('msg', "incoming subject ack stream $f[3]\n");
220 $busy{$f[2]} = $ref; # interlock
221 push @{$ref->{lines}}, ($ref->read_msg_body);
222 $ref->send_tranche($self);
223 $ref->{lastt} = $main::systime;
225 dbg('msg', "PC30 from unknown stream $f[3] from $f[2]" );
226 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
231 if ($pcno == 31) { # acknowledge a tranche of lines
232 my $ref = $work{"$f[2]$f[3]"};
234 dbg('msg', "tranche ack stream $f[3]\n");
235 $ref->send_tranche($self);
236 $ref->{lastt} = $main::systime;
238 dbg('msg', "PC31 from unknown stream $f[3] from $f[2]" );
239 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
244 if ($pcno == 32) { # incoming EOM
245 dbg('msg', "stream $f[3]: EOM received\n");
246 my $ref = $work{"$f[2]$f[3]"};
248 $self->send(DXProt::pc33($f[2], $f[1], $f[3])); # acknowledge it
250 # get the next msg no - note that this has NOTHING to do with the stream number in PC protocol
251 # store the file or message
252 # remove extraneous rubbish from the hash
253 # remove it from the work in progress vector
254 # stuff it on the msg queue
257 $ref->store($ref->{lines});
260 # does an identical message already exist?
263 if ($ref->{subject} eq $m->{subject} && $ref->{t} == $m->{t} && $ref->{from} eq $m->{from} && $ref->{to} eq $m->{to}) {
264 $ref->stop_msg($self->call);
265 my $msgno = $m->{msgno};
266 dbg('msg', "duplicate message from $ref->{from} -> $ref->{to} to $msgno");
267 Log('msg', "duplicate message from $ref->{from} -> $ref->{to} to $msgno");
273 $ref->swop_it($self->call);
275 # look for 'bad' to addresses
277 $ref->stop_msg($self->call);
278 dbg('msg', "'Bad' message $ref->{to}");
279 Log('msg', "'Bad' message $ref->{to}");
283 $ref->{msgno} = next_transno("Msgno");
284 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
285 $ref->store($ref->{lines});
287 my $dxchan = DXChannel->get($ref->{to});
288 $dxchan->send($dxchan->msg('m9')) if $dxchan && $dxchan->is_user;
289 Log('msg', "Message $ref->{msgno} from $ref->{from} received from $f[2] for $ref->{to}");
292 $ref->stop_msg($self->call);
294 dbg('msg', "PC32 from unknown stream $f[3] from $f[2]" );
295 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
301 if ($pcno == 33) { # acknowledge the end of message
302 my $ref = $work{"$f[2]$f[3]"};
304 if ($ref->{private}) { # remove it if it private and gone off site#
305 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2] and deleted");
308 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2]");
309 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
310 $ref->store($ref->{lines}); # re- store the file
312 $ref->stop_msg($self->call);
314 dbg('msg', "PC33 from unknown stream $f[3] from $f[2]" );
315 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
318 # send next one if present
323 if ($pcno == 40) { # this is a file request
324 $f[3] =~ s/\\/\//og; # change the slashes
325 $f[3] =~ s/\.//og; # remove dots
326 $f[3] =~ s/^\///o; # remove the leading /
327 $f[3] = lc $f[3]; # to lower case;
328 dbg('msg', "incoming file $f[3]\n");
329 $f[3] = 'packclus/' . $f[3] unless $f[3] =~ /^packclus\//o;
331 # create any directories
332 my @part = split /\//, $f[3];
334 my $fn = "$main::root";
335 pop @part; # remove last part
336 foreach $part (@part) {
339 last SWITCH if !mkdir $fn, 0777;
340 dbg('msg', "created directory $fn\n");
342 my $stream = next_transno($f[2]);
343 my $ref = DXMsg->alloc($stream, "$main::root/$f[3]", $self->call, time, !$f[4], $f[3], ' ', '0', '0');
345 # forwarding variables
346 $ref->{fromnode} = $f[1];
347 $ref->{tonode} = $f[2];
348 $ref->{linesreq} = $f[5];
349 $ref->{stream} = $stream;
350 $ref->{count} = 0; # no of lines between PC31s
352 $ref->{lastt} = $main::systime;
353 $work{"$f[2]$stream"} = $ref; # store in work
354 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
359 if ($pcno == 42) { # abort transfer
360 dbg('msg', "stream $f[3]: abort received\n");
361 my $ref = $work{"$f[2]$f[3]"};
363 $ref->stop_msg($self->call);
369 if ($pcno == 49) { # global delete on subject
371 if ($_->{from} eq $f[1] && $_->{subject} eq $f[2]) {
373 Log('msg', "Message $_->{msgno} from $_->{from} ($_->{subject}) fully deleted");
374 DXProt::broadcast_ak1a($line, $self);
382 # store a message away on disc or whatever
384 # NOTE the second arg is a REFERENCE not a list
390 if ($ref->{file}) { # a file
391 dbg('msg', "To be stored in $ref->{to}\n");
393 my $fh = new IO::File "$ref->{to}", "w";
396 foreach $line (@{$lines}) {
400 dbg('msg', "file $ref->{to} stored\n");
401 Log('msg', "file $ref->{to} from $ref->{from} stored" );
403 confess "can't open file $ref->{to} $!";
405 } else { # a normal message
407 # attempt to open the message file
408 my $fn = filename($ref->{msgno});
410 dbg('msg', "To be stored in $fn\n");
412 # now save the file, overwriting what's there, YES I KNOW OK! (I will change it if it's a problem)
413 my $fh = new IO::File "$fn", "w";
415 my $rr = $ref->{rrreq} ? '1' : '0';
416 my $priv = $ref->{private} ? '1': '0';
417 print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$priv^$ref->{subject}^$ref->{origin}^$ref->{'read'}^$rr\n";
418 print $fh "=== ", join('^', @{$ref->{gotit}}), "\n";
421 foreach $line (@{$lines}) {
422 $ref->{size} += (length $line) + 1;
426 dbg('msg', "msg $ref->{msgno} stored\n");
427 Log('msg', "msg $ref->{msgno} from $ref->{from} to $ref->{to} stored" );
429 confess "can't open msg file $fn $!";
439 # remove it from the active message list
440 dbg('msg', "\@msg = " . scalar @msg . " before delete");
441 @msg = grep { $_ != $self } @msg;
444 unlink filename($self->{msgno});
445 dbg('msg', "deleting $self->{msgno}\n");
446 dbg('msg', "\@msg = " . scalar @msg . " after delete");
449 # clean out old messages from the message queue
454 # mark old messages for deletion
455 dbg('msg', "\@msg = " . scalar @msg . " before delete");
456 foreach $ref (@msg) {
457 if (ref($ref) && !$ref->{keep} && $ref->{t} < $main::systime - $maxage) {
458 $ref->{deleteme} = 1;
459 unlink filename($ref->{msgno});
460 dbg('msg', "deleting old $ref->{msgno}\n");
464 # remove them all from the active message list
465 @msg = grep { !$_->{deleteme} } @msg;
466 dbg('msg', "\@msg = " . scalar @msg . " after delete");
467 $last_clean = $main::systime;
470 # read in a message header
480 $file = new IO::File "$fn";
482 dbg('err', "Error reading $fn $!");
483 Log('err', "Error reading $fn $!");
487 $line = <$file>; # first line
488 if ($size == 0 || !$line) {
489 dbg('err', "Empty $fn $!");
490 Log('err', "Empty $fn $!");
494 $size -= length $line;
495 if (! $line =~ /^===/o) {
496 dbg('err', "corrupt first line in $fn ($line)");
497 Log('err', "corrupt first line in $fn ($line)");
501 @f = split /\^/, $line;
502 $ref = DXMsg->alloc(@f);
504 $line = <$file>; # second line
506 $size -= length $line;
507 if (! $line =~ /^===/o) {
508 dbg('err', "corrupt second line in $fn ($line)");
509 Log('err', "corrupt second line in $fn ($line)");
514 @f = split /\^/, $line;
515 push @{$ref->{gotit}}, @f;
516 $ref->{size} = $size;
523 # read in a message header
527 my $msgno = $self->{msgno};
530 my $fn = filename($msgno);
533 $file = new IO::File;
534 if (!open($file, $fn)) {
535 dbg('err' ,"Error reading $fn $!");
536 Log('err' ,"Error reading $fn $!");
539 @out = map {chomp; $_} <$file>;
542 shift @out if $out[0] =~ /^=== /;
543 shift @out if $out[0] =~ /^=== /;
547 # send a tranche of lines to the other end
550 my ($self, $dxchan) = @_;
552 my $to = $self->{tonode};
553 my $from = $self->{fromnode};
554 my $stream = $self->{stream};
555 my $lines = $self->{lines};
558 for ($i = 0, $c = $self->{count}; $i < $self->{linesreq} && $c < @$lines; $i++, $c++) {
559 push @out, DXProt::pc29($to, $from, $stream, $lines->[$c]);
563 push @out, DXProt::pc32($to, $from, $stream) if $i < $self->{linesreq};
568 # find a message to send out and start the ball rolling
576 # bat down the message list looking for one that needs to go off site and whose
577 # nearest node is not busy.
579 dbg('msg', "queue msg ($sort)\n");
580 my @nodelist = DXChannel::get_all_nodes;
581 foreach $ref (@msg) {
583 # ignore 'delayed' messages until their waiting time has expired
584 if (exists $ref->{waitt}) {
585 next if $ref->{waitt} > $main::systime;
586 delete $ref->{waitt};
590 if (exists $ref->{lastt} && $main::systime >= $ref->{lastt} + $timeout) {
591 my $node = $ref->{tonode};
592 dbg('msg', "Timeout, stopping msgno: $ref->{msgno} -> $node");
593 Log('msg', "Timeout, stopping msgno: $ref->{msgno} -> $node");
594 $ref->stop_msg($node);
596 # delay any outgoing messages that fail
597 $ref->{waitt} = $main::systime + $waittime + rand(120) if $node ne $main::mycall;
598 delete $ref->{lastt};
602 # firstly, is it private and unread? if so can I find the recipient
603 # in my cluster node list offsite?
605 # deal with routed private messages
607 if ($ref->{private}) {
608 next if $ref->{'read'}; # if it is read, it is stuck here
609 $clref = Route::get($ref->{to});
610 # unless ($clref) { # otherwise look for a homenode
611 # my $uref = DXUser->get_current($ref->{to});
612 # my $hnode = $uref->homenode if $uref;
613 # $clref = Route::Node::get($hnode) if $hnode;
616 my $dxc = $clref->dxchan;
618 if (!grep $dxc == $_, DXCommandmode::get_all()) {
619 next if $clref->call eq $main::mycall; # i.e. it lives here
621 $ref->start_msg($dxchan) if $dxchan && !get_busy($dxchan->call) && $dxchan->state eq 'normal';
624 dbg('route', "Route: No dxchan for $ref->{to} " . ref($clref) );
629 # otherwise we are dealing with a bulletin or forwarded private message
630 # compare the gotit list with
631 # the nodelist up above, if there are sites that haven't got it yet
632 # then start sending it - what happens when we get loops is anyone's
633 # guess, use (to, from, time, subject) tuple?
634 foreach $dxchan (@nodelist) {
635 my $call = $dxchan->call;
637 next if $call eq $main::mycall;
638 next if ref $ref->{gotit} && grep $_ eq $call, @{$ref->{gotit}};
639 next unless $ref->forward_it($call); # check the forwarding file
641 # if we are here we have a node that doesn't have this message
642 $ref->start_msg($dxchan) if !get_busy($call) && $dxchan->state eq 'normal';
646 # if all the available nodes are busy then stop
647 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
651 # is there a message for me?
657 foreach $ref (@msg) {
658 # is it for me, private and unread?
659 if ($ref->{to} eq $call && $ref->{private}) {
660 return 1 if !$ref->{'read'};
666 # start the message off on its travels with a PC28
669 my ($self, $dxchan) = @_;
671 dbg('msg', "start msg $self->{msgno}\n");
672 $self->{linesreq} = 10;
674 $self->{tonode} = $dxchan->call;
675 $self->{fromnode} = $main::mycall;
676 $busy{$self->{tonode}} = $self;
677 $work{$self->{tonode}} = $self;
678 $self->{lastt} = $main::systime;
679 $dxchan->send(DXProt::pc28($self->{tonode}, $self->{fromnode}, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $self->{origin}, $self->{rrreq}));
682 # get the ref of a busy node
695 # get the forwarding queue
701 # stop a message from continuing, clean it out, unlock interlocks etc
706 my $stream = $self->{stream} if exists $self->{stream};
709 dbg('msg', "stop msg $self->{msgno} -> node $node\n");
711 delete $work{"$node$stream"} if $stream;
716 # get a new transaction number from the file specified
720 $name =~ s/\W//og; # remove non-word characters
721 my $fn = "$msgdir/$name";
724 my $fh = new IO::File;
725 if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
727 $msgno = $fh->getline;
731 $fh->print("$msgno\n");
732 dbg('msg', "msgno $msgno allocated for $name\n");
735 confess "can't open $fn $!";
740 # initialise the message 'system', read in all the message headers
743 my $dir = new IO::File;
747 # load various control files
748 dbg('err', "load badmsg: " . (load_badmsg() or "Ok"));
749 dbg('err', "load forward: " . (load_forward() or "Ok"));
750 dbg('err', "load swop: " . (load_swop() or "Ok"));
752 # read in the directory
753 opendir($dir, $msgdir) or confess "can't open $msgdir $!";
754 @dir = readdir($dir);
759 next unless /^m\d\d\d\d\d\d$/;
761 $ref = read_msg_header("$msgdir/$_");
763 dbg('err', "Deleting $_");
764 Log('err', "Deleting $_");
769 # delete any messages to 'badmsg.pl' places
771 dbg('msg', "'Bad' TO address $ref->{to}");
772 Log('msg', "'Bad' TO address $ref->{to}");
777 # add the message to the available queue
782 # add the message to the directory listing
786 confess "tried to add a non-ref to the msg directory" if !ref $ref;
790 # return all the current messages
796 # get a particular message
801 return $_ if $_->{msgno} == $msgno;
802 last if $_->{msgno} > $msgno;
807 # return the official filename for a message no
810 return sprintf "$msgdir/m%06d", shift;
814 # return a list of valid elements
823 # return a prompt for a field
828 my ($self, $ele) = @_;
833 # send a message state machine
840 if ($self->state eq 'send1') {
842 confess "local var gone missing" if !ref $self->{loc};
843 my $loc = $self->{loc};
844 $loc->{subject} = $line;
846 $self->state('sendbody');
847 #push @out, $self->msg('sendbody');
848 push @out, $self->msg('m8');
849 } elsif ($self->state eq 'sendbody') {
850 confess "local var gone missing" if !ref $self->{loc};
851 my $loc = $self->{loc};
852 if ($line eq "\032" || $line eq '%1A' || uc $line eq "/EX") {
855 foreach $to (@{$loc->{to}}) {
857 my $systime = $main::systime;
858 my $mycall = $main::mycall;
859 $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
861 exists $loc->{from} ? $loc->{from} : $self->call,
865 exists $loc->{origin} ? $loc->{origin} : $mycall,
868 $ref->swop_it($self->call);
869 $ref->store($loc->{lines});
871 push @out, $self->msg('m11', $ref->{msgno}, $to);
872 #push @out, "msgno $ref->{msgno} sent to $to";
873 my $dxchan = DXChannel->get(uc $to);
875 if ($dxchan->is_user()) {
876 $dxchan->send($dxchan->msg('m9'));
881 delete $loc->{lines};
886 $self->state('prompt');
887 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
888 #push @out, $self->msg('sendabort');
889 push @out, $self->msg('m10');
890 delete $loc->{lines};
894 $self->state('prompt');
897 # i.e. it ain't and end or abort, therefore store the line
898 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
904 # return the standard directory line for this ref
908 return sprintf "%6d%s%s%5d %8.8s %8.8s %-6.6s %5.5s %-30.30s",
909 $ref->msgno, $ref->read ? '-' : ' ', $ref->private ? 'p' : ' ', $ref->size,
910 $ref->to, $ref->from, cldate($ref->t), ztime($ref->t), $ref->subject;
913 # load the forward table
917 my $s = readfilestr($forwardfn);
925 # load the bad message table
929 my $s = readfilestr($badmsgfn);
937 # load the swop message table
941 my $s = readfilestr($swopfn);
950 # forward that message or not according to the forwarding table
951 # returns 1 for forward, 0 - to ignore
960 for ($i = 0; $i < @forward; $i += 5) {
961 my ($sort, $field, $pattern, $action, $bbs) = @forward[$i..($i+4)];
965 next if $ref->{private} && $sort ne 'P';
966 next if !$ref->{private} && $sort ne 'B';
969 $tested = $ref->{to} if $field eq 'T';
970 $tested = $ref->{from} if $field eq 'F';
971 $tested = $ref->{origin} if $field eq 'O';
972 $tested = $ref->{subject} if $field eq 'S';
974 if (!$pattern || $tested =~ m{$pattern}i) {
975 return 0 if $action eq 'I';
976 return 1 if !$bbs || grep $_ eq $call, @{$bbs};
987 for ($i = 0; $i < @badmsg; $i += 3) {
988 my ($sort, $field, $pattern) = @badmsg[$i..($i+2)];
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) {
1015 for ($i = 0; $i < @swop; $i += 5) {
1016 my ($sort, $field, $pattern, $tfield, $topattern) = @swop[$i..($i+4)];
1021 # are we interested?
1022 next if $ref->{private} && $sort ne 'P';
1023 next if !$ref->{private} && $sort ne 'B';
1026 $tested = $ref->{to} if $field eq 'T';
1027 $tested = $ref->{from} if $field eq 'F';
1028 $tested = $ref->{origin} if $field eq 'O';
1029 $tested = $ref->{subject} if $field eq 'S';
1032 $old = $swop = $ref->{to} if $tfield eq 'T';
1033 $old = $swop = $ref->{from} if $tfield eq 'F';
1034 $old = $swop = $ref->{origin} if $tfield eq 'O';
1035 $old = $swop = $ref->{subject} if $tfield eq 'S';
1037 if ($tested =~ m{$pattern}i) {
1038 if ($tested eq $swop) {
1039 $swop =~ s{$pattern}{$topattern}i;
1043 Log('msg', "Msg $ref->{msgno}: $tfield $old -> $swop");
1044 Log('dbg', "Msg $ref->{msgno}: $tfield $old -> $swop");
1045 $ref->{to} = $swop if $tfield eq 'T';
1046 $ref->{from} = $swop if $tfield eq 'F';
1047 $ref->{origin} = $swop if $tfield eq 'O';
1048 $ref->{subject} = $swop if $tfield eq 'S';
1055 # import any msgs in the import directory
1056 # the messages are in BBS format (but may have cluster extentions
1057 # so SB UK < GB7TLH is legal
1060 # are there any to do in this directory?
1061 return unless -d $importfn;
1062 unless (opendir(DIR, $importfn)) {
1063 dbg('msg', "can\'t open $importfn $!");
1064 Log('msg', "can\'t open $importfn $!");
1068 my @names = readdir(DIR);
1071 foreach $name (@names) {
1072 next if $name =~ /^\./;
1073 my $splitit = $name =~ /^split/;
1074 my $fn = "$importfn/$name";
1076 unless (open(MSG, $fn)) {
1077 dbg('msg', "can\'t open import file $fn $!");
1078 Log('msg', "can\'t open import file $fn $!");
1082 my @msg = map { chomp; $_ } <MSG>;
1085 my @out = import_one($DXProt::me, \@msg, $splitit);
1090 # import one message as a list in bbs (as extended) mode
1091 # takes a reference to an array containing the whole message
1096 my $splitit = shift;
1100 my $from = $dxchan->call;
1101 my $origin = $main::mycall;
1106 my $line = shift @$ref;
1107 my @f = split /\s+/, $line;
1108 unless (@f && $f[0] =~ /^(:?S|SP|SB|SEND)$/ ) {
1109 my $m = "invalid first line in import '$line'";
1114 my $f = uc shift @f;
1115 next if $f eq 'SEND';
1117 # private / noprivate / rr
1118 if ($notincalls && ($f eq 'B' || $f eq 'SB' || $f =~ /^NOP/oi)) {
1120 } elsif ($notincalls && ($f eq 'P' || $f eq 'SP' || $f =~ /^PRI/oi)) {
1122 } elsif ($notincalls && ($f eq 'RR')) {
1124 } elsif ($f eq '@' && @f) { # this is bbs syntax, for origin
1125 $origin = uc shift @f;
1126 } elsif ($f eq '<' && @f) { # this is bbs syntax for from call
1127 $from = uc shift @f;
1128 } elsif ($f =~ /^\$/) { # this is bbs syntax for a bid
1130 } elsif ($f =~ /^<\S+/) { # this is bbs syntax for from call
1131 ($from) = $f =~ /^<(\S+)$/;
1132 } elsif ($f =~ /^\@\S+/) { # this is bbs syntax for origin
1133 ($origin) = $f =~ /^\@(\S+)$/;
1139 # is this callsign a distro?
1140 my $fn = "$msgdir/distro/$f.pl";
1142 my $fh = new IO::File $fn;
1149 return (1, "Error in Distro $f.pl:", $@) if $@;
1157 if (grep $_ eq $f, @DXMsg::badmsg) {
1158 push @out, $dxchan->msg('m3', $f);
1165 # subject is the next line
1166 my $subject = shift @$ref;
1168 # strip off trailing lines
1169 pop @$ref while (@$ref && $$ref[-1] =~ /^\s*$/);
1171 # strip off /EX or /ABORT
1172 return ("aborted") if @$ref && $$ref[-1] =~ m{^/ABORT$}i;
1173 pop @$ref if (@$ref && $$ref[-1] =~ m{^/EX$}i);
1175 # sort out any splitting that needs to be done
1181 if ($lth >= $maxchunk || ($lth > $minchunk && /^\s*$/)) {
1182 push @chunk, $lines;
1189 push @chunk, $lines if @$lines;
1194 # write all the messages away
1196 for ( $i = 0; $i < @chunk; $i++) {
1197 my $chunk = $chunk[$i];
1200 my $num = " [" . ($i+1) . "/" . scalar @chunk . "]";
1201 $ch_subject = substr($subject, 0, 27 - length $num) . $num;
1203 $ch_subject = $subject;
1207 my $systime = $main::systime;
1208 my $mycall = $main::mycall;
1209 my $mref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
1218 $mref->swop_it($main::mycall);
1219 $mref->store($chunk);
1221 push @out, $dxchan->msg('m11', $mref->{msgno}, $to);
1222 #push @out, "msgno $ref->{msgno} sent to $to";
1223 my $todxchan = DXChannel->get(uc $to);
1225 if ($todxchan->is_user()) {
1226 $todxchan->send($todxchan->msg('m9'));
1238 my $name = $AUTOLOAD;
1239 return if $name =~ /::DESTROY$/;
1242 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
1243 # this clever line of code creates a subroutine which takes over from autoload
1244 # from OO Perl - Conway
1245 *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
1246 @_ ? $self->{$name} = shift : $self->{$name} ;