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,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 || ($self->{t} + $maxage);
129 $self->{keep} = shift;
131 # $self->{lastt} = $main::systime;
133 $self->{private} = 1 if $bulltopriv && DXUser->get_current($self->{to});
141 # this is periodic processing
142 if ($main::systime >= $lastq + $queueinterval) {
144 # queue some message if the interval timer has gone off
147 # import any messages in the import directory
150 $lastq = $main::systime;
153 # clean the message queue
154 clean_old() if $main::systime - $last_clean > 3600 ;
156 # actual remove all the 'deleted' messages in one hit.
157 # this has to be delayed until here otherwise it only does one at
158 # a time because @msg is rewritten everytime del_msg is called.
159 my @del = grep {!$_->{tonode} && $_->{delete} && !$_->{keep} && $_->{deletetime} < $main::systime} @msg;
164 $last_clean = $main::systime;
171 my ($tonode, $fromnode) = @_[1..2];
173 # sort out various extant protocol errors that occur
175 $origin = $dxchan->call unless $origin && $origin gt ' ';
177 # first look for any messages in the busy queue
178 # and cancel them this should both resolve timed out incoming messages
179 # and crossing of message between nodes, incoming messages have priority
181 my $ref = get_busy($fromnode);
183 my $otonode = $ref->{tonode} || "unknown";
184 dbg("Busy, stopping msgno: $ref->{msgno} $fromnode->$otonode") if isdbg('msg');
185 $ref->stop_msg($fromnode);
188 my $t = cltounix($_[5], $_[6]);
189 my $stream = next_transno($fromnode);
190 $ref = DXMsg->alloc($stream, uc $_[3], $_[4], $t, $_[7], $_[8], $origin, '0', $_[11]);
192 # fill in various forwarding state variables
193 $ref->{fromnode} = $fromnode;
194 $ref->{tonode} = $tonode;
195 $ref->{rrreq} = $_[11];
196 $ref->{linesreq} = $_[10];
197 $ref->{stream} = $stream;
198 $ref->{count} = 0; # no of lines between PC31s
199 dbg("new message from $_[4] to $_[3] '$_[8]' stream $fromnode/$stream\n") if isdbg('msg');
200 Log('msg', "Incoming message $_[4] to $_[3] '$_[8]' origin: $origin" );
201 set_fwq($fromnode, $stream, $ref); # store in work
202 set_busy($fromnode, $ref); # set interlock
203 $dxchan->send(DXProt::pc30($fromnode, $tonode, $stream)); # send ack
204 $ref->{lastt} = $main::systime;
206 # look to see whether this is a non private message sent to a known callsign
207 my $uref = DXUser->get_current($ref->{to});
208 if (is_callsign($ref->{to}) && !$ref->{private} && $uref && $uref->homenode) {
210 dbg("set bull to $ref->{to} to private") if isdbg('msg');
211 Log('msg', "set bull to $ref->{to} to private");
219 my ($tonode, $fromnode, $stream) = @_[1..3];
221 my $ref = get_fwq($fromnode, $stream);
224 if (@{$ref->{lines}}) {
225 push @{$ref->{lines}}, $_[4];
227 # temporarily store any R: lines so that we end up with
228 # only the first and last ones stored.
229 if ($_[4] =~ m|^R:\d{6}/\d{4}|) {
230 push @{$ref->{tempr}}, $_[4];
232 if (exists $ref->{tempr}) {
233 push @{$ref->{lines}}, shift @{$ref->{tempr}};
234 push @{$ref->{lines}}, pop @{$ref->{tempr}} if @{$ref->{tempr}};
235 delete $ref->{tempr};
237 push @{$ref->{lines}}, $_[4];
241 if ($ref->{count} >= $ref->{linesreq}) {
242 $dxchan->send(DXProt::pc31($fromnode, $tonode, $stream));
243 dbg("stream $stream: $ref->{count} lines received\n") if isdbg('msg');
246 $ref->{lastt} = $main::systime;
248 dbg("PC29 from unknown stream $stream from $fromnode") if isdbg('msg');
249 $dxchan->send(DXProt::pc42($fromnode, $tonode, $stream)); # unknown stream
253 # this is a incoming subject ack
257 my ($tonode, $fromnode, $stream) = @_[1..3];
259 my $ref = get_fwq($fromnode); # note no stream at this stage
262 $ref->{stream} = $stream;
264 $ref->{linesreq} = 5;
265 set_fwq($fromnode, $stream, $ref); # new ref
266 set_busy($fromnode, $ref); # interlock
267 dbg("incoming subject ack stream $stream\n") if isdbg('msg');
268 $ref->{lines} = [ $ref->read_msg_body ];
269 $ref->send_tranche($dxchan);
270 $ref->{lastt} = $main::systime;
272 dbg("PC30 from unknown stream $stream from $fromnode") if isdbg('msg');
273 $dxchan->send(DXProt::pc42($fromnode, $tonode, $stream)); # unknown stream
277 # acknowledge a tranche of lines
281 my ($tonode, $fromnode, $stream) = @_[1..3];
283 my $ref = get_fwq($fromnode, $stream);
285 dbg("tranche ack stream $stream\n") if isdbg('msg');
286 $ref->send_tranche($dxchan);
287 $ref->{lastt} = $main::systime;
289 dbg("PC31 from unknown stream $stream from $fromnode") if isdbg('msg');
290 $dxchan->send(DXProt::pc42($fromnode, $tonode, $stream)); # unknown stream
298 my ($tonode, $fromnode, $stream) = @_[1..3];
300 dbg("stream $stream: EOM received\n") if isdbg('msg');
301 my $ref = get_fwq($fromnode, $stream);
303 $dxchan->send(DXProt::pc33($fromnode, $tonode, $stream)); # acknowledge it
305 # get the next msg no - note that this has NOTHING to do with the stream number in PC protocol
306 # store the file or message
307 # remove extraneous rubbish from the hash
308 # remove it from the work in progress vector
309 # stuff it on the msg queue
312 $ref->store($ref->{lines});
315 # does an identical message already exist?
318 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}) {
319 $ref->stop_msg($fromnode);
320 my $msgno = $m->{msgno};
321 dbg("duplicate message from $ref->{from} -> $ref->{to} to msg: $msgno") if isdbg('msg');
322 Log('msg', "duplicate message from $ref->{from} -> $ref->{to} to msg: $msgno");
328 $ref->swop_it($dxchan->call);
330 # look for 'bad' to addresses
331 if ($ref->dump_it($dxchan->call)) {
332 $ref->stop_msg($fromnode);
333 dbg("'Bad' message $ref->{to}") if isdbg('msg');
334 Log('msg', "'Bad' message $ref->{to}");
338 # check the message for bad words
340 for (@{$ref->{lines}}) {
341 push @words, BadWords::check($_);
343 push @words, BadWords::check($ref->{subject});
345 dbg("$ref->{from} swore: '@words' -> $ref->{to} '$ref->{subject}' origin: $ref->{origin} via " . $dxchan->call) if isdbg('msg');
346 Log('msg',"$ref->{from} swore: '@words' -> $ref->{to} origin: $ref->{origin} via " . $dxchan->call);
347 Log('msg',"subject: $ref->{subject}");
348 for (@{$ref->{lines}}) {
349 Log('msg', "line: $_");
351 $ref->stop_msg($fromnode);
355 $ref->{msgno} = next_transno("Msgno");
356 push @{$ref->{gotit}}, $fromnode; # mark this up as being received
357 $ref->store($ref->{lines});
360 Log('msg', "Message $ref->{msgno} from $ref->{from} received from $fromnode for $ref->{to}");
363 $ref->stop_msg($fromnode);
365 dbg("PC32 from unknown stream $stream from $fromnode") if isdbg('msg');
366 $dxchan->send(DXProt::pc42($fromnode, $tonode, $stream)); # unknown stream
371 # acknowledge the end of message
375 my ($tonode, $fromnode, $stream) = @_[1..3];
377 my $ref = get_fwq($fromnode, $stream);
379 if ($ref->{private}) { # remove it if it private and gone off site#
380 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $fromnode and deleted");
383 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $fromnode");
384 push @{$ref->{gotit}}, $fromnode; # mark this up as being received
385 $ref->store($ref->{lines}); # re- store the file
387 $ref->stop_msg($fromnode);
389 dbg("PC33 from unknown stream $stream from $fromnode") if isdbg('msg');
390 $dxchan->send(DXProt::pc42($fromnode, $tonode, $stream)); # unknown stream
393 # send next one if present
397 # this is a file request
401 my ($tonode, $fromnode) = @_[1..2];
403 $_[3] =~ s/\\/\//og; # change the slashes
404 $_[3] =~ s/\.//og; # remove dots
405 $_[3] =~ s/^\///o; # remove the leading /
406 $_[3] = lc $_[3]; # to lower case;
407 dbg("incoming file $_[3]\n") if isdbg('msg');
408 $_[3] = 'packclus/' . $_[3] unless $_[3] =~ /^packclus\//o;
410 # create any directories
411 my @part = split /\//, $_[3];
413 my $fn = "$main::root";
414 pop @part; # remove last part
415 foreach $part (@part) {
418 last SWITCH if !mkdir $fn, 0777;
419 dbg("created directory $fn\n") if isdbg('msg');
421 my $stream = next_transno($fromnode);
422 my $ref = DXMsg->alloc($stream, "$main::root/$_[3]", $dxchan->call, time, !$_[4], $_[3], ' ', '0', '0');
424 # forwarding variables
425 $ref->{fromnode} = $tonode;
426 $ref->{tonode} = $fromnode;
427 $ref->{linesreq} = $_[5];
428 $ref->{stream} = $stream;
429 $ref->{count} = 0; # no of lines between PC31s
431 $ref->{lastt} = $main::systime;
432 set_fwq($fromnode, $stream, $ref); # store in work
433 $dxchan->send(DXProt::pc30($fromnode, $tonode, $stream)); # send ack
440 my ($tonode, $fromnode, $stream) = @_[1..3];
442 dbg("stream $stream: abort received\n") if isdbg('msg');
443 my $ref = get_fwq($fromnode, $stream);
445 $ref->stop_msg($fromnode);
450 # global delete on subject
457 if ($_->{from} eq $_[1] && $_->{subject} eq $_[2]) {
459 Log('msg', "Message $_->{msgno} from $_->{from} ($_->{subject}) fully deleted");
460 DXChannel::broadcast_nodes($line, $dxchan);
471 my $uref = DXUser->get_current($to);
472 my $dxchan = DXChannel->get($to);
473 if (((*Net::SMTP && $email_server) || $email_prog) && $uref && $uref->wantemail) {
474 my $email = $uref->email;
476 my @rcpt = ref $email ? @{$email} : $email;
477 my $fromaddr = $email_from || $main::myemail;
478 my @headers = ("To: $ref->{to}",
480 "Subject: [DXSpider: $ref->{from}] $ref->{subject}",
481 "X-DXSpider-To: $ref->{to}",
482 "X-DXSpider-From: $ref->{from}\@$ref->{origin}",
483 "X-DXSpider-Gateway: $main::mycall"
485 my @data = ("Msgno: $ref->{msgno} To: $to From: $ref->{from}\@$ref->{origin} Gateway: $main::mycall",
491 if (*Net::SMTP && $email_server) {
492 $msg = Net::SMTP->new($email_server);
494 $msg->mail($fromaddr);
496 $msg->data(map {"$_\n"} @headers, '', @data);
499 } elsif ($email_prog) {
500 $msg = new IO::File "|$email_prog " . join(' ', @rcpt);
502 print $msg map {"$_\r\n"} @headers, '', @data, '.';
506 dbg("email forwarding error $!") if isdbg('msg') && !$msg && defined $!;
509 $dxchan->send($dxchan->msg('m9')) if $dxchan && $dxchan->is_user;
512 # store a message away on disc or whatever
514 # NOTE the second arg is a REFERENCE not a list
520 if ($ref->{file}) { # a file
521 dbg("To be stored in $ref->{to}\n") if isdbg('msg');
523 my $fh = new IO::File "$ref->{to}", "w";
526 foreach $line (@{$lines}) {
530 dbg("file $ref->{to} stored\n") if isdbg('msg');
531 Log('msg', "file $ref->{to} from $ref->{from} stored" );
533 confess "can't open file $ref->{to} $!";
535 } else { # a normal message
537 # attempt to open the message file
538 my $fn = filename($ref->{msgno});
540 dbg("To be stored in $fn\n") if isdbg('msg');
542 # now save the file, overwriting what's there, YES I KNOW OK! (I will change it if it's a problem)
543 my $fh = new IO::File "$fn", "w";
545 my $rr = $ref->{rrreq} ? '1' : '0';
546 my $priv = $ref->{private} ? '1': '0';
547 my $del = $ref->{delete} ? '1' : '0';
548 my $delt = $ref->{deletetime} || ($ref->{t} + $maxage);
549 my $keep = $ref->{keep} || '0';
550 print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$priv^$ref->{subject}^$ref->{origin}^$ref->{'read'}^$rr^$del^$delt^$keep\n";
551 print $fh "=== ", join('^', @{$ref->{gotit}}), "\n";
554 foreach $line (@{$lines}) {
555 $line =~ s/[\x00-\x08\x0a-\x1f\x80-\x9f]/./g;
556 $ref->{size} += (length $line) + 1;
560 dbg("msg $ref->{msgno} stored\n") if isdbg('msg');
561 Log('msg', "msg $ref->{msgno} from $ref->{from} to $ref->{to} stored" );
563 confess "can't open msg file $fn $!";
575 $call = ' by ' . $dxchan->call if $dxchan;
577 if ($self->{tonode}) {
579 $self->{deletetime} = 0;
580 dbg("Msgno $self->{msgno} but marked as expunged$call") if isdbg('msg');
582 # remove it from the active message list
583 @msg = grep { $_ != $self } @msg;
585 Log('msg', "Msgno $self->{msgno} expunged$call");
586 dbg("Msgno $self->{msgno} expunged$call") if isdbg('msg');
589 unlink filename($self->{msgno});
598 return if $ref->{keep};
600 $t = $main::systime + $residencetime unless defined $t;
603 $ref->{deletetime} = $t;
604 $ref->store( [$ref->read_msg_body] );
612 $ref->{deletetime} = 0;
615 # clean out old messages from the message queue
620 # mark old messages for deletion
621 foreach $ref (@msg) {
622 if (ref($ref) && !$ref->{keep} && $ref->{deletetime} < $main::systime) {
624 # this is for IMMEDIATE destruction
626 $ref->{deletetime} = 0;
631 # read in a message header
641 $file = new IO::File "$fn";
643 dbg("Error reading $fn $!");
644 Log('err', "Error reading $fn $!");
648 $line = <$file>; # first line
649 if ($size == 0 || !$line) {
651 Log('err', "Empty $fn $!");
655 $size -= length $line;
656 if (! $line =~ /^===/o) {
657 dbg("corrupt first line in $fn ($line)");
658 Log('err', "corrupt first line in $fn ($line)");
662 @f = split /\^/, $line;
663 $ref = DXMsg->alloc(@f);
665 $line = <$file>; # second line
667 $size -= length $line;
668 if (! $line =~ /^===/o) {
669 dbg("corrupt second line in $fn ($line)");
670 Log('err', "corrupt second line in $fn ($line)");
675 @f = split /\^/, $line;
676 push @{$ref->{gotit}}, @f;
677 $ref->{size} = $size;
684 # read in a message header
688 my $msgno = $self->{msgno};
691 my $fn = filename($msgno);
694 $file = new IO::File;
695 if (!open($file, $fn)) {
696 dbg("Error reading $fn $!");
697 Log('err' ,"Error reading $fn $!");
700 @out = map {chomp; $_} <$file>;
703 shift @out if $out[0] =~ /^=== /;
704 shift @out if $out[0] =~ /^=== /;
708 # send a tranche of lines to the other end
711 my ($self, $dxchan) = @_;
713 my $to = $self->{tonode};
714 my $from = $self->{fromnode};
715 my $stream = $self->{stream};
716 my $lines = $self->{lines};
719 for ($i = 0, $c = $self->{count}; $i < $self->{linesreq} && $c < @$lines; $i++, $c++) {
720 push @out, DXProt::pc29($to, $from, $stream, $lines->[$c]);
724 push @out, DXProt::pc32($to, $from, $stream) if $i < $self->{linesreq};
729 # find a message to send out and start the ball rolling
736 # bat down the message list looking for one that needs to go off site and whose
737 # nearest node is not busy.
739 dbg("queue msg ($sort)\n") if isdbg('msg');
740 my @nodelist = DXChannel::get_all_nodes;
741 foreach $ref (@msg) {
743 # ignore 'delayed' messages until their waiting time has expired
744 if (exists $ref->{waitt}) {
745 next if $ref->{waitt} > $main::systime;
746 delete $ref->{waitt};
750 if (exists $ref->{lastt} && $main::systime >= $ref->{lastt} + $timeout) {
751 my $node = $ref->{tonode};
752 dbg("Timeout, stopping msgno: $ref->{msgno} -> $node") if isdbg('msg');
753 Log('msg', "Timeout, stopping msgno: $ref->{msgno} -> $node");
754 $ref->stop_msg($node);
756 # delay any outgoing messages that fail
757 $ref->{waitt} = $main::systime + $waittime + int rand(120) if $node ne $main::mycall;
758 delete $ref->{lastt};
762 # is it being sent anywhere currently?
763 next if $ref->{tonode}; # ignore it if it already being processed
765 # is it awaiting deletion?
766 next if $ref->{delete};
768 # firstly, is it private and unread? if so can I find the recipient
769 # in my cluster node list offsite?
771 # deal with routed private messages
773 if ($ref->{private}) {
774 next if $ref->{'read'}; # if it is read, it is stuck here
775 $clref = Route::get($ref->{to});
777 $dxchan = $clref->dxchan;
779 if ($dxchan->is_node) {
780 next if $clref->call eq $main::mycall; # i.e. it lives here
781 $ref->start_msg($dxchan) if !get_busy($dxchan->call) && $dxchan->state eq 'normal';
784 dbg("Route: No dxchan for $ref->{to} " . ref($clref) ) if isdbg('msg');
789 # otherwise we are dealing with a bulletin or forwarded private message
790 # compare the gotit list with
791 # the nodelist up above, if there are sites that haven't got it yet
792 # then start sending it - what happens when we get loops is anyone's
793 # guess, use (to, from, time, subject) tuple?
794 foreach $dxchan (@nodelist) {
795 my $call = $dxchan->call;
797 next if $call eq $main::mycall;
798 next if ref $ref->{gotit} && grep $_ eq $call, @{$ref->{gotit}};
799 next unless $ref->forward_it($call); # check the forwarding file
800 next if $ref->{tonode}; # ignore it if it already being processed
802 # if we are here we have a node that doesn't have this message
803 if (!get_busy($call) && $dxchan->state eq 'normal') {
804 $ref->start_msg($dxchan);
810 # if all the available nodes are busy then stop
811 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
817 # is there a message for me?
824 foreach $ref (@msg) {
825 # is it for me, private and unread?
826 if ($ref->{to} eq $call && $ref->{private}) {
827 $count++ unless $ref->{'read'} || $ref->{delete};
833 # start the message off on its travels with a PC28
836 my ($self, $dxchan) = @_;
838 confess("trying to start started msg $self->{msgno} nodes: $self->{fromnode} -> $self->{tonode}") if $self->{tonode};
839 dbg("start msg $self->{msgno}\n") if isdbg('msg');
840 $self->{linesreq} = 10;
842 $self->{tonode} = $dxchan->call;
843 $self->{fromnode} = $main::mycall;
844 set_busy($self->{tonode}, $self);
845 set_fwq($self->{tonode}, undef, $self);
846 $self->{lastt} = $main::systime;
847 my ($fromnode, $origin);
848 $fromnode = $self->{fromnode};
849 $origin = $self->{origin};
850 $dxchan->send(DXProt::pc28($self->{tonode}, $fromnode, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $origin, $self->{rrreq}));
853 # get the ref of a busy node
863 return $busy{$call} = shift;
869 return delete $busy{$call};
872 # get the whole busy queue
878 # get a forwarding queue entry
882 my $stream = shift || '0';
883 return $work{"$call,$stream"};
886 # delete a forwarding queue entry
890 my $stream = shift || '0';
891 return delete $work{"$call,$stream"};
898 my $stream = shift || '0';
899 return $work{"$call,$stream"} = shift;
902 # get the whole forwarding queue
908 # stop a message from continuing, clean it out, unlock interlocks etc
913 my $stream = $self->{stream};
916 dbg("stop msg $self->{msgno} -> node $node\n") if isdbg('msg');
917 del_fwq($node, $stream);
925 delete $ref->{lines};
926 delete $ref->{linesreq};
927 delete $ref->{tonode};
928 delete $ref->{fromnode};
929 delete $ref->{stream};
931 delete $ref->{count};
932 delete $ref->{tempr};
933 delete $ref->{lastt};
934 delete $ref->{waitt};
937 # get a new transaction number from the file specified
941 $name =~ s/\W//og; # remove non-word characters
942 my $fn = "$msgdir/$name";
945 my $fh = new IO::File;
946 if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
948 $msgno = $fh->getline || '0';
952 $fh->print("$msgno\n");
953 dbg("msgno $msgno allocated for $name\n") if isdbg('msg');
956 confess "can't open $fn $!";
961 # initialise the message 'system', read in all the message headers
964 my $dir = new IO::File;
968 # load various control files
969 dbg("load badmsg: " . (load_badmsg() or "Ok"));
970 dbg("load forward: " . (load_forward() or "Ok"));
971 dbg("load swop: " . (load_swop() or "Ok"));
973 # read in the directory
974 opendir($dir, $msgdir) or confess "can't open $msgdir $!";
975 @dir = readdir($dir);
980 next unless /^m\d\d\d\d\d\d$/;
982 $ref = read_msg_header("$msgdir/$_");
985 Log('err', "Deleting $_");
990 # delete any messages to 'badmsg.pl' places
991 if ($ref->dump_it('')) {
992 dbg("'Bad' TO address $ref->{to}") if isdbg('msg');
993 Log('msg', "'Bad' TO address $ref->{to}");
998 # add the message to the available queue
1003 # add the message to the directory listing
1007 confess "tried to add a non-ref to the msg directory" if !ref $ref;
1011 # return all the current messages
1017 # get a particular message
1022 return $_ if $_->{msgno} == $msgno;
1023 last if $_->{msgno} > $msgno;
1028 # return the official filename for a message no
1031 return sprintf "$msgdir/m%06d", shift;
1035 # return a list of valid elements
1040 return keys(%valid);
1044 # return a prompt for a field
1049 my ($self, $ele) = @_;
1050 return $valid{$ele};
1054 # send a message state machine
1061 if ($self->state eq 'send1') {
1063 confess "local var gone missing" if !ref $self->{loc};
1064 my $loc = $self->{loc};
1065 if (my @ans = BadWords::check($line)) {
1066 $self->{badcount} += @ans;
1067 Log('msg', $self->call . " used badwords: @ans to @{$loc->{to}} in msg");
1070 $loc->{subject} = $line;
1072 $self->state('sendbody');
1073 #push @out, $self->msg('sendbody');
1074 push @out, $self->msg('m8');
1075 } elsif ($self->state eq 'sendbody') {
1076 confess "local var gone missing" if !ref $self->{loc};
1077 my $loc = $self->{loc};
1078 if ($line eq "\032" || $line eq '%1A' || uc $line eq "/EX") {
1080 unless ($loc->{reject}) {
1081 foreach $to (@{$loc->{to}}) {
1083 my $systime = $main::systime;
1084 my $mycall = $main::mycall;
1085 $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
1087 exists $loc->{from} ? $loc->{from} : $self->call,
1091 exists $loc->{origin} ? $loc->{origin} : $mycall,
1094 $ref->swop_it($self->call);
1095 $ref->store($loc->{lines});
1097 push @out, $self->msg('m11', $ref->{msgno}, $to);
1098 #push @out, "msgno $ref->{msgno} sent to $to";
1102 Log('msg', $self->call . " swore to @{$loc->{to}} subject: '$loc->{subject}' in msg, REJECTED");
1105 delete $loc->{lines};
1107 delete $self->{loc};
1110 $self->state('prompt');
1111 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
1112 #push @out, $self->msg('sendabort');
1113 push @out, $self->msg('m10');
1114 delete $loc->{lines};
1116 delete $self->{loc};
1118 $self->state('prompt');
1119 } elsif ($line =~ m|^/+\w+|) {
1120 # this is a command that you want display for your own reference
1121 # or if it has TWO slashes is a command
1123 my $store = $line =~ s|^/+||;
1124 my @in = $self->run_cmd($line);
1127 foreach my $l (@in) {
1128 if (my @ans = BadWords::check($l)) {
1129 $self->{badcount} += @ans;
1130 Log('msg', $self->call . " used badwords: @ans to @{$loc->{to}} subject: '$loc->{subject}' in msg") unless $loc->{reject};
1131 Log('msg', "line: $l");
1134 push @{$loc->{lines}}, length($l) > 0 ? $l : " ";
1138 if (my @ans = BadWords::check($line)) {
1139 $self->{badcount} += @ans;
1140 Log('msg', $self->call . " used badwords: @ans to @{$loc->{to}} subject: '$loc->{subject}' in msg") unless $loc->{reject};
1141 Log('msg', "line: $line");
1145 if ($loc->{lines} && @{$loc->{lines}}) {
1146 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
1148 # temporarily store any R: lines so that we end up with
1149 # only the first and last ones stored.
1150 if ($line =~ m|^R:\d{6}/\d{4}|) {
1151 push @{$loc->{tempr}}, $line;
1153 if (exists $loc->{tempr}) {
1154 push @{$loc->{lines}}, shift @{$loc->{tempr}};
1155 push @{$loc->{lines}}, pop @{$loc->{tempr}} if @{$loc->{tempr}};
1156 delete $loc->{tempr};
1158 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
1162 # i.e. it ain't and end or abort, therefore store the line
1168 # return the standard directory line for this ref
1172 my $flag = $ref->{private} && $ref->{read} ? '-' : ' ';
1175 } elsif ($ref->{delete}) {
1176 $flag = $ref->{deletetime} > $main::systime ? 'D' : 'E';
1178 return sprintf("%6d%s%s%5d %8.8s %8.8s %-6.6s %5.5s %-30.30s",
1179 $ref->{msgno}, $flag, $ref->{private} ? 'p' : ' ',
1180 $ref->{size}, $ref->{to}, $ref->{from}, cldate($ref->{t}),
1181 ztime($ref->{t}), $ref->{subject});
1184 # load the forward table
1188 my $s = readfilestr($forwardfn);
1191 push @out, $@ if $@;
1196 # load the bad message table
1200 my $s = readfilestr($badmsgfn);
1203 push @out, $@ if $@;
1208 # load the swop message table
1212 my $s = readfilestr($swopfn);
1215 push @out, $@ if $@;
1221 # forward that message or not according to the forwarding table
1222 # returns 1 for forward, 0 - to ignore
1231 for ($i = 0; $i < @forward; $i += 5) {
1232 my ($sort, $field, $pattern, $action, $bbs) = @forward[$i..($i+4)];
1235 # are we interested?
1236 next if $ref->{private} && $sort ne 'P';
1237 next if !$ref->{private} && $sort ne 'B';
1240 $tested = $ref->{to} if $field eq 'T';
1241 $tested = $ref->{from} if $field eq 'F';
1242 $tested = $ref->{origin} if $field eq 'O';
1243 $tested = $ref->{subject} if $field eq 'S';
1245 if (!$pattern || $tested =~ m{$pattern}i) {
1246 return 0 if $action eq 'I';
1247 return 1 if !$bbs || grep $_ eq $call, @{$bbs};
1254 # look down the forward table to see whether this is a valid bull
1255 # or not (ie it will forward somewhere even if it is only here)
1263 return 1 if $call =~ /^ALL/;
1264 return 1 if $call =~ /^DX/;
1268 for ($i = 0; $i < @forward; $i += 5) {
1269 my ($sort, $field, $pattern, $action, $bbs) = @forward[$i..($i+4)];
1270 if ($field eq 'T') {
1271 if (!$pattern || $call =~ m{$pattern}i) {
1285 for ($i = 0; $i < @badmsg; $i += 3) {
1286 my ($sort, $field, $pattern) = @badmsg[$i..($i+2)];
1289 # are we interested?
1290 next if $ref->{private} && $sort ne 'P';
1291 next if !$ref->{private} && $sort ne 'B';
1294 $tested = $ref->{to} if $field eq 'T';
1295 $tested = $ref->{from} if $field eq 'F';
1296 $tested = $ref->{origin} if $field eq 'O';
1297 $tested = $ref->{subject} if $field eq 'S';
1298 $tested = $call if $field eq 'I';
1300 if (!$pattern || $tested =~ m{$pattern}i) {
1314 for ($i = 0; $i < @swop; $i += 5) {
1315 my ($sort, $field, $pattern, $tfield, $topattern) = @swop[$i..($i+4)];
1320 # are we interested?
1321 next if $ref->{private} && $sort ne 'P';
1322 next if !$ref->{private} && $sort ne 'B';
1325 $tested = $ref->{to} if $field eq 'T';
1326 $tested = $ref->{from} if $field eq 'F';
1327 $tested = $ref->{origin} if $field eq 'O';
1328 $tested = $ref->{subject} if $field eq 'S';
1331 $old = $swop = $ref->{to} if $tfield eq 'T';
1332 $old = $swop = $ref->{from} if $tfield eq 'F';
1333 $old = $swop = $ref->{origin} if $tfield eq 'O';
1334 $old = $swop = $ref->{subject} if $tfield eq 'S';
1336 if ($tested =~ m{$pattern}i) {
1337 if ($tested eq $swop) {
1338 $swop =~ s{$pattern}{$topattern}i;
1342 Log('msg', "Msg $ref->{msgno}: $tfield $old -> $swop");
1343 Log('dbg', "Msg $ref->{msgno}: $tfield $old -> $swop");
1344 $ref->{to} = $swop if $tfield eq 'T';
1345 $ref->{from} = $swop if $tfield eq 'F';
1346 $ref->{origin} = $swop if $tfield eq 'O';
1347 $ref->{subject} = $swop if $tfield eq 'S';
1354 # import any msgs in the import directory
1355 # the messages are in BBS format (but may have cluster extentions
1356 # so SB UK < GB7TLH is legal
1359 # are there any to do in this directory?
1360 return unless -d $importfn;
1361 unless (opendir(DIR, $importfn)) {
1362 dbg("can\'t open $importfn $!") if isdbg('msg');
1363 Log('msg', "can\'t open $importfn $!");
1367 my @names = readdir(DIR);
1370 foreach $name (@names) {
1371 next if $name =~ /^\./;
1372 my $splitit = $name =~ /^split/;
1373 my $fn = "$importfn/$name";
1375 unless (open(MSG, $fn)) {
1376 dbg("can\'t open import file $fn $!") if isdbg('msg');
1377 Log('msg', "can\'t open import file $fn $!");
1381 my @msg = map { chomp; $_ } <MSG>;
1384 my @out = import_one($main::me, \@msg, $splitit);
1389 # import one message as a list in bbs (as extended) mode
1390 # takes a reference to an array containing the whole message
1395 my $splitit = shift;
1399 my $from = $dxchan->call;
1400 my $origin = $main::mycall;
1405 my $line = shift @$ref;
1406 my @f = split /([\s\@\$])/, $line;
1407 @f = map {s/\s+//g; length $_ ? $_ : ()} @f;
1409 unless (@f && $f[0] =~ /^(:?S|SP|SB|SEND)$/ ) {
1410 my $m = "invalid first line in import '$line'";
1411 dbg($m) if isdbg('msg');
1415 my $f = uc shift @f;
1416 next if $f eq 'SEND';
1418 # private / noprivate / rr
1419 if ($notincalls && ($f eq 'B' || $f eq 'SB' || $f =~ /^NOP/oi)) {
1421 } elsif ($notincalls && ($f eq 'P' || $f eq 'SP' || $f =~ /^PRI/oi)) {
1423 } elsif ($notincalls && ($f eq 'RR')) {
1425 } elsif (($f =~ /^[\@\.\#\$]$/ || $f eq '.#') && @f) { # this is bbs syntax, for AT
1427 } elsif ($f eq '<' && @f) { # this is bbs syntax for from call
1428 $from = uc shift @f;
1429 } elsif ($f =~ /^\$/) { # this is bbs syntax for a bid
1431 } elsif ($f =~ /^<(\S+)/) { # this is bbs syntax for from call
1433 } elsif ($f =~ /^\$\S+/) { # this is bbs syntax for bid
1440 # is this callsign a distro?
1441 my $fn = "$msgdir/distro/$f.pl";
1443 my $fh = new IO::File $fn;
1450 return (1, "Error in Distro $f.pl:", $@) if $@;
1458 if (grep $_ eq $f, @DXMsg::badmsg) {
1459 push @out, $dxchan->msg('m3', $f);
1466 # subject is the next line
1467 my $subject = shift @$ref;
1469 # strip off trailing lines
1470 pop @$ref while (@$ref && $$ref[-1] =~ /^\s*$/);
1472 # strip off /EX or /ABORT
1473 return ("aborted") if @$ref && $$ref[-1] =~ m{^/ABORT$}i;
1474 pop @$ref if (@$ref && $$ref[-1] =~ m{^/EX$}i);
1476 # sort out any splitting that needs to be done
1482 if ($lth >= $maxchunk || ($lth > $minchunk && /^\s*$/)) {
1483 push @chunk, $lines;
1490 push @chunk, $lines if @$lines;
1495 # does an identical message already exist?
1498 if (substr($subject,0,28) eq substr($m->{subject},0,28) && $from eq $m->{from} && grep $m->{to} eq $_, @to) {
1499 my $msgno = $m->{msgno};
1500 dbg("duplicate message from $from -> $m->{to} to msg: $msgno") if isdbg('msg');
1501 Log('msg', "duplicate message from $from -> $m->{to} to msg: $msgno");
1506 # write all the messages away
1508 for ( $i = 0; $i < @chunk; $i++) {
1509 my $chunk = $chunk[$i];
1512 my $num = " [" . ($i+1) . "/" . scalar @chunk . "]";
1513 $ch_subject = substr($subject, 0, 27 - length $num) . $num;
1515 $ch_subject = $subject;
1519 my $systime = $main::systime;
1520 my $mycall = $main::mycall;
1521 my $mref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
1530 $mref->swop_it($main::mycall);
1531 $mref->store($chunk);
1533 push @out, $dxchan->msg('m11', $mref->{msgno}, $to);
1534 #push @out, "msgno $ref->{msgno} sent to $to";
1545 my $name = $AUTOLOAD;
1546 return if $name =~ /::DESTROY$/;
1547 $name =~ s/^.*:://o;
1549 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
1550 # this clever line of code creates a subroutine which takes over from autoload
1551 # from OO Perl - Conway
1552 *$AUTOLOAD = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};