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 Log('msg', "Timeout, stopping msgno: $ref->{msgno} -> $node");
148 $ref->stop_msg($node);
150 # delay any outgoing messages that fail
151 $ref->{waitt} = $main::systime + $waittime + rand(120) if $node ne $main::mycall;
155 # queue some message if the interval timer has gone off
158 # import any messages in the import directory
161 $lastq = $main::systime;
164 # clean the message queue
165 clean_old() if $main::systime - $last_clean > 3600 ;
169 my @f = split /\^/, $line;
170 my ($pcno) = $f[0] =~ /^PC(\d\d)/; # just get the number
173 if ($pcno == 28) { # incoming message
175 # first look for any messages in the busy queue
176 # and cancel them this should both resolve timed out incoming messages
177 # and crossing of message between nodes, incoming messages have priority
178 if (exists $busy{$f[2]}) {
179 my $ref = $busy{$f[2]};
180 my $tonode = $ref->{tonode};
181 dbg('msg', "Busy, stopping msgno: $ref->{msgno} -> $f[2]");
182 $ref->stop_msg($self->call);
185 my $t = cltounix($f[5], $f[6]);
186 my $stream = next_transno($f[2]);
187 $f[13] = $self->call unless $f[13] && $f[13] gt ' ';
188 my $ref = DXMsg->alloc($stream, uc $f[3], $f[4], $t, $f[7], $f[8], $f[13], '0', $f[11]);
190 # fill in various forwarding state variables
191 $ref->{fromnode} = $f[2];
192 $ref->{tonode} = $f[1];
193 $ref->{rrreq} = $f[11];
194 $ref->{linesreq} = $f[10];
195 $ref->{stream} = $stream;
196 $ref->{count} = 0; # no of lines between PC31s
197 dbg('msg', "new message from $f[4] to $f[3] '$f[8]' stream $stream\n");
198 Log('msg', "Incoming message $f[4] to $f[3] '$f[8]'" );
199 $work{"$f[2]$stream"} = $ref; # store in work
200 $busy{$f[2]} = $ref; # set interlock
201 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
202 $ref->{lastt} = $main::systime;
204 # look to see whether this is a non private message sent to a known callsign
205 my $uref = DXUser->get_current($ref->{to});
206 if (is_callsign($ref->{to}) && !$ref->{private} && $uref && $uref->homenode) {
208 dbg('msg', "set bull to $ref->{to} to private");
213 if ($pcno == 29) { # incoming text
214 my $ref = $work{"$f[2]$f[3]"};
217 push @{$ref->{lines}}, $f[4];
219 if ($ref->{count} >= $ref->{linesreq}) {
220 $self->send(DXProt::pc31($f[2], $f[1], $f[3]));
221 dbg('msg', "stream $f[3]: $ref->{count} lines received\n");
224 $ref->{lastt} = $main::systime;
226 dbg('msg', "PC29 from unknown stream $f[3] from $f[2]" );
227 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
232 if ($pcno == 30) { # this is a incoming subject ack
233 my $ref = $work{$f[2]}; # note no stream at this stage
236 $ref->{stream} = $f[3];
238 $ref->{linesreq} = 5;
239 $work{"$f[2]$f[3]"} = $ref; # new ref
240 dbg('msg', "incoming subject ack stream $f[3]\n");
241 $busy{$f[2]} = $ref; # interlock
242 push @{$ref->{lines}}, ($ref->read_msg_body);
243 $ref->send_tranche($self);
244 $ref->{lastt} = $main::systime;
246 dbg('msg', "PC30 from unknown stream $f[3] from $f[2]" );
247 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
252 if ($pcno == 31) { # acknowledge a tranche of lines
253 my $ref = $work{"$f[2]$f[3]"};
255 dbg('msg', "tranche ack stream $f[3]\n");
256 $ref->send_tranche($self);
257 $ref->{lastt} = $main::systime;
259 dbg('msg', "PC31 from unknown stream $f[3] from $f[2]" );
260 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
265 if ($pcno == 32) { # incoming EOM
266 dbg('msg', "stream $f[3]: EOM received\n");
267 my $ref = $work{"$f[2]$f[3]"};
269 $self->send(DXProt::pc33($f[2], $f[1], $f[3])); # acknowledge it
271 # get the next msg no - note that this has NOTHING to do with the stream number in PC protocol
272 # store the file or message
273 # remove extraneous rubbish from the hash
274 # remove it from the work in progress vector
275 # stuff it on the msg queue
278 $ref->store($ref->{lines});
281 # does an identical message already exist?
284 if ($ref->{subject} eq $m->{subject} && $ref->{t} == $m->{t} && $ref->{from} eq $m->{from} && $ref->{to} eq $m->{to}) {
285 $ref->stop_msg($self->call);
286 my $msgno = $m->{msgno};
287 dbg('msg', "duplicate message from $ref->{from} -> $ref->{to} to $msgno");
288 Log('msg', "duplicate message from $ref->{from} -> $ref->{to} to $msgno");
294 $ref->swop_it($self->call);
296 # look for 'bad' to addresses
297 # if (grep $ref->{to} eq $_, @badmsg) {
298 if ($ref->dump_it($self->call)) {
299 $ref->stop_msg($self->call);
300 dbg('msg', "'Bad' message $ref->{to}");
301 Log('msg', "'Bad' message $ref->{to}");
305 $ref->{msgno} = next_transno("Msgno");
306 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
307 $ref->store($ref->{lines});
309 my $dxchan = DXChannel->get($ref->{to});
310 $dxchan->send($dxchan->msg('m9')) if $dxchan && $dxchan->is_user;
311 Log('msg', "Message $ref->{msgno} from $ref->{from} received from $f[2] for $ref->{to}");
314 $ref->stop_msg($self->call);
316 dbg('msg', "PC32 from unknown stream $f[3] from $f[2]" );
317 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
323 if ($pcno == 33) { # acknowledge the end of message
324 my $ref = $work{"$f[2]$f[3]"};
326 if ($ref->{private}) { # remove it if it private and gone off site#
327 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2] and deleted");
330 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2]");
331 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
332 $ref->store($ref->{lines}); # re- store the file
334 $ref->stop_msg($self->call);
336 dbg('msg', "PC33 from unknown stream $f[3] from $f[2]" );
337 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
340 # send next one if present
345 if ($pcno == 40) { # this is a file request
346 $f[3] =~ s/\\/\//og; # change the slashes
347 $f[3] =~ s/\.//og; # remove dots
348 $f[3] =~ s/^\///o; # remove the leading /
349 $f[3] = lc $f[3]; # to lower case;
350 dbg('msg', "incoming file $f[3]\n");
351 $f[3] = 'packclus/' . $f[3] unless $f[3] =~ /^packclus\//o;
353 # create any directories
354 my @part = split /\//, $f[3];
356 my $fn = "$main::root";
357 pop @part; # remove last part
358 foreach $part (@part) {
361 last SWITCH if !mkdir $fn, 0777;
362 dbg('msg', "created directory $fn\n");
364 my $stream = next_transno($f[2]);
365 my $ref = DXMsg->alloc($stream, "$main::root/$f[3]", $self->call, time, !$f[4], $f[3], ' ', '0', '0');
367 # forwarding variables
368 $ref->{fromnode} = $f[1];
369 $ref->{tonode} = $f[2];
370 $ref->{linesreq} = $f[5];
371 $ref->{stream} = $stream;
372 $ref->{count} = 0; # no of lines between PC31s
374 $ref->{lastt} = $main::systime;
375 $work{"$f[2]$stream"} = $ref; # store in work
376 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
381 if ($pcno == 42) { # abort transfer
382 dbg('msg', "stream $f[3]: abort received\n");
383 my $ref = $work{"$f[2]$f[3]"};
385 $ref->stop_msg($self->call);
391 if ($pcno == 49) { # global delete on subject
393 if ($_->{from} eq $f[1] && $_->{subject} eq $f[2]) {
395 Log('msg', "Message $_->{msgno} from $_->{from} ($_->{subject}) fully deleted");
396 DXProt::broadcast_ak1a($line, $self);
404 # store a message away on disc or whatever
406 # NOTE the second arg is a REFERENCE not a list
412 if ($ref->{file}) { # a file
413 dbg('msg', "To be stored in $ref->{to}\n");
415 my $fh = new IO::File "$ref->{to}", "w";
418 foreach $line (@{$lines}) {
422 dbg('msg', "file $ref->{to} stored\n");
423 Log('msg', "file $ref->{to} from $ref->{from} stored" );
425 confess "can't open file $ref->{to} $!";
427 } else { # a normal message
429 # attempt to open the message file
430 my $fn = filename($ref->{msgno});
432 dbg('msg', "To be stored in $fn\n");
434 # now save the file, overwriting what's there, YES I KNOW OK! (I will change it if it's a problem)
435 my $fh = new IO::File "$fn", "w";
437 my $rr = $ref->{rrreq} ? '1' : '0';
438 my $priv = $ref->{private} ? '1': '0';
439 print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$priv^$ref->{subject}^$ref->{origin}^$ref->{'read'}^$rr\n";
440 print $fh "=== ", join('^', @{$ref->{gotit}}), "\n";
443 foreach $line (@{$lines}) {
444 $ref->{size} += (length $line) + 1;
448 dbg('msg', "msg $ref->{msgno} stored\n");
449 Log('msg', "msg $ref->{msgno} from $ref->{from} to $ref->{to} stored" );
451 confess "can't open msg file $fn $!";
461 # remove it from the active message list
462 @msg = grep { ref($_) && $_ != $self } @msg;
464 # belt and braces (one day I will ask someone if this is REALLY necessary)
465 delete $self->{gotit};
466 delete $self->{list};
469 unlink filename($self->{msgno});
470 dbg('msg', "deleting $self->{msgno}\n");
473 # clean out old messages from the message queue
478 # mark old messages for deletion
479 foreach $ref (@msg) {
480 if (ref($ref) && !$ref->{keep} && $ref->{t} < $main::systime - $maxage) {
481 $ref->{deleteme} = 1;
482 delete $ref->{gotit};
484 unlink filename($ref->{msgno});
485 dbg('msg', "deleting old $ref->{msgno}\n");
489 # remove them all from the active message list
490 @msg = grep { ref($_) && !$_->{deleteme} } @msg;
491 $last_clean = $main::systime;
494 # read in a message header
504 $file = new IO::File "$fn";
506 dbg('err', "Error reading $fn $!");
507 Log('err', "Error reading $fn $!");
511 $line = <$file>; # first line
512 if ($size == 0 || !$line) {
513 dbg('err', "Empty $fn $!");
514 Log('err', "Empty $fn $!");
518 $size -= length $line;
519 if (! $line =~ /^===/o) {
520 dbg('err', "corrupt first line in $fn ($line)");
521 Log('err', "corrupt first line in $fn ($line)");
525 @f = split /\^/, $line;
526 $ref = DXMsg->alloc(@f);
528 $line = <$file>; # second line
530 $size -= length $line;
531 if (! $line =~ /^===/o) {
532 dbg('err', "corrupt second line in $fn ($line)");
533 Log('err', "corrupt second line in $fn ($line)");
538 @f = split /\^/, $line;
539 push @{$ref->{gotit}}, @f;
540 $ref->{size} = $size;
547 # read in a message header
551 my $msgno = $self->{msgno};
554 my $fn = filename($msgno);
557 $file = new IO::File;
558 if (!open($file, $fn)) {
559 dbg('err' ,"Error reading $fn $!");
560 Log('err' ,"Error reading $fn $!");
563 @out = map {chomp; $_} <$file>;
566 shift @out if $out[0] =~ /^=== /;
567 shift @out if $out[0] =~ /^=== /;
571 # send a tranche of lines to the other end
574 my ($self, $dxchan) = @_;
576 my $to = $self->{tonode};
577 my $from = $self->{fromnode};
578 my $stream = $self->{stream};
579 my $lines = $self->{lines};
582 for ($i = 0, $c = $self->{count}; $i < $self->{linesreq} && $c < @$lines; $i++, $c++) {
583 push @out, DXProt::pc29($to, $from, $stream, $lines->[$c]);
587 push @out, DXProt::pc32($to, $from, $stream) if $i < $self->{linesreq};
592 # find a message to send out and start the ball rolling
600 # bat down the message list looking for one that needs to go off site and whose
601 # nearest node is not busy.
603 dbg('msg', "queue msg ($sort)\n");
604 my @nodelist = DXChannel::get_all_nodes;
605 foreach $ref (@msg) {
606 # firstly, is it private and unread? if so can I find the recipient
607 # in my cluster node list offsite?
609 # ignore 'delayed' messages until their waiting time has expired
610 if (exists $ref->{waitt}) {
611 next if $ref->{waitt} > $main::systime;
612 delete $ref->{waitt};
615 # deal with routed private messages
617 if ($ref->{private}) {
618 next if $ref->{'read'}; # if it is read, it is stuck here
619 $clref = DXCluster->get_exact($ref->{to});
620 unless ($clref) { # otherwise look for a homenode
621 my $uref = DXUser->get_current($ref->{to});
622 my $hnode = $uref->homenode if $uref;
623 $clref = DXCluster->get_exact($hnode) if $hnode;
625 if ($clref && !grep { $clref->dxchan == $_ } DXCommandmode::get_all()) {
626 next if $clref->call eq $main::mycall; # i.e. it lives here
627 $dxchan = $clref->dxchan;
628 $ref->start_msg($dxchan) if $dxchan && !get_busy($dxchan->call) && $dxchan->state eq 'normal';
632 # otherwise we are dealing with a bulletin or forwarded private message
633 # compare the gotit list with
634 # the nodelist up above, if there are sites that haven't got it yet
635 # then start sending it - what happens when we get loops is anyone's
636 # guess, use (to, from, time, subject) tuple?
637 foreach $dxchan (@nodelist) {
638 next if $dxchan->call eq $main::mycall;
639 next if ref $ref->{gotit} && grep $_ eq $dxchan->call, @{$ref->{gotit}};
640 next unless $ref->forward_it($dxchan->call); # check the forwarding file
642 # if we are here we have a node that doesn't have this message
643 $ref->start_msg($dxchan) if !get_busy($dxchan->call) && $dxchan->state eq 'normal';
647 # if all the available nodes are busy then stop
648 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
652 # is there a message for me?
658 foreach $ref (@msg) {
659 # is it for me, private and unread?
660 if ($ref->{to} eq $call && $ref->{private}) {
661 return 1 if !$ref->{'read'};
667 # start the message off on its travels with a PC28
670 my ($self, $dxchan) = @_;
672 dbg('msg', "start msg $self->{msgno}\n");
673 $self->{linesreq} = 10;
675 $self->{tonode} = $dxchan->call;
676 $self->{fromnode} = $main::mycall;
677 $busy{$self->{tonode}} = $self;
678 $work{$self->{tonode}} = $self;
679 $self->{lastt} = $main::systime;
680 $dxchan->send(DXProt::pc28($self->{tonode}, $self->{fromnode}, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $self->{origin}, $self->{rrreq}));
683 # get the ref of a busy node
696 # get the forwarding queue
702 # stop a message from continuing, clean it out, unlock interlocks etc
707 my $stream = $self->{stream} if exists $self->{stream};
710 dbg('msg', "stop msg $self->{msgno} -> node $node\n");
712 delete $work{"$node$stream"} if $stream;
717 # get a new transaction number from the file specified
721 $name =~ s/\W//og; # remove non-word characters
722 my $fn = "$msgdir/$name";
725 my $fh = new IO::File;
726 if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
728 $msgno = $fh->getline;
732 $fh->print("$msgno\n");
733 dbg('msg', "msgno $msgno allocated for $name\n");
736 confess "can't open $fn $!";
741 # initialise the message 'system', read in all the message headers
744 my $dir = new IO::File;
748 # load various control files
749 dbg('err', "load badmsg: " . (load_badmsg() or "Ok"));
750 dbg('err', "load forward: " . (load_forward() or "Ok"));
751 dbg('err', "load swop: " . (load_swop() or "Ok"));
753 # read in the directory
754 opendir($dir, $msgdir) or confess "can't open $msgdir $!";
755 @dir = readdir($dir);
760 next unless /^m\d\d\d\d\d\d$/;
762 $ref = read_msg_header("$msgdir/$_");
764 dbg('err', "Deleting $_");
765 Log('err', "Deleting $_");
770 # delete any messages to 'badmsg.pl' places
771 if (grep $ref->{to} eq $_, @badmsg) {
772 dbg('msg', "'Bad' TO address $ref->{to}");
773 Log('msg', "'Bad' TO address $ref->{to}");
778 # add the message to the available queue
783 # add the message to the directory listing
787 confess "tried to add a non-ref to the msg directory" if !ref $ref;
791 # return all the current messages
797 # get a particular message
802 return $_ if $_->{msgno} == $msgno;
803 last if $_->{msgno} > $msgno;
808 # return the official filename for a message no
811 return sprintf "$msgdir/m%06d", shift;
815 # return a list of valid elements
824 # return a prompt for a field
829 my ($self, $ele) = @_;
834 # send a message state machine
841 if ($self->state eq 'send1') {
843 confess "local var gone missing" if !ref $self->{loc};
844 my $loc = $self->{loc};
845 $loc->{subject} = $line;
847 $self->state('sendbody');
848 #push @out, $self->msg('sendbody');
849 push @out, $self->msg('m8');
850 } elsif ($self->state eq 'sendbody') {
851 confess "local var gone missing" if !ref $self->{loc};
852 my $loc = $self->{loc};
853 if ($line eq "\032" || $line eq '%1A' || uc $line eq "/EX") {
856 foreach $to (@{$loc->{to}}) {
858 my $systime = $main::systime;
859 my $mycall = $main::mycall;
860 $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
862 exists $loc->{from} ? $loc->{from} : $self->call,
866 exists $loc->{origin} ? $loc->{origin} : $mycall,
869 $ref->swop_it($self->call);
870 $ref->store($loc->{lines});
872 push @out, $self->msg('m11', $ref->{msgno}, $to);
873 #push @out, "msgno $ref->{msgno} sent to $to";
874 my $dxchan = DXChannel->get(uc $to);
876 if ($dxchan->is_user()) {
877 $dxchan->send($dxchan->msg('m9'));
882 delete $loc->{lines};
887 $self->state('prompt');
888 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
889 #push @out, $self->msg('sendabort');
890 push @out, $self->msg('m10');
891 delete $loc->{lines};
895 $self->state('prompt');
898 # i.e. it ain't and end or abort, therefore store the line
899 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
905 # return the standard directory line for this ref
909 return sprintf "%6d%s%s%5d %8.8s %8.8s %-6.6s %5.5s %-30.30s",
910 $ref->msgno, $ref->read ? '-' : ' ', $ref->private ? 'p' : ' ', $ref->size,
911 $ref->to, $ref->from, cldate($ref->t), ztime($ref->t), $ref->subject;
914 # load the forward table
918 my $s = readfilestr($forwardfn);
926 # load the bad message table
930 my $s = readfilestr($badmsgfn);
938 # load the swop message table
942 my $s = readfilestr($swopfn);
951 # forward that message or not according to the forwarding table
952 # returns 1 for forward, 0 - to ignore
961 for ($i = 0; $i < @forward; $i += 5) {
962 my ($sort, $field, $pattern, $action, $bbs) = @forward[$i..($i+4)];
966 next if $ref->{private} && $sort ne 'P';
967 next if !$ref->{private} && $sort ne 'B';
970 $tested = $ref->{to} if $field eq 'T';
971 $tested = $ref->{from} if $field eq 'F';
972 $tested = $ref->{origin} if $field eq 'O';
973 $tested = $ref->{subject} if $field eq 'S';
975 if (!$pattern || $tested =~ m{$pattern}i) {
976 return 0 if $action eq 'I';
977 return 1 if !$bbs || grep $_ eq $call, @{$bbs};
989 for ($i = 0; $i < @badmsg; $i += 3) {
990 my ($sort, $field, $pattern) = @badmsg[$i..($i+2)];
994 next if $ref->{private} && $sort ne 'P';
995 next if !$ref->{private} && $sort ne 'B';
998 $tested = $ref->{to} if $field eq 'T';
999 $tested = $ref->{from} if $field eq 'F';
1000 $tested = $ref->{origin} if $field eq 'O';
1001 $tested = $ref->{subject} if $field eq 'S';
1003 if (!$pattern || $tested =~ m{$pattern}i) {
1017 for ($i = 0; $i < @swop; $i += 5) {
1018 my ($sort, $field, $pattern, $tfield, $topattern) = @swop[$i..($i+4)];
1023 # are we interested?
1024 next if $ref->{private} && $sort ne 'P';
1025 next if !$ref->{private} && $sort ne 'B';
1028 $tested = $ref->{to} if $field eq 'T';
1029 $tested = $ref->{from} if $field eq 'F';
1030 $tested = $ref->{origin} if $field eq 'O';
1031 $tested = $ref->{subject} if $field eq 'S';
1034 $old = $swop = $ref->{to} if $tfield eq 'T';
1035 $old = $swop = $ref->{from} if $tfield eq 'F';
1036 $old = $swop = $ref->{origin} if $tfield eq 'O';
1037 $old = $swop = $ref->{subject} if $tfield eq 'S';
1039 if ($tested =~ m{$pattern}i) {
1040 if ($tested eq $swop) {
1041 $swop =~ s{$pattern}{$topattern}i;
1045 Log('msg', "Msg $ref->{msgno}: $tfield $old -> $swop");
1046 Log('dbg', "Msg $ref->{msgno}: $tfield $old -> $swop");
1047 $ref->{to} = $swop if $tfield eq 'T';
1048 $ref->{from} = $swop if $tfield eq 'F';
1049 $ref->{origin} = $swop if $tfield eq 'O';
1050 $ref->{subject} = $swop if $tfield eq 'S';
1057 # import any msgs in the import directory
1058 # the messages are in BBS format (but may have cluster extentions
1059 # so SB UK < GB7TLH is legal
1062 # are there any to do in this directory?
1063 return unless -d $importfn;
1064 unless (opendir(DIR, $importfn)) {
1065 dbg('msg', "can\'t open $importfn $!");
1066 Log('msg', "can\'t open $importfn $!");
1070 my @names = readdir(DIR);
1073 foreach $name (@names) {
1074 next if $name =~ /^\./;
1075 my $splitit = $name =~ /^split/;
1076 my $fn = "$importfn/$name";
1078 unless (open(MSG, $fn)) {
1079 dbg('msg', "can\'t open import file $fn $!");
1080 Log('msg', "can\'t open import file $fn $!");
1084 my @msg = map { chomp; $_ } <MSG>;
1087 my @out = import_one($DXProt::me, \@msg, $splitit);
1092 # import one message as a list in bbs (as extended) mode
1093 # takes a reference to an array containing the whole message
1098 my $splitit = shift;
1102 my $from = $dxchan->call;
1103 my $origin = $main::mycall;
1108 my $line = shift @$ref;
1109 my @f = split /\s+/, $line;
1110 unless (@f && $f[0] =~ /^(:?S|SP|SB|SEND)$/ ) {
1111 my $m = "invalid first line in import '$line'";
1116 my $f = uc shift @f;
1117 next if $f eq 'SEND';
1119 # private / noprivate / rr
1120 if ($notincalls && ($f eq 'B' || $f eq 'SB' || $f =~ /^NOP/oi)) {
1122 } elsif ($notincalls && ($f eq 'P' || $f eq 'SP' || $f =~ /^PRI/oi)) {
1124 } elsif ($notincalls && ($f eq 'RR')) {
1126 } elsif ($f eq '@' && @f) { # this is bbs syntax, for origin
1127 $origin = uc shift @f;
1128 } elsif ($f eq '<' && @f) { # this is bbs syntax for from call
1129 $from = uc shift @f;
1130 } elsif ($f =~ /^\$/) { # this is bbs syntax for a bid
1132 } elsif ($f =~ /^<\S+/) { # this is bbs syntax for from call
1133 ($from) = $f =~ /^<(\S+)$/;
1134 } elsif ($f =~ /^\@\S+/) { # this is bbs syntax for origin
1135 ($origin) = $f =~ /^\@(\S+)$/;
1141 # is this callsign a distro?
1142 my $fn = "$msgdir/distro/$f.pl";
1144 my $fh = new IO::File $fn;
1151 return (1, "Error in Distro $f.pl:", $@) if $@;
1159 if (grep $_ eq $f, @DXMsg::badmsg) {
1160 push @out, $dxchan->msg('m3', $f);
1167 # subject is the next line
1168 my $subject = shift @$ref;
1170 # strip off trailing lines
1171 pop @$ref while (@$ref && $$ref[-1] =~ /^\s*$/);
1173 # strip off /EX or /ABORT
1174 return ("aborted") if @$ref && $$ref[-1] =~ m{^/ABORT$}i;
1175 pop @$ref if (@$ref && $$ref[-1] =~ m{^/EX$}i);
1177 # sort out any splitting that needs to be done
1183 if ($lth >= $maxchunk || ($lth > $minchunk && /^\s*$/)) {
1184 push @chunk, $lines;
1191 push @chunk, $lines if @$lines;
1196 # write all the messages away
1198 for ( $i = 0; $i < @chunk; $i++) {
1199 my $chunk = $chunk[$i];
1202 my $num = " [" . ($i+1) . "/" . scalar @chunk . "]";
1203 $ch_subject = substr($subject, 0, 27 - length $num) . $num;
1205 $ch_subject = $subject;
1209 my $systime = $main::systime;
1210 my $mycall = $main::mycall;
1211 my $mref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
1220 $mref->swop_it($main::mycall);
1221 $mref->store($chunk);
1223 push @out, $dxchan->msg('m11', $mref->{msgno}, $to);
1224 #push @out, "msgno $ref->{msgno} sent to $to";
1225 my $todxchan = DXChannel->get(uc $to);
1227 if ($todxchan->is_user()) {
1228 $todxchan->send($todxchan->msg('m9'));
1240 my $name = $AUTOLOAD;
1241 return if $name =~ /::DESTROY$/;
1244 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
1245 # this clever line of code creates a subroutine which takes over from autoload
1246 # from OO Perl - Conway
1247 *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
1248 @_ ? $self->{$name} = shift : $self->{$name} ;