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(%work @msg $msgdir %valid %busy $maxage $last_clean
32 @badmsg @swop $swopfn $badmsgfn $forwardfn @forward $timeout $waittime
33 $queueinterval $lastq $importfn $minchunk $maxchunk $bulltopriv);
35 %work = (); # outstanding jobs
36 @msg = (); # messages we have
37 %busy = (); # station interlocks
38 $msgdir = "$main::root/msg"; # directory contain the msgs
39 $maxage = 30 * 86400; # the maximum age that a message shall live for if not marked
40 $last_clean = 0; # last time we did a clean
41 @forward = (); # msg forward table
42 @badmsg = (); # bad message table
43 @swop = (); # swop table
44 $timeout = 30*60; # forwarding timeout
45 $waittime = 30*60; # time an aborted outgoing message waits before trying again
46 $queueinterval = 1*60; # run the queue every 1 minute
49 $minchunk = 4800; # minimum chunk size for a split message
50 $maxchunk = 6000; # maximum chunk size
51 $bulltopriv = 1; # convert msgs with callsigns to private if they are bulls
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',
85 # allocate a new object
86 # called fromnode, tonode, from, to, datetime, private?, subject, nolinesper
90 my $self = bless {}, $pkg;
91 $self->{msgno} = shift;
94 $self->{to} = ($to eq $main::mycall) ? $main::myalias : $to;
97 $self->{from} = uc $from;
99 $self->{private} = shift;
100 $self->{subject} = shift;
101 $self->{origin} = shift;
102 $self->{'read'} = shift;
103 $self->{rrreq} = shift;
105 $self->{lastt} = $main::systime;
107 $self->{private} = 1 if $bulltopriv && DXUser->get_current($self->{to});
115 delete $ref->{lines};
116 delete $ref->{linesreq};
117 delete $ref->{tonode};
118 delete $ref->{fromnode};
119 delete $ref->{stream};
121 delete $ref->{count};
122 delete $ref->{lastt} if exists $ref->{lastt};
123 delete $ref->{waitt} if exists $ref->{waitt};
128 my ($self, $line) = @_;
130 # this is periodic processing
131 if (!$self || !$line) {
133 if ($main::systime >= $lastq + $queueinterval) {
135 # queue some message if the interval timer has gone off
138 # import any messages in the import directory
141 $lastq = $main::systime;
144 # clean the message queue
145 clean_old() if $main::systime - $last_clean > 3600 ;
149 my @f = split /\^/, $line;
150 my ($pcno) = $f[0] =~ /^PC(\d\d)/; # just get the number
153 if ($pcno == 28) { # incoming message
155 # first look for any messages in the busy queue
156 # and cancel them this should both resolve timed out incoming messages
157 # and crossing of message between nodes, incoming messages have priority
158 if (exists $busy{$f[2]}) {
159 my $ref = $busy{$f[2]};
160 my $tonode = $ref->{tonode};
161 dbg('msg', "Busy, stopping msgno: $ref->{msgno} -> $f[2]");
162 $ref->stop_msg($self->call);
165 my $t = cltounix($f[5], $f[6]);
166 my $stream = next_transno($f[2]);
167 $f[13] = $self->call unless $f[13] && $f[13] gt ' ';
168 my $ref = DXMsg->alloc($stream, uc $f[3], $f[4], $t, $f[7], $f[8], $f[13], '0', $f[11]);
170 # fill in various forwarding state variables
171 $ref->{fromnode} = $f[2];
172 $ref->{tonode} = $f[1];
173 $ref->{rrreq} = $f[11];
174 $ref->{linesreq} = $f[10];
175 $ref->{stream} = $stream;
176 $ref->{count} = 0; # no of lines between PC31s
177 dbg('msg', "new message from $f[4] to $f[3] '$f[8]' stream $stream\n");
178 Log('msg', "Incoming message $f[4] to $f[3] '$f[8]'" );
179 $work{"$f[2]$stream"} = $ref; # store in work
180 $busy{$f[2]} = $ref; # set interlock
181 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
182 $ref->{lastt} = $main::systime;
184 # look to see whether this is a non private message sent to a known callsign
185 my $uref = DXUser->get_current($ref->{to});
186 if (is_callsign($ref->{to}) && !$ref->{private} && $uref && $uref->homenode) {
188 dbg('msg', "set bull to $ref->{to} to private");
193 if ($pcno == 29) { # incoming text
194 my $ref = $work{"$f[2]$f[3]"};
197 push @{$ref->{lines}}, $f[4];
199 if ($ref->{count} >= $ref->{linesreq}) {
200 $self->send(DXProt::pc31($f[2], $f[1], $f[3]));
201 dbg('msg', "stream $f[3]: $ref->{count} lines received\n");
204 $ref->{lastt} = $main::systime;
206 dbg('msg', "PC29 from unknown stream $f[3] from $f[2]" );
207 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
212 if ($pcno == 30) { # this is a incoming subject ack
213 my $ref = $work{$f[2]}; # note no stream at this stage
216 $ref->{stream} = $f[3];
218 $ref->{linesreq} = 5;
219 $work{"$f[2]$f[3]"} = $ref; # new ref
220 dbg('msg', "incoming subject ack stream $f[3]\n");
221 $busy{$f[2]} = $ref; # interlock
222 push @{$ref->{lines}}, ($ref->read_msg_body);
223 $ref->send_tranche($self);
224 $ref->{lastt} = $main::systime;
226 dbg('msg', "PC30 from unknown stream $f[3] from $f[2]" );
227 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
232 if ($pcno == 31) { # acknowledge a tranche of lines
233 my $ref = $work{"$f[2]$f[3]"};
235 dbg('msg', "tranche ack stream $f[3]\n");
236 $ref->send_tranche($self);
237 $ref->{lastt} = $main::systime;
239 dbg('msg', "PC31 from unknown stream $f[3] from $f[2]" );
240 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
245 if ($pcno == 32) { # incoming EOM
246 dbg('msg', "stream $f[3]: EOM received\n");
247 my $ref = $work{"$f[2]$f[3]"};
249 $self->send(DXProt::pc33($f[2], $f[1], $f[3])); # acknowledge it
251 # get the next msg no - note that this has NOTHING to do with the stream number in PC protocol
252 # store the file or message
253 # remove extraneous rubbish from the hash
254 # remove it from the work in progress vector
255 # stuff it on the msg queue
258 $ref->store($ref->{lines});
261 # does an identical message already exist?
264 if ($ref->{subject} eq $m->{subject} && $ref->{t} == $m->{t} && $ref->{from} eq $m->{from} && $ref->{to} eq $m->{to}) {
265 $ref->stop_msg($self->call);
266 my $msgno = $m->{msgno};
267 dbg('msg', "duplicate message from $ref->{from} -> $ref->{to} to $msgno");
268 Log('msg', "duplicate message from $ref->{from} -> $ref->{to} to $msgno");
274 $ref->swop_it($self->call);
276 # look for 'bad' to addresses
278 $ref->stop_msg($self->call);
279 dbg('msg', "'Bad' message $ref->{to}");
280 Log('msg', "'Bad' message $ref->{to}");
284 $ref->{msgno} = next_transno("Msgno");
285 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
286 $ref->store($ref->{lines});
288 my $dxchan = DXChannel->get($ref->{to});
289 $dxchan->send($dxchan->msg('m9')) if $dxchan && $dxchan->is_user;
290 Log('msg', "Message $ref->{msgno} from $ref->{from} received from $f[2] for $ref->{to}");
293 $ref->stop_msg($self->call);
295 dbg('msg', "PC32 from unknown stream $f[3] from $f[2]" );
296 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
302 if ($pcno == 33) { # acknowledge the end of message
303 my $ref = $work{"$f[2]$f[3]"};
305 if ($ref->{private}) { # remove it if it private and gone off site#
306 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2] and deleted");
309 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2]");
310 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
311 $ref->store($ref->{lines}); # re- store the file
313 $ref->stop_msg($self->call);
315 dbg('msg', "PC33 from unknown stream $f[3] from $f[2]" );
316 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
319 # send next one if present
324 if ($pcno == 40) { # this is a file request
325 $f[3] =~ s/\\/\//og; # change the slashes
326 $f[3] =~ s/\.//og; # remove dots
327 $f[3] =~ s/^\///o; # remove the leading /
328 $f[3] = lc $f[3]; # to lower case;
329 dbg('msg', "incoming file $f[3]\n");
330 $f[3] = 'packclus/' . $f[3] unless $f[3] =~ /^packclus\//o;
332 # create any directories
333 my @part = split /\//, $f[3];
335 my $fn = "$main::root";
336 pop @part; # remove last part
337 foreach $part (@part) {
340 last SWITCH if !mkdir $fn, 0777;
341 dbg('msg', "created directory $fn\n");
343 my $stream = next_transno($f[2]);
344 my $ref = DXMsg->alloc($stream, "$main::root/$f[3]", $self->call, time, !$f[4], $f[3], ' ', '0', '0');
346 # forwarding variables
347 $ref->{fromnode} = $f[1];
348 $ref->{tonode} = $f[2];
349 $ref->{linesreq} = $f[5];
350 $ref->{stream} = $stream;
351 $ref->{count} = 0; # no of lines between PC31s
353 $ref->{lastt} = $main::systime;
354 $work{"$f[2]$stream"} = $ref; # store in work
355 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
360 if ($pcno == 42) { # abort transfer
361 dbg('msg', "stream $f[3]: abort received\n");
362 my $ref = $work{"$f[2]$f[3]"};
364 $ref->stop_msg($self->call);
370 if ($pcno == 49) { # global delete on subject
372 if ($_->{from} eq $f[1] && $_->{subject} eq $f[2]) {
374 Log('msg', "Message $_->{msgno} from $_->{from} ($_->{subject}) fully deleted");
375 DXProt::broadcast_ak1a($line, $self);
383 # store a message away on disc or whatever
385 # NOTE the second arg is a REFERENCE not a list
391 if ($ref->{file}) { # a file
392 dbg('msg', "To be stored in $ref->{to}\n");
394 my $fh = new IO::File "$ref->{to}", "w";
397 foreach $line (@{$lines}) {
401 dbg('msg', "file $ref->{to} stored\n");
402 Log('msg', "file $ref->{to} from $ref->{from} stored" );
404 confess "can't open file $ref->{to} $!";
406 } else { # a normal message
408 # attempt to open the message file
409 my $fn = filename($ref->{msgno});
411 dbg('msg', "To be stored in $fn\n");
413 # now save the file, overwriting what's there, YES I KNOW OK! (I will change it if it's a problem)
414 my $fh = new IO::File "$fn", "w";
416 my $rr = $ref->{rrreq} ? '1' : '0';
417 my $priv = $ref->{private} ? '1': '0';
418 print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$priv^$ref->{subject}^$ref->{origin}^$ref->{'read'}^$rr\n";
419 print $fh "=== ", join('^', @{$ref->{gotit}}), "\n";
422 foreach $line (@{$lines}) {
423 $ref->{size} += (length $line) + 1;
427 dbg('msg', "msg $ref->{msgno} stored\n");
428 Log('msg', "msg $ref->{msgno} from $ref->{from} to $ref->{to} stored" );
430 confess "can't open msg file $fn $!";
440 # remove it from the active message list
441 dbg('msg', "\@msg = " . scalar @msg . " before delete");
442 @msg = grep { $_ != $self } @msg;
445 unlink filename($self->{msgno});
446 dbg('msg', "deleting $self->{msgno}\n");
447 dbg('msg', "\@msg = " . scalar @msg . " after delete");
450 # clean out old messages from the message queue
455 # mark old messages for deletion
456 dbg('msg', "\@msg = " . scalar @msg . " before delete");
457 foreach $ref (@msg) {
458 if (ref($ref) && !$ref->{keep} && $ref->{t} < $main::systime - $maxage) {
459 $ref->{deleteme} = 1;
460 unlink filename($ref->{msgno});
461 dbg('msg', "deleting old $ref->{msgno}\n");
465 # remove them all from the active message list
466 @msg = grep { !$_->{deleteme} } @msg;
467 dbg('msg', "\@msg = " . scalar @msg . " after delete");
468 $last_clean = $main::systime;
471 # read in a message header
481 $file = new IO::File "$fn";
483 dbg('err', "Error reading $fn $!");
484 Log('err', "Error reading $fn $!");
488 $line = <$file>; # first line
489 if ($size == 0 || !$line) {
490 dbg('err', "Empty $fn $!");
491 Log('err', "Empty $fn $!");
495 $size -= length $line;
496 if (! $line =~ /^===/o) {
497 dbg('err', "corrupt first line in $fn ($line)");
498 Log('err', "corrupt first line in $fn ($line)");
502 @f = split /\^/, $line;
503 $ref = DXMsg->alloc(@f);
505 $line = <$file>; # second line
507 $size -= length $line;
508 if (! $line =~ /^===/o) {
509 dbg('err', "corrupt second line in $fn ($line)");
510 Log('err', "corrupt second line in $fn ($line)");
515 @f = split /\^/, $line;
516 push @{$ref->{gotit}}, @f;
517 $ref->{size} = $size;
524 # read in a message header
528 my $msgno = $self->{msgno};
531 my $fn = filename($msgno);
534 $file = new IO::File;
535 if (!open($file, $fn)) {
536 dbg('err' ,"Error reading $fn $!");
537 Log('err' ,"Error reading $fn $!");
540 @out = map {chomp; $_} <$file>;
543 shift @out if $out[0] =~ /^=== /;
544 shift @out if $out[0] =~ /^=== /;
548 # send a tranche of lines to the other end
551 my ($self, $dxchan) = @_;
553 my $to = $self->{tonode};
554 my $from = $self->{fromnode};
555 my $stream = $self->{stream};
556 my $lines = $self->{lines};
559 for ($i = 0, $c = $self->{count}; $i < $self->{linesreq} && $c < @$lines; $i++, $c++) {
560 push @out, DXProt::pc29($to, $from, $stream, $lines->[$c]);
564 push @out, DXProt::pc32($to, $from, $stream) if $i < $self->{linesreq};
569 # find a message to send out and start the ball rolling
577 # bat down the message list looking for one that needs to go off site and whose
578 # nearest node is not busy.
580 dbg('msg', "queue msg ($sort)\n");
581 my @nodelist = DXChannel::get_all_nodes;
582 foreach $ref (@msg) {
584 # ignore 'delayed' messages until their waiting time has expired
585 if (exists $ref->{waitt}) {
586 next if $ref->{waitt} > $main::systime;
587 delete $ref->{waitt};
591 if (exists $ref->{lastt} && $main::systime >= $ref->{lastt} + $timeout) {
592 my $node = $ref->{tonode};
593 dbg('msg', "Timeout, stopping msgno: $ref->{msgno} -> $node");
594 Log('msg', "Timeout, stopping msgno: $ref->{msgno} -> $node");
595 $ref->stop_msg($node);
597 # delay any outgoing messages that fail
598 $ref->{waitt} = $main::systime + $waittime + rand(120) if $node ne $main::mycall;
599 delete $ref->{lastt};
603 # firstly, is it private and unread? if so can I find the recipient
604 # in my cluster node list offsite?
606 # deal with routed private messages
608 if ($ref->{private}) {
609 next if $ref->{'read'}; # if it is read, it is stuck here
610 $clref = DXCluster->get_exact($ref->{to});
611 unless ($clref) { # otherwise look for a homenode
612 my $uref = DXUser->get_current($ref->{to});
613 my $hnode = $uref->homenode if $uref;
614 $clref = DXCluster->get_exact($hnode) if $hnode;
616 if ($clref && !grep { $clref->dxchan == $_ } DXCommandmode::get_all()) {
617 next if $clref->call eq $main::mycall; # i.e. it lives here
618 $dxchan = $clref->dxchan;
619 $ref->start_msg($dxchan) if $dxchan && !get_busy($dxchan->call) && $dxchan->state eq 'normal';
623 # otherwise we are dealing with a bulletin or forwarded private message
624 # compare the gotit list with
625 # the nodelist up above, if there are sites that haven't got it yet
626 # then start sending it - what happens when we get loops is anyone's
627 # guess, use (to, from, time, subject) tuple?
628 foreach $dxchan (@nodelist) {
629 my $call = $dxchan->call;
631 next if $call eq $main::mycall;
632 next if ref $ref->{gotit} && grep $_ eq $call, @{$ref->{gotit}};
633 next unless $ref->forward_it($call); # check the forwarding file
635 # if we are here we have a node that doesn't have this message
636 $ref->start_msg($dxchan) if !get_busy($call) && $dxchan->state eq 'normal';
640 # if all the available nodes are busy then stop
641 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
645 # is there a message for me?
651 foreach $ref (@msg) {
652 # is it for me, private and unread?
653 if ($ref->{to} eq $call && $ref->{private}) {
654 return 1 if !$ref->{'read'};
660 # start the message off on its travels with a PC28
663 my ($self, $dxchan) = @_;
665 dbg('msg', "start msg $self->{msgno}\n");
666 $self->{linesreq} = 10;
668 $self->{tonode} = $dxchan->call;
669 $self->{fromnode} = $main::mycall;
670 $busy{$self->{tonode}} = $self;
671 $work{$self->{tonode}} = $self;
672 $self->{lastt} = $main::systime;
673 $dxchan->send(DXProt::pc28($self->{tonode}, $self->{fromnode}, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $self->{origin}, $self->{rrreq}));
676 # get the ref of a busy node
689 # get the forwarding queue
695 # stop a message from continuing, clean it out, unlock interlocks etc
700 my $stream = $self->{stream} if exists $self->{stream};
703 dbg('msg', "stop msg $self->{msgno} -> node $node\n");
705 delete $work{"$node$stream"} if $stream;
710 # get a new transaction number from the file specified
714 $name =~ s/\W//og; # remove non-word characters
715 my $fn = "$msgdir/$name";
718 my $fh = new IO::File;
719 if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
721 $msgno = $fh->getline;
725 $fh->print("$msgno\n");
726 dbg('msg', "msgno $msgno allocated for $name\n");
729 confess "can't open $fn $!";
734 # initialise the message 'system', read in all the message headers
737 my $dir = new IO::File;
741 # load various control files
742 dbg('err', "load badmsg: " . (load_badmsg() or "Ok"));
743 dbg('err', "load forward: " . (load_forward() or "Ok"));
744 dbg('err', "load swop: " . (load_swop() or "Ok"));
746 # read in the directory
747 opendir($dir, $msgdir) or confess "can't open $msgdir $!";
748 @dir = readdir($dir);
753 next unless /^m\d\d\d\d\d\d$/;
755 $ref = read_msg_header("$msgdir/$_");
757 dbg('err', "Deleting $_");
758 Log('err', "Deleting $_");
763 # delete any messages to 'badmsg.pl' places
765 dbg('msg', "'Bad' TO address $ref->{to}");
766 Log('msg', "'Bad' TO address $ref->{to}");
771 # add the message to the available queue
776 # add the message to the directory listing
780 confess "tried to add a non-ref to the msg directory" if !ref $ref;
784 # return all the current messages
790 # get a particular message
795 return $_ if $_->{msgno} == $msgno;
796 last if $_->{msgno} > $msgno;
801 # return the official filename for a message no
804 return sprintf "$msgdir/m%06d", shift;
808 # return a list of valid elements
817 # return a prompt for a field
822 my ($self, $ele) = @_;
827 # send a message state machine
834 if ($self->state eq 'send1') {
836 confess "local var gone missing" if !ref $self->{loc};
837 my $loc = $self->{loc};
838 $loc->{subject} = $line;
840 $self->state('sendbody');
841 #push @out, $self->msg('sendbody');
842 push @out, $self->msg('m8');
843 } elsif ($self->state eq 'sendbody') {
844 confess "local var gone missing" if !ref $self->{loc};
845 my $loc = $self->{loc};
846 if ($line eq "\032" || $line eq '%1A' || uc $line eq "/EX") {
849 foreach $to (@{$loc->{to}}) {
851 my $systime = $main::systime;
852 my $mycall = $main::mycall;
853 $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
855 exists $loc->{from} ? $loc->{from} : $self->call,
859 exists $loc->{origin} ? $loc->{origin} : $mycall,
862 $ref->swop_it($self->call);
863 $ref->store($loc->{lines});
865 push @out, $self->msg('m11', $ref->{msgno}, $to);
866 #push @out, "msgno $ref->{msgno} sent to $to";
867 my $dxchan = DXChannel->get(uc $to);
869 if ($dxchan->is_user()) {
870 $dxchan->send($dxchan->msg('m9'));
875 delete $loc->{lines};
880 $self->state('prompt');
881 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
882 #push @out, $self->msg('sendabort');
883 push @out, $self->msg('m10');
884 delete $loc->{lines};
888 $self->state('prompt');
891 # i.e. it ain't and end or abort, therefore store the line
892 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
898 # return the standard directory line for this ref
902 return sprintf "%6d%s%s%5d %8.8s %8.8s %-6.6s %5.5s %-30.30s",
903 $ref->msgno, $ref->read ? '-' : ' ', $ref->private ? 'p' : ' ', $ref->size,
904 $ref->to, $ref->from, cldate($ref->t), ztime($ref->t), $ref->subject;
907 # load the forward table
911 my $s = readfilestr($forwardfn);
919 # load the bad message table
923 my $s = readfilestr($badmsgfn);
931 # load the swop message table
935 my $s = readfilestr($swopfn);
944 # forward that message or not according to the forwarding table
945 # returns 1 for forward, 0 - to ignore
954 for ($i = 0; $i < @forward; $i += 5) {
955 my ($sort, $field, $pattern, $action, $bbs) = @forward[$i..($i+4)];
959 next if $ref->{private} && $sort ne 'P';
960 next if !$ref->{private} && $sort ne 'B';
963 $tested = $ref->{to} if $field eq 'T';
964 $tested = $ref->{from} if $field eq 'F';
965 $tested = $ref->{origin} if $field eq 'O';
966 $tested = $ref->{subject} if $field eq 'S';
968 if (!$pattern || $tested =~ m{$pattern}i) {
969 return 0 if $action eq 'I';
970 return 1 if !$bbs || grep $_ eq $call, @{$bbs};
981 for ($i = 0; $i < @badmsg; $i += 3) {
982 my ($sort, $field, $pattern) = @badmsg[$i..($i+2)];
986 next if $ref->{private} && $sort ne 'P';
987 next if !$ref->{private} && $sort ne 'B';
990 $tested = $ref->{to} if $field eq 'T';
991 $tested = $ref->{from} if $field eq 'F';
992 $tested = $ref->{origin} if $field eq 'O';
993 $tested = $ref->{subject} if $field eq 'S';
995 if (!$pattern || $tested =~ m{$pattern}i) {
1009 for ($i = 0; $i < @swop; $i += 5) {
1010 my ($sort, $field, $pattern, $tfield, $topattern) = @swop[$i..($i+4)];
1015 # are we interested?
1016 next if $ref->{private} && $sort ne 'P';
1017 next if !$ref->{private} && $sort ne 'B';
1020 $tested = $ref->{to} if $field eq 'T';
1021 $tested = $ref->{from} if $field eq 'F';
1022 $tested = $ref->{origin} if $field eq 'O';
1023 $tested = $ref->{subject} if $field eq 'S';
1026 $old = $swop = $ref->{to} if $tfield eq 'T';
1027 $old = $swop = $ref->{from} if $tfield eq 'F';
1028 $old = $swop = $ref->{origin} if $tfield eq 'O';
1029 $old = $swop = $ref->{subject} if $tfield eq 'S';
1031 if ($tested =~ m{$pattern}i) {
1032 if ($tested eq $swop) {
1033 $swop =~ s{$pattern}{$topattern}i;
1037 Log('msg', "Msg $ref->{msgno}: $tfield $old -> $swop");
1038 Log('dbg', "Msg $ref->{msgno}: $tfield $old -> $swop");
1039 $ref->{to} = $swop if $tfield eq 'T';
1040 $ref->{from} = $swop if $tfield eq 'F';
1041 $ref->{origin} = $swop if $tfield eq 'O';
1042 $ref->{subject} = $swop if $tfield eq 'S';
1049 # import any msgs in the import directory
1050 # the messages are in BBS format (but may have cluster extentions
1051 # so SB UK < GB7TLH is legal
1054 # are there any to do in this directory?
1055 return unless -d $importfn;
1056 unless (opendir(DIR, $importfn)) {
1057 dbg('msg', "can\'t open $importfn $!");
1058 Log('msg', "can\'t open $importfn $!");
1062 my @names = readdir(DIR);
1065 foreach $name (@names) {
1066 next if $name =~ /^\./;
1067 my $splitit = $name =~ /^split/;
1068 my $fn = "$importfn/$name";
1070 unless (open(MSG, $fn)) {
1071 dbg('msg', "can\'t open import file $fn $!");
1072 Log('msg', "can\'t open import file $fn $!");
1076 my @msg = map { chomp; $_ } <MSG>;
1079 my @out = import_one($DXProt::me, \@msg, $splitit);
1084 # import one message as a list in bbs (as extended) mode
1085 # takes a reference to an array containing the whole message
1090 my $splitit = shift;
1094 my $from = $dxchan->call;
1095 my $origin = $main::mycall;
1100 my $line = shift @$ref;
1101 my @f = split /\s+/, $line;
1102 unless (@f && $f[0] =~ /^(:?S|SP|SB|SEND)$/ ) {
1103 my $m = "invalid first line in import '$line'";
1108 my $f = uc shift @f;
1109 next if $f eq 'SEND';
1111 # private / noprivate / rr
1112 if ($notincalls && ($f eq 'B' || $f eq 'SB' || $f =~ /^NOP/oi)) {
1114 } elsif ($notincalls && ($f eq 'P' || $f eq 'SP' || $f =~ /^PRI/oi)) {
1116 } elsif ($notincalls && ($f eq 'RR')) {
1118 } elsif ($f eq '@' && @f) { # this is bbs syntax, for origin
1119 $origin = uc shift @f;
1120 } elsif ($f eq '<' && @f) { # this is bbs syntax for from call
1121 $from = uc shift @f;
1122 } elsif ($f =~ /^\$/) { # this is bbs syntax for a bid
1124 } elsif ($f =~ /^<\S+/) { # this is bbs syntax for from call
1125 ($from) = $f =~ /^<(\S+)$/;
1126 } elsif ($f =~ /^\@\S+/) { # this is bbs syntax for origin
1127 ($origin) = $f =~ /^\@(\S+)$/;
1133 # is this callsign a distro?
1134 my $fn = "$msgdir/distro/$f.pl";
1136 my $fh = new IO::File $fn;
1143 return (1, "Error in Distro $f.pl:", $@) if $@;
1151 if (grep $_ eq $f, @DXMsg::badmsg) {
1152 push @out, $dxchan->msg('m3', $f);
1159 # subject is the next line
1160 my $subject = shift @$ref;
1162 # strip off trailing lines
1163 pop @$ref while (@$ref && $$ref[-1] =~ /^\s*$/);
1165 # strip off /EX or /ABORT
1166 return ("aborted") if @$ref && $$ref[-1] =~ m{^/ABORT$}i;
1167 pop @$ref if (@$ref && $$ref[-1] =~ m{^/EX$}i);
1169 # sort out any splitting that needs to be done
1175 if ($lth >= $maxchunk || ($lth > $minchunk && /^\s*$/)) {
1176 push @chunk, $lines;
1183 push @chunk, $lines if @$lines;
1188 # write all the messages away
1190 for ( $i = 0; $i < @chunk; $i++) {
1191 my $chunk = $chunk[$i];
1194 my $num = " [" . ($i+1) . "/" . scalar @chunk . "]";
1195 $ch_subject = substr($subject, 0, 27 - length $num) . $num;
1197 $ch_subject = $subject;
1201 my $systime = $main::systime;
1202 my $mycall = $main::mycall;
1203 my $mref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
1212 $mref->swop_it($main::mycall);
1213 $mref->store($chunk);
1215 push @out, $dxchan->msg('m11', $mref->{msgno}, $to);
1216 #push @out, "msgno $ref->{msgno} sent to $to";
1217 my $todxchan = DXChannel->get(uc $to);
1219 if ($todxchan->is_user()) {
1220 $todxchan->send($todxchan->msg('m9'));
1232 my $name = $AUTOLOAD;
1233 return if $name =~ /::DESTROY$/;
1236 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
1237 # this clever line of code creates a subroutine which takes over from autoload
1238 # from OO Perl - Conway
1239 *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
1240 @_ ? $self->{$name} = shift : $self->{$name} ;