made queueing of messages purely periodic
[spider.git] / perl / DXMsg.pm
1 #!/usr/bin/perl
2 #
3 # This module impliments the message handling for a dx cluster
4 #
5 # Copyright (c) 1998 Dirk Koopman G1TLH
6 #
7 # $Id$
8 #
9 #
10 # Notes for implementors:-
11 #
12 # PC28 field 11 is the RR required flag
13 # PC28 field 12 is a VIA routing (ie it is a node call) 
14 #
15
16 package DXMsg;
17
18 @ISA = qw(DXProt DXChannel);
19
20 use DXUtil;
21 use DXChannel;
22 use DXUser;
23 use DXM;
24 use DXCluster;
25 use DXProtVars;
26 use DXProtout;
27 use DXDebug;
28 use DXLog;
29 use IO::File;
30 use Fcntl;
31 use Carp;
32
33 use strict;
34 use vars qw(%work @msg $msgdir %valid %busy $maxage $last_clean
35                         @badmsg $badmsgfn $forwardfn @forward $timeout $waittime
36                     $queueinterval $lastq);
37
38 %work = ();                                             # outstanding jobs
39 @msg = ();                                              # messages we have
40 %busy = ();                                             # station interlocks
41 $msgdir = "$main::root/msg";    # directory contain the msgs
42 $maxage = 30 * 86400;                   # the maximum age that a message shall live for if not marked 
43 $last_clean = 0;                                # last time we did a clean
44 @forward = ();                  # msg forward table
45 $timeout = 30*60;               # forwarding timeout
46 $waittime = 60*60;              # time an aborted outgoing message waits before trying again
47 $queueinterval = 5*60;          # run the queue every 5 minutes
48 $lastq = 0;
49
50
51 $badmsgfn = "$msgdir/badmsg.pl";  # list of TO address we wont store
52 $forwardfn = "$msgdir/forward.pl";  # the forwarding table
53
54 %valid = (
55                   fromnode => '9,From Node',
56                   tonode => '9,To Node',
57                   to => '0,To',
58                   from => '0,From',
59                   t => '0,Msg Time,cldatetime',
60                   private => '9,Private',
61                   subject => '0,Subject',
62                   linesreq => '0,Lines per Gob',
63                   rrreq => '9,Read Confirm',
64                   origin => '0,Origin',
65                   lines => '5,Data',
66                   stream => '9,Stream No',
67                   count => '9,Gob Linecnt',
68                   file => '9,File?,yesno',
69                   gotit => '9,Got it Nodes,parray',
70                   lines => '9,Lines,parray',
71                   'read' => '9,Times read',
72                   size => '0,Size',
73                   msgno => '0,Msgno',
74                   keep => '0,Keep this?,yesno',
75                   lastt => '9,Last processed,cldatetime',
76                   waitt => '9,Wait until,cldatetime',
77                  );
78
79 sub DESTROY
80 {
81         my $self = shift;
82         undef $self->{lines};
83         undef $self->{gotit};
84 }
85
86 # allocate a new object
87 # called fromnode, tonode, from, to, datetime, private?, subject, nolinesper  
88 sub alloc                  
89 {
90         my $pkg = shift;
91         my $self = bless {}, $pkg;
92         $self->{msgno} = shift;
93         my $to = shift;
94         #  $to =~ s/-\d+$//o;
95         $self->{to} = ($to eq $main::mycall) ? $main::myalias : $to;
96         my $from = shift;
97         $from =~ s/-\d+$//o;
98         $self->{from} = uc $from;
99         $self->{t} = shift;
100         $self->{private} = shift;
101         $self->{subject} = shift;
102         $self->{origin} = shift;
103         $self->{'read'} = shift;
104         $self->{rrreq} = shift;
105         $self->{gotit} = [];
106         $self->{lastt} = $main::systime;
107     
108         return $self;
109 }
110
111 sub workclean
112 {
113         my $ref = shift;
114         delete $ref->{lines};
115         delete $ref->{linesreq};
116         delete $ref->{tonode};
117         delete $ref->{fromnode};
118         delete $ref->{stream};
119         delete $ref->{lines};
120         delete $ref->{file};
121         delete $ref->{count};
122         delete $ref->{lastt} if exists $ref->{lastt};
123         delete $ref->{waitt} if exists $ref->{waitt};
124 }
125
126 sub process
127 {
128         my ($self, $line) = @_;
129
130         # this is periodic processing
131         if (!$self || !$line) {
132
133                 # wander down the work queue stopping any messages that have timed out
134                 for (keys %busy) {
135                         my $node = $_;
136                         my $ref = $busy{$_};
137                         if (exists $ref->{lastt} && $main::systime > $ref->{lastt} + $timeout) {
138                                 $ref->stop_msg($node);
139
140                                 # delay any outgoing messages that fail
141                                 $ref->{waitt} = $main::systime + $waittime + rand(120) if $node ne $main::mycall;
142                         }
143                 }
144
145                 # queue some message if the interval timer has gone off
146                 if ($main::systime > $lastq + $queueinterval) {
147                         queue_msg(0);
148                         $lastq = $main::systime;
149                 }
150
151                 # clean the message queue
152                 clean_old() if $main::systime - $last_clean > 3600 ;
153                 return;
154         }
155
156         my @f = split /\^/, $line;
157         my ($pcno) = $f[0] =~ /^PC(\d\d)/; # just get the number
158
159  SWITCH: {
160                 if ($pcno == 28) {              # incoming message
161
162                         # first look for any messages in the busy queue 
163                         # and cancel them this should both resolve timed out incoming messages
164                         # and crossing of message between nodes, incoming messages have priority
165                         if (exists $busy{$f[2]}) {
166                                 my $ref = $busy{$f[2]};
167                                 my $tonode = $ref->{tonode};
168                                 $ref->stop_msg($self->call);
169                         }
170
171                         my $t = cltounix($f[5], $f[6]);
172                         my $stream = next_transno($f[2]);
173                         my $ref = DXMsg->alloc($stream, uc $f[3], $f[4], $t, $f[7], $f[8], $f[13], '0', $f[11]);
174                         
175                         # fill in various forwarding state variables
176                         $ref->{fromnode} = $f[2];
177                         $ref->{tonode} = $f[1];
178                         $ref->{rrreq} = $f[11];
179                         $ref->{linesreq} = $f[10];
180                         $ref->{stream} = $stream;
181                         $ref->{count} = 0;      # no of lines between PC31s
182                         dbg('msg', "new message from $f[4] to $f[3] '$f[8]' stream $stream\n");
183                         $work{"$f[2]$stream"} = $ref; # store in work
184                         $busy{$f[2]} = $ref; # set interlock
185                         $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
186                         $ref->{lastt} = $main::systime;
187                         last SWITCH;
188                 }
189                 
190                 if ($pcno == 29) {              # incoming text
191                         my $ref = $work{"$f[2]$f[3]"};
192                         if ($ref) {
193                                 push @{$ref->{lines}}, $f[4];
194                                 $ref->{count}++;
195                                 if ($ref->{count} >= $ref->{linesreq}) {
196                                         $self->send(DXProt::pc31($f[2], $f[1], $f[3]));
197                                         dbg('msg', "stream $f[3]: $ref->{count} lines received\n");
198                                         $ref->{count} = 0;
199                                 }
200                                 $ref->{lastt} = $main::systime;
201                         }
202                         last SWITCH;
203                 }
204                 
205                 if ($pcno == 30) {              # this is a incoming subject ack
206                         my $ref = $work{$f[2]}; # note no stream at this stage
207                         if ($ref) {
208                                 delete $work{$f[2]};
209                                 $ref->{stream} = $f[3];
210                                 $ref->{count} = 0;
211                                 $ref->{linesreq} = 5;
212                                 $work{"$f[2]$f[3]"} = $ref;     # new ref
213                                 dbg('msg', "incoming subject ack stream $f[3]\n");
214                                 $busy{$f[2]} = $ref; # interlock
215                                 $ref->{lines} = [];
216                                 push @{$ref->{lines}}, ($ref->read_msg_body);
217                                 $ref->send_tranche($self);
218                                 $ref->{lastt} = $main::systime;
219                         } else {
220                                 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
221                         } 
222                         last SWITCH;
223                 }
224                 
225                 if ($pcno == 31) {              # acknowledge a tranche of lines
226                         my $ref = $work{"$f[2]$f[3]"};
227                         if ($ref) {
228                                 dbg('msg', "tranche ack stream $f[3]\n");
229                                 $ref->send_tranche($self);
230                                 $ref->{lastt} = $main::systime;
231                         } else {
232                                 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
233                         } 
234                         last SWITCH;
235                 }
236                 
237                 if ($pcno == 32) {              # incoming EOM
238                         dbg('msg', "stream $f[3]: EOM received\n");
239                         my $ref = $work{"$f[2]$f[3]"};
240                         if ($ref) {
241                                 $self->send(DXProt::pc33($f[2], $f[1], $f[3])); # acknowledge it
242                                 
243                                 # get the next msg no - note that this has NOTHING to do with the stream number in PC protocol
244                                 # store the file or message
245                                 # remove extraneous rubbish from the hash
246                                 # remove it from the work in progress vector
247                                 # stuff it on the msg queue
248                                 if ($ref->{lines} && @{$ref->{lines}} > 0) { # ignore messages with 0 lines
249                                         if ($ref->{file}) {
250                                                 $ref->store($ref->{lines});
251                                         } else {
252
253                                                 # does an identical message already exist?
254                                                 my $m;
255                                                 for $m (@msg) {
256                                                         if ($ref->{subject} eq $m->{subject} && $ref->{t} == $m->{t} && $ref->{from} eq $m->{from}) {
257                                                                 $ref->stop_msg($self->call);
258                                                                 my $msgno = $m->{msgno};
259                                                                 dbg('msg', "duplicate message to $msgno\n");
260                                                                 Log('msg', "duplicate message to $msgno");
261                                                                 return;
262                                                         }
263                                                 }
264                                                         
265                                                 # look for 'bad' to addresses 
266                                                 if (grep $ref->{to} eq $_, @badmsg) {
267                                                         $ref->stop_msg($self->call);
268                                                         dbg('msg', "'Bad' TO address $ref->{to}");
269                                                         Log('msg', "'Bad' TO address $ref->{to}");
270                                                         return;
271                                                 }
272
273                                                 $ref->{msgno} = next_transno("Msgno");
274                                                 push @{$ref->{gotit}}, $f[2]; # mark this up as being received
275                                                 $ref->store($ref->{lines});
276                                                 add_dir($ref);
277                                                 my $dxchan = DXChannel->get($ref->{to});
278                                                 $dxchan->send($dxchan->msg('m9')) if $dxchan;
279                                                 Log('msg', "Message $ref->{msgno} from $ref->{from} received from $f[2] for $ref->{to}");
280                                         }
281                                 }
282                                 $ref->stop_msg($self->call);
283                         } else {
284                                 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
285                         }
286                         # queue_msg(0);
287                         last SWITCH;
288                 }
289                 
290                 if ($pcno == 33) {              # acknowledge the end of message
291                         my $ref = $work{"$f[2]$f[3]"};
292                         if ($ref) {
293                                 if ($ref->{private}) { # remove it if it private and gone off site#
294                                         Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2] and deleted");
295                                         $ref->del_msg;
296                                 } else {
297                                         Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $f[2]");
298                                         push @{$ref->{gotit}}, $f[2]; # mark this up as being received
299                                         $ref->store($ref->{lines});     # re- store the file
300                                 }
301                                 $ref->stop_msg($self->call);
302                         } else {
303                                 $self->send(DXProt::pc42($f[2], $f[1], $f[3])); # unknown stream
304                         } 
305                         last SWITCH;
306                 }
307                 
308                 if ($pcno == 40) {              # this is a file request
309                         $f[3] =~ s/\\/\//og; # change the slashes
310                         $f[3] =~ s/\.//og;      # remove dots
311                         $f[3] =~ s/^\///o;   # remove the leading /
312                         $f[3] = lc $f[3];       # to lower case;
313                         dbg('msg', "incoming file $f[3]\n");
314                         $f[3] = 'packclus/' . $f[3] unless $f[3] =~ /^packclus\//o;
315                         
316                         # create any directories
317                         my @part = split /\//, $f[3];
318                         my $part;
319                         my $fn = "$main::root";
320                         pop @part;                      # remove last part
321                         foreach $part (@part) {
322                                 $fn .= "/$part";
323                                 next if -e $fn;
324                                 last SWITCH if !mkdir $fn, 0777;
325                                 dbg('msg', "created directory $fn\n");
326                         }
327                         my $stream = next_transno($f[2]);
328                         my $ref = DXMsg->alloc($stream, "$main::root/$f[3]", $self->call, time, !$f[4], $f[3], ' ', '0', '0');
329                         
330                         # forwarding variables
331                         $ref->{fromnode} = $f[1];
332                         $ref->{tonode} = $f[2];
333                         $ref->{linesreq} = $f[5];
334                         $ref->{stream} = $stream;
335                         $ref->{count} = 0;      # no of lines between PC31s
336                         $ref->{file} = 1;
337                         $work{"$f[2]$stream"} = $ref; # store in work
338                         $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack 
339                         
340                         last SWITCH;
341                 }
342                 
343                 if ($pcno == 42) {              # abort transfer
344                         dbg('msg', "stream $f[3]: abort received\n");
345                         my $ref = $work{"$f[2]$f[3]"};
346                         if ($ref) {
347                                 $ref->stop_msg($self->call);
348                                 $ref = undef;
349                         }
350                         
351                         last SWITCH;
352                 }
353
354                 if ($pcno == 49) {      # global delete on subject
355                         for (@msg) {
356                                 if ($_->{from} eq $f[1] && $_->{subject} eq $f[2]) {
357                                         $_->del_msg();
358                                         Log('msg', "Message $_->{msgno} from $_->{from} ($_->{subject}) fully deleted");
359                                         DXProt::broadcast_ak1a($line, $self);
360                                 }
361                         }
362                 }
363         }
364 }
365
366
367 # store a message away on disc or whatever
368 #
369 # NOTE the second arg is a REFERENCE not a list
370 sub store
371 {
372         my $ref = shift;
373         my $lines = shift;
374         
375         # we only proceed if there are actually any lines in the file
376         if (!$lines || @{$lines} == 0) {
377                 return;
378         }
379         
380         if ($ref->{file}) {                     # a file
381                 dbg('msg', "To be stored in $ref->{to}\n");
382                 
383                 my $fh = new IO::File "$ref->{to}", "w";
384                 if (defined $fh) {
385                         my $line;
386                         foreach $line (@{$lines}) {
387                                 print $fh "$line\n";
388                         }
389                         $fh->close;
390                         dbg('msg', "file $ref->{to} stored\n");
391                         Log('msg', "file $ref->{to} from $ref->{from} stored" );
392                 } else {
393                         confess "can't open file $ref->{to} $!";  
394                 }
395         } else {                                        # a normal message
396
397                 # attempt to open the message file
398                 my $fn = filename($ref->{msgno});
399                 
400                 dbg('msg', "To be stored in $fn\n");
401                 
402                 # now save the file, overwriting what's there, YES I KNOW OK! (I will change it if it's a problem)
403                 my $fh = new IO::File "$fn", "w";
404                 if (defined $fh) {
405                         my $rr = $ref->{rrreq} ? '1' : '0';
406                         my $priv = $ref->{private} ? '1': '0';
407                         print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$priv^$ref->{subject}^$ref->{origin}^$ref->{'read'}^$rr\n";
408                         print $fh "=== ", join('^', @{$ref->{gotit}}), "\n";
409                         my $line;
410                         $ref->{size} = 0;
411                         foreach $line (@{$lines}) {
412                                 $ref->{size} += (length $line) + 1;
413                                 print $fh "$line\n";
414                         }
415                         $fh->close;
416                         dbg('msg', "msg $ref->{msgno} stored\n");
417                         Log('msg', "msg $ref->{msgno} from $ref->{from} to $ref->{to} stored" );
418                 } else {
419                         confess "can't open msg file $fn $!";  
420                 }
421         }
422 }
423
424 # delete a message
425 sub del_msg
426 {
427         my $self = shift;
428         
429         # remove it from the active message list
430         @msg = map { $_ != $self ? $_ : () } @msg;
431         
432         # belt and braces (one day I will ask someone if this is REALLY necessary)
433         delete $self->{gotit};
434         delete $self->{list};
435         
436         # remove the file
437         unlink filename($self->{msgno});
438         dbg('msg', "deleting $self->{msgno}\n");
439 }
440
441 # clean out old messages from the message queue
442 sub clean_old
443 {
444         my $ref;
445         
446         # mark old messages for deletion
447         foreach $ref (@msg) {
448                 if (!$ref->{keep} && $ref->{t} < $main::systime - $maxage) {
449                         $ref->{deleteme} = 1;
450                         delete $ref->{gotit};
451                         delete $ref->{list};
452                         unlink filename($ref->{msgno});
453                         dbg('msg', "deleting old $ref->{msgno}\n");
454                 }
455         }
456         
457         # remove them all from the active message list
458         @msg = map { $_->{deleteme} ? () : $_ } @msg;
459         $last_clean = $main::systime;
460 }
461
462 # read in a message header
463 sub read_msg_header
464
465         my $fn = shift;
466         my $file;
467         my $line;
468         my $ref;
469         my @f;
470         my $size;
471         
472         $file = new IO::File;
473         if (!open($file, $fn)) {
474                 print "Error reading $fn $!\n";
475                 return undef;
476         }
477         $size = -s $fn;
478         $line = <$file>;                        # first line
479         chomp $line;
480         $size -= length $line;
481         if (! $line =~ /^===/o) {
482                 print "corrupt first line in $fn ($line)\n";
483                 return undef;
484         }
485         $line =~ s/^=== //o;
486         @f = split /\^/, $line;
487         $ref = DXMsg->alloc(@f);
488         
489         $line = <$file>;                        # second line
490         chomp $line;
491         $size -= length $line;
492         if (! $line =~ /^===/o) {
493                 print "corrupt second line in $fn ($line)\n";
494                 return undef;
495         }
496         $line =~ s/^=== //o;
497         $ref->{gotit} = [];
498         @f = split /\^/, $line;
499         push @{$ref->{gotit}}, @f;
500         $ref->{size} = $size;
501         
502         close($file);
503         
504         return $ref;
505 }
506
507 # read in a message header
508 sub read_msg_body
509 {
510         my $self = shift;
511         my $msgno = $self->{msgno};
512         my $file;
513         my $line;
514         my $fn = filename($msgno);
515         my @out;
516         
517         $file = new IO::File;
518         if (!open($file, $fn)) {
519                 print "Error reading $fn $!\n";
520                 return undef;
521         }
522         chomp (@out = <$file>);
523         close($file);
524         
525         shift @out if $out[0] =~ /^=== /;
526         shift @out if $out[0] =~ /^=== /;
527         return @out;
528 }
529
530 # send a tranche of lines to the other end
531 sub send_tranche
532 {
533         my ($self, $dxchan) = @_;
534         my @out;
535         my $to = $self->{tonode};
536         my $from = $self->{fromnode};
537         my $stream = $self->{stream};
538         my $lines = $self->{lines};
539         my ($c, $i);
540         
541         for ($i = 0, $c = $self->{count}; $i < $self->{linesreq} && $c < @$lines; $i++, $c++) {
542                 push @out, DXProt::pc29($to, $from, $stream, $lines->[$c]);
543     }
544     $self->{count} = $c;
545
546     push @out, DXProt::pc32($to, $from, $stream) if $i < $self->{linesreq};
547         $dxchan->send(@out);
548 }
549
550         
551 # find a message to send out and start the ball rolling
552 sub queue_msg
553 {
554         my $sort = shift;
555         my $call = shift;
556         my $ref;
557         my $clref;
558         my $dxchan;
559         my @nodelist = DXProt::get_all_ak1a();
560         
561         # bat down the message list looking for one that needs to go off site and whose
562         # nearest node is not busy.
563
564         dbg('msg', "queue msg ($sort)\n");
565         foreach $ref (@msg) {
566                 # firstly, is it private and unread? if so can I find the recipient
567                 # in my cluster node list offsite?
568
569                 # ignore 'delayed' messages until their waiting time has expired
570                 if (exists $ref->{waitt}) {
571                         next if $ref->{waitt} < $main::systime;
572                         delete $ref->{waitt};
573                 } 
574                 
575                 if ($ref->{private}) {
576                         if ($ref->{'read'} == 0) {
577                                 $clref = DXCluster->get_exact($ref->{to});
578                                 unless ($clref) {             # otherwise look for a homenode
579                                         my $uref = DXUser->get($ref->{to});
580                                         my $hnode =  $uref->homenode if $uref;
581                                         $clref = DXCluster->get_exact($hnode) if $hnode;
582                                 }
583                                 if ($clref && !grep { $clref->{dxchan} == $_ } DXCommandmode::get_all) {
584                                         $dxchan = $clref->{dxchan};
585                                         $ref->start_msg($dxchan) if $dxchan && $clref && !get_busy($dxchan->call) && $dxchan->state eq 'normal';
586                                 }
587                         }
588                 } elsif (!$sort) {
589                         # otherwise we are dealing with a bulletin, compare the gotit list with
590                         # the nodelist up above, if there are sites that haven't got it yet
591                         # then start sending it - what happens when we get loops is anyone's
592                         # guess, use (to, from, time, subject) tuple?
593                         my $noderef;
594                         foreach $noderef (@nodelist) {
595                                 next if $noderef->call eq $main::mycall;
596                                 next if grep { $_ eq $noderef->call } @{$ref->{gotit}};
597                                 next unless $ref->forward_it($noderef->call);           # check the forwarding file
598                                 # next if $noderef->isolate;               # maybe add code for stuff originated here?
599                                 # next if DXUser->get( ${$ref->{gotit}}[0] )->isolate;  # is the origin isolated?
600                                 
601                                 # if we are here we have a node that doesn't have this message
602                                 $ref->start_msg($noderef) if !get_busy($noderef->call)  && $noderef->state eq 'normal';
603                                 last;
604                         }
605                 }
606                 
607                 # if all the available nodes are busy then stop
608                 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
609         }
610 }
611
612 # is there a message for me?
613 sub for_me
614 {
615         my $call = uc shift;
616         my $ref;
617         
618         foreach $ref (@msg) {
619                 # is it for me, private and unread? 
620                 if ($ref->{to} eq $call && $ref->{private}) {
621                         return 1 if !$ref->{'read'};
622                 }
623         }
624         return 0;
625 }
626
627 # start the message off on its travels with a PC28
628 sub start_msg
629 {
630         my ($self, $dxchan) = @_;
631         
632         dbg('msg', "start msg $self->{msgno}\n");
633         $self->{linesreq} = 5;
634         $self->{count} = 0;
635         $self->{tonode} = $dxchan->call;
636         $self->{fromnode} = $main::mycall;
637         $busy{$self->{tonode}} = $self;
638         $work{$self->{tonode}} = $self;
639         $dxchan->send(DXProt::pc28($self->{tonode}, $self->{fromnode}, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $self->{origin}, $self->{rrreq}));
640 }
641
642 # get the ref of a busy node
643 sub get_busy
644 {
645         my $call = shift;
646         return $busy{$call};
647 }
648
649 # get the busy queue
650 sub get_all_busy
651 {
652         return values %busy;
653 }
654
655 # get the forwarding queue
656 sub get_fwq
657 {
658         return values %work;
659 }
660
661 # stop a message from continuing, clean it out, unlock interlocks etc
662 sub stop_msg
663 {
664         my $self = shift;
665         my $node = shift;
666         my $stream = $self->{stream} if exists $self->{stream};
667         
668         
669         dbg('msg', "stop msg $self->{msgno} -> node $node\n");
670         delete $work{$node};
671         delete $work{"$node$stream"} if $stream;
672         $self->workclean;
673         delete $busy{$node};
674 }
675
676 # get a new transaction number from the file specified
677 sub next_transno
678 {
679         my $name = shift;
680         $name =~ s/\W//og;                      # remove non-word characters
681         my $fn = "$msgdir/$name";
682         my $msgno;
683         
684         my $fh = new IO::File;
685         if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
686                 $fh->autoflush(1);
687                 $msgno = $fh->getline;
688                 chomp $msgno;
689                 $msgno++;
690                 seek $fh, 0, 0;
691                 $fh->print("$msgno\n");
692                 dbg('msg', "msgno $msgno allocated for $name\n");
693                 $fh->close;
694         } else {
695                 confess "can't open $fn $!";
696         }
697         return $msgno;
698 }
699
700 # initialise the message 'system', read in all the message headers
701 sub init
702 {
703         my $dir = new IO::File;
704         my @dir;
705         my $ref;
706
707         # load various control files
708         my @in = load_badmsg();
709         print "@in\n" if @in;
710         @in = load_forward();
711         print "@in\n" if @in;
712
713         # read in the directory
714         opendir($dir, $msgdir) or confess "can't open $msgdir $!";
715         @dir = readdir($dir);
716         closedir($dir);
717
718         @msg = ();
719         for (sort @dir) {
720                 next unless /^m\d+$/o;
721                 
722                 $ref = read_msg_header("$msgdir/$_");
723                 next unless $ref;
724                 
725                 # delete any messages to 'badmsg.pl' places
726                 if (grep $ref->{to} eq $_, @badmsg) {
727                         dbg('msg', "'Bad' TO address $ref->{to}");
728                         Log('msg', "'Bad' TO address $ref->{to}");
729                         $ref->del_msg;
730                         next;
731                 }
732
733                 # add the message to the available queue
734                 add_dir($ref); 
735         }
736 }
737
738 # add the message to the directory listing
739 sub add_dir
740 {
741         my $ref = shift;
742         confess "tried to add a non-ref to the msg directory" if !ref $ref;
743         push @msg, $ref;
744 }
745
746 # return all the current messages
747 sub get_all
748 {
749         return @msg;
750 }
751
752 # get a particular message
753 sub get
754 {
755         my $msgno = shift;
756         for (@msg) {
757                 return $_ if $_->{msgno} == $msgno;
758                 last if $_->{msgno} > $msgno;
759         }
760         return undef;
761 }
762
763 # return the official filename for a message no
764 sub filename
765 {
766         return sprintf "$msgdir/m%06d", shift;
767 }
768
769 #
770 # return a list of valid elements 
771
772
773 sub fields
774 {
775         return keys(%valid);
776 }
777
778 #
779 # return a prompt for a field
780 #
781
782 sub field_prompt
783
784         my ($self, $ele) = @_;
785         return $valid{$ele};
786 }
787
788 #
789 # send a message state machine
790 sub do_send_stuff
791 {
792         my $self = shift;
793         my $line = shift;
794         my @out;
795         
796         if ($self->state eq 'send1') {
797                 #  $DB::single = 1;
798                 confess "local var gone missing" if !ref $self->{loc};
799                 my $loc = $self->{loc};
800                 $loc->{subject} = $line;
801                 $loc->{lines} = [];
802                 $self->state('sendbody');
803                 #push @out, $self->msg('sendbody');
804                 push @out, $self->msg('m8');
805         } elsif ($self->state eq 'sendbody') {
806                 confess "local var gone missing" if !ref $self->{loc};
807                 my $loc = $self->{loc};
808                 if ($line eq "\032" || uc $line eq "/EX") {
809                         my $to;
810                         
811                         if (@{$loc->{lines}} > 0) {
812                                 foreach $to (@{$loc->{to}}) {
813                                         my $ref;
814                                         my $systime = $main::systime;
815                                         my $mycall = $main::mycall;
816                                         $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
817                                                                                 uc $to,
818                                                                                 $self->call, 
819                                                                                 $systime,
820                                                                                 $loc->{private}, 
821                                                                                 $loc->{subject}, 
822                                                                                 $mycall,
823                                                                                 '0',
824                                                                                 $loc->{rrreq});
825                                         $ref->store($loc->{lines});
826                                         $ref->add_dir();
827                                         push @out, $self->msg('m11', $ref->{msgno}, $to);
828                                         #push @out, "msgno $ref->{msgno} sent to $to";
829                                         my $dxchan = DXChannel->get(uc $to);
830                                         if ($dxchan) {
831                                                 if ($dxchan->is_user()) {
832                                                         $dxchan->send($dxchan->msg('m9'));
833                                                 }
834                                         }
835                                 }
836                         }
837                         delete $loc->{lines};
838                         delete $loc->{to};
839                         delete $self->{loc};
840                         $self->func(undef);
841                         
842                         $self->state('prompt');
843                 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
844                         #push @out, $self->msg('sendabort');
845                         push @out, $self->msg('m10');
846                         delete $loc->{lines};
847                         delete $loc->{to};
848                         delete $self->{loc};
849                         $self->func(undef);
850                         $self->state('prompt');
851                 } else {
852                         
853                         # i.e. it ain't and end or abort, therefore store the line
854                         push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
855                 }
856         }
857         return (1, @out);
858 }
859
860 # return the standard directory line for this ref 
861 sub dir
862 {
863         my $ref = shift;
864         return sprintf "%6d%s%s%5d %8.8s %8.8s %-6.6s %5.5s %-30.30s", 
865                 $ref->msgno, $ref->read ? '-' : ' ', $ref->private ? 'p' : ' ', $ref->size,
866                         $ref->to, $ref->from, cldate($ref->t), ztime($ref->t), $ref->subject;
867 }
868
869 # load the forward table
870 sub load_forward
871 {
872         my @out;
873         do "$forwardfn" if -e "$forwardfn";
874         push @out, $@ if $@;
875         return @out;
876 }
877
878 # load the bad message table
879 sub load_badmsg
880 {
881         my @out;
882         do "$badmsgfn" if -e "$badmsgfn";
883         push @out, $@ if $@;
884         return @out;
885 }
886
887 #
888 # forward that message or not according to the forwarding table
889 # returns 1 for forward, 0 - to ignore
890 #
891
892 sub forward_it
893 {
894         my $ref = shift;
895         my $call = shift;
896         my $i;
897         
898         for ($i = 0; $i < @forward; $i += 5) {
899                 my ($sort, $field, $pattern, $action, $bbs) = @forward[$i..($i+4)]; 
900                 my $tested;
901                 
902                 # are we interested?
903                 last if $ref->{private} && $sort ne 'P';
904                 last if !$ref->{private} && $sort ne 'B';
905                 
906                 # select field
907                 $tested = $ref->{to} if $field eq 'T';
908                 $tested = $ref->{from} if $field eq 'F';
909                 $tested = $ref->{origin} if $field eq 'O';
910                 $tested = $ref->{subject} if $field eq 'S';
911
912                 if (!$pattern || $tested =~ m{$pattern}i) {
913                         return 0 if $action eq 'I';
914                         return 1 if !$bbs || grep $_ eq $call, @{$bbs};
915                 }
916         }
917         return 0;
918 }
919
920 no strict;
921 sub AUTOLOAD
922 {
923         my $self = shift;
924         my $name = $AUTOLOAD;
925         return if $name =~ /::DESTROY$/;
926         $name =~ s/.*:://o;
927         
928         confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
929         @_ ? $self->{$name} = shift : $self->{$name} ;
930 }
931
932 1;
933
934 __END__