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