75a0c452c0e9034d830afc042820dc02be28b801
[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                 
652                 # firstly, is it private and unread? if so can I find the recipient
653                 # in my cluster node list offsite?
654
655                 # deal with routed private messages
656                 my $dxchan;
657                 if ($ref->{private}) {
658                         next if $ref->{'read'};           # if it is read, it is stuck here
659                         $clref = Route::get($ref->{to});
660                         if ($clref) {
661                                 $dxchan = $clref->dxchan;
662                                 if ($dxchan) {
663                                         if ($dxchan->is_node) {
664                                                 next if $clref->call eq $main::mycall;  # i.e. it lives here
665                                                 $ref->start_msg($dxchan) if !get_busy($dxchan->call)  && $dxchan->state eq 'normal';
666                                         }
667                                 } else {
668                                         dbg("Route: No dxchan for $ref->{to} " . ref($clref) ) if isdbg('msg');
669                                 }
670                         }
671                 } else {
672                         
673                         # otherwise we are dealing with a bulletin or forwarded private message
674                         # compare the gotit list with
675                         # the nodelist up above, if there are sites that haven't got it yet
676                         # then start sending it - what happens when we get loops is anyone's
677                         # guess, use (to, from, time, subject) tuple?
678                         foreach $dxchan (@nodelist) {
679                                 my $call = $dxchan->call;
680                                 next unless $call;
681                                 next if $call eq $main::mycall;
682                                 next if ref $ref->{gotit} && grep $_ eq $call, @{$ref->{gotit}};
683                                 next unless $ref->forward_it($call);           # check the forwarding file
684                                 next if $ref->{tonode};           # ignore it if it already being processed
685                                 
686                                 # if we are here we have a node that doesn't have this message
687                                 if (!get_busy($call)  && $dxchan->state eq 'normal') {
688                                         $ref->start_msg($dxchan);
689                                         last;
690                                 }
691                         }
692                 }
693
694                 # if all the available nodes are busy then stop
695                 last if @nodelist == scalar grep { get_busy($_->call) } @nodelist;
696         }
697 }
698
699 # is there a message for me?
700 sub for_me
701 {
702         my $call = uc shift;
703         my $ref;
704         
705         foreach $ref (@msg) {
706                 # is it for me, private and unread? 
707                 if ($ref->{to} eq $call && $ref->{private}) {
708                         return 1 if !$ref->{'read'};
709                 }
710         }
711         return 0;
712 }
713
714 # start the message off on its travels with a PC28
715 sub start_msg
716 {
717         my ($self, $dxchan) = @_;
718         
719         confess("trying to start started msg $self->{msgno} nodes: $self->{fromnode} -> $self->{tonode}") if $self->{tonode};
720         dbg("start msg $self->{msgno}\n") if isdbg('msg');
721         $self->{linesreq} = 10;
722         $self->{count} = 0;
723         $self->{tonode} = $dxchan->call;
724         $self->{fromnode} = $main::mycall;
725         set_busy($self->{tonode}, $self);
726         set_fwq($self->{tonode}, undef, $self);
727         $self->{lastt} = $main::systime;
728         my ($fromnode, $origin);
729         $fromnode = $self->{fromnode};
730         $origin = $self->{origin};
731         $dxchan->send(DXProt::pc28($self->{tonode}, $fromnode, $self->{to}, $self->{from}, $self->{t}, $self->{private}, $self->{subject}, $origin, $self->{rrreq}));
732 }
733
734 # get the ref of a busy node
735 sub get_busy
736 {
737         my $call = shift;
738         return $busy{$call};
739 }
740
741 sub set_busy
742 {
743         my $call = shift;
744         return $busy{$call} = shift;
745 }
746
747 sub del_busy
748 {
749         my $call = shift;
750         return delete $busy{$call};
751 }
752
753 # get the whole busy queue
754 sub get_all_busy
755 {
756         return keys %busy;
757 }
758
759 # get a forwarding queue entry
760 sub get_fwq
761 {
762         my $call = shift;
763         my $stream = shift || '0';
764         return $work{"$call,$stream"};
765 }
766
767 # delete a forwarding queue entry
768 sub del_fwq
769 {
770         my $call = shift;
771         my $stream = shift || '0';
772         return delete $work{"$call,$stream"};
773 }
774
775 # set a fwq entry
776 sub set_fwq
777 {
778         my $call = shift;
779         my $stream = shift || '0';
780         return $work{"$call,$stream"} = shift;
781 }
782
783 # get the whole forwarding queue
784 sub get_all_fwq
785 {
786         return keys %work;
787 }
788
789 # stop a message from continuing, clean it out, unlock interlocks etc
790 sub stop_msg
791 {
792         my $self = shift;
793         my $node = shift;
794         my $stream = $self->{stream};
795         
796         
797         dbg("stop msg $self->{msgno} -> node $node\n") if isdbg('msg');
798         del_fwq($node, $stream);
799         $self->workclean;
800         del_busy($node);
801 }
802
803 sub workclean
804 {
805         my $ref = shift;
806         delete $ref->{lines};
807         delete $ref->{linesreq};
808         delete $ref->{tonode};
809         delete $ref->{fromnode};
810         delete $ref->{stream};
811         delete $ref->{file};
812         delete $ref->{count};
813         delete $ref->{tempr};
814         delete $ref->{lastt};
815         delete $ref->{waitt};
816 }
817
818 # get a new transaction number from the file specified
819 sub next_transno
820 {
821         my $name = shift;
822         $name =~ s/\W//og;                      # remove non-word characters
823         my $fn = "$msgdir/$name";
824         my $msgno;
825         
826         my $fh = new IO::File;
827         if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
828                 $fh->autoflush(1);
829                 $msgno = $fh->getline || '0';
830                 chomp $msgno;
831                 $msgno++;
832                 seek $fh, 0, 0;
833                 $fh->print("$msgno\n");
834                 dbg("msgno $msgno allocated for $name\n") if isdbg('msg');
835                 $fh->close;
836         } else {
837                 confess "can't open $fn $!";
838         }
839         return $msgno;
840 }
841
842 # initialise the message 'system', read in all the message headers
843 sub init
844 {
845         my $dir = new IO::File;
846         my @dir;
847         my $ref;
848                 
849         # load various control files
850         dbg("load badmsg: " . (load_badmsg() or "Ok"));
851         dbg("load forward: " . (load_forward() or "Ok"));
852         dbg("load swop: " . (load_swop() or "Ok"));
853
854         # read in the directory
855         opendir($dir, $msgdir) or confess "can't open $msgdir $!";
856         @dir = readdir($dir);
857         closedir($dir);
858
859         @msg = ();
860         for (sort @dir) {
861                 next unless /^m\d\d\d\d\d\d$/;
862                 
863                 $ref = read_msg_header("$msgdir/$_");
864                 unless ($ref) {
865                         dbg("Deleting $_");
866                         Log('err', "Deleting $_");
867                         unlink "$msgdir/$_";
868                         next;
869                 }
870                 
871                 # delete any messages to 'badmsg.pl' places
872                 if ($ref->dump_it('')) {
873                         dbg("'Bad' TO address $ref->{to}") if isdbg('msg');
874                         Log('msg', "'Bad' TO address $ref->{to}");
875                         $ref->del_msg;
876                         next;
877                 }
878
879                 # add the message to the available queue
880                 add_dir($ref); 
881         }
882 }
883
884 # add the message to the directory listing
885 sub add_dir
886 {
887         my $ref = shift;
888         confess "tried to add a non-ref to the msg directory" if !ref $ref;
889         push @msg, $ref;
890 }
891
892 # return all the current messages
893 sub get_all
894 {
895         return @msg;
896 }
897
898 # get a particular message
899 sub get
900 {
901         my $msgno = shift;
902         for (@msg) {
903                 return $_ if $_->{msgno} == $msgno;
904                 last if $_->{msgno} > $msgno;
905         }
906         return undef;
907 }
908
909 # return the official filename for a message no
910 sub filename
911 {
912         return sprintf "$msgdir/m%06d", shift;
913 }
914
915 #
916 # return a list of valid elements 
917
918
919 sub fields
920 {
921         return keys(%valid);
922 }
923
924 #
925 # return a prompt for a field
926 #
927
928 sub field_prompt
929
930         my ($self, $ele) = @_;
931         return $valid{$ele};
932 }
933
934 #
935 # send a message state machine
936 sub do_send_stuff
937 {
938         my $self = shift;
939         my $line = shift;
940         my @out;
941         
942         if ($self->state eq 'send1') {
943                 #  $DB::single = 1;
944                 confess "local var gone missing" if !ref $self->{loc};
945                 my $loc = $self->{loc};
946                 if (my @ans = BadWords::check($line)) {
947                         $self->{badcount} += @ans;
948                         Log('msg', $self->call . " used badwords: @ans to @{$loc->{to}} in msg");
949                         return ($self->msg('e17', @ans), $self->msg('m1'));
950                 }
951                 $loc->{subject} = $line;
952                 $loc->{lines} = [];
953                 $self->state('sendbody');
954                 #push @out, $self->msg('sendbody');
955                 push @out, $self->msg('m8');
956         } elsif ($self->state eq 'sendbody') {
957                 confess "local var gone missing" if !ref $self->{loc};
958                 my $loc = $self->{loc};
959                 if ($line eq "\032" || $line eq '%1A' || uc $line eq "/EX") {
960                         my $to;
961                         
962                         foreach $to (@{$loc->{to}}) {
963                                 my $ref;
964                                 my $systime = $main::systime;
965                                 my $mycall = $main::mycall;
966                                 $ref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
967                                                                         uc $to,
968                                                                         exists $loc->{from} ? $loc->{from} : $self->call, 
969                                                                         $systime,
970                                                                         $loc->{private}, 
971                                                                         $loc->{subject}, 
972                                                                         exists $loc->{origin} ? $loc->{origin} : $mycall,
973                                                                         '0',
974                                                                         $loc->{rrreq});
975                                 $ref->swop_it($self->call);
976                                 $ref->store($loc->{lines});
977                                 $ref->add_dir();
978                                 push @out, $self->msg('m11', $ref->{msgno}, $to);
979                                 #push @out, "msgno $ref->{msgno} sent to $to";
980                                 my $dxchan = DXChannel->get(uc $to);
981                                 if ($dxchan) {
982                                         if ($dxchan->is_user()) {
983                                                 $dxchan->send($dxchan->msg('m9'));
984                                         }
985                                 }
986                         }
987
988                         delete $loc->{lines};
989                         delete $loc->{to};
990                         delete $self->{loc};
991                         $self->func(undef);
992                         
993                         $self->state('prompt');
994                 } elsif ($line eq "\031" || uc $line eq "/ABORT" || uc $line eq "/QUIT") {
995                         #push @out, $self->msg('sendabort');
996                         push @out, $self->msg('m10');
997                         delete $loc->{lines};
998                         delete $loc->{to};
999                         delete $self->{loc};
1000                         $self->func(undef);
1001                         $self->state('prompt');
1002                 } else {
1003                         if (my @ans = BadWords::check($line)) {
1004                                 $self->{badcount} += @ans;
1005                                 Log('msg', $self->call . " used badwords: @ans to @{$loc->{to}} subject: '$loc->{subject}' in msg");
1006                                 Log('msg', "line: $line");
1007                                 return ($self->msg('e17', @ans));
1008                         }
1009                         
1010                         # i.e. it ain't and end or abort, therefore store the line
1011                         push @{$loc->{lines}}, length($line) > 0 ? $line : " ";
1012                 }
1013         }
1014         return @out;
1015 }
1016
1017 # return the standard directory line for this ref 
1018 sub dir
1019 {
1020         my $ref = shift;
1021         my $flag = $ref->read ? '-' : ' ';
1022         $flag = 'D' if $ref->delete;
1023         return sprintf "%6d%s%s%5d %8.8s %8.8s %-6.6s %5.5s %-30.30s", 
1024                 $ref->msgno, $flag, $ref->private ? 'p' : ' ', $ref->size,
1025                         $ref->to, $ref->from, cldate($ref->t), ztime($ref->t), $ref->subject;
1026 }
1027
1028 # load the forward table
1029 sub load_forward
1030 {
1031         my @out;
1032         my $s = readfilestr($forwardfn);
1033         if ($s) {
1034                 eval $s;
1035                 push @out, $@ if $@;
1036         }
1037         return @out;
1038 }
1039
1040 # load the bad message table
1041 sub load_badmsg
1042 {
1043         my @out;
1044         my $s = readfilestr($badmsgfn);
1045         if ($s) {
1046                 eval $s;
1047                 push @out, $@ if $@;
1048         }
1049         return @out;
1050 }
1051
1052 # load the swop message table
1053 sub load_swop
1054 {
1055         my @out;
1056         my $s = readfilestr($swopfn);
1057         if ($s) {
1058                 eval $s;
1059                 push @out, $@ if $@;
1060         }
1061         return @out;
1062 }
1063
1064 #
1065 # forward that message or not according to the forwarding table
1066 # returns 1 for forward, 0 - to ignore
1067 #
1068
1069 sub forward_it
1070 {
1071         my $ref = shift;
1072         my $call = shift;
1073         my $i;
1074         
1075         for ($i = 0; $i < @forward; $i += 5) {
1076                 my ($sort, $field, $pattern, $action, $bbs) = @forward[$i..($i+4)]; 
1077                 my $tested;
1078                 
1079                 # are we interested?
1080                 next if $ref->{private} && $sort ne 'P';
1081                 next if !$ref->{private} && $sort ne 'B';
1082                 
1083                 # select field
1084                 $tested = $ref->{to} if $field eq 'T';
1085                 $tested = $ref->{from} if $field eq 'F';
1086                 $tested = $ref->{origin} if $field eq 'O';
1087                 $tested = $ref->{subject} if $field eq 'S';
1088
1089                 if (!$pattern || $tested =~ m{$pattern}i) {
1090                         return 0 if $action eq 'I';
1091                         return 1 if !$bbs || grep $_ eq $call, @{$bbs};
1092                 }
1093         }
1094         return 0;
1095 }
1096
1097 sub dump_it
1098 {
1099         my $ref = shift;
1100         my $call = shift;
1101         my $i;
1102         
1103         for ($i = 0; $i < @badmsg; $i += 3) {
1104                 my ($sort, $field, $pattern) = @badmsg[$i..($i+2)]; 
1105                 my $tested;
1106                 
1107                 # are we interested?
1108                 next if $ref->{private} && $sort ne 'P';
1109                 next if !$ref->{private} && $sort ne 'B';
1110                 
1111                 # select field
1112                 $tested = $ref->{to} if $field eq 'T';
1113                 $tested = $ref->{from} if $field eq 'F';
1114                 $tested = $ref->{origin} if $field eq 'O';
1115                 $tested = $ref->{subject} if $field eq 'S';
1116                 $tested = $call if $field eq 'I';
1117
1118                 if (!$pattern || $tested =~ m{$pattern}i) {
1119                         return 1;
1120                 }
1121         }
1122         return 0;
1123 }
1124
1125 sub swop_it
1126 {
1127         my $ref = shift;
1128         my $call = shift;
1129         my $i;
1130         my $count = 0;
1131         
1132         for ($i = 0; $i < @swop; $i += 5) {
1133                 my ($sort, $field, $pattern, $tfield, $topattern) = @swop[$i..($i+4)]; 
1134                 my $tested;
1135                 my $swop;
1136                 my $old;
1137                 
1138                 # are we interested?
1139                 next if $ref->{private} && $sort ne 'P';
1140                 next if !$ref->{private} && $sort ne 'B';
1141                 
1142                 # select field
1143                 $tested = $ref->{to} if $field eq 'T';
1144                 $tested = $ref->{from} if $field eq 'F';
1145                 $tested = $ref->{origin} if $field eq 'O';
1146                 $tested = $ref->{subject} if $field eq 'S';
1147
1148                 # select swop field
1149                 $old = $swop = $ref->{to} if $tfield eq 'T';
1150                 $old = $swop = $ref->{from} if $tfield eq 'F';
1151                 $old = $swop = $ref->{origin} if $tfield eq 'O';
1152                 $old = $swop = $ref->{subject} if $tfield eq 'S';
1153
1154                 if ($tested =~ m{$pattern}i) {
1155                         if ($tested eq $swop) {
1156                                 $swop =~ s{$pattern}{$topattern}i;
1157                         } else {
1158                                 $swop = $topattern;
1159                         }
1160                         Log('msg', "Msg $ref->{msgno}: $tfield $old -> $swop");
1161                         Log('dbg', "Msg $ref->{msgno}: $tfield $old -> $swop");
1162                         $ref->{to} = $swop if $tfield eq 'T';
1163                         $ref->{from} = $swop if $tfield eq 'F';
1164                         $ref->{origin} = $swop if $tfield eq 'O';
1165                         $ref->{subject} = $swop if $tfield eq 'S';
1166                         ++$count;
1167                 }
1168         }
1169         return $count;
1170 }
1171
1172 # import any msgs in the import directory
1173 # the messages are in BBS format (but may have cluster extentions
1174 # so SB UK < GB7TLH is legal
1175 sub import_msgs
1176 {
1177         # are there any to do in this directory?
1178         return unless -d $importfn;
1179         unless (opendir(DIR, $importfn)) {
1180                 dbg("can\'t open $importfn $!") if isdbg('msg');
1181                 Log('msg', "can\'t open $importfn $!");
1182                 return;
1183         } 
1184
1185         my @names = readdir(DIR);
1186         closedir(DIR);
1187         my $name;
1188         foreach $name (@names) {
1189                 next if $name =~ /^\./;
1190                 my $splitit = $name =~ /^split/;
1191                 my $fn = "$importfn/$name";
1192                 next unless -f $fn;
1193                 unless (open(MSG, $fn)) {
1194                         dbg("can\'t open import file $fn $!") if isdbg('msg');
1195                         Log('msg', "can\'t open import file $fn $!");
1196                         unlink($fn);
1197                         next;
1198                 }
1199                 my @msg = map { chomp; $_ } <MSG>;
1200                 close(MSG);
1201                 unlink($fn);
1202                 my @out = import_one($main::me, \@msg, $splitit);
1203                 Log('msg', @out);
1204         }
1205         queue_msg(0);
1206 }
1207
1208 # import one message as a list in bbs (as extended) mode
1209 # takes a reference to an array containing the whole message
1210 sub import_one
1211 {
1212         my $dxchan = shift;
1213         my $ref = shift;
1214         my $splitit = shift;
1215         my $private = '1';
1216         my $rr = '0';
1217         my $notincalls = 1;
1218         my $from = $dxchan->call;
1219         my $origin = $main::mycall;
1220         my @to;
1221         my @out;
1222                                 
1223         # first line;
1224         my $line = shift @$ref;
1225         my @f = split /\b/, $line;
1226         @f = map {s/\s+//g; length $_ ? $_ : ()} @f;
1227
1228         unless (@f && $f[0] =~ /^(:?S|SP|SB|SEND)$/ ) {
1229                 my $m = "invalid first line in import '$line'";
1230                 dbg($m) if isdbg('msg');
1231                 return (1, $m);
1232         }
1233         while (@f) {
1234                 my $f = uc shift @f;
1235                 next if $f eq 'SEND';
1236
1237                 # private / noprivate / rr
1238                 if ($notincalls && ($f eq 'B' || $f eq 'SB' || $f =~ /^NOP/oi)) {
1239                         $private = '0';
1240                 } elsif ($notincalls && ($f eq 'P' || $f eq 'SP' || $f =~ /^PRI/oi)) {
1241                         ;
1242                 } elsif ($notincalls && ($f eq 'RR')) {
1243                         $rr = '1';
1244                 } elsif (($f =~ /^[\@\.\#\$]$/ || $f eq '.#') && @f) {       # this is bbs syntax, for AT
1245                         shift @f;
1246                 } elsif ($f eq '<' && @f) {     # this is bbs syntax  for from call
1247                         $from = uc shift @f;
1248                 } elsif ($f =~ /^\$/) {     # this is bbs syntax  for a bid
1249                         next;
1250                 } elsif ($f =~ /^<(\S+)/) {     # this is bbs syntax  for from call
1251                         $from = $1;
1252                 } elsif ($f =~ /^\$\S+/) {     # this is bbs syntax for bid
1253                         ;
1254                 } else {
1255
1256                         # callsign ?
1257                         $notincalls = 0;
1258
1259                         # is this callsign a distro?
1260                         my $fn = "$msgdir/distro/$f.pl";
1261                         if (-e $fn) {
1262                                 my $fh = new IO::File $fn;
1263                                 if ($fh) {
1264                                         local $/ = undef;
1265                                         my $s = <$fh>;
1266                                         $fh->close;
1267                                         my @call;
1268                                         @call = eval $s;
1269                                         return (1, "Error in Distro $f.pl:", $@) if $@;
1270                                         if (@call > 0) {
1271                                                 push @f, @call;
1272                                                 next;
1273                                         }
1274                                 }
1275                         }
1276                         
1277                         if (grep $_ eq $f, @DXMsg::badmsg) {
1278                                 push @out, $dxchan->msg('m3', $f);
1279                         } else {
1280                                 push @to, $f;
1281                         }
1282                 }
1283         }
1284         
1285         # subject is the next line
1286         my $subject = shift @$ref;
1287         
1288         # strip off trailing lines 
1289         pop @$ref while (@$ref && $$ref[-1] =~ /^\s*$/);
1290         
1291         # strip off /EX or /ABORT
1292         return ("aborted") if @$ref && $$ref[-1] =~ m{^/ABORT$}i; 
1293         pop @$ref if (@$ref && $$ref[-1] =~ m{^/EX$}i);                                                                  
1294
1295         # sort out any splitting that needs to be done
1296         my @chunk;
1297         if ($splitit) {
1298                 my $lth = 0;
1299                 my $lines = [];
1300                 for (@$ref) {
1301                         if ($lth >= $maxchunk || ($lth > $minchunk && /^\s*$/)) {
1302                                 push @chunk, $lines;
1303                                 $lines = [];
1304                                 $lth = 0;
1305                         } 
1306                         push @$lines, $_;
1307                         $lth += length; 
1308                 }
1309                 push @chunk, $lines if @$lines;
1310         } else {
1311                 push @chunk, $ref;
1312         }
1313                                   
1314     # write all the messages away
1315         my $i;
1316         for ( $i = 0;  $i < @chunk; $i++) {
1317                 my $chunk = $chunk[$i];
1318                 my $ch_subject;
1319                 if (@chunk > 1) {
1320                         my $num = " [" . ($i+1) . "/" . scalar @chunk . "]";
1321                         $ch_subject = substr($subject, 0, 27 - length $num) .  $num;
1322                 } else {
1323                         $ch_subject = $subject;
1324                 }
1325                 my $to;
1326                 foreach $to (@to) {
1327                         my $systime = $main::systime;
1328                         my $mycall = $main::mycall;
1329                         my $mref = DXMsg->alloc(DXMsg::next_transno('Msgno'),
1330                                                                         $to,
1331                                                                         $from, 
1332                                                                         $systime,
1333                                                                         $private, 
1334                                                                         $ch_subject, 
1335                                                                         $origin,
1336                                                                         '0',
1337                                                                         $rr);
1338                         $mref->swop_it($main::mycall);
1339                         $mref->store($chunk);
1340                         $mref->add_dir();
1341                         push @out, $dxchan->msg('m11', $mref->{msgno}, $to);
1342                         #push @out, "msgno $ref->{msgno} sent to $to";
1343                         my $todxchan = DXChannel->get(uc $to);
1344                         if ($todxchan) {
1345                                 if ($todxchan->is_user()) {
1346                                         $todxchan->send($todxchan->msg('m9'));
1347                                 }
1348                         }
1349                 }
1350         }
1351         return @out;
1352 }
1353
1354 no strict;
1355 sub AUTOLOAD
1356 {
1357         my $self = shift;
1358         my $name = $AUTOLOAD;
1359         return if $name =~ /::DESTROY$/;
1360         $name =~ s/.*:://o;
1361         
1362         confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
1363         # this clever line of code creates a subroutine which takes over from autoload
1364         # from OO Perl - Conway
1365         *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
1366         @_ ? $self->{$name} = shift : $self->{$name} ;
1367 }
1368
1369 1;
1370
1371 __END__