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;
121 delete $ref->{lines};
122 delete $ref->{linesreq};
123 delete $ref->{tonode};
124 delete $ref->{fromnode};
125 delete $ref->{stream};
127 delete $ref->{count};
128 delete $ref->{lastt} if exists $ref->{lastt};
129 delete $ref->{waitt} if exists $ref->{waitt};
134 my ($self, $line) = @_;
136 # this is periodic processing
137 if (!$self || !$line) {
139 if ($main::systime >= $lastq + $queueinterval) {
141 # 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]"};
216 push @{$ref->{lines}}, $f[4];
218 if ($ref->{count} >= $ref->{linesreq}) {
219 $self->send(DXProt::pc31($f[2], $f[1], $f[3]));
220 dbg('msg', "stream $f[3]: $ref->{count} lines received\n");
223 $ref->{lastt} = $main::systime;
225 dbg('msg', "PC29 from unknown stream $f[3] from $f[2]" );
226 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
231 if ($pcno == 30) { # this is a incoming subject ack
232 my $ref = $work{$f[2]}; # note no stream at this stage
235 $ref->{stream} = $f[3];
237 $ref->{linesreq} = 5;
238 $work{"$f[2]$f[3]"} = $ref; # new ref
239 dbg('msg', "incoming subject ack stream $f[3]\n");
240 $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 if ($ref->{file}) { # a file
412 dbg('msg', "To be stored in $ref->{to}\n");
414 my $fh = new IO::File "$ref->{to}", "w";
417 foreach $line (@{$lines}) {
421 dbg('msg', "file $ref->{to} stored\n");
422 Log('msg', "file $ref->{to} from $ref->{from} stored" );
424 confess "can't open file $ref->{to} $!";
426 } else { # a normal message
428 # attempt to open the message file
429 my $fn = filename($ref->{msgno});
431 dbg('msg', "To be stored in $fn\n");
433 # now save the file, overwriting what's there, YES I KNOW OK! (I will change it if it's a problem)
434 my $fh = new IO::File "$fn", "w";
436 my $rr = $ref->{rrreq} ? '1' : '0';
437 my $priv = $ref->{private} ? '1': '0';
438 print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$priv^$ref->{subject}^$ref->{origin}^$ref->{'read'}^$rr\n";
439 print $fh "=== ", join('^', @{$ref->{gotit}}), "\n";
442 foreach $line (@{$lines}) {
443 $ref->{size} += (length $line) + 1;
447 dbg('msg', "msg $ref->{msgno} stored\n");
448 Log('msg', "msg $ref->{msgno} from $ref->{from} to $ref->{to} stored" );
450 confess "can't open msg file $fn $!";
460 # remove it from the active message list
461 @msg = grep { ref($_) && $_ != $self } @msg;
463 # belt and braces (one day I will ask someone if this is REALLY necessary)
464 delete $self->{gotit};
465 delete $self->{list};
468 unlink filename($self->{msgno});
469 dbg('msg', "deleting $self->{msgno}\n");
472 # clean out old messages from the message queue
477 # mark old messages for deletion
478 foreach $ref (@msg) {
479 if (ref($ref) && !$ref->{keep} && $ref->{t} < $main::systime - $maxage) {
480 $ref->{deleteme} = 1;
481 delete $ref->{gotit};
483 unlink filename($ref->{msgno});
484 dbg('msg', "deleting old $ref->{msgno}\n");
488 # remove them all from the active message list
489 @msg = grep { ref($_) && !$_->{deleteme} } @msg;
490 $last_clean = $main::systime;
493 # read in a message header
503 $file = new IO::File "$fn";
505 dbg('err', "Error reading $fn $!");
506 Log('err', "Error reading $fn $!");
510 $line = <$file>; # first line
511 if ($size == 0 || !$line) {
512 dbg('err', "Empty $fn $!");
513 Log('err', "Empty $fn $!");
517 $size -= length $line;
518 if (! $line =~ /^===/o) {
519 dbg('err', "corrupt first line in $fn ($line)");
520 Log('err', "corrupt first line in $fn ($line)");
524 @f = split /\^/, $line;
525 $ref = DXMsg->alloc(@f);
527 $line = <$file>; # second line
529 $size -= length $line;
530 if (! $line =~ /^===/o) {
531 dbg('err', "corrupt second line in $fn ($line)");
532 Log('err', "corrupt second line in $fn ($line)");
537 @f = split /\^/, $line;
538 push @{$ref->{gotit}}, @f;
539 $ref->{size} = $size;
546 # read in a message header
550 my $msgno = $self->{msgno};
553 my $fn = filename($msgno);
556 $file = new IO::File;
557 if (!open($file, $fn)) {
558 dbg('err' ,"Error reading $fn $!");
559 Log('err' ,"Error reading $fn $!");
562 @out = map {chomp; $_} <$file>;
565 shift @out if $out[0] =~ /^=== /;
566 shift @out if $out[0] =~ /^=== /;
570 # send a tranche of lines to the other end
573 my ($self, $dxchan) = @_;
575 my $to = $self->{tonode};
576 my $from = $self->{fromnode};
577 my $stream = $self->{stream};
578 my $lines = $self->{lines};
581 for ($i = 0, $c = $self->{count}; $i < $self->{linesreq} && $c < @$lines; $i++, $c++) {
582 push @out, DXProt::pc29($to, $from, $stream, $lines->[$c]);
586 push @out, DXProt::pc32($to, $from, $stream) if $i < $self->{linesreq};
591 # find a message to send out and start the ball rolling
599 # bat down the message list looking for one that needs to go off site and whose
600 # nearest node is not busy.
602 dbg('msg', "queue msg ($sort)\n");
603 my @nodelist = DXChannel::get_all_nodes;
604 foreach $ref (@msg) {
605 # firstly, is it private and unread? if so can I find the recipient
606 # in my cluster node list offsite?
608 # ignore 'delayed' messages until their waiting time has expired
609 if (exists $ref->{waitt}) {
610 next if $ref->{waitt} > $main::systime;
611 delete $ref->{waitt};
614 # deal with routed private messages
616 if ($ref->{private}) {
617 next if $ref->{'read'}; # if it is read, it is stuck here
618 $clref = DXCluster->get_exact($ref->{to});
619 unless ($clref) { # otherwise look for a homenode
620 my $uref = DXUser->get_current($ref->{to});
621 my $hnode = $uref->homenode if $uref;
622 $clref = DXCluster->get_exact($hnode) if $hnode;
624 if ($clref && !grep { $clref->dxchan == $_ } DXCommandmode::get_all()) {
625 next if $clref->call eq $main::mycall; # i.e. it lives here
626 $dxchan = $clref->dxchan;
627 $ref->start_msg($dxchan) if $dxchan && !get_busy($dxchan->call) && $dxchan->state eq 'normal';
631 # otherwise we are dealing with a bulletin or forwarded private message
632 # compare the gotit list with
633 # the nodelist up above, if there are sites that haven't got it yet
634 # then start sending it - what happens when we get loops is anyone's
635 # guess, use (to, from, time, subject) tuple?
636 foreach $dxchan (@nodelist) {
637 next if $dxchan->call eq $main::mycall;
638 next if grep { $_ eq $dxchan->call } @{$ref->{gotit}};
639 next unless $ref->forward_it($dxchan->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($dxchan->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
770 if (grep $ref->{to} eq $_, @badmsg) {
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};
988 for ($i = 0; $i < @badmsg; $i += 3) {
989 my ($sort, $field, $pattern) = @badmsg[$i..($i+2)];
993 next if $ref->{private} && $sort ne 'P';
994 next if !$ref->{private} && $sort ne 'B';
997 $tested = $ref->{to} if $field eq 'T';
998 $tested = $ref->{from} if $field eq 'F';
999 $tested = $ref->{origin} if $field eq 'O';
1000 $tested = $ref->{subject} if $field eq 'S';
1002 if (!$pattern || $tested =~ m{$pattern}i) {
1016 for ($i = 0; $i < @swop; $i += 5) {
1017 my ($sort, $field, $pattern, $tfield, $topattern) = @swop[$i..($i+4)];
1022 # are we interested?
1023 next if $ref->{private} && $sort ne 'P';
1024 next if !$ref->{private} && $sort ne 'B';
1027 $tested = $ref->{to} if $field eq 'T';
1028 $tested = $ref->{from} if $field eq 'F';
1029 $tested = $ref->{origin} if $field eq 'O';
1030 $tested = $ref->{subject} if $field eq 'S';
1033 $old = $swop = $ref->{to} if $tfield eq 'T';
1034 $old = $swop = $ref->{from} if $tfield eq 'F';
1035 $old = $swop = $ref->{origin} if $tfield eq 'O';
1036 $old = $swop = $ref->{subject} if $tfield eq 'S';
1038 if ($tested =~ m{$pattern}i) {
1039 if ($tested eq $swop) {
1040 $swop =~ s{$pattern}{$topattern}i;
1044 Log('msg', "Msg $ref->{msgno}: $tfield $old -> $swop");
1045 Log('dbg', "Msg $ref->{msgno}: $tfield $old -> $swop");
1046 $ref->{to} = $swop if $tfield eq 'T';
1047 $ref->{from} = $swop if $tfield eq 'F';
1048 $ref->{origin} = $swop if $tfield eq 'O';
1049 $ref->{subject} = $swop if $tfield eq 'S';
1056 # import any msgs in the import directory
1057 # the messages are in BBS format (but may have cluster extentions
1058 # so SB UK < GB7TLH is legal
1061 # are there any to do in this directory?
1062 return unless -d $importfn;
1063 unless (opendir(DIR, $importfn)) {
1064 dbg('msg', "can\'t open $importfn $!");
1065 Log('msg', "can\'t open $importfn $!");
1069 my @names = readdir(DIR);
1072 foreach $name (@names) {
1073 next if $name =~ /^\./;
1074 my $splitit = $name =~ /^split/;
1075 my $fn = "$importfn/$name";
1077 unless (open(MSG, $fn)) {
1078 dbg('msg', "can\'t open import file $fn $!");
1079 Log('msg', "can\'t open import file $fn $!");
1083 my @msg = map { chomp; $_ } <MSG>;
1086 my @out = import_one($DXProt::me, \@msg, $splitit);
1091 # import one message as a list in bbs (as extended) mode
1092 # takes a reference to an array containing the whole message
1097 my $splitit = shift;
1101 my $from = $dxchan->call;
1102 my $origin = $main::mycall;
1107 my $line = shift @$ref;
1108 my @f = split /\s+/, $line;
1109 unless (@f && $f[0] =~ /^(:?S|SP|SB|SEND)$/ ) {
1110 my $m = "invalid first line in import '$line'";
1115 my $f = uc shift @f;
1116 next if $f eq 'SEND';
1118 # private / noprivate / rr
1119 if ($notincalls && ($f eq 'B' || $f eq 'SB' || $f =~ /^NOP/oi)) {
1121 } elsif ($notincalls && ($f eq 'P' || $f eq 'SP' || $f =~ /^PRI/oi)) {
1123 } elsif ($notincalls && ($f eq 'RR')) {
1125 } elsif ($f eq '@' && @f) { # this is bbs syntax, for origin
1126 $origin = uc shift @f;
1127 } elsif ($f eq '<' && @f) { # this is bbs syntax for from call
1128 $from = uc shift @f;
1129 } elsif ($f =~ /^\$/) { # this is bbs syntax for a bid
1131 } elsif ($f =~ /^<\S+/) { # this is bbs syntax for from call
1132 ($from) = $f =~ /^<(\S+)$/;
1133 } elsif ($f =~ /^\@\S+/) { # this is bbs syntax for origin
1134 ($origin) = $f =~ /^\@(\S+)$/;
1140 # is this callsign a distro?
1141 my $fn = "$msgdir/distro/$f.pl";
1143 my $fh = new IO::File $fn;
1150 return (1, "Error in Distro $f.pl:", $@) if $@;
1158 if (grep $_ eq $f, @DXMsg::badmsg) {
1159 push @out, $dxchan->msg('m3', $f);
1166 # subject is the next line
1167 my $subject = shift @$ref;
1169 # strip off trailing lines
1170 pop @$ref while (@$ref && $$ref[-1] =~ /^\s*$/);
1172 # strip off /EX or /ABORT
1173 return ("aborted") if @$ref && $$ref[-1] =~ m{^/ABORT$}i;
1174 pop @$ref if (@$ref && $$ref[-1] =~ m{^/EX$}i);
1176 # sort out any splitting that needs to be done
1182 if ($lth >= $maxchunk || ($lth > $minchunk && /^\s*$/)) {
1183 push @chunk, $lines;
1190 push @chunk, $lines if @$lines;
1195 # write all the messages away
1197 for ( $i = 0; $i < @chunk; $i++) {
1198 my $chunk = $chunk[$i];
1201 my $num = " [" . ($i+1) . "/" . scalar @chunk . "]";
1202 $ch_subject = substr($subject, 0, 27 - length $num) . $num;
1204 $ch_subject = $subject;
1208 my $systime = $main::systime;
1209 my $mycall = $main::mycall;
1210 my $mref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
1219 $mref->swop_it($main::mycall);
1220 $mref->store($chunk);
1222 push @out, $dxchan->msg('m11', $mref->{msgno}, $to);
1223 #push @out, "msgno $ref->{msgno} sent to $to";
1224 my $todxchan = DXChannel->get(uc $to);
1226 if ($todxchan->is_user()) {
1227 $todxchan->send($todxchan->msg('m9'));
1239 my $name = $AUTOLOAD;
1240 return if $name =~ /::DESTROY$/;
1243 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
1244 # this clever line of code creates a subroutine which takes over from autoload
1245 # from OO Perl - Conway
1246 *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
1247 @_ ? $self->{$name} = shift : $self->{$name} ;