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 $email_server $email_prog $email_from
44 $queueinterval $lastq $importfn $minchunk $maxchunk $bulltopriv);
46 %work = (); # outstanding jobs
47 @msg = (); # messages we have
48 %busy = (); # station interlocks
49 $msgdir = "$main::root/msg"; # directory contain the msgs
50 $maxage = 30 * 86400; # the maximum age that a message shall live for if not marked
51 $last_clean = 0; # last time we did a clean
52 @forward = (); # msg forward table
53 @badmsg = (); # bad message table
54 @swop = (); # swop table
55 $timeout = 30*60; # forwarding timeout
56 $waittime = 30*60; # time an aborted outgoing message waits before trying again
57 $queueinterval = 1*60; # run the queue every 1 minute
60 $minchunk = 4800; # minimum chunk size for a split message
61 $maxchunk = 6000; # maximum chunk size
62 $bulltopriv = 1; # convert msgs with callsigns to private if they are bulls
63 $residencetime = 2*86400; # keep deleted messages for this amount of time
64 $email_server = undef; # DNS address of smtp server if 'smtp'
65 $email_prog = undef; # program name + args for sending mail
66 $email_from = undef; # the from address the email will appear to be from
68 $badmsgfn = "$msgdir/badmsg.pl"; # list of TO address we wont store
69 $forwardfn = "$msgdir/forward.pl"; # the forwarding table
70 $swopfn = "$msgdir/swop.pl"; # the swopping table
71 $importfn = "$msgdir/import"; # import directory
75 fromnode => '5,From Node',
76 tonode => '5,To Node',
79 t => '0,Msg Time,cldatetime',
80 private => '5,Private,yesno',
81 subject => '0,Subject',
82 linesreq => '0,Lines per Gob',
83 rrreq => '5,Read Confirm,yesno',
86 stream => '9,Stream No',
87 count => '5,Gob Linecnt',
88 file => '5,File?,yesno',
89 gotit => '5,Got it Nodes,parray',
90 lines => '5,Lines,parray',
91 'read' => '5,Times read',
94 keep => '0,Keep this?,yesno',
95 lastt => '5,Last processed,cldatetime',
96 waitt => '5,Wait until,cldatetime',
97 delete => '5,Awaiting Delete,yesno',
98 deletetime => '5,Deletion Time,cldatetime',
101 # fix up the default sendmail if available
102 for (qw(/usr/sbin/sendmail /usr/lib/sendmail /usr/sbin/sendmail)) {
109 # allocate a new object
110 # called fromnode, tonode, from, to, datetime, private?, subject, nolinesper
114 my $self = bless {}, $pkg;
115 $self->{msgno} = shift;
118 $self->{to} = ($to eq $main::mycall) ? $main::myalias : $to;
120 $self->{from} = uc $from;
122 $self->{private} = shift;
123 $self->{subject} = shift;
124 $self->{origin} = shift;
125 $self->{'read'} = shift;
126 $self->{rrreq} = shift;
127 $self->{delete} = shift;
128 $self->{deletetime} = shift;
130 # $self->{lastt} = $main::systime;
132 $self->{private} = 1 if $bulltopriv && DXUser->get_current($self->{to});
140 my ($self, $line) = @_;
142 # this is periodic processing
143 if (!$self || !$line) {
145 if ($main::systime >= $lastq + $queueinterval) {
147 # queue some message if the interval timer has gone off
150 # import any messages in the import directory
153 $lastq = $main::systime;
156 # clean the message queue
157 clean_old() if $main::systime - $last_clean > 3600 ;
158 $last_clean = $main::systime;
162 my @f = split /\^/, $line;
163 my ($pcno) = $f[0] =~ /^PC(\d\d)/; # just get the number
164 my ($tonode, $fromnode) = @f[1, 2];
165 my $stream = $f[3] if ($pcno >= 29 && $pcno <= 33) || $pcno == 42;
168 if ($pcno == 28) { # incoming message
170 # sort out various extant protocol errors that occur
172 $origin = $self->call unless $origin && $origin gt ' ';
174 # first look for any messages in the busy queue
175 # and cancel them this should both resolve timed out incoming messages
176 # and crossing of message between nodes, incoming messages have priority
178 my $ref = get_busy($fromnode);
180 my $otonode = $ref->{tonode} || "unknown";
181 dbg("Busy, stopping msgno: $ref->{msgno} $fromnode->$otonode") if isdbg('msg');
182 $ref->stop_msg($fromnode);
185 my $t = cltounix($f[5], $f[6]);
186 $stream = next_transno($fromnode);
187 $ref = DXMsg->alloc($stream, uc $f[3], $f[4], $t, $f[7], $f[8], $origin, '0', $f[11]);
189 # fill in various forwarding state variables
190 $ref->{fromnode} = $fromnode;
191 $ref->{tonode} = $tonode;
192 $ref->{rrreq} = $f[11];
193 $ref->{linesreq} = $f[10];
194 $ref->{stream} = $stream;
195 $ref->{count} = 0; # no of lines between PC31s
196 dbg("new message from $f[4] to $f[3] '$f[8]' stream $fromnode/$stream\n") if isdbg('msg');
197 Log('msg', "Incoming message $f[4] to $f[3] '$f[8]' origin: $origin" );
198 set_fwq($fromnode, $stream, $ref); # store in work
199 set_busy($fromnode, $ref); # set interlock
200 $self->send(DXProt::pc30($fromnode, $tonode, $stream)); # send ack
201 $ref->{lastt} = $main::systime;
203 # look to see whether this is a non private message sent to a known callsign
204 my $uref = DXUser->get_current($ref->{to});
205 if (is_callsign($ref->{to}) && !$ref->{private} && $uref && $uref->homenode) {
207 dbg("set bull to $ref->{to} to private") if isdbg('msg');
208 Log('msg', "set bull to $ref->{to} to private");
213 if ($pcno == 29) { # incoming text
214 my $ref = get_fwq($fromnode, $stream);
217 if (@{$ref->{lines}}) {
218 push @{$ref->{lines}}, $f[4];
220 # temporarily store any R: lines so that we end up with
221 # only the first and last ones stored.
222 if ($f[4] =~ m|^R:\d{6}/\d{4}|) {
223 push @{$ref->{tempr}}, $f[4];
225 if (exists $ref->{tempr}) {
226 push @{$ref->{lines}}, shift @{$ref->{tempr}};
227 push @{$ref->{lines}}, pop @{$ref->{tempr}} if @{$ref->{tempr}};
228 delete $ref->{tempr};
230 push @{$ref->{lines}}, $f[4];
234 if ($ref->{count} >= $ref->{linesreq}) {
235 $self->send(DXProt::pc31($fromnode, $tonode, $stream));
236 dbg("stream $stream: $ref->{count} lines received\n") if isdbg('msg');
239 $ref->{lastt} = $main::systime;
241 dbg("PC29 from unknown stream $stream from $fromnode") if isdbg('msg');
242 $self->send(DXProt::pc42($fromnode, $tonode, $stream)); # unknown stream
247 if ($pcno == 30) { # this is a incoming subject ack
248 my $ref = get_fwq($fromnode); # note no stream at this stage
251 $ref->{stream} = $stream;
253 $ref->{linesreq} = 5;
254 set_fwq($fromnode, $stream, $ref); # new ref
255 set_busy($fromnode, $ref); # interlock
256 dbg("incoming subject ack stream $stream\n") if isdbg('msg');
257 $ref->{lines} = [ $ref->read_msg_body ];
258 $ref->send_tranche($self);
259 $ref->{lastt} = $main::systime;
261 dbg("PC30 from unknown stream $stream from $fromnode") if isdbg('msg');
262 $self->send(DXProt::pc42($fromnode, $tonode, $stream)); # unknown stream
267 if ($pcno == 31) { # acknowledge a tranche of lines
268 my $ref = get_fwq($fromnode, $stream);
270 dbg("tranche ack stream $stream\n") if isdbg('msg');
271 $ref->send_tranche($self);
272 $ref->{lastt} = $main::systime;
274 dbg("PC31 from unknown stream $stream from $fromnode") if isdbg('msg');
275 $self->send(DXProt::pc42($fromnode, $tonode, $stream)); # unknown stream
280 if ($pcno == 32) { # incoming EOM
281 dbg("stream $stream: EOM received\n") if isdbg('msg');
282 my $ref = get_fwq($fromnode, $stream);
284 $self->send(DXProt::pc33($fromnode, $tonode, $stream)); # acknowledge it
286 # get the next msg no - note that this has NOTHING to do with the stream number in PC protocol
287 # store the file or message
288 # remove extraneous rubbish from the hash
289 # remove it from the work in progress vector
290 # stuff it on the msg queue
293 $ref->store($ref->{lines});
296 # does an identical message already exist?
299 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}) {
300 $ref->stop_msg($fromnode);
301 my $msgno = $m->{msgno};
302 dbg("duplicate message from $ref->{from} -> $ref->{to} to msg: $msgno") if isdbg('msg');
303 Log('msg', "duplicate message from $ref->{from} -> $ref->{to} to msg: $msgno");
309 $ref->swop_it($self->call);
311 # look for 'bad' to addresses
312 if ($ref->dump_it($self->call)) {
313 $ref->stop_msg($fromnode);
314 dbg("'Bad' message $ref->{to}") if isdbg('msg');
315 Log('msg', "'Bad' message $ref->{to}");
319 # check the message for bad words
321 for (@{$ref->{lines}}) {
322 push @words, BadWords::check($_);
324 push @words, BadWords::check($ref->{subject});
326 dbg("$ref->{from} swore: '@words' -> $ref->{to} '$ref->{subject}' origin: $ref->{origin} via " . $self->call) if isdbg('msg');
327 Log('msg',"$ref->{from} swore: '@words' -> $ref->{to} origin: $ref->{origin} via " . $self->call);
328 Log('msg',"subject: $ref->{subject}");
329 for (@{$ref->{lines}}) {
330 Log('msg', "line: $_");
332 $ref->stop_msg($fromnode);
336 $ref->{msgno} = next_transno("Msgno");
337 push @{$ref->{gotit}}, $fromnode; # mark this up as being received
338 $ref->store($ref->{lines});
341 Log('msg', "Message $ref->{msgno} from $ref->{from} received from $fromnode for $ref->{to}");
344 $ref->stop_msg($fromnode);
346 dbg("PC32 from unknown stream $stream from $fromnode") if isdbg('msg');
347 $self->send(DXProt::pc42($fromnode, $tonode, $stream)); # unknown stream
353 if ($pcno == 33) { # acknowledge the end of message
354 my $ref = get_fwq($fromnode, $stream);
356 if ($ref->{private}) { # remove it if it private and gone off site#
357 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $fromnode and deleted");
360 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $fromnode");
361 push @{$ref->{gotit}}, $fromnode; # mark this up as being received
362 $ref->store($ref->{lines}); # re- store the file
364 $ref->stop_msg($fromnode);
366 dbg("PC33 from unknown stream $stream from $fromnode") if isdbg('msg');
367 $self->send(DXProt::pc42($fromnode, $tonode, $stream)); # unknown stream
370 # send next one if present
375 if ($pcno == 40) { # this is a file request
376 $f[3] =~ s/\\/\//og; # change the slashes
377 $f[3] =~ s/\.//og; # remove dots
378 $f[3] =~ s/^\///o; # remove the leading /
379 $f[3] = lc $f[3]; # to lower case;
380 dbg("incoming file $f[3]\n") if isdbg('msg');
381 $f[3] = 'packclus/' . $f[3] unless $f[3] =~ /^packclus\//o;
383 # create any directories
384 my @part = split /\//, $f[3];
386 my $fn = "$main::root";
387 pop @part; # remove last part
388 foreach $part (@part) {
391 last SWITCH if !mkdir $fn, 0777;
392 dbg("created directory $fn\n") if isdbg('msg');
394 my $stream = next_transno($fromnode);
395 my $ref = DXMsg->alloc($stream, "$main::root/$f[3]", $self->call, time, !$f[4], $f[3], ' ', '0', '0');
397 # forwarding variables
398 $ref->{fromnode} = $tonode;
399 $ref->{tonode} = $fromnode;
400 $ref->{linesreq} = $f[5];
401 $ref->{stream} = $stream;
402 $ref->{count} = 0; # no of lines between PC31s
404 $ref->{lastt} = $main::systime;
405 set_fwq($fromnode, $stream, $ref); # store in work
406 $self->send(DXProt::pc30($fromnode, $tonode, $stream)); # send ack
411 if ($pcno == 42) { # abort transfer
412 dbg("stream $stream: abort received\n") if isdbg('msg');
413 my $ref = get_fwq($fromnode, $stream);
415 $ref->stop_msg($fromnode);
421 if ($pcno == 49) { # global delete on subject
423 if ($_->{from} eq $f[1] && $_->{subject} eq $f[2]) {
425 Log('msg', "Message $_->{msgno} from $_->{from} ($_->{subject}) fully deleted");
426 DXChannel::broadcast_nodes($line, $self);
438 my $uref = DXUser->get($to);
439 my $dxchan = DXChannel->get($to);
440 if (((*Net::SMTP && $email_server) || $email_prog) && $uref && $uref->wantemail) {
441 my $email = $uref->email;
443 my @rcpt = ref $email ? @{$email} : $email;
444 my $fromaddr = $email_from || $main::myemail;
445 my @headers = ("To: $ref->{to}",
447 "Subject: [DXSpider: $ref->{from}] $ref->{subject}",
448 "X-DXSpider-To: $ref->{to}",
449 "X-DXSpider-From: $ref->{from}\@$ref->{origin}",
450 "X-DXSpider-Gateway: $main::mycall"
452 my @data = ("Msgno: $ref->{msgno} To: $to From: $ref->{from}\@$ref->{origin} Gateway: $main::mycall",
458 if (*Net::SMTP && $email_server) {
459 $msg = Net::SMTP->new($email_server);
461 $msg->mail($fromaddr);
463 $msg->data(map {"$_\n"} @headers, '', @data);
466 } elsif ($email_prog) {
467 $msg = new IO::File "|$email_prog " . join(' ', @rcpt);
469 print $msg map {"$_\r\n"} @headers, '', @data, '.';
473 dbg("email forwarding error $!") if isdbg('msg') && !$msg && defined $!;
476 $dxchan->send($dxchan->msg('m9')) if $dxchan && $dxchan->is_user;
479 # store a message away on disc or whatever
481 # NOTE the second arg is a REFERENCE not a list
487 if ($ref->{file}) { # a file
488 dbg("To be stored in $ref->{to}\n") if isdbg('msg');
490 my $fh = new IO::File "$ref->{to}", "w";
493 foreach $line (@{$lines}) {
497 dbg("file $ref->{to} stored\n") if isdbg('msg');
498 Log('msg', "file $ref->{to} from $ref->{from} stored" );
500 confess "can't open file $ref->{to} $!";
502 } else { # a normal message
504 # attempt to open the message file
505 my $fn = filename($ref->{msgno});
507 dbg("To be stored in $fn\n") if isdbg('msg');
509 # now save the file, overwriting what's there, YES I KNOW OK! (I will change it if it's a problem)
510 my $fh = new IO::File "$fn", "w";
512 my $rr = $ref->{rrreq} ? '1' : '0';
513 my $priv = $ref->{private} ? '1': '0';
514 my $del = $ref->{delete} ? '1' : '0';
515 my $delt = $ref->{deletetime} || '0';
516 print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$priv^$ref->{subject}^$ref->{origin}^$ref->{'read'}^$rr^$del^$delt\n";
517 print $fh "=== ", join('^', @{$ref->{gotit}}), "\n";
520 foreach $line (@{$lines}) {
521 $ref->{size} += (length $line) + 1;
525 dbg("msg $ref->{msgno} stored\n") if isdbg('msg');
526 Log('msg', "msg $ref->{msgno} from $ref->{from} to $ref->{to} stored" );
528 confess "can't open msg file $fn $!";
532 # actual remove all the 'deleted' messages in one hit.
533 # this has to me delayed until here otherwise it only does one at
534 # a time because @msg is rewritten everytime del_msg is called.
535 my @del = grep {!$_->{tonode} && $_->{delete} && $_->{deletetime} < $main::systime} @msg;
547 $call = ' by ' . $dxchan->call if $dxchan;
549 if ($self->{tonode}) {
551 $self->{deletetime} = 0;
552 dbg("Msgno $self->{msgno} but marked as expunged$call") if isdbg('msg');
554 # remove it from the active message list
555 @msg = grep { $_ != $self } @msg;
557 Log('msg', "Msgno $self->{msgno} expunged$call");
558 dbg("Msgno $self->{msgno} expunged$call") if isdbg('msg');
561 unlink filename($self->{msgno});
569 $t = $main::systime + $residencetime unless defined $t;
572 $ref->{deletetime} = $t;
573 $ref->store( [$ref->read_msg_body] );
580 delete $ref->{delete};
581 delete $ref->{deletetime};
584 # clean out old messages from the message queue
589 # mark old messages for deletion
590 foreach $ref (@msg) {
591 if (ref($ref) && !$ref->{keep} && $ref->{t} < $main::systime - $maxage) {
593 # this is for IMMEDIATE destruction
595 $ref->{deletetime} = 0;
600 # read in a message header
610 $file = new IO::File "$fn";
612 dbg("Error reading $fn $!");
613 Log('err', "Error reading $fn $!");
617 $line = <$file>; # first line
618 if ($size == 0 || !$line) {
620 Log('err', "Empty $fn $!");
624 $size -= length $line;
625 if (! $line =~ /^===/o) {
626 dbg("corrupt first line in $fn ($line)");
627 Log('err', "corrupt first line in $fn ($line)");
631 @f = split /\^/, $line;
632 $ref = DXMsg->alloc(@f);
634 $line = <$file>; # second line
636 $size -= length $line;
637 if (! $line =~ /^===/o) {
638 dbg("corrupt second line in $fn ($line)");
639 Log('err', "corrupt second line in $fn ($line)");
644 @f = split /\^/, $line;
645 push @{$ref->{gotit}}, @f;
646 $ref->{size} = $size;
653 # read in a message header
657 my $msgno = $self->{msgno};
660 my $fn = filename($msgno);
663 $file = new IO::File;
664 if (!open($file, $fn)) {
665 dbg("Error reading $fn $!");
666 Log('err' ,"Error reading $fn $!");
669 @out = map {chomp; $_} <$file>;
672 shift @out if $out[0] =~ /^=== /;
673 shift @out if $out[0] =~ /^=== /;
677 # send a tranche of lines to the other end
680 my ($self, $dxchan) = @_;
682 my $to = $self->{tonode};
683 my $from = $self->{fromnode};
684 my $stream = $self->{stream};
685 my $lines = $self->{lines};
688 for ($i = 0, $c = $self->{count}; $i < $self->{linesreq} && $c < @$lines; $i++, $c++) {
689 push @out, DXProt::pc29($to, $from, $stream, $lines->[$c]);
693 push @out, DXProt::pc32($to, $from, $stream) if $i < $self->{linesreq};
698 # find a message to send out and start the ball rolling
705 # bat down the message list looking for one that needs to go off site and whose
706 # nearest node is not busy.
708 dbg("queue msg ($sort)\n") if isdbg('msg');
709 my @nodelist = DXChannel::get_all_nodes;
710 foreach $ref (@msg) {
712 # ignore 'delayed' messages until their waiting time has expired
713 if (exists $ref->{waitt}) {
714 next if $ref->{waitt} > $main::systime;
715 delete $ref->{waitt};
719 if (exists $ref->{lastt} && $main::systime >= $ref->{lastt} + $timeout) {
720 my $node = $ref->{tonode};
721 dbg("Timeout, stopping msgno: $ref->{msgno} -> $node") if isdbg('msg');
722 Log('msg', "Timeout, stopping msgno: $ref->{msgno} -> $node");
723 $ref->stop_msg($node);
725 # delay any outgoing messages that fail
726 $ref->{waitt} = $main::systime + $waittime + rand(120) if $node ne $main::mycall;
727 delete $ref->{lastt};
731 # is it being sent anywhere currently?
732 next if $ref->{tonode}; # ignore it if it already being processed
734 # is it awaiting deletion?
735 next if $ref->{delete};
737 # firstly, is it private and unread? if so can I find the recipient
738 # in my cluster node list offsite?
740 # deal with routed private messages
742 if ($ref->{private}) {
743 next if $ref->{'read'}; # if it is read, it is stuck here
744 $clref = Route::get($ref->{to});
746 $dxchan = $clref->dxchan;
748 if ($dxchan->is_node) {
749 next if $clref->call eq $main::mycall; # i.e. it lives here
750 $ref->start_msg($dxchan) if !get_busy($dxchan->call) && $dxchan->state eq 'normal';
753 dbg("Route: No dxchan for $ref->{to} " . ref($clref) ) if isdbg('msg');
758 # otherwise we are dealing with a bulletin or forwarded private message
759 # compare the gotit list with
760 # the nodelist up above, if there are sites that haven't got it yet
761 # then start sending it - what happens when we get loops is anyone's
762 # guess, use (to, from, time, subject) tuple?
763 foreach $dxchan (@nodelist) {
764 my $call = $dxchan->call;
766 next if $call eq $main::mycall;
767 next if ref $ref->{gotit} && grep $_ eq $call, @{$ref->{gotit}};
768 next unless $ref->forward_it($call); # check the forwarding file
769 next if $ref->{tonode}; # ignore it if it already being processed
771 # if we are here we have a node that doesn't have this message
772 if (!get_busy($call) && $dxchan->state eq 'normal') {
773 $ref->start_msg($dxchan);
779 # if all the available nodes are busy then stop
780 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
786 # is there a message for me?
793 foreach $ref (@msg) {
794 # is it for me, private and unread?
795 if ($ref->{to} eq $call && $ref->{private}) {
796 $count++ unless $ref->{'read'} || $ref->{delete};
802 # start the message off on its travels with a PC28
805 my ($self, $dxchan) = @_;
807 confess("trying to start started msg $self->{msgno} nodes: $self->{fromnode} -> $self->{tonode}") if $self->{tonode};
808 dbg("start msg $self->{msgno}\n") if isdbg('msg');
809 $self->{linesreq} = 10;
811 $self->{tonode} = $dxchan->call;
812 $self->{fromnode} = $main::mycall;
813 set_busy($self->{tonode}, $self);
814 set_fwq($self->{tonode}, undef, $self);
815 $self->{lastt} = $main::systime;
816 my ($fromnode, $origin);
817 $fromnode = $self->{fromnode};
818 $origin = $self->{origin};
819 $dxchan->send(DXProt::pc28($self->{tonode}, $fromnode, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $origin, $self->{rrreq}));
822 # get the ref of a busy node
832 return $busy{$call} = shift;
838 return delete $busy{$call};
841 # get the whole busy queue
847 # get a forwarding queue entry
851 my $stream = shift || '0';
852 return $work{"$call,$stream"};
855 # delete a forwarding queue entry
859 my $stream = shift || '0';
860 return delete $work{"$call,$stream"};
867 my $stream = shift || '0';
868 return $work{"$call,$stream"} = shift;
871 # get the whole forwarding queue
877 # stop a message from continuing, clean it out, unlock interlocks etc
882 my $stream = $self->{stream};
885 dbg("stop msg $self->{msgno} -> node $node\n") if isdbg('msg');
886 del_fwq($node, $stream);
894 delete $ref->{lines};
895 delete $ref->{linesreq};
896 delete $ref->{tonode};
897 delete $ref->{fromnode};
898 delete $ref->{stream};
900 delete $ref->{count};
901 delete $ref->{tempr};
902 delete $ref->{lastt};
903 delete $ref->{waitt};
906 # get a new transaction number from the file specified
910 $name =~ s/\W//og; # remove non-word characters
911 my $fn = "$msgdir/$name";
914 my $fh = new IO::File;
915 if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
917 $msgno = $fh->getline || '0';
921 $fh->print("$msgno\n");
922 dbg("msgno $msgno allocated for $name\n") if isdbg('msg');
925 confess "can't open $fn $!";
930 # initialise the message 'system', read in all the message headers
933 my $dir = new IO::File;
937 # load various control files
938 dbg("load badmsg: " . (load_badmsg() or "Ok"));
939 dbg("load forward: " . (load_forward() or "Ok"));
940 dbg("load swop: " . (load_swop() or "Ok"));
942 # read in the directory
943 opendir($dir, $msgdir) or confess "can't open $msgdir $!";
944 @dir = readdir($dir);
949 next unless /^m\d\d\d\d\d\d$/;
951 $ref = read_msg_header("$msgdir/$_");
954 Log('err', "Deleting $_");
959 # delete any messages to 'badmsg.pl' places
960 if ($ref->dump_it('')) {
961 dbg("'Bad' TO address $ref->{to}") if isdbg('msg');
962 Log('msg', "'Bad' TO address $ref->{to}");
967 # add the message to the available queue
972 # add the message to the directory listing
976 confess "tried to add a non-ref to the msg directory" if !ref $ref;
980 # return all the current messages
986 # get a particular message
991 return $_ if $_->{msgno} == $msgno;
992 last if $_->{msgno} > $msgno;
997 # return the official filename for a message no
1000 return sprintf "$msgdir/m%06d", shift;
1004 # return a list of valid elements
1009 return keys(%valid);
1013 # return a prompt for a field
1018 my ($self, $ele) = @_;
1019 return $valid{$ele};
1023 # send a message state machine
1030 if ($self->state eq 'send1') {
1032 confess "local var gone missing" if !ref $self->{loc};
1033 my $loc = $self->{loc};
1034 if (my @ans = BadWords::check($line)) {
1035 $self->{badcount} += @ans;
1036 Log('msg', $self->call . " used badwords: @ans to @{$loc->{to}} in msg");
1039 $loc->{subject} = $line;
1041 $self->state('sendbody');
1042 #push @out, $self->msg('sendbody');
1043 push @out, $self->msg('m8');
1044 } elsif ($self->state eq 'sendbody') {
1045 confess "local var gone missing" if !ref $self->{loc};
1046 my $loc = $self->{loc};
1047 if ($line eq "\032" || $line eq '%1A' || uc $line eq "/EX") {
1049 unless ($loc->{reject}) {
1050 foreach $to (@{$loc->{to}}) {
1052 my $systime = $main::systime;
1053 my $mycall = $main::mycall;
1054 $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
1056 exists $loc->{from} ? $loc->{from} : $self->call,
1060 exists $loc->{origin} ? $loc->{origin} : $mycall,
1063 $ref->swop_it($self->call);
1064 $ref->store($loc->{lines});
1066 push @out, $self->msg('m11', $ref->{msgno}, $to);
1067 #push @out, "msgno $ref->{msgno} sent to $to";
1071 Log('msg', $self->call . " swore to @{$loc->{to}} subject: '$loc->{subject}' in msg, REJECTED");
1074 delete $loc->{lines};
1076 delete $self->{loc};
1079 $self->state('prompt');
1080 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
1081 #push @out, $self->msg('sendabort');
1082 push @out, $self->msg('m10');
1083 delete $loc->{lines};
1085 delete $self->{loc};
1087 $self->state('prompt');
1089 if (my @ans = BadWords::check($line)) {
1090 $self->{badcount} += @ans;
1091 Log('msg', $self->call . " used badwords: @ans to @{$loc->{to}} subject: '$loc->{subject}' in msg") unless $loc->{reject};
1092 Log('msg', "line: $line");
1096 if ($loc->{lines} && @{$loc->{lines}}) {
1097 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
1099 # temporarily store any R: lines so that we end up with
1100 # only the first and last ones stored.
1101 if ($line =~ m|^R:\d{6}/\d{4}|) {
1102 push @{$loc->{tempr}}, $line;
1104 if (exists $loc->{tempr}) {
1105 push @{$loc->{lines}}, shift @{$loc->{tempr}};
1106 push @{$loc->{lines}}, pop @{$loc->{tempr}} if @{$loc->{tempr}};
1107 delete $loc->{tempr};
1109 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
1113 # i.e. it ain't and end or abort, therefore store the line
1119 # return the standard directory line for this ref
1123 my $flag = $ref->{private} && $ref->{read} ? '-' : ' ';
1124 if ($ref->{delete}) {
1125 $flag = $ref->{deletetime} > $main::systime ? 'D' : 'E';
1127 return sprintf("%6d%s%s%5d %8.8s %8.8s %-6.6s %5.5s %-30.30s",
1128 $ref->{msgno}, $flag, $ref->{private} ? 'p' : ' ',
1129 $ref->{size}, $ref->{to}, $ref->{from}, cldate($ref->{t}),
1130 ztime($ref->{t}), $ref->{subject});
1133 # load the forward table
1137 my $s = readfilestr($forwardfn);
1140 push @out, $@ if $@;
1145 # load the bad message table
1149 my $s = readfilestr($badmsgfn);
1152 push @out, $@ if $@;
1157 # load the swop message table
1161 my $s = readfilestr($swopfn);
1164 push @out, $@ if $@;
1170 # forward that message or not according to the forwarding table
1171 # returns 1 for forward, 0 - to ignore
1180 for ($i = 0; $i < @forward; $i += 5) {
1181 my ($sort, $field, $pattern, $action, $bbs) = @forward[$i..($i+4)];
1184 # are we interested?
1185 next if $ref->{private} && $sort ne 'P';
1186 next if !$ref->{private} && $sort ne 'B';
1189 $tested = $ref->{to} if $field eq 'T';
1190 $tested = $ref->{from} if $field eq 'F';
1191 $tested = $ref->{origin} if $field eq 'O';
1192 $tested = $ref->{subject} if $field eq 'S';
1194 if (!$pattern || $tested =~ m{$pattern}i) {
1195 return 0 if $action eq 'I';
1196 return 1 if !$bbs || grep $_ eq $call, @{$bbs};
1203 # look down the forward table to see whether this is a valid bull
1204 # or not (ie it will forward somewhere even if it is only here)
1212 return 1 if $call =~ /^ALL/;
1213 return 1 if $call =~ /^DX/;
1217 for ($i = 0; $i < @forward; $i += 5) {
1218 my ($sort, $field, $pattern, $action, $bbs) = @forward[$i..($i+4)];
1219 if ($field eq 'T') {
1220 if (!$pattern || $call =~ m{$pattern}i) {
1234 for ($i = 0; $i < @badmsg; $i += 3) {
1235 my ($sort, $field, $pattern) = @badmsg[$i..($i+2)];
1238 # are we interested?
1239 next if $ref->{private} && $sort ne 'P';
1240 next if !$ref->{private} && $sort ne 'B';
1243 $tested = $ref->{to} if $field eq 'T';
1244 $tested = $ref->{from} if $field eq 'F';
1245 $tested = $ref->{origin} if $field eq 'O';
1246 $tested = $ref->{subject} if $field eq 'S';
1247 $tested = $call if $field eq 'I';
1249 if (!$pattern || $tested =~ m{$pattern}i) {
1263 for ($i = 0; $i < @swop; $i += 5) {
1264 my ($sort, $field, $pattern, $tfield, $topattern) = @swop[$i..($i+4)];
1269 # are we interested?
1270 next if $ref->{private} && $sort ne 'P';
1271 next if !$ref->{private} && $sort ne 'B';
1274 $tested = $ref->{to} if $field eq 'T';
1275 $tested = $ref->{from} if $field eq 'F';
1276 $tested = $ref->{origin} if $field eq 'O';
1277 $tested = $ref->{subject} if $field eq 'S';
1280 $old = $swop = $ref->{to} if $tfield eq 'T';
1281 $old = $swop = $ref->{from} if $tfield eq 'F';
1282 $old = $swop = $ref->{origin} if $tfield eq 'O';
1283 $old = $swop = $ref->{subject} if $tfield eq 'S';
1285 if ($tested =~ m{$pattern}i) {
1286 if ($tested eq $swop) {
1287 $swop =~ s{$pattern}{$topattern}i;
1291 Log('msg', "Msg $ref->{msgno}: $tfield $old -> $swop");
1292 Log('dbg', "Msg $ref->{msgno}: $tfield $old -> $swop");
1293 $ref->{to} = $swop if $tfield eq 'T';
1294 $ref->{from} = $swop if $tfield eq 'F';
1295 $ref->{origin} = $swop if $tfield eq 'O';
1296 $ref->{subject} = $swop if $tfield eq 'S';
1303 # import any msgs in the import directory
1304 # the messages are in BBS format (but may have cluster extentions
1305 # so SB UK < GB7TLH is legal
1308 # are there any to do in this directory?
1309 return unless -d $importfn;
1310 unless (opendir(DIR, $importfn)) {
1311 dbg("can\'t open $importfn $!") if isdbg('msg');
1312 Log('msg', "can\'t open $importfn $!");
1316 my @names = readdir(DIR);
1319 foreach $name (@names) {
1320 next if $name =~ /^\./;
1321 my $splitit = $name =~ /^split/;
1322 my $fn = "$importfn/$name";
1324 unless (open(MSG, $fn)) {
1325 dbg("can\'t open import file $fn $!") if isdbg('msg');
1326 Log('msg', "can\'t open import file $fn $!");
1330 my @msg = map { chomp; $_ } <MSG>;
1333 my @out = import_one($main::me, \@msg, $splitit);
1338 # import one message as a list in bbs (as extended) mode
1339 # takes a reference to an array containing the whole message
1344 my $splitit = shift;
1348 my $from = $dxchan->call;
1349 my $origin = $main::mycall;
1354 my $line = shift @$ref;
1355 my @f = split /([\s\@\$])/, $line;
1356 @f = map {s/\s+//g; length $_ ? $_ : ()} @f;
1358 unless (@f && $f[0] =~ /^(:?S|SP|SB|SEND)$/ ) {
1359 my $m = "invalid first line in import '$line'";
1360 dbg($m) if isdbg('msg');
1364 my $f = uc shift @f;
1365 next if $f eq 'SEND';
1367 # private / noprivate / rr
1368 if ($notincalls && ($f eq 'B' || $f eq 'SB' || $f =~ /^NOP/oi)) {
1370 } elsif ($notincalls && ($f eq 'P' || $f eq 'SP' || $f =~ /^PRI/oi)) {
1372 } elsif ($notincalls && ($f eq 'RR')) {
1374 } elsif (($f =~ /^[\@\.\#\$]$/ || $f eq '.#') && @f) { # this is bbs syntax, for AT
1376 } elsif ($f eq '<' && @f) { # this is bbs syntax for from call
1377 $from = uc shift @f;
1378 } elsif ($f =~ /^\$/) { # this is bbs syntax for a bid
1380 } elsif ($f =~ /^<(\S+)/) { # this is bbs syntax for from call
1382 } elsif ($f =~ /^\$\S+/) { # this is bbs syntax for bid
1389 # is this callsign a distro?
1390 my $fn = "$msgdir/distro/$f.pl";
1392 my $fh = new IO::File $fn;
1399 return (1, "Error in Distro $f.pl:", $@) if $@;
1407 if (grep $_ eq $f, @DXMsg::badmsg) {
1408 push @out, $dxchan->msg('m3', $f);
1415 # subject is the next line
1416 my $subject = shift @$ref;
1418 # strip off trailing lines
1419 pop @$ref while (@$ref && $$ref[-1] =~ /^\s*$/);
1421 # strip off /EX or /ABORT
1422 return ("aborted") if @$ref && $$ref[-1] =~ m{^/ABORT$}i;
1423 pop @$ref if (@$ref && $$ref[-1] =~ m{^/EX$}i);
1425 # sort out any splitting that needs to be done
1431 if ($lth >= $maxchunk || ($lth > $minchunk && /^\s*$/)) {
1432 push @chunk, $lines;
1439 push @chunk, $lines if @$lines;
1444 # does an identical message already exist?
1447 if (substr($subject,0,28) eq substr($m->{subject},0,28) && $from eq $m->{from} && grep $m->{to} eq $_, @to) {
1448 my $msgno = $m->{msgno};
1449 dbg("duplicate message from $from -> $m->{to} to msg: $msgno") if isdbg('msg');
1450 Log('msg', "duplicate message from $from -> $m->{to} to msg: $msgno");
1455 # write all the messages away
1457 for ( $i = 0; $i < @chunk; $i++) {
1458 my $chunk = $chunk[$i];
1461 my $num = " [" . ($i+1) . "/" . scalar @chunk . "]";
1462 $ch_subject = substr($subject, 0, 27 - length $num) . $num;
1464 $ch_subject = $subject;
1468 my $systime = $main::systime;
1469 my $mycall = $main::mycall;
1470 my $mref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
1479 $mref->swop_it($main::mycall);
1480 $mref->store($chunk);
1482 push @out, $dxchan->msg('m11', $mref->{msgno}, $to);
1483 #push @out, "msgno $ref->{msgno} sent to $to";
1494 my $name = $AUTOLOAD;
1495 return if $name =~ /::DESTROY$/;
1498 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
1499 # this clever line of code creates a subroutine which takes over from autoload
1500 # from OO Perl - Conway
1501 *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
1502 @_ ? $self->{$name} = shift : $self->{$name} ;