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}";
438 print $fh "From: $ref->{from} To: $to On Node: $main::mycall Origin: $ref->{origin} Msgno: $ref->{msgno}\r\n\r\n";
439 print $fh map {"$_\r\n"} $ref->read_msg_body;
442 Log('msg', "Msgno $ref->{msgno} from $ref->{from} emailed to $_");
445 dbg("email forwarding error $!") if isdbg('msg');
449 $dxchan->send($dxchan->msg('m9')) if $dxchan && $dxchan->is_user;
452 # store a message away on disc or whatever
454 # NOTE the second arg is a REFERENCE not a list
460 if ($ref->{file}) { # a file
461 dbg("To be stored in $ref->{to}\n") if isdbg('msg');
463 my $fh = new IO::File "$ref->{to}", "w";
466 foreach $line (@{$lines}) {
470 dbg("file $ref->{to} stored\n") if isdbg('msg');
471 Log('msg', "file $ref->{to} from $ref->{from} stored" );
473 confess "can't open file $ref->{to} $!";
475 } else { # a normal message
477 # attempt to open the message file
478 my $fn = filename($ref->{msgno});
480 dbg("To be stored in $fn\n") if isdbg('msg');
482 # now save the file, overwriting what's there, YES I KNOW OK! (I will change it if it's a problem)
483 my $fh = new IO::File "$fn", "w";
485 my $rr = $ref->{rrreq} ? '1' : '0';
486 my $priv = $ref->{private} ? '1': '0';
487 my $del = $ref->{delete} ? '1' : '0';
488 my $delt = $ref->{deletetime} || '0';
489 print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$priv^$ref->{subject}^$ref->{origin}^$ref->{'read'}^$rr^$del^$delt\n";
490 print $fh "=== ", join('^', @{$ref->{gotit}}), "\n";
493 foreach $line (@{$lines}) {
494 $ref->{size} += (length $line) + 1;
498 dbg("msg $ref->{msgno} stored\n") if isdbg('msg');
499 Log('msg', "msg $ref->{msgno} from $ref->{from} to $ref->{to} stored" );
501 confess "can't open msg file $fn $!";
512 if ($self->{tonode}) {
514 $self->{deletetime} = 0;
516 # remove it from the active message list
517 @msg = grep { $_ != $self } @msg;
520 $call = ' by ' . $dxchan->call if $dxchan;
521 Log('msg', "Msgno $self->{msgno} expunged$call");
524 unlink filename($self->{msgno});
532 $t = $main::systime + $residencetime unless defined $t;
535 $ref->{deletetime} = $t;
536 $ref->store( [$ref->read_msg_body] );
539 # clean out old messages from the message queue
544 # mark old messages for deletion
545 foreach $ref (@msg) {
546 if (ref($ref) && !$ref->{keep} && $ref->{t} < $main::systime - $maxage) {
548 # this is for IMMEDIATE destruction
550 $ref->{deletetime} = 0;
555 # read in a message header
565 $file = new IO::File "$fn";
567 dbg("Error reading $fn $!");
568 Log('err', "Error reading $fn $!");
572 $line = <$file>; # first line
573 if ($size == 0 || !$line) {
575 Log('err', "Empty $fn $!");
579 $size -= length $line;
580 if (! $line =~ /^===/o) {
581 dbg("corrupt first line in $fn ($line)");
582 Log('err', "corrupt first line in $fn ($line)");
586 @f = split /\^/, $line;
587 $ref = DXMsg->alloc(@f);
589 $line = <$file>; # second line
591 $size -= length $line;
592 if (! $line =~ /^===/o) {
593 dbg("corrupt second line in $fn ($line)");
594 Log('err', "corrupt second line in $fn ($line)");
599 @f = split /\^/, $line;
600 push @{$ref->{gotit}}, @f;
601 $ref->{size} = $size;
608 # read in a message header
612 my $msgno = $self->{msgno};
615 my $fn = filename($msgno);
618 $file = new IO::File;
619 if (!open($file, $fn)) {
620 dbg("Error reading $fn $!");
621 Log('err' ,"Error reading $fn $!");
624 @out = map {chomp; $_} <$file>;
627 shift @out if $out[0] =~ /^=== /;
628 shift @out if $out[0] =~ /^=== /;
632 # send a tranche of lines to the other end
635 my ($self, $dxchan) = @_;
637 my $to = $self->{tonode};
638 my $from = $self->{fromnode};
639 my $stream = $self->{stream};
640 my $lines = $self->{lines};
643 for ($i = 0, $c = $self->{count}; $i < $self->{linesreq} && $c < @$lines; $i++, $c++) {
644 push @out, DXProt::pc29($to, $from, $stream, $lines->[$c]);
648 push @out, DXProt::pc32($to, $from, $stream) if $i < $self->{linesreq};
653 # find a message to send out and start the ball rolling
660 # bat down the message list looking for one that needs to go off site and whose
661 # nearest node is not busy.
663 dbg("queue msg ($sort)\n") if isdbg('msg');
664 my @nodelist = DXChannel::get_all_nodes;
665 foreach $ref (@msg) {
667 # ignore 'delayed' messages until their waiting time has expired
668 if (exists $ref->{waitt}) {
669 next if $ref->{waitt} > $main::systime;
670 delete $ref->{waitt};
674 if (exists $ref->{lastt} && $main::systime >= $ref->{lastt} + $timeout) {
675 my $node = $ref->{tonode};
676 dbg("Timeout, stopping msgno: $ref->{msgno} -> $node") if isdbg('msg');
677 Log('msg', "Timeout, stopping msgno: $ref->{msgno} -> $node");
678 $ref->stop_msg($node);
680 # delay any outgoing messages that fail
681 $ref->{waitt} = $main::systime + $waittime + rand(120) if $node ne $main::mycall;
682 delete $ref->{lastt};
686 # is it being sent anywhere currently?
687 next if $ref->{tonode}; # ignore it if it already being processed
689 # is it awaiting deletion?
690 if ($ref->{delete} && $main::systime >= $ref->{deletetime}) {
694 next if $ref->{delete};
696 # firstly, is it private and unread? if so can I find the recipient
697 # in my cluster node list offsite?
699 # deal with routed private messages
701 if ($ref->{private}) {
702 next if $ref->{'read'}; # if it is read, it is stuck here
703 $clref = Route::get($ref->{to});
705 $dxchan = $clref->dxchan;
707 if ($dxchan->is_node) {
708 next if $clref->call eq $main::mycall; # i.e. it lives here
709 $ref->start_msg($dxchan) if !get_busy($dxchan->call) && $dxchan->state eq 'normal';
712 dbg("Route: No dxchan for $ref->{to} " . ref($clref) ) if isdbg('msg');
717 # otherwise we are dealing with a bulletin or forwarded private message
718 # compare the gotit list with
719 # the nodelist up above, if there are sites that haven't got it yet
720 # then start sending it - what happens when we get loops is anyone's
721 # guess, use (to, from, time, subject) tuple?
722 foreach $dxchan (@nodelist) {
723 my $call = $dxchan->call;
725 next if $call eq $main::mycall;
726 next if ref $ref->{gotit} && grep $_ eq $call, @{$ref->{gotit}};
727 next unless $ref->forward_it($call); # check the forwarding file
728 next if $ref->{tonode}; # ignore it if it already being processed
730 # if we are here we have a node that doesn't have this message
731 if (!get_busy($call) && $dxchan->state eq 'normal') {
732 $ref->start_msg($dxchan);
738 # if all the available nodes are busy then stop
739 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
743 # is there a message for me?
749 foreach $ref (@msg) {
750 # is it for me, private and unread?
751 if ($ref->{to} eq $call && $ref->{private}) {
752 return 1 if !$ref->{'read'};
758 # start the message off on its travels with a PC28
761 my ($self, $dxchan) = @_;
763 confess("trying to start started msg $self->{msgno} nodes: $self->{fromnode} -> $self->{tonode}") if $self->{tonode};
764 dbg("start msg $self->{msgno}\n") if isdbg('msg');
765 $self->{linesreq} = 10;
767 $self->{tonode} = $dxchan->call;
768 $self->{fromnode} = $main::mycall;
769 set_busy($self->{tonode}, $self);
770 set_fwq($self->{tonode}, undef, $self);
771 $self->{lastt} = $main::systime;
772 my ($fromnode, $origin);
773 $fromnode = $self->{fromnode};
774 $origin = $self->{origin};
775 $dxchan->send(DXProt::pc28($self->{tonode}, $fromnode, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $origin, $self->{rrreq}));
778 # get the ref of a busy node
788 return $busy{$call} = shift;
794 return delete $busy{$call};
797 # get the whole busy queue
803 # get a forwarding queue entry
807 my $stream = shift || '0';
808 return $work{"$call,$stream"};
811 # delete a forwarding queue entry
815 my $stream = shift || '0';
816 return delete $work{"$call,$stream"};
823 my $stream = shift || '0';
824 return $work{"$call,$stream"} = shift;
827 # get the whole forwarding queue
833 # stop a message from continuing, clean it out, unlock interlocks etc
838 my $stream = $self->{stream};
841 dbg("stop msg $self->{msgno} -> node $node\n") if isdbg('msg');
842 del_fwq($node, $stream);
850 delete $ref->{lines};
851 delete $ref->{linesreq};
852 delete $ref->{tonode};
853 delete $ref->{fromnode};
854 delete $ref->{stream};
856 delete $ref->{count};
857 delete $ref->{tempr};
858 delete $ref->{lastt};
859 delete $ref->{waitt};
862 # get a new transaction number from the file specified
866 $name =~ s/\W//og; # remove non-word characters
867 my $fn = "$msgdir/$name";
870 my $fh = new IO::File;
871 if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
873 $msgno = $fh->getline || '0';
877 $fh->print("$msgno\n");
878 dbg("msgno $msgno allocated for $name\n") if isdbg('msg');
881 confess "can't open $fn $!";
886 # initialise the message 'system', read in all the message headers
889 my $dir = new IO::File;
893 # load various control files
894 dbg("load badmsg: " . (load_badmsg() or "Ok"));
895 dbg("load forward: " . (load_forward() or "Ok"));
896 dbg("load swop: " . (load_swop() or "Ok"));
898 # read in the directory
899 opendir($dir, $msgdir) or confess "can't open $msgdir $!";
900 @dir = readdir($dir);
905 next unless /^m\d\d\d\d\d\d$/;
907 $ref = read_msg_header("$msgdir/$_");
910 Log('err', "Deleting $_");
915 # delete any messages to 'badmsg.pl' places
916 if ($ref->dump_it('')) {
917 dbg("'Bad' TO address $ref->{to}") if isdbg('msg');
918 Log('msg', "'Bad' TO address $ref->{to}");
923 # add the message to the available queue
928 # add the message to the directory listing
932 confess "tried to add a non-ref to the msg directory" if !ref $ref;
936 # return all the current messages
942 # get a particular message
947 return $_ if $_->{msgno} == $msgno;
948 last if $_->{msgno} > $msgno;
953 # return the official filename for a message no
956 return sprintf "$msgdir/m%06d", shift;
960 # return a list of valid elements
969 # return a prompt for a field
974 my ($self, $ele) = @_;
979 # send a message state machine
986 if ($self->state eq 'send1') {
988 confess "local var gone missing" if !ref $self->{loc};
989 my $loc = $self->{loc};
990 if (my @ans = BadWords::check($line)) {
991 $self->{badcount} += @ans;
992 Log('msg', $self->call . " used badwords: @ans to @{$loc->{to}} in msg");
995 $loc->{subject} = $line;
997 $self->state('sendbody');
998 #push @out, $self->msg('sendbody');
999 push @out, $self->msg('m8');
1000 } elsif ($self->state eq 'sendbody') {
1001 confess "local var gone missing" if !ref $self->{loc};
1002 my $loc = $self->{loc};
1003 if ($line eq "\032" || $line eq '%1A' || uc $line eq "/EX") {
1005 unless ($loc->{reject}) {
1006 foreach $to (@{$loc->{to}}) {
1008 my $systime = $main::systime;
1009 my $mycall = $main::mycall;
1010 $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
1012 exists $loc->{from} ? $loc->{from} : $self->call,
1016 exists $loc->{origin} ? $loc->{origin} : $mycall,
1019 $ref->swop_it($self->call);
1020 $ref->store($loc->{lines});
1022 push @out, $self->msg('m11', $ref->{msgno}, $to);
1023 #push @out, "msgno $ref->{msgno} sent to $to";
1027 Log('msg', $self->call . " swore to @{$loc->{to}} subject: '$loc->{subject}' in msg, REJECTED");
1030 delete $loc->{lines};
1032 delete $self->{loc};
1035 $self->state('prompt');
1036 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
1037 #push @out, $self->msg('sendabort');
1038 push @out, $self->msg('m10');
1039 delete $loc->{lines};
1041 delete $self->{loc};
1043 $self->state('prompt');
1045 if (my @ans = BadWords::check($line)) {
1046 $self->{badcount} += @ans;
1047 Log('msg', $self->call . " used badwords: @ans to @{$loc->{to}} subject: '$loc->{subject}' in msg") unless $loc->{reject};
1048 Log('msg', "line: $line");
1052 # i.e. it ain't and end or abort, therefore store the line
1053 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
1059 # return the standard directory line for this ref
1063 my $flag = $ref->read ? '-' : ' ';
1064 $flag = 'D' if $ref->delete;
1065 return sprintf "%6d%s%s%5d %8.8s %8.8s %-6.6s %5.5s %-30.30s",
1066 $ref->msgno, $flag, $ref->private ? 'p' : ' ', $ref->size,
1067 $ref->to, $ref->from, cldate($ref->t), ztime($ref->t), $ref->subject;
1070 # load the forward table
1074 my $s = readfilestr($forwardfn);
1077 push @out, $@ if $@;
1082 # load the bad message table
1086 my $s = readfilestr($badmsgfn);
1089 push @out, $@ if $@;
1094 # load the swop message table
1098 my $s = readfilestr($swopfn);
1101 push @out, $@ if $@;
1107 # forward that message or not according to the forwarding table
1108 # returns 1 for forward, 0 - to ignore
1117 for ($i = 0; $i < @forward; $i += 5) {
1118 my ($sort, $field, $pattern, $action, $bbs) = @forward[$i..($i+4)];
1121 # are we interested?
1122 next if $ref->{private} && $sort ne 'P';
1123 next if !$ref->{private} && $sort ne 'B';
1126 $tested = $ref->{to} if $field eq 'T';
1127 $tested = $ref->{from} if $field eq 'F';
1128 $tested = $ref->{origin} if $field eq 'O';
1129 $tested = $ref->{subject} if $field eq 'S';
1131 if (!$pattern || $tested =~ m{$pattern}i) {
1132 return 0 if $action eq 'I';
1133 return 1 if !$bbs || grep $_ eq $call, @{$bbs};
1145 for ($i = 0; $i < @badmsg; $i += 3) {
1146 my ($sort, $field, $pattern) = @badmsg[$i..($i+2)];
1149 # are we interested?
1150 next if $ref->{private} && $sort ne 'P';
1151 next if !$ref->{private} && $sort ne 'B';
1154 $tested = $ref->{to} if $field eq 'T';
1155 $tested = $ref->{from} if $field eq 'F';
1156 $tested = $ref->{origin} if $field eq 'O';
1157 $tested = $ref->{subject} if $field eq 'S';
1158 $tested = $call if $field eq 'I';
1160 if (!$pattern || $tested =~ m{$pattern}i) {
1174 for ($i = 0; $i < @swop; $i += 5) {
1175 my ($sort, $field, $pattern, $tfield, $topattern) = @swop[$i..($i+4)];
1180 # are we interested?
1181 next if $ref->{private} && $sort ne 'P';
1182 next if !$ref->{private} && $sort ne 'B';
1185 $tested = $ref->{to} if $field eq 'T';
1186 $tested = $ref->{from} if $field eq 'F';
1187 $tested = $ref->{origin} if $field eq 'O';
1188 $tested = $ref->{subject} if $field eq 'S';
1191 $old = $swop = $ref->{to} if $tfield eq 'T';
1192 $old = $swop = $ref->{from} if $tfield eq 'F';
1193 $old = $swop = $ref->{origin} if $tfield eq 'O';
1194 $old = $swop = $ref->{subject} if $tfield eq 'S';
1196 if ($tested =~ m{$pattern}i) {
1197 if ($tested eq $swop) {
1198 $swop =~ s{$pattern}{$topattern}i;
1202 Log('msg', "Msg $ref->{msgno}: $tfield $old -> $swop");
1203 Log('dbg', "Msg $ref->{msgno}: $tfield $old -> $swop");
1204 $ref->{to} = $swop if $tfield eq 'T';
1205 $ref->{from} = $swop if $tfield eq 'F';
1206 $ref->{origin} = $swop if $tfield eq 'O';
1207 $ref->{subject} = $swop if $tfield eq 'S';
1214 # import any msgs in the import directory
1215 # the messages are in BBS format (but may have cluster extentions
1216 # so SB UK < GB7TLH is legal
1219 # are there any to do in this directory?
1220 return unless -d $importfn;
1221 unless (opendir(DIR, $importfn)) {
1222 dbg("can\'t open $importfn $!") if isdbg('msg');
1223 Log('msg', "can\'t open $importfn $!");
1227 my @names = readdir(DIR);
1230 foreach $name (@names) {
1231 next if $name =~ /^\./;
1232 my $splitit = $name =~ /^split/;
1233 my $fn = "$importfn/$name";
1235 unless (open(MSG, $fn)) {
1236 dbg("can\'t open import file $fn $!") if isdbg('msg');
1237 Log('msg', "can\'t open import file $fn $!");
1241 my @msg = map { chomp; $_ } <MSG>;
1244 my @out = import_one($main::me, \@msg, $splitit);
1249 # import one message as a list in bbs (as extended) mode
1250 # takes a reference to an array containing the whole message
1255 my $splitit = shift;
1259 my $from = $dxchan->call;
1260 my $origin = $main::mycall;
1265 my $line = shift @$ref;
1266 my @f = split /([\s\@\$])/, $line;
1267 @f = map {s/\s+//g; length $_ ? $_ : ()} @f;
1269 unless (@f && $f[0] =~ /^(:?S|SP|SB|SEND)$/ ) {
1270 my $m = "invalid first line in import '$line'";
1271 dbg($m) if isdbg('msg');
1275 my $f = uc shift @f;
1276 next if $f eq 'SEND';
1278 # private / noprivate / rr
1279 if ($notincalls && ($f eq 'B' || $f eq 'SB' || $f =~ /^NOP/oi)) {
1281 } elsif ($notincalls && ($f eq 'P' || $f eq 'SP' || $f =~ /^PRI/oi)) {
1283 } elsif ($notincalls && ($f eq 'RR')) {
1285 } elsif (($f =~ /^[\@\.\#\$]$/ || $f eq '.#') && @f) { # this is bbs syntax, for AT
1287 } elsif ($f eq '<' && @f) { # this is bbs syntax for from call
1288 $from = uc shift @f;
1289 } elsif ($f =~ /^\$/) { # this is bbs syntax for a bid
1291 } elsif ($f =~ /^<(\S+)/) { # this is bbs syntax for from call
1293 } elsif ($f =~ /^\$\S+/) { # this is bbs syntax for bid
1300 # is this callsign a distro?
1301 my $fn = "$msgdir/distro/$f.pl";
1303 my $fh = new IO::File $fn;
1310 return (1, "Error in Distro $f.pl:", $@) if $@;
1318 if (grep $_ eq $f, @DXMsg::badmsg) {
1319 push @out, $dxchan->msg('m3', $f);
1326 # subject is the next line
1327 my $subject = shift @$ref;
1329 # strip off trailing lines
1330 pop @$ref while (@$ref && $$ref[-1] =~ /^\s*$/);
1332 # strip off /EX or /ABORT
1333 return ("aborted") if @$ref && $$ref[-1] =~ m{^/ABORT$}i;
1334 pop @$ref if (@$ref && $$ref[-1] =~ m{^/EX$}i);
1336 # sort out any splitting that needs to be done
1342 if ($lth >= $maxchunk || ($lth > $minchunk && /^\s*$/)) {
1343 push @chunk, $lines;
1350 push @chunk, $lines if @$lines;
1355 # does an identical message already exist?
1358 if (substr($subject,0,28) eq substr($m->{subject},0,28) && $from eq $m->{from} && grep $m->{to} eq $_, @to) {
1359 my $msgno = $m->{msgno};
1360 dbg("duplicate message from $from -> $m->{to} to msg: $msgno") if isdbg('msg');
1361 Log('msg', "duplicate message from $from -> $m->{to} to msg: $msgno");
1366 # write all the messages away
1368 for ( $i = 0; $i < @chunk; $i++) {
1369 my $chunk = $chunk[$i];
1372 my $num = " [" . ($i+1) . "/" . scalar @chunk . "]";
1373 $ch_subject = substr($subject, 0, 27 - length $num) . $num;
1375 $ch_subject = $subject;
1379 my $systime = $main::systime;
1380 my $mycall = $main::mycall;
1381 my $mref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
1390 $mref->swop_it($main::mycall);
1391 $mref->store($chunk);
1393 push @out, $dxchan->msg('m11', $mref->{msgno}, $to);
1394 #push @out, "msgno $ref->{msgno} sent to $to";
1405 my $name = $AUTOLOAD;
1406 return if $name =~ /::DESTROY$/;
1409 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
1410 # this clever line of code creates a subroutine which takes over from autoload
1411 # from OO Perl - Conway
1412 *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
1413 @_ ? $self->{$name} = shift : $self->{$name} ;