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);
37 main::mkver($VERSION = q$Revision$);
39 use vars qw(%work @msg $msgdir %valid %busy $maxage $last_clean $residencetime
40 @badmsg @swop $swopfn $badmsgfn $forwardfn @forward $timeout $waittime
41 $email_server $email_prog $email_from
42 $queueinterval $lastq $importfn $minchunk $maxchunk $bulltopriv);
44 %work = (); # outstanding jobs
45 @msg = (); # messages we have
46 %busy = (); # station interlocks
47 $msgdir = "$main::root/msg"; # directory contain the msgs
48 $maxage = 30 * 86400; # the maximum age that a message shall live for if not marked
49 $last_clean = 0; # last time we did a clean
50 @forward = (); # msg forward table
51 @badmsg = (); # bad message table
52 @swop = (); # swop table
53 $timeout = 30*60; # forwarding timeout
54 $waittime = 30*60; # time an aborted outgoing message waits before trying again
55 $queueinterval = 1*60; # run the queue every 1 minute
58 $minchunk = 4800; # minimum chunk size for a split message
59 $maxchunk = 6000; # maximum chunk size
60 $bulltopriv = 1; # convert msgs with callsigns to private if they are bulls
61 $residencetime = 2*86400; # keep deleted messages for this amount of time
62 $email_server = undef; # DNS address of smtp server if 'smtp'
63 $email_prog = undef; # program name + args for sending mail
64 $email_from = undef; # the from address the email will appear to be from
66 $badmsgfn = "$msgdir/badmsg.pl"; # list of TO address we wont store
67 $forwardfn = "$msgdir/forward.pl"; # the forwarding table
68 $swopfn = "$msgdir/swop.pl"; # the swopping table
69 $importfn = "$msgdir/import"; # import directory
73 fromnode => '5,From Node',
74 tonode => '5,To Node',
77 t => '0,Msg Time,cldatetime',
78 private => '5,Private,yesno',
79 subject => '0,Subject',
80 linesreq => '0,Lines per Gob',
81 rrreq => '5,Read Confirm,yesno',
84 stream => '9,Stream No',
85 count => '5,Gob Linecnt',
86 file => '5,File?,yesno',
87 gotit => '5,Got it Nodes,parray',
88 lines => '5,Lines,parray',
89 'read' => '5,Times read',
92 keep => '0,Keep this?,yesno',
93 lastt => '5,Last processed,cldatetime',
94 waitt => '5,Wait until,cldatetime',
95 delete => '5,Awaiting Delete,yesno',
96 deletetime => '5,Deletion Time,cldatetime',
99 # fix up the default sendmail if available
100 for (qw(/usr/sbin/sendmail /usr/lib/sendmail /usr/sbin/sendmail)) {
107 # allocate a new object
108 # called fromnode, tonode, from, to, datetime, private?, subject, nolinesper
112 my $self = bless {}, $pkg;
113 $self->{msgno} = shift;
116 $self->{to} = ($to eq $main::mycall) ? $main::myalias : $to;
118 $self->{from} = uc $from;
120 $self->{private} = shift;
121 $self->{subject} = shift;
122 $self->{origin} = shift;
123 $self->{'read'} = shift;
124 $self->{rrreq} = shift;
125 $self->{delete} = shift;
126 $self->{deletetime} = shift || ($self->{t} + $maxage);
127 $self->{keep} = shift;
129 # $self->{lastt} = $main::systime;
131 $self->{private} = 1 if $bulltopriv && DXUser->get_current($self->{to});
139 # this is periodic processing
140 if ($main::systime >= $lastq + $queueinterval) {
142 # queue some message if the interval timer has gone off
145 # import any messages in the import directory
148 $lastq = $main::systime;
151 # clean the message queue
152 if ($main::systime >= $last_clean+3600) {
154 $last_clean = $main::systime;
157 # actual remove all the 'deleted' messages in one hit.
158 # this has to be delayed until here otherwise it only does one at
159 # a time because @msg is rewritten everytime del_msg is called.
160 my @del = grep {!$_->{tonode} && $_->{delete} && !$_->{keep} && $_->{deletetime} < $main::systime} @msg;
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});
316 if ($ref->{t}+$maxage < $main::systime ) {
317 $ref->stop_msg($fromnode);
318 dbg("old message from $ref->{from} -> $ref->{to} " . atime($ref->{t}) . " ignored") if isdbg('msg');
319 Log('msg', "old message from $ref->{from} -> $ref->{to} " . cldatetime($ref->{t}) . " ignored");
323 # does an identical message already exist?
326 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}) {
327 $ref->stop_msg($fromnode);
328 my $msgno = $m->{msgno};
329 dbg("duplicate message from $ref->{from} -> $ref->{to} to msg: $msgno") if isdbg('msg');
330 Log('msg', "duplicate message from $ref->{from} -> $ref->{to} to msg: $msgno");
336 $ref->swop_it($dxchan->call);
338 # look for 'bad' to addresses
339 if ($ref->dump_it($dxchan->call)) {
340 $ref->stop_msg($fromnode);
341 dbg("'Bad' message $ref->{to}") if isdbg('msg');
342 Log('msg', "'Bad' message $ref->{to}");
346 # check the message for bad words
348 for (@{$ref->{lines}}) {
349 push @words, BadWords::check($_);
351 push @words, BadWords::check($ref->{subject});
353 dbg("$ref->{from} swore: '@words' -> $ref->{to} '$ref->{subject}' origin: $ref->{origin} via " . $dxchan->call) if isdbg('msg');
354 Log('msg',"$ref->{from} swore: '@words' -> $ref->{to} origin: $ref->{origin} via " . $dxchan->call);
355 Log('msg',"subject: $ref->{subject}");
356 for (@{$ref->{lines}}) {
357 Log('msg', "line: $_");
359 $ref->stop_msg($fromnode);
363 $ref->{msgno} = next_transno("Msgno");
364 push @{$ref->{gotit}}, $fromnode; # mark this up as being received
365 $ref->store($ref->{lines});
368 Log('msg', "Message $ref->{msgno} from $ref->{from} received from $fromnode for $ref->{to}");
371 $ref->stop_msg($fromnode);
373 dbg("PC32 from unknown stream $stream from $fromnode") if isdbg('msg');
374 $dxchan->send(DXProt::pc42($fromnode, $tonode, $stream)); # unknown stream
379 # acknowledge the end of message
383 my ($tonode, $fromnode, $stream) = @_[1..3];
385 my $ref = get_fwq($fromnode, $stream);
387 if ($ref->{private}) { # remove it if it private and gone off site#
388 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $fromnode and deleted");
391 Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $fromnode");
392 push @{$ref->{gotit}}, $fromnode; # mark this up as being received
393 $ref->store($ref->{lines}); # re- store the file
395 $ref->stop_msg($fromnode);
397 dbg("PC33 from unknown stream $stream from $fromnode") if isdbg('msg');
398 $dxchan->send(DXProt::pc42($fromnode, $tonode, $stream)); # unknown stream
401 # send next one if present
405 # this is a file request
409 my ($tonode, $fromnode) = @_[1..2];
411 $_[3] =~ s/\\/\//og; # change the slashes
412 $_[3] =~ s/\.//og; # remove dots
413 $_[3] =~ s/^\///o; # remove the leading /
414 $_[3] = lc $_[3]; # to lower case;
415 dbg("incoming file $_[3]\n") if isdbg('msg');
416 $_[3] = 'packclus/' . $_[3] unless $_[3] =~ /^packclus\//o;
418 # create any directories
419 my @part = split /\//, $_[3];
421 my $fn = "$main::root";
422 pop @part; # remove last part
423 foreach $part (@part) {
426 last SWITCH if !mkdir $fn, 0777;
427 dbg("created directory $fn\n") if isdbg('msg');
429 my $stream = next_transno($fromnode);
430 my $ref = DXMsg->alloc($stream, "$main::root/$_[3]", $dxchan->call, time, !$_[4], $_[3], ' ', '0', '0');
432 # forwarding variables
433 $ref->{fromnode} = $tonode;
434 $ref->{tonode} = $fromnode;
435 $ref->{linesreq} = $_[5];
436 $ref->{stream} = $stream;
437 $ref->{count} = 0; # no of lines between PC31s
439 $ref->{lastt} = $main::systime;
440 set_fwq($fromnode, $stream, $ref); # store in work
441 $dxchan->send(DXProt::pc30($fromnode, $tonode, $stream)); # send ack
448 my ($tonode, $fromnode, $stream) = @_[1..3];
450 dbg("stream $stream: abort received\n") if isdbg('msg');
451 my $ref = get_fwq($fromnode, $stream);
453 $ref->stop_msg($fromnode);
458 # global delete on subject
465 if ($_->{from} eq $_[1] && $_->{subject} eq $_[2]) {
467 Log('msg', "Message $_->{msgno} from $_->{from} ($_->{subject}) fully deleted");
468 DXChannel::broadcast_nodes($line, $dxchan);
479 my $uref = DXUser->get_current($to);
480 my $dxchan = DXChannel::get($to);
481 if (((*Net::SMTP && $email_server) || $email_prog) && $uref && $uref->wantemail) {
482 my $email = $uref->email;
484 my @rcpt = ref $email ? @{$email} : $email;
485 my $fromaddr = $email_from || $main::myemail;
486 my @headers = ("To: $ref->{to}",
488 "Subject: [DXSpider: $ref->{from}] $ref->{subject}",
489 "X-DXSpider-To: $ref->{to}",
490 "X-DXSpider-From: $ref->{from}\@$ref->{origin}",
491 "X-DXSpider-Gateway: $main::mycall"
493 my @data = ("Msgno: $ref->{msgno} To: $to From: $ref->{from}\@$ref->{origin} Gateway: $main::mycall",
499 if (*Net::SMTP && $email_server) {
500 $msg = Net::SMTP->new($email_server);
502 $msg->mail($fromaddr);
504 $msg->data(map {"$_\n"} @headers, '', @data);
507 } elsif ($email_prog) {
508 $msg = new IO::File "|$email_prog " . join(' ', @rcpt);
510 print $msg map {"$_\r\n"} @headers, '', @data, '.';
514 dbg("email forwarding error $!") if isdbg('msg') && !$msg && defined $!;
517 $dxchan->send($dxchan->msg('m9')) if $dxchan && $dxchan->is_user;
520 # store a message away on disc or whatever
522 # NOTE the second arg is a REFERENCE not a list
528 if ($ref->{file}) { # a file
529 dbg("To be stored in $ref->{to}\n") if isdbg('msg');
531 my $fh = new IO::File "$ref->{to}", "w";
534 foreach $line (@{$lines}) {
538 dbg("file $ref->{to} stored\n") if isdbg('msg');
539 Log('msg', "file $ref->{to} from $ref->{from} stored" );
541 confess "can't open file $ref->{to} $!";
543 } else { # a normal message
545 # attempt to open the message file
546 my $fn = filename($ref->{msgno});
548 dbg("To be stored in $fn\n") if isdbg('msg');
550 # now save the file, overwriting what's there, YES I KNOW OK! (I will change it if it's a problem)
551 my $fh = new IO::File "$fn", "w";
553 my $rr = $ref->{rrreq} ? '1' : '0';
554 my $priv = $ref->{private} ? '1': '0';
555 my $del = $ref->{delete} ? '1' : '0';
556 my $delt = $ref->{deletetime} || ($ref->{t} + $maxage);
557 my $keep = $ref->{keep} || '0';
558 print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$priv^$ref->{subject}^$ref->{origin}^$ref->{'read'}^$rr^$del^$delt^$keep\n";
559 print $fh "=== ", join('^', @{$ref->{gotit}}), "\n";
562 foreach $line (@{$lines}) {
563 $line =~ s/[\x00-\x08\x0a-\x1f\x80-\x9f]/./g;
564 $ref->{size} += (length $line) + 1;
568 dbg("msg $ref->{msgno} stored\n") if isdbg('msg');
569 Log('msg', "msg $ref->{msgno} from $ref->{from} to $ref->{to} stored" );
571 confess "can't open msg file $fn $!";
583 $call = ' by ' . $dxchan->call if $dxchan;
585 if ($self->{tonode}) {
587 $self->{deletetime} = 0;
588 dbg("Msgno $self->{msgno} but marked as expunged$call") if isdbg('msg');
590 # remove it from the active message list
591 @msg = grep { $_ != $self } @msg;
593 Log('msg', "Msgno $self->{msgno} expunged$call");
594 dbg("Msgno $self->{msgno} expunged$call") if isdbg('msg');
597 unlink filename($self->{msgno});
606 return if $ref->{keep};
608 $t = $main::systime + $residencetime unless defined $t;
611 $ref->{deletetime} = $t;
612 $ref->store( [$ref->read_msg_body] );
620 $ref->{deletetime} = 0;
623 # clean out old messages from the message queue
628 # mark old messages for deletion
629 foreach $ref (@msg) {
630 if (ref($ref) && !$ref->{keep} && $ref->{deletetime} < $main::systime) {
632 # this is for IMMEDIATE destruction
634 $ref->{deletetime} = 0;
639 # read in a message header
649 $file = new IO::File "$fn";
651 dbg("Error reading $fn $!");
652 Log('err', "Error reading $fn $!");
656 $line = <$file>; # first line
657 if ($size == 0 || !$line) {
659 Log('err', "Empty $fn $!");
663 $size -= length $line;
664 if (! $line =~ /^===/o) {
665 dbg("corrupt first line in $fn ($line)");
666 Log('err', "corrupt first line in $fn ($line)");
670 @f = split /\^/, $line;
671 $ref = DXMsg->alloc(@f);
673 $line = <$file>; # second line
675 $size -= length $line;
676 if (! $line =~ /^===/o) {
677 dbg("corrupt second line in $fn ($line)");
678 Log('err', "corrupt second line in $fn ($line)");
683 @f = split /\^/, $line;
684 push @{$ref->{gotit}}, @f;
685 $ref->{size} = $size;
692 # read in a message header
696 my $msgno = $self->{msgno};
699 my $fn = filename($msgno);
702 $file = new IO::File;
703 if (!open($file, $fn)) {
704 dbg("Error reading $fn $!");
705 Log('err' ,"Error reading $fn $!");
708 @out = map {chomp; $_} <$file>;
711 shift @out if $out[0] =~ /^=== /;
712 shift @out if $out[0] =~ /^=== /;
716 # send a tranche of lines to the other end
719 my ($self, $dxchan) = @_;
721 my $to = $self->{tonode};
722 my $from = $self->{fromnode};
723 my $stream = $self->{stream};
724 my $lines = $self->{lines};
727 for ($i = 0, $c = $self->{count}; $i < $self->{linesreq} && $c < @$lines; $i++, $c++) {
728 push @out, DXProt::pc29($to, $from, $stream, $lines->[$c]);
732 push @out, DXProt::pc32($to, $from, $stream) if $i < $self->{linesreq};
737 # find a message to send out and start the ball rolling
744 # bat down the message list looking for one that needs to go off site and whose
745 # nearest node is not busy.
747 dbg("queue msg ($sort)\n") if isdbg('msg');
748 my @nodelist = DXChannel::get_all_nodes;
749 foreach $ref (@msg) {
751 # ignore 'delayed' messages until their waiting time has expired
752 if (exists $ref->{waitt}) {
753 next if $ref->{waitt} > $main::systime;
754 delete $ref->{waitt};
758 if (exists $ref->{lastt} && $main::systime >= $ref->{lastt} + $timeout) {
759 my $node = $ref->{tonode};
760 dbg("Timeout, stopping msgno: $ref->{msgno} -> $node") if isdbg('msg');
761 Log('msg', "Timeout, stopping msgno: $ref->{msgno} -> $node");
762 $ref->stop_msg($node);
764 # delay any outgoing messages that fail
765 $ref->{waitt} = $main::systime + $waittime + int rand(120) if $node ne $main::mycall;
766 delete $ref->{lastt};
770 # is it being sent anywhere currently?
771 next if $ref->{tonode}; # ignore it if it already being processed
773 # is it awaiting deletion?
774 next if $ref->{delete};
776 # firstly, is it private and unread? if so can I find the recipient
777 # in my cluster node list offsite?
779 # deal with routed private messages
781 if ($ref->{private}) {
782 next if $ref->{'read'}; # if it is read, it is stuck here
783 $clref = Route::get($ref->{to});
785 $dxchan = $clref->dxchan;
787 if ($dxchan->is_node) {
788 next if $clref->call eq $main::mycall; # i.e. it lives here
789 $ref->start_msg($dxchan) if !get_busy($dxchan->call) && $dxchan->state eq 'normal';
792 dbg("Route: No dxchan for $ref->{to} " . ref($clref) ) if isdbg('msg');
797 # otherwise we are dealing with a bulletin or forwarded private message
798 # compare the gotit list with
799 # the nodelist up above, if there are sites that haven't got it yet
800 # then start sending it - what happens when we get loops is anyone's
801 # guess, use (to, from, time, subject) tuple?
802 foreach $dxchan (@nodelist) {
803 my $call = $dxchan->call;
805 next if $call eq $main::mycall;
806 next if ref $ref->{gotit} && grep $_ eq $call, @{$ref->{gotit}};
807 next unless $ref->forward_it($call); # check the forwarding file
808 next if $ref->{tonode}; # ignore it if it already being processed
810 # if we are here we have a node that doesn't have this message
811 if (!get_busy($call) && $dxchan->state eq 'normal') {
812 $ref->start_msg($dxchan);
818 # if all the available nodes are busy then stop
819 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
825 # is there a message for me?
832 foreach $ref (@msg) {
833 # is it for me, private and unread?
834 if ($ref->{to} eq $call && $ref->{private}) {
835 $count++ unless $ref->{'read'} || $ref->{delete};
841 # start the message off on its travels with a PC28
844 my ($self, $dxchan) = @_;
846 confess("trying to start started msg $self->{msgno} nodes: $self->{fromnode} -> $self->{tonode}") if $self->{tonode};
847 dbg("start msg $self->{msgno}\n") if isdbg('msg');
848 $self->{linesreq} = 10;
850 $self->{tonode} = $dxchan->call;
851 $self->{fromnode} = $main::mycall;
852 set_busy($self->{tonode}, $self);
853 set_fwq($self->{tonode}, undef, $self);
854 $self->{lastt} = $main::systime;
855 my ($fromnode, $origin);
856 $fromnode = $self->{fromnode};
857 $origin = $self->{origin};
858 $dxchan->send(DXProt::pc28($self->{tonode}, $fromnode, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $origin, $self->{rrreq}));
861 # get the ref of a busy node
871 return $busy{$call} = shift;
877 return delete $busy{$call};
880 # get the whole busy queue
886 # get a forwarding queue entry
890 my $stream = shift || '0';
891 return $work{"$call,$stream"};
894 # delete a forwarding queue entry
898 my $stream = shift || '0';
899 return delete $work{"$call,$stream"};
906 my $stream = shift || '0';
907 return $work{"$call,$stream"} = shift;
910 # get the whole forwarding queue
916 # stop a message from continuing, clean it out, unlock interlocks etc
921 my $stream = $self->{stream};
924 dbg("stop msg $self->{msgno} -> node $node\n") if isdbg('msg');
925 del_fwq($node, $stream);
933 delete $ref->{lines};
934 delete $ref->{linesreq};
935 delete $ref->{tonode};
936 delete $ref->{fromnode};
937 delete $ref->{stream};
939 delete $ref->{count};
940 delete $ref->{tempr};
941 delete $ref->{lastt};
942 delete $ref->{waitt};
945 # get a new transaction number from the file specified
949 $name =~ s/\W//og; # remove non-word characters
950 my $fn = "$msgdir/$name";
953 my $fh = new IO::File;
954 if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
956 $msgno = $fh->getline || '0';
960 $fh->print("$msgno\n");
961 dbg("msgno $msgno allocated for $name\n") if isdbg('msg');
964 confess "can't open $fn $!";
969 # initialise the message 'system', read in all the message headers
972 my $dir = new IO::File;
976 # load various control files
977 dbg("load badmsg: " . (load_badmsg() or "Ok"));
978 dbg("load forward: " . (load_forward() or "Ok"));
979 dbg("load swop: " . (load_swop() or "Ok"));
981 # read in the directory
982 opendir($dir, $msgdir) or confess "can't open $msgdir $!";
983 @dir = readdir($dir);
988 next unless /^m\d\d\d\d\d\d$/;
990 $ref = read_msg_header("$msgdir/$_");
993 Log('err', "Deleting $_");
998 # delete any messages to 'badmsg.pl' places
999 if ($ref->dump_it('')) {
1000 dbg("'Bad' TO address $ref->{to}") if isdbg('msg');
1001 Log('msg', "'Bad' TO address $ref->{to}");
1006 # add the message to the available queue
1011 # add the message to the directory listing
1015 confess "tried to add a non-ref to the msg directory" if !ref $ref;
1019 # return all the current messages
1025 # get a particular message
1030 return $_ if $_->{msgno} == $msgno;
1031 last if $_->{msgno} > $msgno;
1036 # return the official filename for a message no
1039 return sprintf "$msgdir/m%06d", shift;
1043 # return a list of valid elements
1048 return keys(%valid);
1052 # return a prompt for a field
1057 my ($self, $ele) = @_;
1058 return $valid{$ele};
1062 # send a message state machine
1069 if ($self->state eq 'send1') {
1071 confess "local var gone missing" if !ref $self->{loc};
1072 my $loc = $self->{loc};
1073 if (my @ans = BadWords::check($line)) {
1074 $self->{badcount} += @ans;
1075 Log('msg', $self->call . " used badwords: @ans to @{$loc->{to}} in msg");
1078 $loc->{subject} = $line;
1080 $self->state('sendbody');
1081 #push @out, $self->msg('sendbody');
1082 push @out, $self->msg('m8');
1083 } elsif ($self->state eq 'sendbody') {
1084 confess "local var gone missing" if !ref $self->{loc};
1085 my $loc = $self->{loc};
1086 if ($line eq "\032" || $line eq '%1A' || uc $line eq "/EX") {
1088 unless ($loc->{reject}) {
1089 foreach $to (@{$loc->{to}}) {
1091 my $systime = $main::systime;
1092 my $mycall = $main::mycall;
1093 $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
1095 exists $loc->{from} ? $loc->{from} : $self->call,
1099 exists $loc->{origin} ? $loc->{origin} : $mycall,
1102 $ref->swop_it($self->call);
1103 $ref->store($loc->{lines});
1105 push @out, $self->msg('m11', $ref->{msgno}, $to);
1106 #push @out, "msgno $ref->{msgno} sent to $to";
1110 Log('msg', $self->call . " swore to @{$loc->{to}} subject: '$loc->{subject}' in msg, REJECTED");
1113 delete $loc->{lines};
1115 delete $self->{loc};
1118 $self->state('prompt');
1119 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
1120 #push @out, $self->msg('sendabort');
1121 push @out, $self->msg('m10');
1122 delete $loc->{lines};
1124 delete $self->{loc};
1126 $self->state('prompt');
1127 } elsif ($line =~ m|^/+\w+|) {
1128 # this is a command that you want display for your own reference
1129 # or if it has TWO slashes is a command
1131 my $store = $line =~ s|^/+||;
1132 my @in = $self->run_cmd($line);
1135 foreach my $l (@in) {
1136 if (my @ans = BadWords::check($l)) {
1137 $self->{badcount} += @ans;
1138 Log('msg', $self->call . " used badwords: @ans to @{$loc->{to}} subject: '$loc->{subject}' in msg") unless $loc->{reject};
1139 Log('msg', "line: $l");
1142 push @{$loc->{lines}}, length($l) > 0 ? $l : " ";
1146 if (my @ans = BadWords::check($line)) {
1147 $self->{badcount} += @ans;
1148 Log('msg', $self->call . " used badwords: @ans to @{$loc->{to}} subject: '$loc->{subject}' in msg") unless $loc->{reject};
1149 Log('msg', "line: $line");
1153 if ($loc->{lines} && @{$loc->{lines}}) {
1154 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
1156 # temporarily store any R: lines so that we end up with
1157 # only the first and last ones stored.
1158 if ($line =~ m|^R:\d{6}/\d{4}|) {
1159 push @{$loc->{tempr}}, $line;
1161 if (exists $loc->{tempr}) {
1162 push @{$loc->{lines}}, shift @{$loc->{tempr}};
1163 push @{$loc->{lines}}, pop @{$loc->{tempr}} if @{$loc->{tempr}};
1164 delete $loc->{tempr};
1166 push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
1170 # i.e. it ain't and end or abort, therefore store the line
1176 # return the standard directory line for this ref
1180 my $flag = $ref->{private} && $ref->{read} ? '-' : ' ';
1183 } elsif ($ref->{delete}) {
1184 $flag = $ref->{deletetime} > $main::systime ? 'D' : 'E';
1186 return sprintf("%6d%s%s%5d %8.8s %8.8s %-6.6s %5.5s %-30.30s",
1187 $ref->{msgno}, $flag, $ref->{private} ? 'p' : ' ',
1188 $ref->{size}, $ref->{to}, $ref->{from}, cldate($ref->{t}),
1189 ztime($ref->{t}), $ref->{subject});
1192 # load the forward table
1196 my $s = readfilestr($forwardfn);
1199 push @out, $@ if $@;
1204 # load the bad message table
1208 my $s = readfilestr($badmsgfn);
1211 push @out, $@ if $@;
1216 # load the swop message table
1220 my $s = readfilestr($swopfn);
1223 push @out, $@ if $@;
1229 # forward that message or not according to the forwarding table
1230 # returns 1 for forward, 0 - to ignore
1239 for ($i = 0; $i < @forward; $i += 5) {
1240 my ($sort, $field, $pattern, $action, $bbs) = @forward[$i..($i+4)];
1243 # are we interested?
1244 next if $ref->{private} && $sort ne 'P';
1245 next if !$ref->{private} && $sort ne 'B';
1248 $tested = $ref->{to} if $field eq 'T';
1249 $tested = $ref->{from} if $field eq 'F';
1250 $tested = $ref->{origin} if $field eq 'O';
1251 $tested = $ref->{subject} if $field eq 'S';
1253 if (!$pattern || $tested =~ m{$pattern}i) {
1254 return 0 if $action eq 'I';
1255 return 1 if !$bbs || grep $_ eq $call, @{$bbs};
1262 # look down the forward table to see whether this is a valid bull
1263 # or not (ie it will forward somewhere even if it is only here)
1271 return 1 if $call =~ /^ALL/;
1272 return 1 if $call =~ /^DX/;
1276 for ($i = 0; $i < @forward; $i += 5) {
1277 my ($sort, $field, $pattern, $action, $bbs) = @forward[$i..($i+4)];
1278 if ($field eq 'T') {
1279 if (!$pattern || $call =~ m{$pattern}i) {
1293 for ($i = 0; $i < @badmsg; $i += 3) {
1294 my ($sort, $field, $pattern) = @badmsg[$i..($i+2)];
1297 # are we interested?
1298 next if $ref->{private} && $sort ne 'P';
1299 next if !$ref->{private} && $sort ne 'B';
1302 $tested = $ref->{to} if $field eq 'T';
1303 $tested = $ref->{from} if $field eq 'F';
1304 $tested = $ref->{origin} if $field eq 'O';
1305 $tested = $ref->{subject} if $field eq 'S';
1306 $tested = $call if $field eq 'I';
1308 if (!$pattern || $tested =~ m{$pattern}i) {
1322 for ($i = 0; $i < @swop; $i += 5) {
1323 my ($sort, $field, $pattern, $tfield, $topattern) = @swop[$i..($i+4)];
1328 # are we interested?
1329 next if $ref->{private} && $sort ne 'P';
1330 next if !$ref->{private} && $sort ne 'B';
1333 $tested = $ref->{to} if $field eq 'T';
1334 $tested = $ref->{from} if $field eq 'F';
1335 $tested = $ref->{origin} if $field eq 'O';
1336 $tested = $ref->{subject} if $field eq 'S';
1339 $old = $swop = $ref->{to} if $tfield eq 'T';
1340 $old = $swop = $ref->{from} if $tfield eq 'F';
1341 $old = $swop = $ref->{origin} if $tfield eq 'O';
1342 $old = $swop = $ref->{subject} if $tfield eq 'S';
1344 if ($tested =~ m{$pattern}i) {
1345 if ($tested eq $swop) {
1346 $swop =~ s{$pattern}{$topattern}i;
1350 Log('msg', "Msg $ref->{msgno}: $tfield $old -> $swop");
1351 Log('dbg', "Msg $ref->{msgno}: $tfield $old -> $swop");
1352 $ref->{to} = $swop if $tfield eq 'T';
1353 $ref->{from} = $swop if $tfield eq 'F';
1354 $ref->{origin} = $swop if $tfield eq 'O';
1355 $ref->{subject} = $swop if $tfield eq 'S';
1362 # import any msgs in the import directory
1363 # the messages are in BBS format (but may have cluster extentions
1364 # so SB UK < GB7TLH is legal
1367 # are there any to do in this directory?
1368 return unless -d $importfn;
1369 unless (opendir(DIR, $importfn)) {
1370 dbg("can\'t open $importfn $!") if isdbg('msg');
1371 Log('msg', "can\'t open $importfn $!");
1375 my @names = readdir(DIR);
1378 foreach $name (@names) {
1379 next if $name =~ /^\./;
1380 my $splitit = $name =~ /^split/;
1381 my $fn = "$importfn/$name";
1383 unless (open(MSG, $fn)) {
1384 dbg("can\'t open import file $fn $!") if isdbg('msg');
1385 Log('msg', "can\'t open import file $fn $!");
1389 my @msg = map { chomp; $_ } <MSG>;
1392 my @out = import_one($main::me, \@msg, $splitit);
1397 # import one message as a list in bbs (as extended) mode
1398 # takes a reference to an array containing the whole message
1403 my $splitit = shift;
1407 my $from = $dxchan->call;
1408 my $origin = $main::mycall;
1413 my $line = shift @$ref;
1414 my @f = split /([\s\@\$])/, $line;
1415 @f = map {s/\s+//g; length $_ ? $_ : ()} @f;
1417 unless (@f && $f[0] =~ /^(:?S|SP|SB|SEND)$/ ) {
1418 my $m = "invalid first line in import '$line'";
1419 dbg($m) if isdbg('msg');
1423 my $f = uc shift @f;
1424 next if $f eq 'SEND';
1426 # private / noprivate / rr
1427 if ($notincalls && ($f eq 'B' || $f eq 'SB' || $f =~ /^NOP/oi)) {
1429 } elsif ($notincalls && ($f eq 'P' || $f eq 'SP' || $f =~ /^PRI/oi)) {
1431 } elsif ($notincalls && ($f eq 'RR')) {
1433 } elsif (($f =~ /^[\@\.\#\$]$/ || $f eq '.#') && @f) { # this is bbs syntax, for AT
1435 } elsif ($f eq '<' && @f) { # this is bbs syntax for from call
1436 $from = uc shift @f;
1437 } elsif ($f =~ /^\$/) { # this is bbs syntax for a bid
1439 } elsif ($f =~ /^<(\S+)/) { # this is bbs syntax for from call
1441 } elsif ($f =~ /^\$\S+/) { # this is bbs syntax for bid
1448 # is this callsign a distro?
1449 my $fn = "$msgdir/distro/$f.pl";
1451 my $fh = new IO::File $fn;
1458 return (1, "Error in Distro $f.pl:", $@) if $@;
1466 if (grep $_ eq $f, @DXMsg::badmsg) {
1467 push @out, $dxchan->msg('m3', $f);
1474 # subject is the next line
1475 my $subject = shift @$ref;
1477 # strip off trailing lines
1478 pop @$ref while (@$ref && $$ref[-1] =~ /^\s*$/);
1480 # strip off /EX or /ABORT
1481 return ("aborted") if @$ref && $$ref[-1] =~ m{^/ABORT$}i;
1482 pop @$ref if (@$ref && $$ref[-1] =~ m{^/EX$}i);
1484 # sort out any splitting that needs to be done
1490 if ($lth >= $maxchunk || ($lth > $minchunk && /^\s*$/)) {
1491 push @chunk, $lines;
1498 push @chunk, $lines if @$lines;
1503 # does an identical message already exist?
1506 if (substr($subject,0,28) eq substr($m->{subject},0,28) && $from eq $m->{from} && grep $m->{to} eq $_, @to) {
1507 my $msgno = $m->{msgno};
1508 dbg("duplicate message from $from -> $m->{to} to msg: $msgno") if isdbg('msg');
1509 Log('msg', "duplicate message from $from -> $m->{to} to msg: $msgno");
1514 # write all the messages away
1516 for ( $i = 0; $i < @chunk; $i++) {
1517 my $chunk = $chunk[$i];
1520 my $num = " [" . ($i+1) . "/" . scalar @chunk . "]";
1521 $ch_subject = substr($subject, 0, 27 - length $num) . $num;
1523 $ch_subject = $subject;
1527 my $systime = $main::systime;
1528 my $mycall = $main::mycall;
1529 my $mref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
1538 $mref->swop_it($main::mycall);
1539 $mref->store($chunk);
1541 push @out, $dxchan->msg('m11', $mref->{msgno}, $to);
1542 #push @out, "msgno $ref->{msgno} sent to $to";
1553 my $name = $AUTOLOAD;
1554 return if $name =~ /::DESTROY$/;
1555 $name =~ s/^.*:://o;
1557 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
1558 # this clever line of code creates a subroutine which takes over from autoload
1559 # from OO Perl - Conway
1560 *$AUTOLOAD = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};