3 # This module impliments the message handling for a dx cluster
5 # Copyright (c) 1998 Dirk Koopman G1TLH
12 @ISA = qw(DXProt DXChannel);
26 use vars qw(%work @msg $msgdir %valid);
28 %work = (); # outstanding jobs
29 @msg = (); # messages we have
30 $msgdir = "$main::root/msg"; # directory contain the msgs
33 fromnode => '9,From Node',
34 tonode => '9,To Node',
37 t => '0,Msg Time,cldatetime',
38 private => '9,Private',
39 subject => '0,Subject',
40 linesreq => '0,Lines per Gob',
41 rrreq => '9,Read Confirm',
44 stream => '9,Stream No',
45 count => '9,Gob Linecnt',
46 file => '9,File?,yesno',
47 gotit => '9,Got it Nodes,parray',
48 lines => '9,Lines,parray',
49 read => '9,Times read',
54 # allocate a new object
55 # called fromnode, tonode, from, to, datetime, private?, subject, nolinesper
59 my $self = bless {}, $pkg;
60 $self->{msgno} = shift;
62 $self->{from} = shift;
64 $self->{private} = shift;
65 $self->{subject} = shift;
66 $self->{origin} = shift;
67 $self->{read} = shift;
76 delete $ref->{linesreq};
77 delete $ref->{tonode};
78 delete $ref->{fromnode};
79 delete $ref->{stream};
87 my ($self, $line) = @_;
88 my @f = split /[\^\~]/, $line;
89 my ($pcno) = $f[0] =~ /^PC(\d\d)/; # just get the number
92 if ($pcno == 28) { # incoming message
93 my $t = cltounix($f[5], $f[6]);
94 my $stream = next_transno($f[2]);
95 my $ref = DXMsg->alloc($stream, $f[3], $f[4], $t, $f[7], $f[8], $f[13], '0');
97 # fill in various forwarding state variables
98 $ref->{fromnode} = $f[2];
99 $ref->{tonode} = $f[1];
100 $ref->{rrreq} = $f[11];
101 $ref->{linesreq} = $f[10];
102 $ref->{stream} = $stream;
103 $ref->{count} = 0; # no of lines between PC31s
104 dbg('msg', "new message from $f[4] to $f[3] '$f[8]' stream $stream\n");
105 $work{"$f[1]$f[2]$stream"} = $ref; # store in work
106 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
110 if ($pcno == 29) { # incoming text
111 my $ref = $work{"$f[1]$f[2]$f[3]"};
113 push @{$ref->{lines}}, $f[4];
115 if ($ref->{count} >= $ref->{linesreq}) {
116 $self->send(DXProt::pc31($f[2], $f[1], $f[3]));
117 dbg('msg', "stream $f[3]: $ref->{linereq} lines received\n");
132 if ($pcno == 32) { # incoming EOM
133 dbg('msg', "stream $f[3]: EOM received\n");
134 my $ref = $work{"$f[1]$f[2]$f[3]"};
136 $self->send(DXProt::pc33($f[2], $f[1], $f[3]));# acknowledge it
138 # get the next msg no - note that this has NOTHING to do with the stream number in PC protocol
139 my $msgno = next_transno("Msgno") if !$ref->{file};
141 $ref->store($ref->{lines}); # store it (whatever that may mean)
143 delete $work{"$f[1]$f[2]$f[3]"}; # remove the reference from the work vector
144 push @msg, $ref; # add this message to the incore message list
153 if ($pcno == 40) { # this is a file request
154 $f[3] =~ s/\\/\//og; # change the slashes
155 $f[3] =~ s/\.//og; # remove dots
156 $f[3] = lc $f[3]; # to lower case;
157 dbg('msg', "incoming file $f[3]\n");
158 last SWITCH if $f[3] =~ /^\/(perl|cmd|local_cmd|src|lib|include|sys|msg)\//; # prevent access to executables
160 # create any directories
161 my @part = split /\//, $f[3];
163 my $fn = "$main::root";
164 pop @part; # remove last part
165 foreach $part (@part) {
168 last SWITCH if !mkdir $fn, 0777;
169 dbg('msg', "created directory $fn\n");
171 my $stream = next_transno($f[2]);
172 my $ref = DXMsg->alloc($stream, "$main::root/$f[3]", $self->call, time, !$f[4], $f[3], ' ', '0');
174 # forwarding variables
175 $ref->{fromnode} = $f[1];
176 $ref->{tonode} = $f[2];
177 $ref->{linesreq} = $f[5];
178 $ref->{stream} = $stream;
179 $ref->{count} = 0; # no of lines between PC31s
181 $work{"$f[1]$f[2]$stream"} = $ref; # store in work
182 $self->send(DXProt::pc30($f[2], $f[1], $stream)); # send ack
190 # store a message away on disc or whatever
196 # we only proceed if there are actually any lines in the file
197 if (@{$lines} == 0) {
201 if ($ref->{file}) { # a file
202 dbg('msg', "To be stored in $ref->{to}\n");
204 my $fh = new FileHandle "$ref->{to}", "w";
207 foreach $line (@{$lines}) {
211 dbg('msg', "file $ref->{to} stored\n");
213 confess "can't open file $ref->{to} $!";
215 # push @{$ref->{gotit}}, $ref->{fromnode} if $ref->{fromnode};
216 } else { # a normal message
218 # attempt to open the message file
219 my $fn = filename($ref->{msgno});
221 dbg('msg', "To be stored in $fn\n");
223 my $fh = new FileHandle "$fn", "w";
225 print $fh "=== $ref->{msgno}^$ref->{to}^$ref->{from}^$ref->{t}^$ref->{private}^$ref->{subject}^$ref->{origin}^$ref->{read}\n";
226 print $fh "=== $ref->{fromnode}\n";
228 foreach $line (@{$lines}) {
229 $ref->{size} += (length $line) + 1;
233 push @{$ref->{gotit}}, $ref->{fromnode} if $ref->{fromnode};
235 dbg('msg', "msg $ref->{msgno} stored\n");
237 confess "can't open msg file $fn $!";
247 # remove it from the active message list
248 @msg = map { $_ != $self ? $_ : () } @msg;
251 unlink filename($self->{msgno});
254 # read in a message header
264 $file = new FileHandle;
265 if (!open($file, $fn)) {
266 print "Error reading $fn $!\n";
270 $line = <$file>; # first line
272 $size -= length $line;
273 if (! $line =~ /^===/o) {
274 print "corrupt first line in $fn ($line)\n";
278 @f = split /\^/, $line;
279 $ref = DXMsg->alloc(@f);
281 $line = <$file>; # second line
283 $size -= length $line;
284 if (! $line =~ /^===/o) {
285 print "corrupt second line in $fn ($line)\n";
290 @f = split /\^/, $line;
291 push @{$ref->{goit}}, @f;
292 $ref->{size} = $size;
299 # read in a message header
303 my $msgno = $self->{msgno};
306 my $fn = filename($msgno);
309 $file = new FileHandle;
310 if (!open($file, $fn)) {
311 print "Error reading $fn $!\n";
314 chomp (@out = <$file>);
317 shift @out if $out[0] =~ /^=== /;
318 shift @out if $out[0] =~ /^=== /;
322 # get a new transaction number from the file specified
326 $name =~ s/\W//og; # remove non-word characters
327 my $fn = "$msgdir/$name";
330 my $fh = new FileHandle;
331 if (sysopen($fh, $fn, O_RDWR|O_CREAT, 0666)) {
333 $msgno = $fh->getline;
337 $fh->print("$msgno\n");
338 dbg('msg', "msgno $msgno allocated for $name\n");
341 confess "can't open $fn $!";
346 # initialise the message 'system', read in all the message headers
349 my $dir = new FileHandle;
353 # read in the directory
354 opendir($dir, $msgdir) or confess "can't open $msgdir $!";
355 @dir = readdir($dir);
362 $ref = read_msg_header("$msgdir/$_");
365 # add the clusters that have this
371 # return all the current messages
377 # get a particular message
382 return $_ if $_->{msgno} == $msgno;
383 last if $_->{msgno} > $msgno;
388 # return the official filename for a message no
391 return sprintf "$msgdir/m%06d", shift;
395 # return a list of valid elements
404 # return a prompt for a field
409 my ($self, $ele) = @_;
417 my $name = $AUTOLOAD;
418 return if $name =~ /::DESTROY$/;
421 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
422 @_ ? $self->{$name} = shift : $self->{$name} ;