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