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 next if $ref->{tonode}; # ignore it if it already being processed
640 $clref = Route::get($ref->{to});
642 $dxchan = $clref->dxchan;
644 if ($dxchan->is_node) {
645 next if $clref->call eq $main::mycall; # i.e. it lives here
646 $ref->start_msg($dxchan) if !get_busy($dxchan->call) && $dxchan->state eq 'normal';
649 dbg("Route: No dxchan for $ref->{to} " . ref($clref) ) if isdbg('msg');
654 # otherwise we are dealing with a bulletin or forwarded private message
655 # compare the gotit list with
656 # the nodelist up above, if there are sites that haven't got it yet
657 # then start sending it - what happens when we get loops is anyone's
658 # guess, use (to, from, time, subject) tuple?
659 foreach $dxchan (@nodelist) {
660 my $call = $dxchan->call;
662 next if $call eq $main::mycall;
663 next if ref $ref->{gotit} && grep $_ eq $call, @{$ref->{gotit}};
664 next unless $ref->forward_it($call); # check the forwarding file
665 next if $ref->{tonode}; # ignore it if it already being processed
667 # if we are here we have a node that doesn't have this message
668 if (!get_busy($call) && $dxchan->state eq 'normal') {
669 $ref->start_msg($dxchan);
675 # if all the available nodes are busy then stop
676 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
680 # is there a message for me?
686 foreach $ref (@msg) {
687 # is it for me, private and unread?
688 if ($ref->{to} eq $call && $ref->{private}) {
689 return 1 if !$ref->{'read'};
695 # start the message off on its travels with a PC28
698 my ($self, $dxchan) = @_;
700 confess("trying to start started msg $self->{msgno} nodes: $self->{fromnode} -> $self->{tonode}") if $self->{tonode};
701 dbg("start msg $self->{msgno}\n") if isdbg('msg');
702 $self->{linesreq} = 10;
704 $self->{tonode} = $dxchan->call;
705 $self->{fromnode} = $main::mycall;
706 $busy{$self->{tonode}} = $self;
707 $work{"$self->{tonode},"} = $self;
708 $self->{lastt} = $main::systime;
709 my ($fromnode, $origin);
710 $fromnode = $self->{fromnode};
711 $origin = $self->{origin};
712 $dxchan->send(DXProt::pc28($self->{tonode}, $fromnode, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $origin, $self->{rrreq}));
715 # get the ref of a busy node
728 # get a forwarding queue entry
732 my $stream = shift || '0';
733 return $work{"$call,$stream"};
736 # delete a forwarding queue entry
740 my $stream = shift || '0';
741 return delete $work{"$call,$stream"};
748 my $stream = shift || '0';
749 return $work{"$call,$stream"} = shift;
752 # get the whole forwarding queue
758 # stop a message from continuing, clean it out, unlock interlocks etc
763 my $stream = $self->{stream} if exists $self->{stream};
766 dbg("stop msg $self->{msgno} -> node $node\n") if isdbg('msg');
767 delete $work{"$node,"};
768 delete $work{"$node,$stream"} if $stream;
773 # get a new transaction number from the file specified
777 $name =~ s/\W//og; # remove non-word characters
778 my $fn = "$msgdir/$name";
781 my $fh = new IO::File;
782 if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
784 $msgno = $fh->getline || '0';
788 $fh->print("$msgno\n");
789 dbg("msgno $msgno allocated for $name\n") if isdbg('msg');
792 confess "can't open $fn $!";
797 # initialise the message 'system', read in all the message headers
800 my $dir = new IO::File;
804 # load various control files
805 dbg("load badmsg: " . (load_badmsg() or "Ok"));
806 dbg("load forward: " . (load_forward() or "Ok"));
807 dbg("load swop: " . (load_swop() or "Ok"));
809 # read in the directory
810 opendir($dir, $msgdir) or confess "can't open $msgdir $!";
811 @dir = readdir($dir);
816 next unless /^m\d\d\d\d\d\d$/;
818 $ref = read_msg_header("$msgdir/$_");
821 Log('err', "Deleting $_");
826 # delete any messages to 'badmsg.pl' places
827 if ($ref->dump_it('')) {
828 dbg("'Bad' TO address $ref->{to}") if isdbg('msg');
829 Log('msg', "'Bad' TO address $ref->{to}");
834 # add the message to the available queue
839 # add the message to the directory listing
843 confess "tried to add a non-ref to the msg directory" if !ref $ref;
847 # return all the current messages
853 # get a particular message
858 return $_ if $_->{msgno} == $msgno;
859 last if $_->{msgno} > $msgno;
864 # return the official filename for a message no
867 return sprintf "$msgdir/m%06d", shift;
871 # return a list of valid elements
880 # return a prompt for a field
885 my ($self, $ele) = @_;
890 # send a message state machine
897 if ($self->state eq 'send1') {
899 confess "local var gone missing" if !ref $self->{loc};
900 my $loc = $self->{loc};
901 if (my @ans = BadWords::check($line)) {
902 $self->{badcount} += @ans;
903 Log('msg', $self->call . " used badwords: @ans to @{$loc->{to}} in msg");
904 return ($self->msg('e17', @ans), $self->msg('m1'));
906 $loc->{subject} = $line;
908 $self->state('sendbody');
909 #push @out, $self->msg('sendbody');
910 push @out, $self->msg('m8');
911 } elsif ($self->state eq 'sendbody') {
912 confess "local var gone missing" if !ref $self->{loc};
913 my $loc = $self->{loc};
914 if ($line eq "\032" || $line eq '%1A' || uc $line eq "/EX") {
917 foreach $to (@{$loc->{to}}) {
919 my $systime = $main::systime;
920 my $mycall = $main::mycall;
921 $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
923 exists $loc->{from} ? $loc->{from} : $self->call,
927 exists $loc->{origin} ? $loc->{origin} : $mycall,
930 $ref->swop_it($self->call);
931 $ref->store($loc->{lines});
933 push @out, $self->msg('m11', $ref->{msgno}, $to);
934 #push @out, "msgno $ref->{msgno} sent to $to";
935 my $dxchan = DXChannel->get(uc $to);
937 if ($dxchan->is_user()) {
938 $dxchan->send($dxchan->msg('m9'));
943 delete $loc->{lines};
948 $self->state('prompt');
949 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
950 #push @out, $self->msg('sendabort');
951 push @out, $self->msg('m10');
952 delete $loc->{lines};
956 $self->state('prompt');
958 if (my @ans = BadWords::check($line)) {
959 $self->{badcount} += @ans;
960 Log('msg', $self->call . " used badwords: @ans to @{$loc->{to}} subject: '$loc->{subject}' in msg");
961 Log('msg', "line: $line");
962 return ($self->msg('e17', @ans));
965 # i.e. it ain't and end or abort, therefore store the line
966 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
972 # return the standard directory line for this ref
976 return sprintf "%6d%s%s%5d %8.8s %8.8s %-6.6s %5.5s %-30.30s",
977 $ref->msgno, $ref->read ? '-' : ' ', $ref->private ? 'p' : ' ', $ref->size,
978 $ref->to, $ref->from, cldate($ref->t), ztime($ref->t), $ref->subject;
981 # load the forward table
985 my $s = readfilestr($forwardfn);
993 # load the bad message table
997 my $s = readfilestr($badmsgfn);
1000 push @out, $@ if $@;
1005 # load the swop message table
1009 my $s = readfilestr($swopfn);
1012 push @out, $@ if $@;
1018 # forward that message or not according to the forwarding table
1019 # returns 1 for forward, 0 - to ignore
1028 for ($i = 0; $i < @forward; $i += 5) {
1029 my ($sort, $field, $pattern, $action, $bbs) = @forward[$i..($i+4)];
1032 # are we interested?
1033 next if $ref->{private} && $sort ne 'P';
1034 next if !$ref->{private} && $sort ne 'B';
1037 $tested = $ref->{to} if $field eq 'T';
1038 $tested = $ref->{from} if $field eq 'F';
1039 $tested = $ref->{origin} if $field eq 'O';
1040 $tested = $ref->{subject} if $field eq 'S';
1042 if (!$pattern || $tested =~ m{$pattern}i) {
1043 return 0 if $action eq 'I';
1044 return 1 if !$bbs || grep $_ eq $call, @{$bbs};
1056 for ($i = 0; $i < @badmsg; $i += 3) {
1057 my ($sort, $field, $pattern) = @badmsg[$i..($i+2)];
1060 # are we interested?
1061 next if $ref->{private} && $sort ne 'P';
1062 next if !$ref->{private} && $sort ne 'B';
1065 $tested = $ref->{to} if $field eq 'T';
1066 $tested = $ref->{from} if $field eq 'F';
1067 $tested = $ref->{origin} if $field eq 'O';
1068 $tested = $ref->{subject} if $field eq 'S';
1069 $tested = $call if $field eq 'I';
1071 if (!$pattern || $tested =~ m{$pattern}i) {
1085 for ($i = 0; $i < @swop; $i += 5) {
1086 my ($sort, $field, $pattern, $tfield, $topattern) = @swop[$i..($i+4)];
1091 # are we interested?
1092 next if $ref->{private} && $sort ne 'P';
1093 next if !$ref->{private} && $sort ne 'B';
1096 $tested = $ref->{to} if $field eq 'T';
1097 $tested = $ref->{from} if $field eq 'F';
1098 $tested = $ref->{origin} if $field eq 'O';
1099 $tested = $ref->{subject} if $field eq 'S';
1102 $old = $swop = $ref->{to} if $tfield eq 'T';
1103 $old = $swop = $ref->{from} if $tfield eq 'F';
1104 $old = $swop = $ref->{origin} if $tfield eq 'O';
1105 $old = $swop = $ref->{subject} if $tfield eq 'S';
1107 if ($tested =~ m{$pattern}i) {
1108 if ($tested eq $swop) {
1109 $swop =~ s{$pattern}{$topattern}i;
1113 Log('msg', "Msg $ref->{msgno}: $tfield $old -> $swop");
1114 Log('dbg', "Msg $ref->{msgno}: $tfield $old -> $swop");
1115 $ref->{to} = $swop if $tfield eq 'T';
1116 $ref->{from} = $swop if $tfield eq 'F';
1117 $ref->{origin} = $swop if $tfield eq 'O';
1118 $ref->{subject} = $swop if $tfield eq 'S';
1125 # import any msgs in the import directory
1126 # the messages are in BBS format (but may have cluster extentions
1127 # so SB UK < GB7TLH is legal
1130 # are there any to do in this directory?
1131 return unless -d $importfn;
1132 unless (opendir(DIR, $importfn)) {
1133 dbg("can\'t open $importfn $!") if isdbg('msg');
1134 Log('msg', "can\'t open $importfn $!");
1138 my @names = readdir(DIR);
1141 foreach $name (@names) {
1142 next if $name =~ /^\./;
1143 my $splitit = $name =~ /^split/;
1144 my $fn = "$importfn/$name";
1146 unless (open(MSG, $fn)) {
1147 dbg("can\'t open import file $fn $!") if isdbg('msg');
1148 Log('msg', "can\'t open import file $fn $!");
1152 my @msg = map { chomp; $_ } <MSG>;
1155 my @out = import_one($main::me, \@msg, $splitit);
1160 # import one message as a list in bbs (as extended) mode
1161 # takes a reference to an array containing the whole message
1166 my $splitit = shift;
1170 my $from = $dxchan->call;
1171 my $origin = $main::mycall;
1176 my $line = shift @$ref;
1177 my @f = split /\s+/, $line;
1178 unless (@f && $f[0] =~ /^(:?S|SP|SB|SEND)$/ ) {
1179 my $m = "invalid first line in import '$line'";
1180 dbg($m) if isdbg('msg');
1184 my $f = uc shift @f;
1185 next if $f eq 'SEND';
1187 # private / noprivate / rr
1188 if ($notincalls && ($f eq 'B' || $f eq 'SB' || $f =~ /^NOP/oi)) {
1190 } elsif ($notincalls && ($f eq 'P' || $f eq 'SP' || $f =~ /^PRI/oi)) {
1192 } elsif ($notincalls && ($f eq 'RR')) {
1194 } elsif ($f eq '@' && @f) { # this is bbs syntax, for origin
1195 $origin = uc shift @f;
1196 } elsif ($f eq '<' && @f) { # this is bbs syntax for from call
1197 $from = uc shift @f;
1198 } elsif ($f =~ /^\$/) { # this is bbs syntax for a bid
1200 } elsif ($f =~ /^<\S+/) { # this is bbs syntax for from call
1201 ($from) = $f =~ /^<(\S+)$/;
1202 } elsif ($f =~ /^\@\S+/) { # this is bbs syntax for origin
1203 ($origin) = $f =~ /^\@(\S+)$/;
1209 # is this callsign a distro?
1210 my $fn = "$msgdir/distro/$f.pl";
1212 my $fh = new IO::File $fn;
1219 return (1, "Error in Distro $f.pl:", $@) if $@;
1227 if (grep $_ eq $f, @DXMsg::badmsg) {
1228 push @out, $dxchan->msg('m3', $f);
1235 # subject is the next line
1236 my $subject = shift @$ref;
1238 # strip off trailing lines
1239 pop @$ref while (@$ref && $$ref[-1] =~ /^\s*$/);
1241 # strip off /EX or /ABORT
1242 return ("aborted") if @$ref && $$ref[-1] =~ m{^/ABORT$}i;
1243 pop @$ref if (@$ref && $$ref[-1] =~ m{^/EX$}i);
1245 # sort out any splitting that needs to be done
1251 if ($lth >= $maxchunk || ($lth > $minchunk && /^\s*$/)) {
1252 push @chunk, $lines;
1259 push @chunk, $lines if @$lines;
1264 # write all the messages away
1266 for ( $i = 0; $i < @chunk; $i++) {
1267 my $chunk = $chunk[$i];
1270 my $num = " [" . ($i+1) . "/" . scalar @chunk . "]";
1271 $ch_subject = substr($subject, 0, 27 - length $num) . $num;
1273 $ch_subject = $subject;
1277 my $systime = $main::systime;
1278 my $mycall = $main::mycall;
1279 my $mref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
1288 $mref->swop_it($main::mycall);
1289 $mref->store($chunk);
1291 push @out, $dxchan->msg('m11', $mref->{msgno}, $to);
1292 #push @out, "msgno $ref->{msgno} sent to $to";
1293 my $todxchan = DXChannel->get(uc $to);
1295 if ($todxchan->is_user()) {
1296 $todxchan->send($todxchan->msg('m9'));
1308 my $name = $AUTOLOAD;
1309 return if $name =~ /::DESTROY$/;
1312 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
1313 # this clever line of code creates a subroutine which takes over from autoload
1314 # from OO Perl - Conway
1315 *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
1316 @_ ? $self->{$name} = shift : $self->{$name} ;