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)
35 use vars qw($VERSION $BRANCH);
36 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
37 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ ) || 0;
38 $main::build += $VERSION;
39 $main::branch += $BRANCH;
41 use vars qw(%work @msg $msgdir %valid %busy $maxage $last_clean $residencetime
42 @badmsg @swop $swopfn $badmsgfn $forwardfn @forward $timeout $waittime
43 $queueinterval $lastq $importfn $minchunk $maxchunk $bulltopriv);
45 %work = (); # outstanding jobs
46 @msg = (); # messages we have
47 %busy = (); # station interlocks
48 $msgdir = "$main::root/msg"; # directory contain the msgs
49 $maxage = 30 * 86400; # the maximum age that a message shall live for if not marked
50 $last_clean = 0; # last time we did a clean
51 @forward = (); # msg forward table
52 @badmsg = (); # bad message table
53 @swop = (); # swop table
54 $timeout = 30*60; # forwarding timeout
55 $waittime = 30*60; # time an aborted outgoing message waits before trying again
56 $queueinterval = 1*60; # run the queue every 1 minute
59 $minchunk = 4800; # minimum chunk size for a split message
60 $maxchunk = 6000; # maximum chunk size
61 $bulltopriv = 1; # convert msgs with callsigns to private if they are bulls
62 $residencetime = 2*86400; # keep deleted messages for this amount of time
65 $badmsgfn = "$msgdir/badmsg.pl"; # list of TO address we wont store
66 $forwardfn = "$msgdir/forward.pl"; # the forwarding table
67 $swopfn = "$msgdir/swop.pl"; # the swopping table
68 $importfn = "$msgdir/import"; # import directory
72 fromnode => '5,From Node',
73 tonode => '5,To Node',
76 t => '0,Msg Time,cldatetime',
77 private => '5,Private,yesno',
78 subject => '0,Subject',
79 linesreq => '0,Lines per Gob',
80 rrreq => '5,Read Confirm,yesno',
83 stream => '9,Stream No',
84 count => '5,Gob Linecnt',
85 file => '5,File?,yesno',
86 gotit => '5,Got it Nodes,parray',
87 lines => '5,Lines,parray',
88 'read' => '5,Times read',
91 keep => '0,Keep this?,yesno',
92 lastt => '5,Last processed,cldatetime',
93 waitt => '5,Wait until,cldatetime',
94 delete => '5,Awaiting Delete,yesno',
95 deletetime => '5,Deletion Time,cldatetime',
98 # allocate a new object
99 # called fromnode, tonode, from, to, datetime, private?, subject, nolinesper
103 my $self = bless {}, $pkg;
104 $self->{msgno} = shift;
107 $self->{to} = ($to eq $main::mycall) ? $main::myalias : $to;
110 $self->{from} = uc $from;
112 $self->{private} = shift;
113 $self->{subject} = shift;
114 $self->{origin} = shift;
115 $self->{'read'} = shift;
116 $self->{rrreq} = shift;
117 $self->{delete} = shift;
118 $self->{deletetime} = shift;
120 # $self->{lastt} = $main::systime;
122 $self->{private} = 1 if $bulltopriv && DXUser->get_current($self->{to});
130 my ($self, $line) = @_;
132 # this is periodic processing
133 if (!$self || !$line) {
135 if ($main::systime >= $lastq + $queueinterval) {
137 # queue some message if the interval timer has gone off
140 # import any messages in the import directory
143 $lastq = $main::systime;
146 # clean the message queue
147 clean_old() if $main::systime - $last_clean > 3600 ;
148 $last_clean = $main::systime;
152 my @f = split /\^/, $line;
153 my ($pcno) = $f[0] =~ /^PC(\d\d)/; # just get the number
154 my ($tonode, $fromnode) = @f[1, 2];
155 my $stream = $f[3] if ($pcno >= 29 && $pcno <= 33) || $pcno == 42;
158 if ($pcno == 28) { # incoming message
160 # sort out various extant protocol errors that occur
162 $origin = $self->call unless $origin && $origin gt ' ';
164 # first look for any messages in the busy queue
165 # and cancel them this should both resolve timed out incoming messages
166 # and crossing of message between nodes, incoming messages have priority
168 my $ref = get_busy($fromnode);
170 my $otonode = $ref->{tonode} || "unknown";
171 dbg("Busy, stopping msgno: $ref->{msgno} $fromnode->$otonode") if isdbg('msg');
172 $ref->stop_msg($fromnode);
175 my $t = cltounix($f[5], $f[6]);
176 $stream = next_transno($fromnode);
177 $ref = DXMsg->alloc($stream, uc $f[3], $f[4], $t, $f[7], $f[8], $origin, '0', $f[11]);
179 # fill in various forwarding state variables
180 $ref->{fromnode} = $fromnode;
181 $ref->{tonode} = $tonode;
182 $ref->{rrreq} = $f[11];
183 $ref->{linesreq} = $f[10];
184 $ref->{stream} = $stream;
185 $ref->{count} = 0; # no of lines between PC31s
186 dbg("new message from $f[4] to $f[3] '$f[8]' stream $fromnode/$stream\n") if isdbg('msg');
187 Log('msg', "Incoming message $f[4] to $f[3] '$f[8]' origin: $origin" );
188 set_fwq($fromnode, $stream, $ref); # store in work
189 set_busy($fromnode, $ref); # set interlock
190 $self->send(DXProt::pc30($fromnode, $tonode, $stream)); # send ack
191 $ref->{lastt} = $main::systime;
193 # look to see whether this is a non private message sent to a known callsign
194 my $uref = DXUser->get_current($ref->{to});
195 if (is_callsign($ref->{to}) && !$ref->{private} && $uref && $uref->homenode) {
197 dbg("set bull to $ref->{to} to private") if isdbg('msg');
198 Log('msg', "set bull to $ref->{to} to private");
203 if ($pcno == 29) { # incoming text
204 my $ref = get_fwq($fromnode, $stream);
207 if (@{$ref->{lines}}) {
208 push @{$ref->{lines}}, $f[4];
210 # temporarily store any R: lines so that we end up with
211 # only the first and last ones stored.
212 if ($f[4] =~ m|^R:\d{6}/\d{4}|) {
213 push @{$ref->{tempr}}, $f[4];
215 if (exists $ref->{tempr}) {
216 push @{$ref->{lines}}, shift @{$ref->{tempr}};
217 push @{$ref->{lines}}, pop @{$ref->{tempr}} if @{$ref->{tempr}};
218 delete $ref->{tempr};
220 push @{$ref->{lines}}, $f[4];
224 if ($ref->{count} >= $ref->{linesreq}) {
225 $self->send(DXProt::pc31($fromnode, $tonode, $stream));
226 dbg("stream $stream: $ref->{count} lines received\n") if isdbg('msg');
229 $ref->{lastt} = $main::systime;
231 dbg("PC29 from unknown stream $stream from $fromnode") if isdbg('msg');
232 $self->send(DXProt::pc42($fromnode, $tonode, $stream)); # unknown stream
237 if ($pcno == 30) { # this is a incoming subject ack
238 my $ref = get_fwq($fromnode); # note no stream at this stage
241 $ref->{stream} = $stream;
243 $ref->{linesreq} = 5;
244 set_fwq($fromnode, $stream, $ref); # new ref
245 set_busy($fromnode, $ref); # interlock
246 dbg("incoming subject ack stream $stream\n") if isdbg('msg');
247 $ref->{lines} = [ $ref->read_msg_body ];
248 $ref->send_tranche($self);
249 $ref->{lastt} = $main::systime;
251 dbg("PC30 from unknown stream $stream from $fromnode") if isdbg('msg');
252 $self->send(DXProt::pc42($fromnode, $tonode, $stream)); # unknown stream
257 if ($pcno == 31) { # acknowledge a tranche of lines
258 my $ref = get_fwq($fromnode, $stream);
260 dbg("tranche ack stream $stream\n") if isdbg('msg');
261 $ref->send_tranche($self);
262 $ref->{lastt} = $main::systime;
264 dbg("PC31 from unknown stream $stream from $fromnode") if isdbg('msg');
265 $self->send(DXProt::pc42($fromnode, $tonode, $stream)); # unknown stream
270 if ($pcno == 32) { # incoming EOM
271 dbg("stream $stream: EOM received\n") if isdbg('msg');
272 my $ref = get_fwq($fromnode, $stream);
274 $self->send(DXProt::pc33($fromnode, $tonode, $stream)); # acknowledge it
276 # get the next msg no - note that this has NOTHING to do with the stream number in PC protocol
277 # store the file or message
278 # remove extraneous rubbish from the hash
279 # remove it from the work in progress vector
280 # stuff it on the msg queue
283 $ref->store($ref->{lines});
286 # does an identical message already exist?
289 if (substr($ref->{subject},0,28) eq substr($m->{subject},0,28) && $ref->{t} == $m->{t} && $ref->{from} eq $m->{from} && $ref->{to} eq $m->{to}) {
290 $ref->stop_msg($fromnode);
291 my $msgno = $m->{msgno};
292 dbg("duplicate message from $ref->{from} -> $ref->{to} to msg: $msgno") if isdbg('msg');
293 Log('msg', "duplicate message from $ref->{from} -> $ref->{to} to msg: $msgno");
299 $ref->swop_it($self->call);
301 # look for 'bad' to addresses
302 if ($ref->dump_it($self->call)) {
303 $ref->stop_msg($fromnode);
304 dbg("'Bad' message $ref->{to}") if isdbg('msg');
305 Log('msg', "'Bad' message $ref->{to}");
309 # check the message for bad words
311 for (@{$ref->{lines}}) {
312 push @words, BadWords::check($_);
314 push @words, BadWords::check($ref->{subject});
316 dbg("$ref->{from} swore: '@words' -> $ref->{to} '$ref->{subject}' origin: $ref->{origin} via " . $self->call) if isdbg('msg');
317 Log('msg',"$ref->{from} swore: '@words' -> $ref->{to} origin: $ref->{origin} via " . $self->call);
318 Log('msg',"subject: $ref->{subject}");
319 for (@{$ref->{lines}}) {
320 Log('msg', "line: $_");
322 $ref->stop_msg($fromnode);
326 $ref->{msgno} = next_transno("Msgno");
327 push @{$ref->{gotit}}, $fromnode; # mark this up as being received
328 $ref->store($ref->{lines});
331 Log('msg', "Message $ref->{msgno} from $ref->{from} received from $fromnode for $ref->{to}");
334 $ref->stop_msg($fromnode);
336 dbg("PC32 from unknown stream $stream from $fromnode") if isdbg('msg');
337 $self->send(DXProt::pc42($fromnode, $tonode, $stream)); # unknown stream
343 if ($pcno == 33) { # acknowledge the end of message
344 my $ref = get_fwq($fromnode, $stream);
346 if ($ref->{private}) { # remove it if it private and gone off site#
347 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $fromnode and deleted");
350 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $fromnode");
351 push @{$ref->{gotit}}, $fromnode; # mark this up as being received
352 $ref->store($ref->{lines}); # re- store the file
354 $ref->stop_msg($fromnode);
356 dbg("PC33 from unknown stream $stream from $fromnode") if isdbg('msg');
357 $self->send(DXProt::pc42($fromnode, $tonode, $stream)); # unknown stream
360 # send next one if present
365 if ($pcno == 40) { # this is a file request
366 $f[3] =~ s/\\/\//og; # change the slashes
367 $f[3] =~ s/\.//og; # remove dots
368 $f[3] =~ s/^\///o; # remove the leading /
369 $f[3] = lc $f[3]; # to lower case;
370 dbg("incoming file $f[3]\n") if isdbg('msg');
371 $f[3] = 'packclus/' . $f[3] unless $f[3] =~ /^packclus\//o;
373 # create any directories
374 my @part = split /\//, $f[3];
376 my $fn = "$main::root";
377 pop @part; # remove last part
378 foreach $part (@part) {
381 last SWITCH if !mkdir $fn, 0777;
382 dbg("created directory $fn\n") if isdbg('msg');
384 my $stream = next_transno($fromnode);
385 my $ref = DXMsg->alloc($stream, "$main::root/$f[3]", $self->call, time, !$f[4], $f[3], ' ', '0', '0');
387 # forwarding variables
388 $ref->{fromnode} = $tonode;
389 $ref->{tonode} = $fromnode;
390 $ref->{linesreq} = $f[5];
391 $ref->{stream} = $stream;
392 $ref->{count} = 0; # no of lines between PC31s
394 $ref->{lastt} = $main::systime;
395 set_fwq($fromnode, $stream, $ref); # store in work
396 $self->send(DXProt::pc30($fromnode, $tonode, $stream)); # send ack
401 if ($pcno == 42) { # abort transfer
402 dbg("stream $stream: abort received\n") if isdbg('msg');
403 my $ref = get_fwq($fromnode, $stream);
405 $ref->stop_msg($fromnode);
411 if ($pcno == 49) { # global delete on subject
413 if ($_->{from} eq $f[1] && $_->{subject} eq $f[2]) {
415 Log('msg', "Message $_->{msgno} from $_->{from} ($_->{subject}) fully deleted");
416 DXChannel::broadcast_nodes($line, $self);
428 my $uref = DXUser->get($to);
429 my $dxchan = DXChannel->get($to);
430 if (*Mail::Send && $uref && $uref->wantemail) {
431 my $email = $uref->email;
433 my @list = ref $email ? @{$email} : $email;
434 my $msg = new Mail::Send Subject=>"[DXSpider: $ref->{from}] $ref->{subject}";
437 print $fh "From: $ref->{from} To: $to On Node: $main::mycall Origin: $ref->{origin} Msgno: $ref->{msgno}\r\n\r\n";
438 print $fh map {"$_\r\n"} $ref->read_msg_body;
441 Log('msg', "Msgno $ref->{msgno} from $ref->{from} emailed to $_");
445 $dxchan->send($dxchan->msg('m9')) if $dxchan && $dxchan->is_user;
448 # store a message away on disc or whatever
450 # NOTE the second arg is a REFERENCE not a list
456 if ($ref->{file}) { # a file
457 dbg("To be stored in $ref->{to}\n") if isdbg('msg');
459 my $fh = new IO::File "$ref->{to}", "w";
462 foreach $line (@{$lines}) {
466 dbg("file $ref->{to} stored\n") if isdbg('msg');
467 Log('msg', "file $ref->{to} from $ref->{from} stored" );
469 confess "can't open file $ref->{to} $!";
471 } else { # a normal message
473 # attempt to open the message file
474 my $fn = filename($ref->{msgno});
476 dbg("To be stored in $fn\n") if isdbg('msg');
478 # now save the file, overwriting what's there, YES I KNOW OK! (I will change it if it's a problem)
479 my $fh = new IO::File "$fn", "w";
481 my $rr = $ref->{rrreq} ? '1' : '0';
482 my $priv = $ref->{private} ? '1': '0';
483 my $del = $ref->{delete} ? '1' : '0';
484 my $delt = $ref->{deletetime} || '0';
485 print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$priv^$ref->{subject}^$ref->{origin}^$ref->{'read'}^$rr^$del^$delt\n";
486 print $fh "=== ", join('^', @{$ref->{gotit}}), "\n";
489 foreach $line (@{$lines}) {
490 $ref->{size} += (length $line) + 1;
494 dbg("msg $ref->{msgno} stored\n") if isdbg('msg');
495 Log('msg', "msg $ref->{msgno} from $ref->{from} to $ref->{to} stored" );
497 confess "can't open msg file $fn $!";
508 if ($self->{tonode}) {
510 $self->{deletetime} = 0;
512 # remove it from the active message list
513 @msg = grep { $_ != $self } @msg;
516 $call = ' by ' . $dxchan->call if $dxchan;
517 Log('msg', "Msgno $self->{msgno} expunged$call");
520 unlink filename($self->{msgno});
528 $t = $main::systime + $residencetime unless defined $t;
531 $ref->{deletetime} = $t;
532 $ref->store( [$ref->read_msg_body] );
535 # clean out old messages from the message queue
540 # mark old messages for deletion
541 foreach $ref (@msg) {
542 if (ref($ref) && !$ref->{keep} && $ref->{t} < $main::systime - $maxage) {
544 # this is for IMMEDIATE destruction
546 $ref->{deletetime} = 0;
551 # read in a message header
561 $file = new IO::File "$fn";
563 dbg("Error reading $fn $!");
564 Log('err', "Error reading $fn $!");
568 $line = <$file>; # first line
569 if ($size == 0 || !$line) {
571 Log('err', "Empty $fn $!");
575 $size -= length $line;
576 if (! $line =~ /^===/o) {
577 dbg("corrupt first line in $fn ($line)");
578 Log('err', "corrupt first line in $fn ($line)");
582 @f = split /\^/, $line;
583 $ref = DXMsg->alloc(@f);
585 $line = <$file>; # second line
587 $size -= length $line;
588 if (! $line =~ /^===/o) {
589 dbg("corrupt second line in $fn ($line)");
590 Log('err', "corrupt second line in $fn ($line)");
595 @f = split /\^/, $line;
596 push @{$ref->{gotit}}, @f;
597 $ref->{size} = $size;
604 # read in a message header
608 my $msgno = $self->{msgno};
611 my $fn = filename($msgno);
614 $file = new IO::File;
615 if (!open($file, $fn)) {
616 dbg("Error reading $fn $!");
617 Log('err' ,"Error reading $fn $!");
620 @out = map {chomp; $_} <$file>;
623 shift @out if $out[0] =~ /^=== /;
624 shift @out if $out[0] =~ /^=== /;
628 # send a tranche of lines to the other end
631 my ($self, $dxchan) = @_;
633 my $to = $self->{tonode};
634 my $from = $self->{fromnode};
635 my $stream = $self->{stream};
636 my $lines = $self->{lines};
639 for ($i = 0, $c = $self->{count}; $i < $self->{linesreq} && $c < @$lines; $i++, $c++) {
640 push @out, DXProt::pc29($to, $from, $stream, $lines->[$c]);
644 push @out, DXProt::pc32($to, $from, $stream) if $i < $self->{linesreq};
649 # find a message to send out and start the ball rolling
656 # bat down the message list looking for one that needs to go off site and whose
657 # nearest node is not busy.
659 dbg("queue msg ($sort)\n") if isdbg('msg');
660 my @nodelist = DXChannel::get_all_nodes;
661 foreach $ref (@msg) {
663 # ignore 'delayed' messages until their waiting time has expired
664 if (exists $ref->{waitt}) {
665 next if $ref->{waitt} > $main::systime;
666 delete $ref->{waitt};
670 if (exists $ref->{lastt} && $main::systime >= $ref->{lastt} + $timeout) {
671 my $node = $ref->{tonode};
672 dbg("Timeout, stopping msgno: $ref->{msgno} -> $node") if isdbg('msg');
673 Log('msg', "Timeout, stopping msgno: $ref->{msgno} -> $node");
674 $ref->stop_msg($node);
676 # delay any outgoing messages that fail
677 $ref->{waitt} = $main::systime + $waittime + rand(120) if $node ne $main::mycall;
678 delete $ref->{lastt};
682 # is it being sent anywhere currently?
683 next if $ref->{tonode}; # ignore it if it already being processed
685 # is it awaiting deletion?
686 if ($ref->{delete} && $main::systime >= $ref->{deletetime}) {
690 next if $ref->{delete};
692 # firstly, is it private and unread? if so can I find the recipient
693 # in my cluster node list offsite?
695 # deal with routed private messages
697 if ($ref->{private}) {
698 next if $ref->{'read'}; # if it is read, it is stuck here
699 $clref = Route::get($ref->{to});
701 $dxchan = $clref->dxchan;
703 if ($dxchan->is_node) {
704 next if $clref->call eq $main::mycall; # i.e. it lives here
705 $ref->start_msg($dxchan) if !get_busy($dxchan->call) && $dxchan->state eq 'normal';
708 dbg("Route: No dxchan for $ref->{to} " . ref($clref) ) if isdbg('msg');
713 # otherwise we are dealing with a bulletin or forwarded private message
714 # compare the gotit list with
715 # the nodelist up above, if there are sites that haven't got it yet
716 # then start sending it - what happens when we get loops is anyone's
717 # guess, use (to, from, time, subject) tuple?
718 foreach $dxchan (@nodelist) {
719 my $call = $dxchan->call;
721 next if $call eq $main::mycall;
722 next if ref $ref->{gotit} && grep $_ eq $call, @{$ref->{gotit}};
723 next unless $ref->forward_it($call); # check the forwarding file
724 next if $ref->{tonode}; # ignore it if it already being processed
726 # if we are here we have a node that doesn't have this message
727 if (!get_busy($call) && $dxchan->state eq 'normal') {
728 $ref->start_msg($dxchan);
734 # if all the available nodes are busy then stop
735 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
739 # is there a message for me?
745 foreach $ref (@msg) {
746 # is it for me, private and unread?
747 if ($ref->{to} eq $call && $ref->{private}) {
748 return 1 if !$ref->{'read'};
754 # start the message off on its travels with a PC28
757 my ($self, $dxchan) = @_;
759 confess("trying to start started msg $self->{msgno} nodes: $self->{fromnode} -> $self->{tonode}") if $self->{tonode};
760 dbg("start msg $self->{msgno}\n") if isdbg('msg');
761 $self->{linesreq} = 10;
763 $self->{tonode} = $dxchan->call;
764 $self->{fromnode} = $main::mycall;
765 set_busy($self->{tonode}, $self);
766 set_fwq($self->{tonode}, undef, $self);
767 $self->{lastt} = $main::systime;
768 my ($fromnode, $origin);
769 $fromnode = $self->{fromnode};
770 $origin = $self->{origin};
771 $dxchan->send(DXProt::pc28($self->{tonode}, $fromnode, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $origin, $self->{rrreq}));
774 # get the ref of a busy node
784 return $busy{$call} = shift;
790 return delete $busy{$call};
793 # get the whole busy queue
799 # get a forwarding queue entry
803 my $stream = shift || '0';
804 return $work{"$call,$stream"};
807 # delete a forwarding queue entry
811 my $stream = shift || '0';
812 return delete $work{"$call,$stream"};
819 my $stream = shift || '0';
820 return $work{"$call,$stream"} = shift;
823 # get the whole forwarding queue
829 # stop a message from continuing, clean it out, unlock interlocks etc
834 my $stream = $self->{stream};
837 dbg("stop msg $self->{msgno} -> node $node\n") if isdbg('msg');
838 del_fwq($node, $stream);
846 delete $ref->{lines};
847 delete $ref->{linesreq};
848 delete $ref->{tonode};
849 delete $ref->{fromnode};
850 delete $ref->{stream};
852 delete $ref->{count};
853 delete $ref->{tempr};
854 delete $ref->{lastt};
855 delete $ref->{waitt};
858 # get a new transaction number from the file specified
862 $name =~ s/\W//og; # remove non-word characters
863 my $fn = "$msgdir/$name";
866 my $fh = new IO::File;
867 if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
869 $msgno = $fh->getline || '0';
873 $fh->print("$msgno\n");
874 dbg("msgno $msgno allocated for $name\n") if isdbg('msg');
877 confess "can't open $fn $!";
882 # initialise the message 'system', read in all the message headers
885 my $dir = new IO::File;
889 # load various control files
890 dbg("load badmsg: " . (load_badmsg() or "Ok"));
891 dbg("load forward: " . (load_forward() or "Ok"));
892 dbg("load swop: " . (load_swop() or "Ok"));
894 # read in the directory
895 opendir($dir, $msgdir) or confess "can't open $msgdir $!";
896 @dir = readdir($dir);
901 next unless /^m\d\d\d\d\d\d$/;
903 $ref = read_msg_header("$msgdir/$_");
906 Log('err', "Deleting $_");
911 # delete any messages to 'badmsg.pl' places
912 if ($ref->dump_it('')) {
913 dbg("'Bad' TO address $ref->{to}") if isdbg('msg');
914 Log('msg', "'Bad' TO address $ref->{to}");
919 # add the message to the available queue
924 # add the message to the directory listing
928 confess "tried to add a non-ref to the msg directory" if !ref $ref;
932 # return all the current messages
938 # get a particular message
943 return $_ if $_->{msgno} == $msgno;
944 last if $_->{msgno} > $msgno;
949 # return the official filename for a message no
952 return sprintf "$msgdir/m%06d", shift;
956 # return a list of valid elements
965 # return a prompt for a field
970 my ($self, $ele) = @_;
975 # send a message state machine
982 if ($self->state eq 'send1') {
984 confess "local var gone missing" if !ref $self->{loc};
985 my $loc = $self->{loc};
986 if (my @ans = BadWords::check($line)) {
987 $self->{badcount} += @ans;
988 Log('msg', $self->call . " used badwords: @ans to @{$loc->{to}} in msg");
991 $loc->{subject} = $line;
993 $self->state('sendbody');
994 #push @out, $self->msg('sendbody');
995 push @out, $self->msg('m8');
996 } elsif ($self->state eq 'sendbody') {
997 confess "local var gone missing" if !ref $self->{loc};
998 my $loc = $self->{loc};
999 if ($line eq "\032" || $line eq '%1A' || uc $line eq "/EX") {
1001 unless ($loc->{reject}) {
1002 foreach $to (@{$loc->{to}}) {
1004 my $systime = $main::systime;
1005 my $mycall = $main::mycall;
1006 $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
1008 exists $loc->{from} ? $loc->{from} : $self->call,
1012 exists $loc->{origin} ? $loc->{origin} : $mycall,
1015 $ref->swop_it($self->call);
1016 $ref->store($loc->{lines});
1018 push @out, $self->msg('m11', $ref->{msgno}, $to);
1019 #push @out, "msgno $ref->{msgno} sent to $to";
1023 Log('msg', $self->call . " swore to @{$loc->{to}} subject: '$loc->{subject}' in msg, REJECTED");
1026 delete $loc->{lines};
1028 delete $self->{loc};
1031 $self->state('prompt');
1032 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
1033 #push @out, $self->msg('sendabort');
1034 push @out, $self->msg('m10');
1035 delete $loc->{lines};
1037 delete $self->{loc};
1039 $self->state('prompt');
1041 if (my @ans = BadWords::check($line)) {
1042 $self->{badcount} += @ans;
1043 Log('msg', $self->call . " used badwords: @ans to @{$loc->{to}} subject: '$loc->{subject}' in msg") unless $loc->{reject};
1044 Log('msg', "line: $line");
1048 # i.e. it ain't and end or abort, therefore store the line
1049 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
1055 # return the standard directory line for this ref
1059 my $flag = $ref->read ? '-' : ' ';
1060 $flag = 'D' if $ref->delete;
1061 return sprintf "%6d%s%s%5d %8.8s %8.8s %-6.6s %5.5s %-30.30s",
1062 $ref->msgno, $flag, $ref->private ? 'p' : ' ', $ref->size,
1063 $ref->to, $ref->from, cldate($ref->t), ztime($ref->t), $ref->subject;
1066 # load the forward table
1070 my $s = readfilestr($forwardfn);
1073 push @out, $@ if $@;
1078 # load the bad message table
1082 my $s = readfilestr($badmsgfn);
1085 push @out, $@ if $@;
1090 # load the swop message table
1094 my $s = readfilestr($swopfn);
1097 push @out, $@ if $@;
1103 # forward that message or not according to the forwarding table
1104 # returns 1 for forward, 0 - to ignore
1113 for ($i = 0; $i < @forward; $i += 5) {
1114 my ($sort, $field, $pattern, $action, $bbs) = @forward[$i..($i+4)];
1117 # are we interested?
1118 next if $ref->{private} && $sort ne 'P';
1119 next if !$ref->{private} && $sort ne 'B';
1122 $tested = $ref->{to} if $field eq 'T';
1123 $tested = $ref->{from} if $field eq 'F';
1124 $tested = $ref->{origin} if $field eq 'O';
1125 $tested = $ref->{subject} if $field eq 'S';
1127 if (!$pattern || $tested =~ m{$pattern}i) {
1128 return 0 if $action eq 'I';
1129 return 1 if !$bbs || grep $_ eq $call, @{$bbs};
1141 for ($i = 0; $i < @badmsg; $i += 3) {
1142 my ($sort, $field, $pattern) = @badmsg[$i..($i+2)];
1145 # are we interested?
1146 next if $ref->{private} && $sort ne 'P';
1147 next if !$ref->{private} && $sort ne 'B';
1150 $tested = $ref->{to} if $field eq 'T';
1151 $tested = $ref->{from} if $field eq 'F';
1152 $tested = $ref->{origin} if $field eq 'O';
1153 $tested = $ref->{subject} if $field eq 'S';
1154 $tested = $call if $field eq 'I';
1156 if (!$pattern || $tested =~ m{$pattern}i) {
1170 for ($i = 0; $i < @swop; $i += 5) {
1171 my ($sort, $field, $pattern, $tfield, $topattern) = @swop[$i..($i+4)];
1176 # are we interested?
1177 next if $ref->{private} && $sort ne 'P';
1178 next if !$ref->{private} && $sort ne 'B';
1181 $tested = $ref->{to} if $field eq 'T';
1182 $tested = $ref->{from} if $field eq 'F';
1183 $tested = $ref->{origin} if $field eq 'O';
1184 $tested = $ref->{subject} if $field eq 'S';
1187 $old = $swop = $ref->{to} if $tfield eq 'T';
1188 $old = $swop = $ref->{from} if $tfield eq 'F';
1189 $old = $swop = $ref->{origin} if $tfield eq 'O';
1190 $old = $swop = $ref->{subject} if $tfield eq 'S';
1192 if ($tested =~ m{$pattern}i) {
1193 if ($tested eq $swop) {
1194 $swop =~ s{$pattern}{$topattern}i;
1198 Log('msg', "Msg $ref->{msgno}: $tfield $old -> $swop");
1199 Log('dbg', "Msg $ref->{msgno}: $tfield $old -> $swop");
1200 $ref->{to} = $swop if $tfield eq 'T';
1201 $ref->{from} = $swop if $tfield eq 'F';
1202 $ref->{origin} = $swop if $tfield eq 'O';
1203 $ref->{subject} = $swop if $tfield eq 'S';
1210 # import any msgs in the import directory
1211 # the messages are in BBS format (but may have cluster extentions
1212 # so SB UK < GB7TLH is legal
1215 # are there any to do in this directory?
1216 return unless -d $importfn;
1217 unless (opendir(DIR, $importfn)) {
1218 dbg("can\'t open $importfn $!") if isdbg('msg');
1219 Log('msg', "can\'t open $importfn $!");
1223 my @names = readdir(DIR);
1226 foreach $name (@names) {
1227 next if $name =~ /^\./;
1228 my $splitit = $name =~ /^split/;
1229 my $fn = "$importfn/$name";
1231 unless (open(MSG, $fn)) {
1232 dbg("can\'t open import file $fn $!") if isdbg('msg');
1233 Log('msg', "can\'t open import file $fn $!");
1237 my @msg = map { chomp; $_ } <MSG>;
1240 my @out = import_one($main::me, \@msg, $splitit);
1245 # import one message as a list in bbs (as extended) mode
1246 # takes a reference to an array containing the whole message
1251 my $splitit = shift;
1255 my $from = $dxchan->call;
1256 my $origin = $main::mycall;
1261 my $line = shift @$ref;
1262 my @f = split /([\s\@\$])/, $line;
1263 @f = map {s/\s+//g; length $_ ? $_ : ()} @f;
1265 unless (@f && $f[0] =~ /^(:?S|SP|SB|SEND)$/ ) {
1266 my $m = "invalid first line in import '$line'";
1267 dbg($m) if isdbg('msg');
1271 my $f = uc shift @f;
1272 next if $f eq 'SEND';
1274 # private / noprivate / rr
1275 if ($notincalls && ($f eq 'B' || $f eq 'SB' || $f =~ /^NOP/oi)) {
1277 } elsif ($notincalls && ($f eq 'P' || $f eq 'SP' || $f =~ /^PRI/oi)) {
1279 } elsif ($notincalls && ($f eq 'RR')) {
1281 } elsif (($f =~ /^[\@\.\#\$]$/ || $f eq '.#') && @f) { # this is bbs syntax, for AT
1283 } elsif ($f eq '<' && @f) { # this is bbs syntax for from call
1284 $from = uc shift @f;
1285 } elsif ($f =~ /^\$/) { # this is bbs syntax for a bid
1287 } elsif ($f =~ /^<(\S+)/) { # this is bbs syntax for from call
1289 } elsif ($f =~ /^\$\S+/) { # this is bbs syntax for bid
1296 # is this callsign a distro?
1297 my $fn = "$msgdir/distro/$f.pl";
1299 my $fh = new IO::File $fn;
1306 return (1, "Error in Distro $f.pl:", $@) if $@;
1314 if (grep $_ eq $f, @DXMsg::badmsg) {
1315 push @out, $dxchan->msg('m3', $f);
1322 # subject is the next line
1323 my $subject = shift @$ref;
1325 # strip off trailing lines
1326 pop @$ref while (@$ref && $$ref[-1] =~ /^\s*$/);
1328 # strip off /EX or /ABORT
1329 return ("aborted") if @$ref && $$ref[-1] =~ m{^/ABORT$}i;
1330 pop @$ref if (@$ref && $$ref[-1] =~ m{^/EX$}i);
1332 # sort out any splitting that needs to be done
1338 if ($lth >= $maxchunk || ($lth > $minchunk && /^\s*$/)) {
1339 push @chunk, $lines;
1346 push @chunk, $lines if @$lines;
1351 # does an identical message already exist?
1354 if (substr($subject,0,28) eq substr($m->{subject},0,28) && $from eq $m->{from} && grep $m->{to} eq $_, @to) {
1355 my $msgno = $m->{msgno};
1356 dbg("duplicate message from $from -> $m->{to} to msg: $msgno") if isdbg('msg');
1357 Log('msg', "duplicate message from $from -> $m->{to} to msg: $msgno");
1362 # write all the messages away
1364 for ( $i = 0; $i < @chunk; $i++) {
1365 my $chunk = $chunk[$i];
1368 my $num = " [" . ($i+1) . "/" . scalar @chunk . "]";
1369 $ch_subject = substr($subject, 0, 27 - length $num) . $num;
1371 $ch_subject = $subject;
1375 my $systime = $main::systime;
1376 my $mycall = $main::mycall;
1377 my $mref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
1386 $mref->swop_it($main::mycall);
1387 $mref->store($chunk);
1389 push @out, $dxchan->msg('m11', $mref->{msgno}, $to);
1390 #push @out, "msgno $ref->{msgno} sent to $to";
1401 my $name = $AUTOLOAD;
1402 return if $name =~ /::DESTROY$/;
1405 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
1406 # this clever line of code creates a subroutine which takes over from autoload
1407 # from OO Perl - Conway
1408 *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
1409 @_ ? $self->{$name} = shift : $self->{$name} ;