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