ignore deleted messages when queueing
[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 use DXUtil;
19 use DXChannel;
20 use DXUser;
21 use DXM;
22 use DXProtVars;
23 use DXProtout;
24 use DXDebug;
25 use DXLog;
26 use IO::File;
27 use Fcntl;
28
29 use strict;
30
31 use vars qw($VERSION $BRANCH);
32 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
33 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ ) || 0;
34 $main::build += $VERSION;
35 $main::branch += $BRANCH;
36
37 use vars qw(%work @msg $msgdir %valid %busy $maxage $last_clean $residencetime
38                         @badmsg @swop $swopfn $badmsgfn $forwardfn @forward $timeout $waittime
39                     $queueinterval $lastq $importfn $minchunk $maxchunk $bulltopriv);
40
41 %work = ();                                             # outstanding jobs
42 @msg = ();                                              # messages we have
43 %busy = ();                                             # station interlocks
44 $msgdir = "$main::root/msg";    # directory contain the msgs
45 $maxage = 30 * 86400;                   # the maximum age that a message shall live for if not marked 
46 $last_clean = 0;                                # last time we did a clean
47 @forward = ();                  # msg forward table
48 @badmsg = ();                                   # bad message table
49 @swop = ();                                             # swop table
50 $timeout = 30*60;               # forwarding timeout
51 $waittime = 30*60;              # time an aborted outgoing message waits before trying again
52 $queueinterval = 1*60;          # run the queue every 1 minute
53 $lastq = 0;
54
55 $minchunk = 4800;               # minimum chunk size for a split message
56 $maxchunk = 6000;               # maximum chunk size
57 $bulltopriv = 1;                                # convert msgs with callsigns to private if they are bulls
58 $residencetime = 2*86400;       # keep deleted messages for this amount of time
59
60
61 $badmsgfn = "$msgdir/badmsg.pl";    # list of TO address we wont store
62 $forwardfn = "$msgdir/forward.pl";  # the forwarding table
63 $swopfn = "$msgdir/swop.pl";        # the swopping table
64 $importfn = "$msgdir/import";       # import directory
65
66
67 %valid = (
68                   fromnode => '5,From Node',
69                   tonode => '5,To Node',
70                   to => '0,To',
71                   from => '0,From',
72                   t => '0,Msg Time,cldatetime',
73                   private => '5,Private,yesno',
74                   subject => '0,Subject',
75                   linesreq => '0,Lines per Gob',
76                   rrreq => '5,Read Confirm,yesno',
77                   origin => '0,Origin',
78                   lines => '5,Data',
79                   stream => '9,Stream No',
80                   count => '5,Gob Linecnt',
81                   file => '5,File?,yesno',
82                   gotit => '5,Got it Nodes,parray',
83                   lines => '5,Lines,parray',
84                   'read' => '5,Times read',
85                   size => '0,Size',
86                   msgno => '0,Msgno',
87                   keep => '0,Keep this?,yesno',
88                   lastt => '5,Last processed,cldatetime',
89                   waitt => '5,Wait until,cldatetime',
90                   delete => '5,Awaiting Delete,yesno',
91                   deletetime => '5,Deletion Time,cldatetime',
92                  );
93
94 # allocate a new object
95 # called fromnode, tonode, from, to, datetime, private?, subject, nolinesper  
96 sub alloc                  
97 {
98         my $pkg = shift;
99         my $self = bless {}, $pkg;
100         $self->{msgno} = shift;
101         my $to = shift;
102         #  $to =~ s/-\d+$//o;
103         $self->{to} = ($to eq $main::mycall) ? $main::myalias : $to;
104         my $from = shift;
105         $from =~ s/-\d+$//o;
106         $self->{from} = uc $from;
107         $self->{t} = shift;
108         $self->{private} = shift;
109         $self->{subject} = shift;
110         $self->{origin} = shift;
111         $self->{'read'} = shift;
112         $self->{rrreq} = shift;
113         $self->{gotit} = [];
114 #       $self->{lastt} = $main::systime;
115         $self->{lines} = [];
116         $self->{private} = 1 if $bulltopriv && DXUser->get_current($self->{to});
117     
118         return $self;
119 }
120
121
122 sub process
123 {
124         my ($self, $line) = @_;
125
126         # this is periodic processing
127         if (!$self || !$line) {
128
129                 if ($main::systime >= $lastq + $queueinterval) {
130
131                         # queue some message if the interval timer has gone off
132                         queue_msg(0);
133
134                         # import any messages in the import directory
135                         import_msgs();
136                         
137                         $lastq = $main::systime;
138                 }
139
140                 # clean the message queue
141                 clean_old() if $main::systime - $last_clean > 3600 ;
142                 $last_clean = $main::systime;
143                 return;
144         }
145
146         my @f = split /\^/, $line;
147         my ($pcno) = $f[0] =~ /^PC(\d\d)/; # just get the number
148         my ($tonode, $fromnode) = @f[1, 2];
149         my $stream = $f[3] if ($pcno >= 29 && $pcno <= 33) || $pcno == 42;
150
151  SWITCH: {
152                 if ($pcno == 28) {              # incoming message
153
154                         # sort out various extant protocol errors that occur
155                         my $origin = $f[13];
156                         $origin = $self->call unless $origin && $origin gt ' ';
157
158                         # first look for any messages in the busy queue 
159                         # and cancel them this should both resolve timed out incoming messages
160                         # and crossing of message between nodes, incoming messages have priority
161
162                         my $ref = get_busy($fromnode);
163                         if ($ref) {
164                                 my $otonode = $ref->{tonode} || "unknown";
165                                 dbg("Busy, stopping msgno: $ref->{msgno} $fromnode->$otonode") if isdbg('msg');
166                                 $ref->stop_msg($fromnode);
167                         }
168
169                         my $t = cltounix($f[5], $f[6]);
170                         $stream = next_transno($fromnode);
171                         $ref = DXMsg->alloc($stream, uc $f[3], $f[4], $t, $f[7], $f[8], $origin, '0', $f[11]);
172                         
173                         # fill in various forwarding state variables
174                         $ref->{fromnode} = $fromnode;
175                         $ref->{tonode} = $tonode;
176                         $ref->{rrreq} = $f[11];
177                         $ref->{linesreq} = $f[10];
178                         $ref->{stream} = $stream;
179                         $ref->{count} = 0;      # no of lines between PC31s
180                         dbg("new message from $f[4] to $f[3] '$f[8]' stream $fromnode/$stream\n") if isdbg('msg');
181                         Log('msg', "Incoming message $f[4] to $f[3] '$f[8]' origin: $origin" );
182                         set_fwq($fromnode, $stream, $ref); # store in work
183                         set_busy($fromnode, $ref); # set interlock
184                         $self->send(DXProt::pc30($fromnode, $tonode, $stream)); # send ack
185                         $ref->{lastt} = $main::systime;
186
187                         # look to see whether this is a non private message sent to a known callsign
188                         my $uref = DXUser->get_current($ref->{to});
189                         if (is_callsign($ref->{to}) && !$ref->{private} && $uref && $uref->homenode) {
190                                 $ref->{private} = 1;
191                                 dbg("set bull to $ref->{to} to private") if isdbg('msg');
192                                 Log('msg', "set bull to $ref->{to} to private");
193                         }
194                         last SWITCH;
195                 }
196                 
197                 if ($pcno == 29) {              # incoming text
198                         my $ref = get_fwq($fromnode, $stream);
199                         if ($ref) {
200                                 $f[4] =~ s/\%5E/^/g;
201                                 if (@{$ref->{lines}}) {
202                                         push @{$ref->{lines}}, $f[4];
203                                 } else {
204                                         # temporarily store any R: lines so that we end up with 
205                                         # only the first and last ones stored.
206                                         if ($f[4] =~ m|^R:\d{6}/\d{4}|) {
207                                                 push @{$ref->{tempr}}, $f[4];
208                                         } else {
209                                                 if (exists $ref->{tempr}) {
210                                                         push @{$ref->{lines}}, shift @{$ref->{tempr}};
211                                                         push @{$ref->{lines}}, pop @{$ref->{tempr}} if @{$ref->{tempr}};
212                                                         delete $ref->{tempr};
213                                                 }
214                                                 push @{$ref->{lines}}, $f[4];
215                                         } 
216                                 }
217                                 $ref->{count}++;
218                                 if ($ref->{count} >= $ref->{linesreq}) {
219                                         $self->send(DXProt::pc31($fromnode, $tonode, $stream));
220                                         dbg("stream $stream: $ref->{count} lines received\n") if isdbg('msg');
221                                         $ref->{count} = 0;
222                                 }
223                                 $ref->{lastt} = $main::systime;
224                         } else {
225                                 dbg("PC29 from unknown stream $stream from $fromnode") if isdbg('msg');
226                                 $self->send(DXProt::pc42($fromnode, $tonode, $stream)); # unknown stream
227                         }
228                         last SWITCH;
229                 }
230                 
231                 if ($pcno == 30) {              # this is a incoming subject ack
232                         my $ref = get_fwq($fromnode);   # note no stream at this stage
233                         if ($ref) {
234                                 del_fwq($fromnode);
235                                 $ref->{stream} = $stream;
236                                 $ref->{count} = 0;
237                                 $ref->{linesreq} = 5;
238                                 set_fwq($fromnode, $stream, $ref);      # new ref
239                                 set_busy($fromnode, $ref); # interlock
240                                 dbg("incoming subject ack stream $stream\n") if isdbg('msg');
241                                 $ref->{lines} = [ $ref->read_msg_body ];
242                                 $ref->send_tranche($self);
243                                 $ref->{lastt} = $main::systime;
244                         } else {
245                                 dbg("PC30 from unknown stream $stream from $fromnode") if isdbg('msg');
246                                 $self->send(DXProt::pc42($fromnode, $tonode, $stream)); # unknown stream
247                         } 
248                         last SWITCH;
249                 }
250                 
251                 if ($pcno == 31) {              # acknowledge a tranche of lines
252                         my $ref = get_fwq($fromnode, $stream);
253                         if ($ref) {
254                                 dbg("tranche ack stream $stream\n") if isdbg('msg');
255                                 $ref->send_tranche($self);
256                                 $ref->{lastt} = $main::systime;
257                         } else {
258                                 dbg("PC31 from unknown stream $stream from $fromnode") if isdbg('msg');
259                                 $self->send(DXProt::pc42($fromnode, $tonode, $stream)); # unknown stream
260                         } 
261                         last SWITCH;
262                 }
263                 
264                 if ($pcno == 32) {              # incoming EOM
265                         dbg("stream $stream: EOM received\n") if isdbg('msg');
266                         my $ref = get_fwq($fromnode, $stream);
267                         if ($ref) {
268                                 $self->send(DXProt::pc33($fromnode, $tonode, $stream)); # acknowledge it
269                                 
270                                 # get the next msg no - note that this has NOTHING to do with the stream number in PC protocol
271                                 # store the file or message
272                                 # remove extraneous rubbish from the hash
273                                 # remove it from the work in progress vector
274                                 # stuff it on the msg queue
275                                 if ($ref->{lines}) {
276                                         if ($ref->{file}) {
277                                                 $ref->store($ref->{lines});
278                                         } else {
279
280                                                 # does an identical message already exist?
281                                                 my $m;
282                                                 for $m (@msg) {
283                                                         if ($ref->{subject} eq $m->{subject} && $ref->{t} == $m->{t} && $ref->{from} eq $m->{from} && $ref->{to} eq $m->{to}) {
284                                                                 $ref->stop_msg($fromnode);
285                                                                 my $msgno = $m->{msgno};
286                                                                 dbg("duplicate message from $ref->{from} -> $ref->{to} to msg: $msgno") if isdbg('msg');
287                                                                 Log('msg', "duplicate message from $ref->{from} -> $ref->{to} to msg: $msgno");
288                                                                 return;
289                                                         }
290                                                 }
291
292                                                 # swop addresses
293                                                 $ref->swop_it($self->call);
294                                                 
295                                                 # look for 'bad' to addresses 
296                                                 if ($ref->dump_it($self->call)) {
297                                                         $ref->stop_msg($fromnode);
298                                                         dbg("'Bad' message $ref->{to}") if isdbg('msg');
299                                                         Log('msg', "'Bad' message $ref->{to}");
300                                                         return;
301                                                 }
302
303                                                 # check the message for bad words 
304                                                 my @words;
305                                                 for (@{$ref->{lines}}) {
306                                                         push @words, BadWords::check($_);
307                                                 }
308                                                 push @words, BadWords::check($ref->{subject});
309                                                 if (@words) {
310                                                         dbg("message with badwords '@words' $ref->{from} -> $ref->{to} '$ref->{subject}' origin: $ref->{origin} via " . $self->call) if isdbg('msg');
311                                                         Log('msg',"message with badwords '@words' $ref->{from} -> $ref->{to} origin: $ref->{origin} via " . $self->call);
312                                                         Log('msg',"subject: $ref->{subject}");
313                                                         for (@{$ref->{lines}}) {
314                                                                 Log('msg', "line: $_");
315                                                         }
316                                                         $ref->stop_msg($fromnode);
317                                                         return;
318                                                 }
319                                                         
320                                                 $ref->{msgno} = next_transno("Msgno");
321                                                 push @{$ref->{gotit}}, $fromnode; # mark this up as being received
322                                                 $ref->store($ref->{lines});
323                                                 add_dir($ref);
324                                                 my $dxchan = DXChannel->get($ref->{to});
325                                                 $dxchan->send($dxchan->msg('m9')) if $dxchan && $dxchan->is_user;
326                                                 Log('msg', "Message $ref->{msgno} from $ref->{from} received from $fromnode for $ref->{to}");
327                                         }
328                                 }
329                                 $ref->stop_msg($fromnode);
330                         } else {
331                                 dbg("PC32 from unknown stream $stream from $fromnode") if isdbg('msg');
332                                 $self->send(DXProt::pc42($fromnode, $tonode, $stream)); # unknown stream
333                         }
334                         # queue_msg(0);
335                         last SWITCH;
336                 }
337                 
338                 if ($pcno == 33) {              # acknowledge the end of message
339                         my $ref = get_fwq($fromnode, $stream);
340                         if ($ref) {
341                                 if ($ref->{private}) { # remove it if it private and gone off site#
342                                         Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $fromnode and deleted");
343                                         $ref->{delete}++;
344                                         $ref->{deletetime} = $main::systime + $residencetime;
345                                 } else {
346                                         Log('msg', "Message $ref->{msgno} from $ref->{from} sent to $fromnode");
347                                         push @{$ref->{gotit}}, $fromnode; # mark this up as being received
348                                         $ref->store($ref->{lines});     # re- store the file
349                                 }
350                                 $ref->stop_msg($fromnode);
351                         } else {
352                                 dbg("PC33 from unknown stream $stream from $fromnode") if isdbg('msg');
353                                 $self->send(DXProt::pc42($fromnode, $tonode, $stream)); # unknown stream
354                         } 
355
356                         # send next one if present
357                         queue_msg(0);
358                         last SWITCH;
359                 }
360                 
361                 if ($pcno == 40) {              # this is a file request
362                         $f[3] =~ s/\\/\//og; # change the slashes
363                         $f[3] =~ s/\.//og;      # remove dots
364                         $f[3] =~ s/^\///o;   # remove the leading /
365                         $f[3] = lc $f[3];       # to lower case;
366                         dbg("incoming file $f[3]\n") if isdbg('msg');
367                         $f[3] = 'packclus/' . $f[3] unless $f[3] =~ /^packclus\//o;
368                         
369                         # create any directories
370                         my @part = split /\//, $f[3];
371                         my $part;
372                         my $fn = "$main::root";
373                         pop @part;                      # remove last part
374                         foreach $part (@part) {
375                                 $fn .= "/$part";
376                                 next if -e $fn;
377                                 last SWITCH if !mkdir $fn, 0777;
378                                 dbg("created directory $fn\n") if isdbg('msg');
379                         }
380                         my $stream = next_transno($fromnode);
381                         my $ref = DXMsg->alloc($stream, "$main::root/$f[3]", $self->call, time, !$f[4], $f[3], ' ', '0', '0');
382                         
383                         # forwarding variables
384                         $ref->{fromnode} = $tonode;
385                         $ref->{tonode} = $fromnode;
386                         $ref->{linesreq} = $f[5];
387                         $ref->{stream} = $stream;
388                         $ref->{count} = 0;      # no of lines between PC31s
389                         $ref->{file} = 1;
390                         $ref->{lastt} = $main::systime;
391                         set_fwq($fromnode, $stream, $ref); # store in work
392                         $self->send(DXProt::pc30($fromnode, $tonode, $stream)); # send ack 
393                         
394                         last SWITCH;
395                 }
396                 
397                 if ($pcno == 42) {              # abort transfer
398                         dbg("stream $stream: abort received\n") if isdbg('msg');
399                         my $ref = get_fwq($fromnode, $stream);
400                         if ($ref) {
401                                 $ref->stop_msg($fromnode);
402                                 $ref = undef;
403                         }
404                         last SWITCH;
405                 }
406
407                 if ($pcno == 49) {      # global delete on subject
408                         for (@msg) {
409                                 if ($_->{from} eq $f[1] && $_->{subject} eq $f[2]) {
410                                         $_->{delete}++;
411                                         $_->{deletetime} = $main::systime + $residencetime;
412                                         Log('msg', "Message $_->{msgno} from $_->{from} ($_->{subject}) fully deleted");
413                                         DXChannel::broadcast_nodes($line, $self);
414                                 }
415                         }
416                 }
417         }
418 }
419
420
421 # store a message away on disc or whatever
422 #
423 # NOTE the second arg is a REFERENCE not a list
424 sub store
425 {
426         my $ref = shift;
427         my $lines = shift;
428
429         if ($ref->{file}) {                     # a file
430                 dbg("To be stored in $ref->{to}\n") if isdbg('msg');
431                 
432                 my $fh = new IO::File "$ref->{to}", "w";
433                 if (defined $fh) {
434                         my $line;
435                         foreach $line (@{$lines}) {
436                                 print $fh "$line\n";
437                         }
438                         $fh->close;
439                         dbg("file $ref->{to} stored\n") if isdbg('msg');
440                         Log('msg', "file $ref->{to} from $ref->{from} stored" );
441                 } else {
442                         confess "can't open file $ref->{to} $!";  
443                 }
444         } else {                                        # a normal message
445
446                 # attempt to open the message file
447                 my $fn = filename($ref->{msgno});
448                 
449                 dbg("To be stored in $fn\n") if isdbg('msg');
450                 
451                 # now save the file, overwriting what's there, YES I KNOW OK! (I will change it if it's a problem)
452                 my $fh = new IO::File "$fn", "w";
453                 if (defined $fh) {
454                         my $rr = $ref->{rrreq} ? '1' : '0';
455                         my $priv = $ref->{private} ? '1': '0';
456                         print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$priv^$ref->{subject}^$ref->{origin}^$ref->{'read'}^$rr\n";
457                         print $fh "=== ", join('^', @{$ref->{gotit}}), "\n";
458                         my $line;
459                         $ref->{size} = 0;
460                         foreach $line (@{$lines}) {
461                                 $ref->{size} += (length $line) + 1;
462                                 print $fh "$line\n";
463                         }
464                         $fh->close;
465                         dbg("msg $ref->{msgno} stored\n") if isdbg('msg');
466                         Log('msg', "msg $ref->{msgno} from $ref->{from} to $ref->{to} stored" );
467                 } else {
468                         confess "can't open msg file $fn $!";  
469                 }
470         }
471 }
472
473 # delete a message
474 sub del_msg
475 {
476         my $self = shift;
477         my $dxchan = shift;
478         
479         if ($self->{tonode}) {
480                 $self->{delete}++;
481                 $self->{deletetime} = 0;
482         } else {
483                 my $call;
484                 if ($dxchan) {
485                         $call = " by " . $dxchan->call;
486                 } else {
487                         $call = '';
488                 }
489
490                 # remove it from the active message list
491                 @msg = grep { $_ != $self } @msg;
492                 
493                 # remove the file
494                 unlink filename($self->{msgno});
495         }
496 }
497
498 # clean out old messages from the message queue
499 sub clean_old
500 {
501         my $ref;
502         
503         # mark old messages for deletion
504         foreach $ref (@msg) {
505                 if (ref($ref) && !$ref->{keep} && $ref->{t} < $main::systime - $maxage) {
506                         $ref->{delete} = 1;
507                         $ref->{deletetime} = 0;
508                 }
509         }
510 }
511
512 # read in a message header
513 sub read_msg_header
514
515         my $fn = shift;
516         my $file;
517         my $line;
518         my $ref;
519         my @f;
520         my $size;
521         
522         $file = new IO::File "$fn";
523         if (!$file) {
524             dbg("Error reading $fn $!");
525             Log('err', "Error reading $fn $!");
526                 return undef;
527         }
528         $size = -s $fn;
529         $line = <$file>;                        # first line
530         if ($size == 0 || !$line) {
531             dbg("Empty $fn $!");
532             Log('err', "Empty $fn $!");
533                 return undef;
534         }
535         chomp $line;
536         $size -= length $line;
537         if (! $line =~ /^===/o) {
538                 dbg("corrupt first line in $fn ($line)");
539                 Log('err', "corrupt first line in $fn ($line)");
540                 return undef;
541         }
542         $line =~ s/^=== //o;
543         @f = split /\^/, $line;
544         $ref = DXMsg->alloc(@f);
545         
546         $line = <$file>;                        # second line
547         chomp $line;
548         $size -= length $line;
549         if (! $line =~ /^===/o) {
550             dbg("corrupt second line in $fn ($line)");
551             Log('err', "corrupt second line in $fn ($line)");
552                 return undef;
553         }
554         $line =~ s/^=== //o;
555         $ref->{gotit} = [];
556         @f = split /\^/, $line;
557         push @{$ref->{gotit}}, @f;
558         $ref->{size} = $size;
559         
560         close($file);
561         
562         return $ref;
563 }
564
565 # read in a message header
566 sub read_msg_body
567 {
568         my $self = shift;
569         my $msgno = $self->{msgno};
570         my $file;
571         my $line;
572         my $fn = filename($msgno);
573         my @out;
574         
575         $file = new IO::File;
576         if (!open($file, $fn)) {
577                 dbg("Error reading $fn $!");
578                 Log('err' ,"Error reading $fn $!");
579                 return ();
580         }
581         @out = map {chomp; $_} <$file>;
582         close($file);
583         
584         shift @out if $out[0] =~ /^=== /;
585         shift @out if $out[0] =~ /^=== /;
586         return @out;
587 }
588
589 # send a tranche of lines to the other end
590 sub send_tranche
591 {
592         my ($self, $dxchan) = @_;
593         my @out;
594         my $to = $self->{tonode};
595         my $from = $self->{fromnode};
596         my $stream = $self->{stream};
597         my $lines = $self->{lines};
598         my ($c, $i);
599         
600         for ($i = 0, $c = $self->{count}; $i < $self->{linesreq} && $c < @$lines; $i++, $c++) {
601                 push @out, DXProt::pc29($to, $from, $stream, $lines->[$c]);
602     }
603     $self->{count} = $c;
604
605     push @out, DXProt::pc32($to, $from, $stream) if $i < $self->{linesreq};
606         $dxchan->send(@out);
607 }
608
609         
610 # find a message to send out and start the ball rolling
611 sub queue_msg
612 {
613         my $sort = shift;
614         my $ref;
615         my $clref;
616         
617         # bat down the message list looking for one that needs to go off site and whose
618         # nearest node is not busy.
619
620         dbg("queue msg ($sort)\n") if isdbg('msg');
621         my @nodelist = DXChannel::get_all_nodes;
622         foreach $ref (@msg) {
623
624                 # ignore 'delayed' messages until their waiting time has expired
625                 if (exists $ref->{waitt}) {
626                         next if $ref->{waitt} > $main::systime;
627                         delete $ref->{waitt};
628                 } 
629
630                 # any time outs?
631                 if (exists $ref->{lastt} && $main::systime >= $ref->{lastt} + $timeout) {
632                         my $node = $ref->{tonode};
633                         dbg("Timeout, stopping msgno: $ref->{msgno} -> $node") if isdbg('msg');
634                         Log('msg', "Timeout, stopping msgno: $ref->{msgno} -> $node");
635                         $ref->stop_msg($node);
636                         
637                         # delay any outgoing messages that fail
638                         $ref->{waitt} = $main::systime + $waittime + rand(120) if $node ne $main::mycall;
639                         delete $ref->{lastt};
640                         next;
641                 }
642
643                 # is it being sent anywhere currently?
644                 next if $ref->{tonode};           # ignore it if it already being processed
645                 
646                 # is it awaiting deletion?
647                 if ($ref->{delete} && $main::systime > $ref->{deletetime}) {
648                         $ref->del_msg;
649                         next;
650                 }
651                 next if $ref->{delete};
652                 
653                 # firstly, is it private and unread? if so can I find the recipient
654                 # in my cluster node list offsite?
655
656                 # deal with routed private messages
657                 my $dxchan;
658                 if ($ref->{private}) {
659                         next if $ref->{'read'};           # if it is read, it is stuck here
660                         $clref = Route::get($ref->{to});
661                         if ($clref) {
662                                 $dxchan = $clref->dxchan;
663                                 if ($dxchan) {
664                                         if ($dxchan->is_node) {
665                                                 next if $clref->call eq $main::mycall;  # i.e. it lives here
666                                                 $ref->start_msg($dxchan) if !get_busy($dxchan->call)  && $dxchan->state eq 'normal';
667                                         }
668                                 } else {
669                                         dbg("Route: No dxchan for $ref->{to} " . ref($clref) ) if isdbg('msg');
670                                 }
671                         }
672                 } else {
673                         
674                         # otherwise we are dealing with a bulletin or forwarded private message
675                         # compare the gotit list with
676                         # the nodelist up above, if there are sites that haven't got it yet
677                         # then start sending it - what happens when we get loops is anyone's
678                         # guess, use (to, from, time, subject) tuple?
679                         foreach $dxchan (@nodelist) {
680                                 my $call = $dxchan->call;
681                                 next unless $call;
682                                 next if $call eq $main::mycall;
683                                 next if ref $ref->{gotit} && grep $_ eq $call, @{$ref->{gotit}};
684                                 next unless $ref->forward_it($call);           # check the forwarding file
685                                 next if $ref->{tonode};           # ignore it if it already being processed
686                                 
687                                 # if we are here we have a node that doesn't have this message
688                                 if (!get_busy($call)  && $dxchan->state eq 'normal') {
689                                         $ref->start_msg($dxchan);
690                                         last;
691                                 }
692                         }
693                 }
694
695                 # if all the available nodes are busy then stop
696                 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
697         }
698 }
699
700 # is there a message for me?
701 sub for_me
702 {
703         my $call = uc shift;
704         my $ref;
705         
706         foreach $ref (@msg) {
707                 # is it for me, private and unread? 
708                 if ($ref->{to} eq $call && $ref->{private}) {
709                         return 1 if !$ref->{'read'};
710                 }
711         }
712         return 0;
713 }
714
715 # start the message off on its travels with a PC28
716 sub start_msg
717 {
718         my ($self, $dxchan) = @_;
719         
720         confess("trying to start started msg $self->{msgno} nodes: $self->{fromnode} -> $self->{tonode}") if $self->{tonode};
721         dbg("start msg $self->{msgno}\n") if isdbg('msg');
722         $self->{linesreq} = 10;
723         $self->{count} = 0;
724         $self->{tonode} = $dxchan->call;
725         $self->{fromnode} = $main::mycall;
726         set_busy($self->{tonode}, $self);
727         set_fwq($self->{tonode}, undef, $self);
728         $self->{lastt} = $main::systime;
729         my ($fromnode, $origin);
730         $fromnode = $self->{fromnode};
731         $origin = $self->{origin};
732         $dxchan->send(DXProt::pc28($self->{tonode}, $fromnode, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $origin, $self->{rrreq}));
733 }
734
735 # get the ref of a busy node
736 sub get_busy
737 {
738         my $call = shift;
739         return $busy{$call};
740 }
741
742 sub set_busy
743 {
744         my $call = shift;
745         return $busy{$call} = shift;
746 }
747
748 sub del_busy
749 {
750         my $call = shift;
751         return delete $busy{$call};
752 }
753
754 # get the whole busy queue
755 sub get_all_busy
756 {
757         return keys %busy;
758 }
759
760 # get a forwarding queue entry
761 sub get_fwq
762 {
763         my $call = shift;
764         my $stream = shift || '0';
765         return $work{"$call,$stream"};
766 }
767
768 # delete a forwarding queue entry
769 sub del_fwq
770 {
771         my $call = shift;
772         my $stream = shift || '0';
773         return delete $work{"$call,$stream"};
774 }
775
776 # set a fwq entry
777 sub set_fwq
778 {
779         my $call = shift;
780         my $stream = shift || '0';
781         return $work{"$call,$stream"} = shift;
782 }
783
784 # get the whole forwarding queue
785 sub get_all_fwq
786 {
787         return keys %work;
788 }
789
790 # stop a message from continuing, clean it out, unlock interlocks etc
791 sub stop_msg
792 {
793         my $self = shift;
794         my $node = shift;
795         my $stream = $self->{stream};
796         
797         
798         dbg("stop msg $self->{msgno} -> node $node\n") if isdbg('msg');
799         del_fwq($node, $stream);
800         $self->workclean;
801         del_busy($node);
802 }
803
804 sub workclean
805 {
806         my $ref = shift;
807         delete $ref->{lines};
808         delete $ref->{linesreq};
809         delete $ref->{tonode};
810         delete $ref->{fromnode};
811         delete $ref->{stream};
812         delete $ref->{file};
813         delete $ref->{count};
814         delete $ref->{tempr};
815         delete $ref->{lastt};
816         delete $ref->{waitt};
817 }
818
819 # get a new transaction number from the file specified
820 sub next_transno
821 {
822         my $name = shift;
823         $name =~ s/\W//og;                      # remove non-word characters
824         my $fn = "$msgdir/$name";
825         my $msgno;
826         
827         my $fh = new IO::File;
828         if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
829                 $fh->autoflush(1);
830                 $msgno = $fh->getline || '0';
831                 chomp $msgno;
832                 $msgno++;
833                 seek $fh, 0, 0;
834                 $fh->print("$msgno\n");
835                 dbg("msgno $msgno allocated for $name\n") if isdbg('msg');
836                 $fh->close;
837         } else {
838                 confess "can't open $fn $!";
839         }
840         return $msgno;
841 }
842
843 # initialise the message 'system', read in all the message headers
844 sub init
845 {
846         my $dir = new IO::File;
847         my @dir;
848         my $ref;
849                 
850         # load various control files
851         dbg("load badmsg: " . (load_badmsg() or "Ok"));
852         dbg("load forward: " . (load_forward() or "Ok"));
853         dbg("load swop: " . (load_swop() or "Ok"));
854
855         # read in the directory
856         opendir($dir, $msgdir) or confess "can't open $msgdir $!";
857         @dir = readdir($dir);
858         closedir($dir);
859
860         @msg = ();
861         for (sort @dir) {
862                 next unless /^m\d\d\d\d\d\d$/;
863                 
864                 $ref = read_msg_header("$msgdir/$_");
865                 unless ($ref) {
866                         dbg("Deleting $_");
867                         Log('err', "Deleting $_");
868                         unlink "$msgdir/$_";
869                         next;
870                 }
871                 
872                 # delete any messages to 'badmsg.pl' places
873                 if ($ref->dump_it('')) {
874                         dbg("'Bad' TO address $ref->{to}") if isdbg('msg');
875                         Log('msg', "'Bad' TO address $ref->{to}");
876                         $ref->del_msg;
877                         next;
878                 }
879
880                 # add the message to the available queue
881                 add_dir($ref); 
882         }
883 }
884
885 # add the message to the directory listing
886 sub add_dir
887 {
888         my $ref = shift;
889         confess "tried to add a non-ref to the msg directory" if !ref $ref;
890         push @msg, $ref;
891 }
892
893 # return all the current messages
894 sub get_all
895 {
896         return @msg;
897 }
898
899 # get a particular message
900 sub get
901 {
902         my $msgno = shift;
903         for (@msg) {
904                 return $_ if $_->{msgno} == $msgno;
905                 last if $_->{msgno} > $msgno;
906         }
907         return undef;
908 }
909
910 # return the official filename for a message no
911 sub filename
912 {
913         return sprintf "$msgdir/m%06d", shift;
914 }
915
916 #
917 # return a list of valid elements 
918
919
920 sub fields
921 {
922         return keys(%valid);
923 }
924
925 #
926 # return a prompt for a field
927 #
928
929 sub field_prompt
930
931         my ($self, $ele) = @_;
932         return $valid{$ele};
933 }
934
935 #
936 # send a message state machine
937 sub do_send_stuff
938 {
939         my $self = shift;
940         my $line = shift;
941         my @out;
942         
943         if ($self->state eq 'send1') {
944                 #  $DB::single = 1;
945                 confess "local var gone missing" if !ref $self->{loc};
946                 my $loc = $self->{loc};
947                 if (my @ans = BadWords::check($line)) {
948                         $self->{badcount} += @ans;
949                         Log('msg', $self->call . " used badwords: @ans to @{$loc->{to}} in msg");
950                         return ($self->msg('e17', @ans), $self->msg('m1'));
951                 }
952                 $loc->{subject} = $line;
953                 $loc->{lines} = [];
954                 $self->state('sendbody');
955                 #push @out, $self->msg('sendbody');
956                 push @out, $self->msg('m8');
957         } elsif ($self->state eq 'sendbody') {
958                 confess "local var gone missing" if !ref $self->{loc};
959                 my $loc = $self->{loc};
960                 if ($line eq "\032" || $line eq '%1A' || uc $line eq "/EX") {
961                         my $to;
962                         
963                         foreach $to (@{$loc->{to}}) {
964                                 my $ref;
965                                 my $systime = $main::systime;
966                                 my $mycall = $main::mycall;
967                                 $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
968                                                                         uc $to,
969                                                                         exists $loc->{from} ? $loc->{from} : $self->call, 
970                                                                         $systime,
971                                                                         $loc->{private}, 
972                                                                         $loc->{subject}, 
973                                                                         exists $loc->{origin} ? $loc->{origin} : $mycall,
974                                                                         '0',
975                                                                         $loc->{rrreq});
976                                 $ref->swop_it($self->call);
977                                 $ref->store($loc->{lines});
978                                 $ref->add_dir();
979                                 push @out, $self->msg('m11', $ref->{msgno}, $to);
980                                 #push @out, "msgno $ref->{msgno} sent to $to";
981                                 my $dxchan = DXChannel->get(uc $to);
982                                 if ($dxchan) {
983                                         if ($dxchan->is_user()) {
984                                                 $dxchan->send($dxchan->msg('m9'));
985                                         }
986                                 }
987                         }
988
989                         delete $loc->{lines};
990                         delete $loc->{to};
991                         delete $self->{loc};
992                         $self->func(undef);
993                         
994                         $self->state('prompt');
995                 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
996                         #push @out, $self->msg('sendabort');
997                         push @out, $self->msg('m10');
998                         delete $loc->{lines};
999                         delete $loc->{to};
1000                         delete $self->{loc};
1001                         $self->func(undef);
1002                         $self->state('prompt');
1003                 } else {
1004                         if (my @ans = BadWords::check($line)) {
1005                                 $self->{badcount} += @ans;
1006                                 Log('msg', $self->call . " used badwords: @ans to @{$loc->{to}} subject: '$loc->{subject}' in msg");
1007                                 Log('msg', "line: $line");
1008                                 return ($self->msg('e17', @ans));
1009                         }
1010                         
1011                         # i.e. it ain't and end or abort, therefore store the line
1012                         push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
1013                 }
1014         }
1015         return @out;
1016 }
1017
1018 # return the standard directory line for this ref 
1019 sub dir
1020 {
1021         my $ref = shift;
1022         my $flag = $ref->read ? '-' : ' ';
1023         $flag = 'D' if $ref->delete;
1024         return sprintf "%6d%s%s%5d %8.8s %8.8s %-6.6s %5.5s %-30.30s", 
1025                 $ref->msgno, $flag, $ref->private ? 'p' : ' ', $ref->size,
1026                         $ref->to, $ref->from, cldate($ref->t), ztime($ref->t), $ref->subject;
1027 }
1028
1029 # load the forward table
1030 sub load_forward
1031 {
1032         my @out;
1033         my $s = readfilestr($forwardfn);
1034         if ($s) {
1035                 eval $s;
1036                 push @out, $@ if $@;
1037         }
1038         return @out;
1039 }
1040
1041 # load the bad message table
1042 sub load_badmsg
1043 {
1044         my @out;
1045         my $s = readfilestr($badmsgfn);
1046         if ($s) {
1047                 eval $s;
1048                 push @out, $@ if $@;
1049         }
1050         return @out;
1051 }
1052
1053 # load the swop message table
1054 sub load_swop
1055 {
1056         my @out;
1057         my $s = readfilestr($swopfn);
1058         if ($s) {
1059                 eval $s;
1060                 push @out, $@ if $@;
1061         }
1062         return @out;
1063 }
1064
1065 #
1066 # forward that message or not according to the forwarding table
1067 # returns 1 for forward, 0 - to ignore
1068 #
1069
1070 sub forward_it
1071 {
1072         my $ref = shift;
1073         my $call = shift;
1074         my $i;
1075         
1076         for ($i = 0; $i < @forward; $i += 5) {
1077                 my ($sort, $field, $pattern, $action, $bbs) = @forward[$i..($i+4)]; 
1078                 my $tested;
1079                 
1080                 # are we interested?
1081                 next if $ref->{private} && $sort ne 'P';
1082                 next if !$ref->{private} && $sort ne 'B';
1083                 
1084                 # select field
1085                 $tested = $ref->{to} if $field eq 'T';
1086                 $tested = $ref->{from} if $field eq 'F';
1087                 $tested = $ref->{origin} if $field eq 'O';
1088                 $tested = $ref->{subject} if $field eq 'S';
1089
1090                 if (!$pattern || $tested =~ m{$pattern}i) {
1091                         return 0 if $action eq 'I';
1092                         return 1 if !$bbs || grep $_ eq $call, @{$bbs};
1093                 }
1094         }
1095         return 0;
1096 }
1097
1098 sub dump_it
1099 {
1100         my $ref = shift;
1101         my $call = shift;
1102         my $i;
1103         
1104         for ($i = 0; $i < @badmsg; $i += 3) {
1105                 my ($sort, $field, $pattern) = @badmsg[$i..($i+2)]; 
1106                 my $tested;
1107                 
1108                 # are we interested?
1109                 next if $ref->{private} && $sort ne 'P';
1110                 next if !$ref->{private} && $sort ne 'B';
1111                 
1112                 # select field
1113                 $tested = $ref->{to} if $field eq 'T';
1114                 $tested = $ref->{from} if $field eq 'F';
1115                 $tested = $ref->{origin} if $field eq 'O';
1116                 $tested = $ref->{subject} if $field eq 'S';
1117                 $tested = $call if $field eq 'I';
1118
1119                 if (!$pattern || $tested =~ m{$pattern}i) {
1120                         return 1;
1121                 }
1122         }
1123         return 0;
1124 }
1125
1126 sub swop_it
1127 {
1128         my $ref = shift;
1129         my $call = shift;
1130         my $i;
1131         my $count = 0;
1132         
1133         for ($i = 0; $i < @swop; $i += 5) {
1134                 my ($sort, $field, $pattern, $tfield, $topattern) = @swop[$i..($i+4)]; 
1135                 my $tested;
1136                 my $swop;
1137                 my $old;
1138                 
1139                 # are we interested?
1140                 next if $ref->{private} && $sort ne 'P';
1141                 next if !$ref->{private} && $sort ne 'B';
1142                 
1143                 # select field
1144                 $tested = $ref->{to} if $field eq 'T';
1145                 $tested = $ref->{from} if $field eq 'F';
1146                 $tested = $ref->{origin} if $field eq 'O';
1147                 $tested = $ref->{subject} if $field eq 'S';
1148
1149                 # select swop field
1150                 $old = $swop = $ref->{to} if $tfield eq 'T';
1151                 $old = $swop = $ref->{from} if $tfield eq 'F';
1152                 $old = $swop = $ref->{origin} if $tfield eq 'O';
1153                 $old = $swop = $ref->{subject} if $tfield eq 'S';
1154
1155                 if ($tested =~ m{$pattern}i) {
1156                         if ($tested eq $swop) {
1157                                 $swop =~ s{$pattern}{$topattern}i;
1158                         } else {
1159                                 $swop = $topattern;
1160                         }
1161                         Log('msg', "Msg $ref->{msgno}: $tfield $old -> $swop");
1162                         Log('dbg', "Msg $ref->{msgno}: $tfield $old -> $swop");
1163                         $ref->{to} = $swop if $tfield eq 'T';
1164                         $ref->{from} = $swop if $tfield eq 'F';
1165                         $ref->{origin} = $swop if $tfield eq 'O';
1166                         $ref->{subject} = $swop if $tfield eq 'S';
1167                         ++$count;
1168                 }
1169         }
1170         return $count;
1171 }
1172
1173 # import any msgs in the import directory
1174 # the messages are in BBS format (but may have cluster extentions
1175 # so SB UK < GB7TLH is legal
1176 sub import_msgs
1177 {
1178         # are there any to do in this directory?
1179         return unless -d $importfn;
1180         unless (opendir(DIR, $importfn)) {
1181                 dbg("can\'t open $importfn $!") if isdbg('msg');
1182                 Log('msg', "can\'t open $importfn $!");
1183                 return;
1184         } 
1185
1186         my @names = readdir(DIR);
1187         closedir(DIR);
1188         my $name;
1189         foreach $name (@names) {
1190                 next if $name =~ /^\./;
1191                 my $splitit = $name =~ /^split/;
1192                 my $fn = "$importfn/$name";
1193                 next unless -f $fn;
1194                 unless (open(MSG, $fn)) {
1195                         dbg("can\'t open import file $fn $!") if isdbg('msg');
1196                         Log('msg', "can\'t open import file $fn $!");
1197                         unlink($fn);
1198                         next;
1199                 }
1200                 my @msg = map { chomp; $_ } <MSG>;
1201                 close(MSG);
1202                 unlink($fn);
1203                 my @out = import_one($main::me, \@msg, $splitit);
1204                 Log('msg', @out);
1205         }
1206         queue_msg(0);
1207 }
1208
1209 # import one message as a list in bbs (as extended) mode
1210 # takes a reference to an array containing the whole message
1211 sub import_one
1212 {
1213         my $dxchan = shift;
1214         my $ref = shift;
1215         my $splitit = shift;
1216         my $private = '1';
1217         my $rr = '0';
1218         my $notincalls = 1;
1219         my $from = $dxchan->call;
1220         my $origin = $main::mycall;
1221         my @to;
1222         my @out;
1223                                 
1224         # first line;
1225         my $line = shift @$ref;
1226         my @f = split /\b/, $line;
1227         @f = map {s/\s+//g; length $_ ? $_ : ()} @f;
1228
1229         unless (@f && $f[0] =~ /^(:?S|SP|SB|SEND)$/ ) {
1230                 my $m = "invalid first line in import '$line'";
1231                 dbg($m) if isdbg('msg');
1232                 return (1, $m);
1233         }
1234         while (@f) {
1235                 my $f = uc shift @f;
1236                 next if $f eq 'SEND';
1237
1238                 # private / noprivate / rr
1239                 if ($notincalls && ($f eq 'B' || $f eq 'SB' || $f =~ /^NOP/oi)) {
1240                         $private = '0';
1241                 } elsif ($notincalls && ($f eq 'P' || $f eq 'SP' || $f =~ /^PRI/oi)) {
1242                         ;
1243                 } elsif ($notincalls && ($f eq 'RR')) {
1244                         $rr = '1';
1245                 } elsif (($f =~ /^[\@\.\#\$]$/ || $f eq '.#') && @f) {       # this is bbs syntax, for AT
1246                         shift @f;
1247                 } elsif ($f eq '<' && @f) {     # this is bbs syntax  for from call
1248                         $from = uc shift @f;
1249                 } elsif ($f =~ /^\$/) {     # this is bbs syntax  for a bid
1250                         next;
1251                 } elsif ($f =~ /^<(\S+)/) {     # this is bbs syntax  for from call
1252                         $from = $1;
1253                 } elsif ($f =~ /^\$\S+/) {     # this is bbs syntax for bid
1254                         ;
1255                 } else {
1256
1257                         # callsign ?
1258                         $notincalls = 0;
1259
1260                         # is this callsign a distro?
1261                         my $fn = "$msgdir/distro/$f.pl";
1262                         if (-e $fn) {
1263                                 my $fh = new IO::File $fn;
1264                                 if ($fh) {
1265                                         local $/ = undef;
1266                                         my $s = <$fh>;
1267                                         $fh->close;
1268                                         my @call;
1269                                         @call = eval $s;
1270                                         return (1, "Error in Distro $f.pl:", $@) if $@;
1271                                         if (@call > 0) {
1272                                                 push @f, @call;
1273                                                 next;
1274                                         }
1275                                 }
1276                         }
1277                         
1278                         if (grep $_ eq $f, @DXMsg::badmsg) {
1279                                 push @out, $dxchan->msg('m3', $f);
1280                         } else {
1281                                 push @to, $f;
1282                         }
1283                 }
1284         }
1285         
1286         # subject is the next line
1287         my $subject = shift @$ref;
1288         
1289         # strip off trailing lines 
1290         pop @$ref while (@$ref && $$ref[-1] =~ /^\s*$/);
1291         
1292         # strip off /EX or /ABORT
1293         return ("aborted") if @$ref && $$ref[-1] =~ m{^/ABORT$}i; 
1294         pop @$ref if (@$ref && $$ref[-1] =~ m{^/EX$}i);                                                                  
1295
1296         # sort out any splitting that needs to be done
1297         my @chunk;
1298         if ($splitit) {
1299                 my $lth = 0;
1300                 my $lines = [];
1301                 for (@$ref) {
1302                         if ($lth >= $maxchunk || ($lth > $minchunk && /^\s*$/)) {
1303                                 push @chunk, $lines;
1304                                 $lines = [];
1305                                 $lth = 0;
1306                         } 
1307                         push @$lines, $_;
1308                         $lth += length; 
1309                 }
1310                 push @chunk, $lines if @$lines;
1311         } else {
1312                 push @chunk, $ref;
1313         }
1314                                   
1315     # write all the messages away
1316         my $i;
1317         for ( $i = 0;  $i < @chunk; $i++) {
1318                 my $chunk = $chunk[$i];
1319                 my $ch_subject;
1320                 if (@chunk > 1) {
1321                         my $num = " [" . ($i+1) . "/" . scalar @chunk . "]";
1322                         $ch_subject = substr($subject, 0, 27 - length $num) .  $num;
1323                 } else {
1324                         $ch_subject = $subject;
1325                 }
1326                 my $to;
1327                 foreach $to (@to) {
1328                         my $systime = $main::systime;
1329                         my $mycall = $main::mycall;
1330                         my $mref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
1331                                                                         $to,
1332                                                                         $from, 
1333                                                                         $systime,
1334                                                                         $private, 
1335                                                                         $ch_subject, 
1336                                                                         $origin,
1337                                                                         '0',
1338                                                                         $rr);
1339                         $mref->swop_it($main::mycall);
1340                         $mref->store($chunk);
1341                         $mref->add_dir();
1342                         push @out, $dxchan->msg('m11', $mref->{msgno}, $to);
1343                         #push @out, "msgno $ref->{msgno} sent to $to";
1344                         my $todxchan = DXChannel->get(uc $to);
1345                         if ($todxchan) {
1346                                 if ($todxchan->is_user()) {
1347                                         $todxchan->send($todxchan->msg('m9'));
1348                                 }
1349                         }
1350                 }
1351         }
1352         return @out;
1353 }
1354
1355 no strict;
1356 sub AUTOLOAD
1357 {
1358         my $self = shift;
1359         my $name = $AUTOLOAD;
1360         return if $name =~ /::DESTROY$/;
1361         $name =~ s/.*:://o;
1362         
1363         confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
1364         # this clever line of code creates a subroutine which takes over from autoload
1365         # from OO Perl - Conway
1366         *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
1367         @_ ? $self->{$name} = shift : $self->{$name} ;
1368 }
1369
1370 1;
1371
1372 __END__