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($VERSION $BRANCH);
32 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
33 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ ) || 0;
34 $main::build += $VERSION;
35 $main::branch += $BRANCH;
37 use vars qw(%work @msg $msgdir %valid %busy $maxage $last_clean
38 @badmsg @swop $swopfn $badmsgfn $forwardfn @forward $timeout $waittime
39 $queueinterval $lastq $importfn $minchunk $maxchunk $bulltopriv);
41 %work = (); # outstanding jobs
42 @msg = (); # messages we have
43 %busy = (); # station interlocks
44 $msgdir = "$main::root/msg"; # directory contain the msgs
45 $maxage = 30 * 86400; # the maximum age that a message shall live for if not marked
46 $last_clean = 0; # last time we did a clean
47 @forward = (); # msg forward table
48 @badmsg = (); # bad message table
49 @swop = (); # swop table
50 $timeout = 30*60; # forwarding timeout
51 $waittime = 30*60; # time an aborted outgoing message waits before trying again
52 $queueinterval = 1*60; # run the queue every 1 minute
55 $minchunk = 4800; # minimum chunk size for a split message
56 $maxchunk = 6000; # maximum chunk size
57 $bulltopriv = 1; # convert msgs with callsigns to private if they are bulls
60 $badmsgfn = "$msgdir/badmsg.pl"; # list of TO address we wont store
61 $forwardfn = "$msgdir/forward.pl"; # the forwarding table
62 $swopfn = "$msgdir/swop.pl"; # the swopping table
63 $importfn = "$msgdir/import"; # import directory
67 fromnode => '5,From Node',
68 tonode => '5,To Node',
71 t => '0,Msg Time,cldatetime',
72 private => '5,Private,yesno',
73 subject => '0,Subject',
74 linesreq => '0,Lines per Gob',
75 rrreq => '5,Read Confirm,yesno',
78 stream => '9,Stream No',
79 count => '5,Gob Linecnt',
80 file => '5,File?,yesno',
81 gotit => '5,Got it Nodes,parray',
82 lines => '5,Lines,parray',
83 'read' => '5,Times read',
86 keep => '0,Keep this?,yesno',
87 lastt => '5,Last processed,cldatetime',
88 waitt => '5,Wait until,cldatetime',
91 # allocate a new object
92 # called fromnode, tonode, from, to, datetime, private?, subject, nolinesper
96 my $self = bless {}, $pkg;
97 $self->{msgno} = shift;
100 $self->{to} = ($to eq $main::mycall) ? $main::myalias : $to;
103 $self->{from} = uc $from;
105 $self->{private} = shift;
106 $self->{subject} = shift;
107 $self->{origin} = shift;
108 $self->{'read'} = shift;
109 $self->{rrreq} = shift;
111 # $self->{lastt} = $main::systime;
113 $self->{private} = 1 if $bulltopriv && DXUser->get_current($self->{to});
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 # queue some message if the interval timer has gone off
144 # import any messages in the import directory
147 $lastq = $main::systime;
150 # clean the message queue
151 clean_old() if $main::systime - $last_clean > 3600 ;
155 my @f = split /\^/, $line;
156 my ($pcno) = $f[0] =~ /^PC(\d\d)/; # just get the number
157 my ($tonode, $fromnode) = @f[1, 2];
158 my $stream = $f[3] if $pcno > 29 && $pcno <= 33;
161 if ($pcno == 28) { # incoming message
163 # sort out various extant protocol errors that occur
165 $origin = $self->call unless $origin && $origin gt ' ';
167 # first look for any messages in the busy queue
168 # and cancel them this should both resolve timed out incoming messages
169 # and crossing of message between nodes, incoming messages have priority
171 if (exists $busy{$fromnode}) {
172 my $ref = $busy{$fromnode};
173 my $otonode = $ref->{tonode} || "unknown";
174 dbg("Busy, stopping msgno: $ref->{msgno} $fromnode->$otonode") if isdbg('msg');
175 $ref->stop_msg($fromnode);
178 my $t = cltounix($f[5], $f[6]);
179 $stream = next_transno($fromnode);
180 my $ref = DXMsg->alloc($stream, uc $f[3], $f[4], $t, $f[7], $f[8], $origin, '0', $f[11]);
182 # fill in various forwarding state variables
183 $ref->{fromnode} = $fromnode;
184 $ref->{tonode} = $tonode;
185 $ref->{rrreq} = $f[11];
186 $ref->{linesreq} = $f[10];
187 $ref->{stream} = $stream;
188 $ref->{count} = 0; # no of lines between PC31s
189 dbg("new message from $f[4] to $f[3] '$f[8]' stream $fromnode/$stream\n") if isdbg('msg');
190 Log('msg', "Incoming message $f[4] to $f[3] '$f[8]' origin: $origin" );
191 $work{"$fromnode,$stream"} = $ref; # store in work
192 $busy{$fromnode} = $ref; # set interlock
193 $self->send(DXProt::pc30($fromnode, $tonode, $stream)); # send ack
194 $ref->{lastt} = $main::systime;
196 # look to see whether this is a non private message sent to a known callsign
197 my $uref = DXUser->get_current($ref->{to});
198 if (is_callsign($ref->{to}) && !$ref->{private} && $uref && $uref->homenode) {
200 dbg("set bull to $ref->{to} to private") if isdbg('msg');
201 Log('msg', "set bull to $ref->{to} to private");
206 if ($pcno == 29) { # incoming text
207 my $ref = $work{"$fromnode,$stream"};
210 push @{$ref->{lines}}, $f[4];
212 if ($ref->{count} >= $ref->{linesreq}) {
213 $self->send(DXProt::pc31($fromnode, $tonode, $stream));
214 dbg("stream $stream: $ref->{count} lines received\n") if isdbg('msg');
217 $ref->{lastt} = $main::systime;
219 dbg("PC29 from unknown stream $stream from $fromnode") if isdbg('msg');
220 $self->send(DXProt::pc42($fromnode, $tonode, $stream)); # unknown stream
225 if ($pcno == 30) { # this is a incoming subject ack
226 my $ref = $work{"$fromnode,"}; # note no stream at this stage
228 delete $work{"$fromnode,"};
229 $ref->{stream} = $stream;
231 $ref->{linesreq} = 5;
232 $work{"$fromnode,$stream"} = $ref; # new ref
233 dbg("incoming subject ack stream $stream\n") if isdbg('msg');
234 $busy{$fromnode} = $ref; # interlock
235 push @{$ref->{lines}}, ($ref->read_msg_body);
236 $ref->send_tranche($self);
237 $ref->{lastt} = $main::systime;
239 dbg("PC30 from unknown stream $stream from $fromnode") if isdbg('msg');
240 $self->send(DXProt::pc42($fromnode, $tonode, $stream)); # unknown stream
245 if ($pcno == 31) { # acknowledge a tranche of lines
246 my $ref = $work{"$fromnode,$stream"};
248 dbg("tranche ack stream $stream\n") if isdbg('msg');
249 $ref->send_tranche($self);
250 $ref->{lastt} = $main::systime;
252 dbg("PC31 from unknown stream $stream from $fromnode") if isdbg('msg');
253 $self->send(DXProt::pc42($fromnode, $tonode, $stream)); # unknown stream
258 if ($pcno == 32) { # incoming EOM
259 dbg("stream $stream: EOM received\n") if isdbg('msg');
260 my $ref = $work{"$fromnode,$stream"};
262 $self->send(DXProt::pc33($fromnode, $tonode, $stream)); # acknowledge it
264 # get the next msg no - note that this has NOTHING to do with the stream number in PC protocol
265 # store the file or message
266 # remove extraneous rubbish from the hash
267 # remove it from the work in progress vector
268 # stuff it on the msg queue
271 $ref->store($ref->{lines});
274 # does an identical message already exist?
277 if ($ref->{subject} eq $m->{subject} && $ref->{t} == $m->{t} && $ref->{from} eq $m->{from} && $ref->{to} eq $m->{to}) {
278 $ref->stop_msg($fromnode);
279 my $msgno = $m->{msgno};
280 dbg("duplicate message from $ref->{from} -> $ref->{to} to msg: $msgno") if isdbg('msg');
281 Log('msg', "duplicate message from $ref->{from} -> $ref->{to} to msg: $msgno");
287 $ref->swop_it($self->call);
289 # look for 'bad' to addresses
290 if ($ref->dump_it($self->call)) {
291 $ref->stop_msg($fromnode);
292 dbg("'Bad' message $ref->{to}") if isdbg('msg');
293 Log('msg', "'Bad' message $ref->{to}");
297 # check the message for bad words
299 for (@{$ref->{lines}}) {
300 push @words, BadWords::check($_);
302 push @words, BadWords::check($ref->{subject});
304 dbg("message with badwords '@words' $ref->{from} -> $ref->{to} '$ref->{subject}' origin: $ref->{origin} via " . $self->call) if isdbg('msg');
305 Log('msg',"message with badwords '@words' $ref->{from} -> $ref->{to} origin: $ref->{origin} via " . $self->call);
306 Log('msg',"subject: $ref->{subject}");
307 for (@{$ref->{lines}}) {
308 Log('msg', "line: $_");
310 $ref->stop_msg($fromnode);
314 $ref->{msgno} = next_transno("Msgno");
315 push @{$ref->{gotit}}, $fromnode; # mark this up as being received
316 $ref->store($ref->{lines});
318 my $dxchan = DXChannel->get($ref->{to});
319 $dxchan->send($dxchan->msg('m9')) if $dxchan && $dxchan->is_user;
320 Log('msg', "Message $ref->{msgno} from $ref->{from} received from $fromnode for $ref->{to}");
323 $ref->stop_msg($fromnode);
325 dbg("PC32 from unknown stream $stream from $fromnode") if isdbg('msg');
326 $self->send(DXProt::pc42($fromnode, $tonode, $stream)); # unknown stream
332 if ($pcno == 33) { # acknowledge the end of message
333 my $ref = $work{"$fromnode,$stream"};
335 if ($ref->{private}) { # remove it if it private and gone off site#
336 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $fromnode and deleted");
339 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $fromnode");
340 push @{$ref->{gotit}}, $fromnode; # mark this up as being received
341 $ref->store($ref->{lines}); # re- store the file
343 $ref->stop_msg($fromnode);
345 dbg("PC33 from unknown stream $stream from $fromnode") if isdbg('msg');
346 $self->send(DXProt::pc42($fromnode, $tonode, $stream)); # unknown stream
349 # send next one if present
354 if ($pcno == 40) { # this is a file request
355 $f[3] =~ s/\\/\//og; # change the slashes
356 $f[3] =~ s/\.//og; # remove dots
357 $f[3] =~ s/^\///o; # remove the leading /
358 $f[3] = lc $f[3]; # to lower case;
359 dbg("incoming file $f[3]\n") if isdbg('msg');
360 $f[3] = 'packclus/' . $f[3] unless $f[3] =~ /^packclus\//o;
362 # create any directories
363 my @part = split /\//, $f[3];
365 my $fn = "$main::root";
366 pop @part; # remove last part
367 foreach $part (@part) {
370 last SWITCH if !mkdir $fn, 0777;
371 dbg("created directory $fn\n") if isdbg('msg');
373 my $stream = next_transno($fromnode);
374 my $ref = DXMsg->alloc($stream, "$main::root/$f[3]", $self->call, time, !$f[4], $f[3], ' ', '0', '0');
376 # forwarding variables
377 $ref->{fromnode} = $tonode;
378 $ref->{tonode} = $fromnode;
379 $ref->{linesreq} = $f[5];
380 $ref->{stream} = $stream;
381 $ref->{count} = 0; # no of lines between PC31s
383 $ref->{lastt} = $main::systime;
384 $work{"$fromnode,$stream"} = $ref; # store in work
385 $self->send(DXProt::pc30($fromnode, $tonode, $stream)); # send ack
390 if ($pcno == 42) { # abort transfer
391 dbg("stream $stream: abort received\n") if isdbg('msg');
392 my $ref = $work{"$fromnode,$stream"};
394 $ref->stop_msg($fromnode);
400 if ($pcno == 49) { # global delete on subject
402 if ($_->{from} eq $f[1] && $_->{subject} eq $f[2]) {
404 Log('msg', "Message $_->{msgno} from $_->{from} ($_->{subject}) fully deleted");
405 DXChannel::broadcast_nodes($line, $self);
413 # store a message away on disc or whatever
415 # NOTE the second arg is a REFERENCE not a list
421 if ($ref->{file}) { # a file
422 dbg("To be stored in $ref->{to}\n") if isdbg('msg');
424 my $fh = new IO::File "$ref->{to}", "w";
427 foreach $line (@{$lines}) {
431 dbg("file $ref->{to} stored\n") if isdbg('msg');
432 Log('msg', "file $ref->{to} from $ref->{from} stored" );
434 confess "can't open file $ref->{to} $!";
436 } else { # a normal message
438 # attempt to open the message file
439 my $fn = filename($ref->{msgno});
441 dbg("To be stored in $fn\n") if isdbg('msg');
443 # now save the file, overwriting what's there, YES I KNOW OK! (I will change it if it's a problem)
444 my $fh = new IO::File "$fn", "w";
446 my $rr = $ref->{rrreq} ? '1' : '0';
447 my $priv = $ref->{private} ? '1': '0';
448 print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$priv^$ref->{subject}^$ref->{origin}^$ref->{'read'}^$rr\n";
449 print $fh "=== ", join('^', @{$ref->{gotit}}), "\n";
452 foreach $line (@{$lines}) {
453 $ref->{size} += (length $line) + 1;
457 dbg("msg $ref->{msgno} stored\n") if isdbg('msg');
458 Log('msg', "msg $ref->{msgno} from $ref->{from} to $ref->{to} stored" );
460 confess "can't open msg file $fn $!";
470 # remove it from the active message list
471 dbg("\@msg = " . scalar @msg . " before delete") if isdbg('msg');
472 @msg = grep { $_ != $self } @msg;
475 unlink filename($self->{msgno});
476 dbg("deleting $self->{msgno}\n") if isdbg('msg');
477 dbg("\@msg = " . scalar @msg . " after delete") if isdbg('msg');
480 # clean out old messages from the message queue
485 # mark old messages for deletion
486 dbg("\@msg = " . scalar @msg . " before delete") if isdbg('msg');
487 foreach $ref (@msg) {
488 if (ref($ref) && !$ref->{keep} && $ref->{t} < $main::systime - $maxage) {
489 $ref->{deleteme} = 1;
490 unlink filename($ref->{msgno});
491 dbg("deleting old $ref->{msgno}\n") if isdbg('msg');
495 # remove them all from the active message list
496 @msg = grep { !$_->{deleteme} } @msg;
497 dbg("\@msg = " . scalar @msg . " after delete") if isdbg('msg');
498 $last_clean = $main::systime;
501 # read in a message header
511 $file = new IO::File "$fn";
513 dbg("Error reading $fn $!");
514 Log('err', "Error reading $fn $!");
518 $line = <$file>; # first line
519 if ($size == 0 || !$line) {
521 Log('err', "Empty $fn $!");
525 $size -= length $line;
526 if (! $line =~ /^===/o) {
527 dbg("corrupt first line in $fn ($line)");
528 Log('err', "corrupt first line in $fn ($line)");
532 @f = split /\^/, $line;
533 $ref = DXMsg->alloc(@f);
535 $line = <$file>; # second line
537 $size -= length $line;
538 if (! $line =~ /^===/o) {
539 dbg("corrupt second line in $fn ($line)");
540 Log('err', "corrupt second line in $fn ($line)");
545 @f = split /\^/, $line;
546 push @{$ref->{gotit}}, @f;
547 $ref->{size} = $size;
554 # read in a message header
558 my $msgno = $self->{msgno};
561 my $fn = filename($msgno);
564 $file = new IO::File;
565 if (!open($file, $fn)) {
566 dbg("Error reading $fn $!");
567 Log('err' ,"Error reading $fn $!");
570 @out = map {chomp; $_} <$file>;
573 shift @out if $out[0] =~ /^=== /;
574 shift @out if $out[0] =~ /^=== /;
578 # send a tranche of lines to the other end
581 my ($self, $dxchan) = @_;
583 my $to = $self->{tonode};
584 my $from = $self->{fromnode};
585 my $stream = $self->{stream};
586 my $lines = $self->{lines};
589 for ($i = 0, $c = $self->{count}; $i < $self->{linesreq} && $c < @$lines; $i++, $c++) {
590 push @out, DXProt::pc29($to, $from, $stream, $lines->[$c]);
594 push @out, DXProt::pc32($to, $from, $stream) if $i < $self->{linesreq};
599 # find a message to send out and start the ball rolling
606 # bat down the message list looking for one that needs to go off site and whose
607 # nearest node is not busy.
609 dbg("queue msg ($sort)\n") if isdbg('msg');
610 my @nodelist = DXChannel::get_all_nodes;
611 foreach $ref (@msg) {
613 # ignore 'delayed' messages until their waiting time has expired
614 if (exists $ref->{waitt}) {
615 next if $ref->{waitt} > $main::systime;
616 delete $ref->{waitt};
620 if (exists $ref->{lastt} && $main::systime >= $ref->{lastt} + $timeout) {
621 my $node = $ref->{tonode};
622 dbg("Timeout, stopping msgno: $ref->{msgno} -> $node") if isdbg('msg');
623 Log('msg', "Timeout, stopping msgno: $ref->{msgno} -> $node");
624 $ref->stop_msg($node);
626 # delay any outgoing messages that fail
627 $ref->{waitt} = $main::systime + $waittime + rand(120) if $node ne $main::mycall;
628 delete $ref->{lastt};
632 # firstly, is it private and unread? if so can I find the recipient
633 # in my cluster node list offsite?
635 # deal with routed private messages
637 if ($ref->{private}) {
638 next if $ref->{'read'}; # if it is read, it is stuck here
639 $clref = Route::get($ref->{to});
640 # unless ($clref) { # otherwise look for a homenode
641 # my $uref = DXUser->get_current($ref->{to});
642 # my $hnode = $uref->homenode if $uref;
643 # $clref = Route::Node::get($hnode) if $hnode;
646 $dxchan = $clref->dxchan;
648 if ($dxchan->is_node) {
649 next if $clref->call eq $main::mycall; # i.e. it lives here
650 $ref->start_msg($dxchan) if !get_busy($dxchan->call) && $dxchan->state eq 'normal';
653 dbg("Route: No dxchan for $ref->{to} " . ref($clref) ) if isdbg('msg');
658 # otherwise we are dealing with a bulletin or forwarded private message
659 # compare the gotit list with
660 # the nodelist up above, if there are sites that haven't got it yet
661 # then start sending it - what happens when we get loops is anyone's
662 # guess, use (to, from, time, subject) tuple?
663 foreach $dxchan (@nodelist) {
664 my $call = $dxchan->call;
666 next if $call eq $main::mycall;
667 next if ref $ref->{gotit} && grep $_ eq $call, @{$ref->{gotit}};
668 next unless $ref->forward_it($call); # check the forwarding file
670 # if we are here we have a node that doesn't have this message
671 if (!get_busy($call) && $dxchan->state eq 'normal') {
672 $ref->start_msg($dxchan);
678 # if all the available nodes are busy then stop
679 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
683 # is there a message for me?
689 foreach $ref (@msg) {
690 # is it for me, private and unread?
691 if ($ref->{to} eq $call && $ref->{private}) {
692 return 1 if !$ref->{'read'};
698 # start the message off on its travels with a PC28
701 my ($self, $dxchan) = @_;
703 dbg("start msg $self->{msgno}\n") if isdbg('msg');
704 $self->{linesreq} = 10;
706 $self->{tonode} = $dxchan->call;
707 $self->{fromnode} = $main::mycall;
708 $busy{$self->{tonode}} = $self;
709 $work{"$self->{tonode},"} = $self;
710 $self->{lastt} = $main::systime;
711 my ($fromnode, $origin);
712 $fromnode = $self->{fromnode};
713 $origin = $self->{origin};
714 $dxchan->send(DXProt::pc28($self->{tonode}, $fromnode, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $origin, $self->{rrreq}));
717 # get the ref of a busy node
730 # get the forwarding queue
736 # stop a message from continuing, clean it out, unlock interlocks etc
741 my $stream = $self->{stream} if exists $self->{stream};
744 dbg("stop msg $self->{msgno} -> node $node\n") if isdbg('msg');
745 delete $work{"$node,"};
746 delete $work{"$node,$stream"} if $stream;
751 # get a new transaction number from the file specified
755 $name =~ s/\W//og; # remove non-word characters
756 my $fn = "$msgdir/$name";
759 my $fh = new IO::File;
760 if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
762 $msgno = $fh->getline || '0';
766 $fh->print("$msgno\n");
767 dbg("msgno $msgno allocated for $name\n") if isdbg('msg');
770 confess "can't open $fn $!";
775 # initialise the message 'system', read in all the message headers
778 my $dir = new IO::File;
782 # load various control files
783 dbg("load badmsg: " . (load_badmsg() or "Ok"));
784 dbg("load forward: " . (load_forward() or "Ok"));
785 dbg("load swop: " . (load_swop() or "Ok"));
787 # read in the directory
788 opendir($dir, $msgdir) or confess "can't open $msgdir $!";
789 @dir = readdir($dir);
794 next unless /^m\d\d\d\d\d\d$/;
796 $ref = read_msg_header("$msgdir/$_");
799 Log('err', "Deleting $_");
804 # delete any messages to 'badmsg.pl' places
805 if ($ref->dump_it('')) {
806 dbg("'Bad' TO address $ref->{to}") if isdbg('msg');
807 Log('msg', "'Bad' TO address $ref->{to}");
812 # add the message to the available queue
817 # add the message to the directory listing
821 confess "tried to add a non-ref to the msg directory" if !ref $ref;
825 # return all the current messages
831 # get a particular message
836 return $_ if $_->{msgno} == $msgno;
837 last if $_->{msgno} > $msgno;
842 # return the official filename for a message no
845 return sprintf "$msgdir/m%06d", shift;
849 # return a list of valid elements
858 # return a prompt for a field
863 my ($self, $ele) = @_;
868 # send a message state machine
875 if ($self->state eq 'send1') {
877 confess "local var gone missing" if !ref $self->{loc};
878 my $loc = $self->{loc};
879 if (my @ans = BadWords::check($line)) {
880 $self->{badcount} += @ans;
881 Log('msg', $self->call . " used badwords: @ans to @{$loc->{to}} in msg");
882 return ($self->msg('e17', @ans), $self->msg('m1'));
884 $loc->{subject} = $line;
886 $self->state('sendbody');
887 #push @out, $self->msg('sendbody');
888 push @out, $self->msg('m8');
889 } elsif ($self->state eq 'sendbody') {
890 confess "local var gone missing" if !ref $self->{loc};
891 my $loc = $self->{loc};
892 if ($line eq "\032" || $line eq '%1A' || uc $line eq "/EX") {
895 foreach $to (@{$loc->{to}}) {
897 my $systime = $main::systime;
898 my $mycall = $main::mycall;
899 $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
901 exists $loc->{from} ? $loc->{from} : $self->call,
905 exists $loc->{origin} ? $loc->{origin} : $mycall,
908 $ref->swop_it($self->call);
909 $ref->store($loc->{lines});
911 push @out, $self->msg('m11', $ref->{msgno}, $to);
912 #push @out, "msgno $ref->{msgno} sent to $to";
913 my $dxchan = DXChannel->get(uc $to);
915 if ($dxchan->is_user()) {
916 $dxchan->send($dxchan->msg('m9'));
921 delete $loc->{lines};
926 $self->state('prompt');
927 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
928 #push @out, $self->msg('sendabort');
929 push @out, $self->msg('m10');
930 delete $loc->{lines};
934 $self->state('prompt');
936 if (my @ans = BadWords::check($line)) {
937 $self->{badcount} += @ans;
938 Log('msg', $self->call . " used badwords: @ans to @{$loc->{to}} subject: '$loc->{subject}' in msg");
939 Log('msg', "line: $line");
940 return ($self->msg('e17', @ans));
943 # i.e. it ain't and end or abort, therefore store the line
944 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
950 # return the standard directory line for this ref
954 return sprintf "%6d%s%s%5d %8.8s %8.8s %-6.6s %5.5s %-30.30s",
955 $ref->msgno, $ref->read ? '-' : ' ', $ref->private ? 'p' : ' ', $ref->size,
956 $ref->to, $ref->from, cldate($ref->t), ztime($ref->t), $ref->subject;
959 # load the forward table
963 my $s = readfilestr($forwardfn);
971 # load the bad message table
975 my $s = readfilestr($badmsgfn);
983 # load the swop message table
987 my $s = readfilestr($swopfn);
996 # forward that message or not according to the forwarding table
997 # returns 1 for forward, 0 - to ignore
1006 for ($i = 0; $i < @forward; $i += 5) {
1007 my ($sort, $field, $pattern, $action, $bbs) = @forward[$i..($i+4)];
1010 # are we interested?
1011 next if $ref->{private} && $sort ne 'P';
1012 next if !$ref->{private} && $sort ne 'B';
1015 $tested = $ref->{to} if $field eq 'T';
1016 $tested = $ref->{from} if $field eq 'F';
1017 $tested = $ref->{origin} if $field eq 'O';
1018 $tested = $ref->{subject} if $field eq 'S';
1020 if (!$pattern || $tested =~ m{$pattern}i) {
1021 return 0 if $action eq 'I';
1022 return 1 if !$bbs || grep $_ eq $call, @{$bbs};
1034 for ($i = 0; $i < @badmsg; $i += 3) {
1035 my ($sort, $field, $pattern) = @badmsg[$i..($i+2)];
1038 # are we interested?
1039 next if $ref->{private} && $sort ne 'P';
1040 next if !$ref->{private} && $sort ne 'B';
1043 $tested = $ref->{to} if $field eq 'T';
1044 $tested = $ref->{from} if $field eq 'F';
1045 $tested = $ref->{origin} if $field eq 'O';
1046 $tested = $ref->{subject} if $field eq 'S';
1047 $tested = $call if $field eq 'I';
1049 if (!$pattern || $tested =~ m{$pattern}i) {
1063 for ($i = 0; $i < @swop; $i += 5) {
1064 my ($sort, $field, $pattern, $tfield, $topattern) = @swop[$i..($i+4)];
1069 # are we interested?
1070 next if $ref->{private} && $sort ne 'P';
1071 next if !$ref->{private} && $sort ne 'B';
1074 $tested = $ref->{to} if $field eq 'T';
1075 $tested = $ref->{from} if $field eq 'F';
1076 $tested = $ref->{origin} if $field eq 'O';
1077 $tested = $ref->{subject} if $field eq 'S';
1080 $old = $swop = $ref->{to} if $tfield eq 'T';
1081 $old = $swop = $ref->{from} if $tfield eq 'F';
1082 $old = $swop = $ref->{origin} if $tfield eq 'O';
1083 $old = $swop = $ref->{subject} if $tfield eq 'S';
1085 if ($tested =~ m{$pattern}i) {
1086 if ($tested eq $swop) {
1087 $swop =~ s{$pattern}{$topattern}i;
1091 Log('msg', "Msg $ref->{msgno}: $tfield $old -> $swop");
1092 Log('dbg', "Msg $ref->{msgno}: $tfield $old -> $swop");
1093 $ref->{to} = $swop if $tfield eq 'T';
1094 $ref->{from} = $swop if $tfield eq 'F';
1095 $ref->{origin} = $swop if $tfield eq 'O';
1096 $ref->{subject} = $swop if $tfield eq 'S';
1103 # import any msgs in the import directory
1104 # the messages are in BBS format (but may have cluster extentions
1105 # so SB UK < GB7TLH is legal
1108 # are there any to do in this directory?
1109 return unless -d $importfn;
1110 unless (opendir(DIR, $importfn)) {
1111 dbg("can\'t open $importfn $!") if isdbg('msg');
1112 Log('msg', "can\'t open $importfn $!");
1116 my @names = readdir(DIR);
1119 foreach $name (@names) {
1120 next if $name =~ /^\./;
1121 my $splitit = $name =~ /^split/;
1122 my $fn = "$importfn/$name";
1124 unless (open(MSG, $fn)) {
1125 dbg("can\'t open import file $fn $!") if isdbg('msg');
1126 Log('msg', "can\'t open import file $fn $!");
1130 my @msg = map { chomp; $_ } <MSG>;
1133 my @out = import_one($main::me, \@msg, $splitit);
1138 # import one message as a list in bbs (as extended) mode
1139 # takes a reference to an array containing the whole message
1144 my $splitit = shift;
1148 my $from = $dxchan->call;
1149 my $origin = $main::mycall;
1154 my $line = shift @$ref;
1155 my @f = split /\s+/, $line;
1156 unless (@f && $f[0] =~ /^(:?S|SP|SB|SEND)$/ ) {
1157 my $m = "invalid first line in import '$line'";
1158 dbg($m) if isdbg('msg');
1162 my $f = uc shift @f;
1163 next if $f eq 'SEND';
1165 # private / noprivate / rr
1166 if ($notincalls && ($f eq 'B' || $f eq 'SB' || $f =~ /^NOP/oi)) {
1168 } elsif ($notincalls && ($f eq 'P' || $f eq 'SP' || $f =~ /^PRI/oi)) {
1170 } elsif ($notincalls && ($f eq 'RR')) {
1172 } elsif ($f eq '@' && @f) { # this is bbs syntax, for origin
1173 $origin = uc shift @f;
1174 } elsif ($f eq '<' && @f) { # this is bbs syntax for from call
1175 $from = uc shift @f;
1176 } elsif ($f =~ /^\$/) { # this is bbs syntax for a bid
1178 } elsif ($f =~ /^<\S+/) { # this is bbs syntax for from call
1179 ($from) = $f =~ /^<(\S+)$/;
1180 } elsif ($f =~ /^\@\S+/) { # this is bbs syntax for origin
1181 ($origin) = $f =~ /^\@(\S+)$/;
1187 # is this callsign a distro?
1188 my $fn = "$msgdir/distro/$f.pl";
1190 my $fh = new IO::File $fn;
1197 return (1, "Error in Distro $f.pl:", $@) if $@;
1205 if (grep $_ eq $f, @DXMsg::badmsg) {
1206 push @out, $dxchan->msg('m3', $f);
1213 # subject is the next line
1214 my $subject = shift @$ref;
1216 # strip off trailing lines
1217 pop @$ref while (@$ref && $$ref[-1] =~ /^\s*$/);
1219 # strip off /EX or /ABORT
1220 return ("aborted") if @$ref && $$ref[-1] =~ m{^/ABORT$}i;
1221 pop @$ref if (@$ref && $$ref[-1] =~ m{^/EX$}i);
1223 # sort out any splitting that needs to be done
1229 if ($lth >= $maxchunk || ($lth > $minchunk && /^\s*$/)) {
1230 push @chunk, $lines;
1237 push @chunk, $lines if @$lines;
1242 # write all the messages away
1244 for ( $i = 0; $i < @chunk; $i++) {
1245 my $chunk = $chunk[$i];
1248 my $num = " [" . ($i+1) . "/" . scalar @chunk . "]";
1249 $ch_subject = substr($subject, 0, 27 - length $num) . $num;
1251 $ch_subject = $subject;
1255 my $systime = $main::systime;
1256 my $mycall = $main::mycall;
1257 my $mref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
1266 $mref->swop_it($main::mycall);
1267 $mref->store($chunk);
1269 push @out, $dxchan->msg('m11', $mref->{msgno}, $to);
1270 #push @out, "msgno $ref->{msgno} sent to $to";
1271 my $todxchan = DXChannel->get(uc $to);
1273 if ($todxchan->is_user()) {
1274 $todxchan->send($todxchan->msg('m9'));
1286 my $name = $AUTOLOAD;
1287 return if $name =~ /::DESTROY$/;
1290 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
1291 # this clever line of code creates a subroutine which takes over from autoload
1292 # from OO Perl - Conway
1293 *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
1294 @_ ? $self->{$name} = shift : $self->{$name} ;