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