blib/lib/DNS/ZoneFile.pm untested

filter: Statementstmt 0.0% 0 / 346 Branchbran 0.0% 0 / 188 Conditioncond 0.0% 0 / 67 MC/DCmcdc 0.0% 0 / 36 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 666 CC 95 SCAR 61.9
CC
95
Cov
0%
canonicalise
870.0
deCanonicalise
650.0
parseRecord
506.0
CRAP 488.4
1 package DNS::ZoneFile;
2  
3 # This is DNS::ZoneFile, an object oriented way to manage information in DNS
4 # master files. This version is intended to be used both to create such files
5 # and to add and delete records.
6 #
7 # $Id: ZoneFile.pm,v 1.5 2000/09/24 23:56:33 mbm Exp $
8 #
9 # (c) Copyright Matthew Byng-Maddick <matthew@codix.net> 2000
10 #
11 # This is distributed under the GNU General Public Licence or the Artistic
12 # Licence as with Perl itself
13 #
14  
15 # We need perl 5.005 regexps, because these are the ones that deal easily with
16 # quoted strings and the suchlike...
17 require 5.005;
18  
19 use strict;
20 use vars qw($VERSION %TYPES @DEFAULTS);
21  
22 $VERSION="0.95";
23  
24 =head1 NAME
25  
26 DNS::ZoneFile - Object-Oriented Management of a Master File
27  
28 =head1 SYNOPSIS
29  
30 use C<DNS::ZoneFile>;
31  
32 my $zone=C<DNS::ZoneFile>->new(
33 $filename_or_file_as_scalar,
34 ZONE_ORIGIN => $ORIGIN,
35 NEW_ZONE => $NEW_ZONE,
36 );
37  
38 $zone->addRecord(
39 Domain => $domain,
40 TTL => $ttl,
41 Class => $class,
42 Type => $type,
43 Data => \@arr,
44 );
45  
46 $zone->deleteRecord(
47 $domain
48 );
49
50 $zone->deleteRecord(
51 $domain,
52 $type
53 );
54  
55 $zone->printZone();
56  
57 =cut
58  
59 # in %TYPES,
60 # A => Address
61 # I => IP Address
62 # M => Mailbox
63 # N => Number
64 # S => String
65 # T => Time
66 %TYPES=(
67 SOA => ['A','M','N','T','T','T','T'],
68 A => ['I'],
69 # must find out about this one...
70 # AA => [],
71 MX => ['N','A'],
72 CNAME => ['A'],
73 NS => ['A'],
74 TXT => ['S'],
75 RP => ['M','S'],
76 NULL => [],
77 PTR => ['A']
78 );
79  
80 @DEFAULTS=(
81 ZONE_ORIGIN => '.',
82 NEW_ZONE => 0,
83 );
84  
85 #### THIS IS *ALL* ALPHA CODE ####
86  
87 =head1 DESCRIPTION
88  
89 =head2 my I<$zone>=DNS::ZoneFile->B<new>(I<$file>,I<%params>);
90  
91 B<new>() creates a new DNS::ZoneFile object. It is initialised either from
92 the filename supplied, or, if the first argument is a reference to a scalar,
93 then the values is read.
94  
95 Params:
96  
97 =over 5
98  
99 =item ZONE_ORIGIN (.)
100  
101 Sets the $ORIGIN for this zone.
102  
103 =item NEW_ZONE (false)
104  
105 If unset and DNS::ZoneFile can't read any data, then return undef. Otherwise
106 create a new SOA. (If this is set, ZONE_ORIGIN shouldn't really be set to '.')
107  
108 =back
109  
110 =cut
111  
112 sub new
113 {
114 my $proto=shift;
115 my $pack=ref $proto or $proto;
116 my $file=shift;
117 my $text="";
118 if($file)
119 {
120 if(ref($file))
121 {
122 return undef unless(ref($file) ne 'SCALAR');
123 $text=$$file;
124 }
125 else
126 {
127 open(ZONEFILE,$file) or return undef;
128 local $/=undef;
129 $text=<ZONEFILE>;
130 close(ZONEFILE);
131 }
132 }
133 my $self=bless {@DEFAULTS,@_}, $pack;
134 return undef unless length $text or $self->{"NEW_ZONE"};
135 if($self->{"ProcessIncludes"})
136 {
137 print STDERR "DNS::ZoneFile: ProcessIncludes: Use of this option is deprecated\n";
138 }
139 unless($self->readZoneFile($text))
140 {
141 return bless {FAILED=>1,ERROR=>$self->{"ERROR"}},$pack;
142 }
143 $self->{"SUCCESS"}=1;
144 return $self;
145 }
146  
147 =head2 I<$zone>->B<success>();
148  
149 Returns true if the object was created OK, false if otherwise.
150  
151 =cut
152  
153 sub success
154 {
155 my $self=shift;
156 return undef if($self->{"FAILED"} || !$self->{"SUCCESS"});
157 return 1;
158 }
159  
160 =head2 I<$zone>->B<fail>();
161  
162 Returns the error message of a a failed object call, or false if
163 the object was created OK.
164  
165 =cut
166  
167 sub fail
168 {
169 my $self=shift;
170 return undef if(!$self->{"FAILED"} || $self->{"SUCCESS"});
171 return ($self->{"ERROR"} || 1);
172 }
173  
174 =head2 I<$zone>->B<addRecord>(I<@RRDATA>);
175  
176 This will add a record to the zone (maybe that should be %RRDATA?)
177  
178 =cut
179  
180 sub addRecord
181 {
182 }
183  
184 =head2 I<$zone>->B<deleteRecord>(I<$domain>[,I<$type>]);
185  
186 This is also unwritten as yet - but I envisage this as a
187 $zone->deleteRecord("rigel.codix.net","MX"); or
188 $zone->deleteRecord("alioth.codix.net");
189  
190 =cut
191  
192 sub deleteRecord
193 {
194 }
195  
196 =head2 I<$zone>->B<printZone>();
197  
198 Returns a (reference to)? a scalar which is the zone file in full.
199 or perhaps it keeps track of the filenames to open?
200  
201 =cut
202  
203 sub printZone
204 {
205 }
206  
207 #### SOME OF THIS IS ALPHA CODE ####
208 #### utility functions ####
209  
210 sub readZoneFile
211 {
212 my $self=shift;
213 my $ZFText=shift;
214  
215 $self->{"RECORDS"}=[];
216  
217 my $currRec="";
218 my $currComm="";
219  
220 my $lines=split/\n/,$ZFText;
221 while($ZFText&&(($ZFText,$currRec,$currComm)=getRecord($self,$ZFText)))
222 {
223 if($currRec)
224 {
225 # this is a full record
226 my $record=parseRecord($self,$currRec);
227 if(defined ($record))
228 {
229 if($record)
230 {
231 # XXX: I really should be using the old
232 # method of having each RR as a separate
233 # record
234 $record->{"COMMENT"}=$currComm;
235 push(@{$self->{"RECORDS"}},$record);
236 }
237 }
238 else
239 {
240 my $new_lines=split/\n/,$ZFText;
241 my $err_line=$lines-$new_lines;
242 $self->{"ERROR"}="Error around line $err_line: ".
243 $self->{"FAIL_REASON"};
244 return undef;
245 }
246 }
247 elsif($currComm)
248 {
249 # this is a comment only
250 # we only want to preserve these if we are
251 # keeping the order the same.
252 if($self->{"KEEP_ORDER"})
253 {
254 # XXX: again the comment should be a RR object.
255 push(@{$self->{"RECORDS"}},{COMMENT=>$currComm});
256 }
257 }
258 else
259 {
260 my $new_lines=split/\n/,$ZFText;
261 my $err_line=$lines-$new_lines;
262 $self->{"ERROR"}="Error around line $err_line: ".
263 $self->{"FAIL_REASON"};
264 return undef;
265 }
266 }
267 return 1;
268 }
269  
270 # getRecord
271 # this gets the next record from the text of the master file passed in
272 # its argument.
273 sub getRecord
274 {
275 my $self=shift;
276 my $currentText=shift;
277  
278 return "" unless($currentText);
279 my $currentRecord="";
280 my $currentComment="";
281  
282 $currentText=~s/^\s*\n//s;
283  
284 my $foundAllRecord=0;
285 do
286 {
287 # strip out comments on this line.
288 $currentText=~s{
289 ^([^"\n]*? # match some non-quoted string stuff
290 (((?<!\\)"([^"]|\\")*(?<!\\)"[^"\n]*?)*?)) # match any number of quoted strings
291 # with trailing non-quoted strings.
292 [ \t]*((?<!\\);[^\n]*)?\n # match a comment (trailing space or ;.*)
293 }{}xs;
294 # $1 should now contain the actual record text
295 $currentRecord.=$1;
296  
297 # keep track of the comments
298 my $ccLine=$5;
299 $ccLine=~s/;\s*(\S(.*\S)?)?\s*$/$1/msg;
300 ($currentComment ne "") ? ($currentComment.="\n".$ccLine) : ($currentComment=$ccLine);
301  
302 if(!length($currentRecord) && !length($currentComment))
303 {
304 $foundAllRecord=2;
305 }
306  
307 my $infoRecord=$currentRecord;
308  
309 # get rid of quoted strings
310 $infoRecord=~s/^([^"]*?)((?<!\\)"([^"]|\\")*(?<!\\)")/$1/sg;
311  
312 # get rid of nested parenthetised expressions
313 while($infoRecord=~s{
314 (?<!\\)\( # non-backslash prefixed open bracket
315 ([^\(\)]|\\\(|\\\))* # any number of backslashed brackets or
316 # non-bracket characters
317 (?<!\\)\) # non-backslash prefixed close bracket
318 }{}xs){}
319
320 # end if we haven't got an unmatched open bracket or a half finished comment
321 $foundAllRecord=1 if(!$foundAllRecord && $infoRecord!~/(?<!\\)\(/s && $currentText!~/^[ \t]*;/s)
322 }
323 while(!$foundAllRecord);
324 if($foundAllRecord==2)
325 {
326 $self->{"FAIL_REASON"}="Unterminated string.";
327 return($currentText,"","");
328 }
329  
330 # tidy up the comments
331 $currentComment=~s/\n\s*\n/\n/sg;
332  
333 # tidy up the record itself
334 my @record=split/((?<!\\)"(?:[^"]|\\")*")/,$currentRecord; # split the record into quoted strings
335 for my $part (@record)
336 {
337 if($part!~/^".*"$/)
338 {
339 # we're not inside a quoted string
340 $part=~s/(?<!\\)[\(\)]//g; # get rid of all non quoted parens
341 $part=~s/\s+/ /g; # reduce spaces
342 }
343 }
344 $currentRecord=join("",@record); # glue it back together
345  
346 return($currentText,$currentRecord,$currentComment);
347 }
348  
349 # parseRecord
350 # Turn record into hash reference, checking for syntactic validity of
351 # its components. This assumes you've run getRecord() first.
352 # This is a horrid function, even though it does just about work. :)
353 sub parseRecord
354 {
355 my $self=shift;
356 my $record=shift;
357 my %hash=();
358  
359 # the following code all assumes that we've run getRecord() above
360 if($record=~/^\$ORIGIN\s+(.*)$/si)
361 {
362 my $origin;
363 if(defined($origin=canonicalise($self,$1,"A")))
364 {
365 $self->{"CURRENT_ORIGIN"}=$origin;
366 return 0;
367 }
368 $self->{"FAIL_REASON"}="\$ORIGIN requires a domain.";
369 return undef;
370 }
371 if($record=~/^\$TTL\s+(.*)$/si)
372 {
373 my $ttl;
374 if(defined($ttl=canonicalise($self,$1,"T")))
375 {
376 $self->{"CURRENT_TTL"}=$ttl;
377 return 0;
378 }
379 $self->{"FAIL_REASON"}="\$TTL requires a time.";
380 return undef;
381 }
382 else
383 {
384 # split record into its various parts, keeping all the quoting.
385 my @record=split/\s+((?<!\\)"(?:[^"]|\\")*(?<!\\)")?/,$record;
386 my @r2=();
387 my $first=0;
388 for my $part (@record)
389 {
390 push(@r2,$part) if(!($first++)||(length($part)));
391 }
392 @record=@r2;
393 if($record[0] eq "\$INCLUDE")
394 {
395 $hash{"SPECIAL"}="INCLUDE";
396 $hash{"INCLUDE_FILENAME"}=canonicalise($self,$record[1],"S");
397 if($record[2])
398 {
399 $hash{"INCLUDE_ORIGIN"}=canonicalise($self,$record[2],"A");
400 if(!defined($hash{"INCLUDE_ORIGIN"}))
401 {
402 $self->{"FAIL_REASON"}="\$INCLUDE second argument must be a domain.";
403 return undef;
404 }
405 }
406 else
407 {
408 $hash{"INCLUDE_ORIGIN"}=$self->{"ZONE_ORIGIN"};
409 }
410 unless($hash{"INCLUDE_FILENAME"})
411 {
412 $self->{"FAIL_REASON"}="\$INCLUDE requires filename.";
413 return undef;
414 }
415 }
416 else
417 {
418 if($record[0] eq "")
419 {
420 $hash{"DOMAIN"}=$self->{"LAST_DOMAIN"};
421 }
422 else
423 {
424 my $domain;
425 if(defined($domain=canonicalise($self,$record[0],"A")))
426 {
427 $hash{"DOMAIN"}=$domain;
428 $self->{"LAST_DOMAIN"}=$domain;
429 }
430 else
431 {
432 $self->{"FAIL_REASON"}="Couldn't canonicalise domain part.";
433 return undef;
434 }
435 }
436 shift(@record);
437 my $class=0;
438 my $ttl=0;
439 # deal with class and ttl as necessary
440 if(lc($record[0]) eq "in")
441 {
442 shift(@record);
443 $class++;
444 }
445 if(defined($ttl=canonicalise($self,$record[0],"T")))
446 {
447 if(defined($self->{"MINIMUM_TTL"}))
448 {
449 if($ttl<$self->{"MINIMUM_TTL"})
450 {
451 $hash{"TTL"}=$self->{"MINIMUM_TTL"};
452 }
453 else
454 {
455 $hash{"TTL"}=$ttl;
456 }
457 }
458 else
459 {
460 $hash{"TTL"}=$ttl;
461 }
462 shift(@record);
463 }
464 if(lc($record[0]) eq "in")
465 {
466 if($class)
467 {
468 $self->{"FAIL_REASON"}="Found two class definitions for RR.";
469 return undef;
470 }
471 shift(@record);
472 }
473 $hash{"TTL"}=$self->{"CURRENT_TTL"} if(!defined($hash{"TTL"}));
474 # by the time we get here, we should have @record containing just the RR
475 if(ref($TYPES{uc($record[0])}))
476 {
477 $hash{"TYPE"}=uc(shift(@record));
478 $hash{"RR_DATA"}=[];
479 if($#record != $#{$TYPES{$hash{"TYPE"}}})
480 {
481 $self->{"FAIL_REASON"}="Argument inconsistency: expected ".
482 ($#{$TYPES{$hash{"TYPE"}}}+1)." arguments for ".$hash{"TYPE"}.
483 " record, and got ".($#record+1)." arguments.";
484 return undef;
485 }
486 for my $i (0..$#{$TYPES{$hash{"TYPE"}}})
487 {
488 my $part=canonicalise($self,$record[$i],${$TYPES{$hash{"TYPE"}}}[$i]);
489 if(!defined($part))
490 {
491 $i++;
492 $self->{"FAIL_REASON"}="Incorrect format for part '".$record[$i-1].
493 "'($i) of RR for '".$hash{"DOMAIN"}."'.";
494 return undef;
495 }
496 push(@{$hash{"RR_DATA"}},$part);
497 }
498 }
499 else
500 {
501 $self->{"FAIL_REASON"}="Unknown RR type \"".uc($record[0])."\".";
502 return undef;
503 }
504 }
505 }
506 return \%hash;
507 }
508  
509 #updateSerial
510 # This function will update the serial number in the zone file loaded.
511  
512 sub updateSerial
513 {
514 my $self=shift;
515  
516 my $snum;
517  
518 # need to add some kind of check here.....
519  
520 for my $record (@{$self->{"RECORDS"}})
521 {
522 if($record->{"TYPE"} eq "SOA")
523 {
524 # read the current serial number;
525 my($oyr,$omth,$oday,$onum)=unpack("a4a2a2a2",$record->{"RR_DATA"}->[2]);
526
527 # read the time
528 my @t=localtime();
529  
530 # is this another version today?
531 if( ($t[5]+1900==$oyr) && ($t[4]+1==$omth) && ($t[3]==$oday) )
532 {
533 # Yes
534 $snum=printWithZeros($oyr,4);
535 $snum.=printWithZeros($omth,2);
536 $snum.=printWithZeros($oday,2);
537 $snum.=printWithZeros(++$onum,2);
538 }
539 else
540 {
541 # No
542 $snum=printWithZeros($t[5]+1900,4);
543 $snum.=printWithZeros($t[4]+1,2);
544 $snum.=printWithZeros($t[3],2);
545 $snum.=printWithZeros(0,2);
546 }
547 $record->{"RR_DATA"}->[2]=$snum;
548 }
549 }
550 }
551  
552 #printWithZeros
553 # Utility function to print a number with enough zeros to fill the
554 # passed width.
555 sub printWithZeros
556 {
557 my $num=shift;
558 my $wdth=shift;
559  
560 # XXX: this must be horribly inefficient, not sure what the best
561 # way to fix it is.
562 $num+=0;
563 return "0" x ($wdth - length($num)).$num;
564 }
565  
566 # canonicalise
567 # This function turns an element of an RR into it's fully qualified and
568 # unquoted form, checking that it conforms to the relevant syntax. It
569 # returns the relevant text if it succeeds, or undef if the syntax fails.
570 sub canonicalise
571 {
572 my $self=shift;
573 my $text=shift;
574 my $type=shift;
575  
576 $self->{"CURRENT_ORIGIN"}=$self->{"ZONE_ORIGIN"}
577 if(!$self->{"CURRENT_ORIGIN"});
578 if($type eq 'A')
579 {
580 # domain name
581 $text=$self->{"CURRENT_ORIGIN"} if($text eq '@');
582 if($text=~/^"(.*)"$/s)
583 {
584 $text=$1;
585 $text=~s/\\"/"/sg;
586 }
587 else
588 {
589 $text=~s/\\(\d{3})/chr($1)/eg;
590 $text=~s/\\(\D)/$1/g;
591 }
592 $text.=".".$self->{"CURRENT_ORIGIN"} if($text!~/\.$/);
593 $text=~s/\.\.$/./;
594 return undef if($text!~/^(\.|([a-z0-9]([a-z0-9-]*[a-z0-9])?\.)+)$/i);
595 return $text;
596 }
597 elsif($type eq 'I')
598 {
599 # IP address
600 return undef if($text!~/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/);
601 return undef if(($1>255) || ($2>255) || ($3>255) || ($4>255));
602 return $text;
603 }
604 elsif($type eq 'M')
605 {
606 # mailbox name
607 my($lp,$dp);
608 return undef if($text eq '@');
609 if($text=~/^"(.*)"$/s)
610 {
611 $text=$1;
612 $text=~s/\\"/"/sg;
613 # I can nowhere find documentation on what happens
614 # if a mailbox is a quoted string. The following
615 # code is therefore an assumption.
616 $lp=$text;
617 $dp=$self->{"CURRENT_ORIGIN"};
618 }
619 else
620 {
621 $text.=".".$self->{"CURRENT_ORIGIN"} if($text!~/\.$/);
622 $text=~s/\.\.$/./;
623 my @mb=split/(?<!\\)\./,$text;
624 $lp=shift(@mb);
625 $dp=join(".",@mb).".";
626 $dp=~s/\.\.$/./;
627  
628 $lp=~s/\\(\d{3})/chr($1)/eg; # unquote, as in the RFCs
629 $lp=~s/\\(\D)/$1/g; # more unquoting
630 $dp=~s/\\(\d{3})/chr($1)/eg;
631 $dp=~s/\\(\D)/$1/g;
632 }
633 return undef if($dp!~/^(\.|([a-z0-9]([a-z0-9-]*[a-z0-9])?\.)+)$/i);
634 return $lp.'@'.$dp;
635 }
636 elsif($type eq 'N')
637 {
638 # number
639 return undef if($text!~/^\d+$/);
640 return $text;
641 }
642 elsif($type eq 'S')
643 {
644 # string
645 if($text=~/^"(.*)"$/s)
646 {
647 $text=$1;
648 $text=~s/\\"/"/sg;
649 }
650 else
651 {
652 $text=~s/\\(\d{3})/chr($1)/eg;
653 $text=~s/\\(\D)/$1/g;
654 }
655 return $text;
656 }
657 elsif($type eq 'T')
658 {
659 # time
660 if($text=~/^"(.*)"$/s)
661 {
662 $text=$1;
663 $text=~s/\\"/"/sg;
664 }
665 else
666 {
667 $text=~s/\\(\d{3})/chr($1)/eg;
668 $text=~s/\\(\D)/$1/g;
669 }
670 return undef if($text!~/^(\d+|(\d+[wdhms])+)$/i);
671 if($text=~/^\d+$/)
672 {
673 return $text;
674 }
675 else
676 {
677 my $total=0;
678 my $lastnum=0;
679 my @parts=split/([WwDdHhMmSs])/,$text;
680 for my $part (@parts)
681 {
682 if($part=~/^\d+$/)
683 {
684 $lastnum=$part;
685 }
686 else
687 {
688 if(lc($part) eq "w")
689 {
690 $total+=$lastnum*608400;
691 $lastnum=0;
692 }
693 elsif(lc($part) eq "d")
694 {
695 $total+=$lastnum*86400;
696 $lastnum=0;
697 }
698 elsif(lc($part) eq "h")
699 {
700 $total+=$lastnum*3600;
701 $lastnum=0;
702 }
703 elsif(lc($part) eq "m")
704 {
705 $total+=$lastnum*60;
706 $lastnum=0;
707 }
708 elsif(lc($part) eq "s")
709 {
710 $total+=$lastnum;
711 $lastnum=0;
712 }
713 }
714 }
715 $total+=$lastnum;
716 return $total;
717 }
718 }
719 return undef; # unrecognised type
720 }
721  
722 sub deCanonicalise
723 {
724 my $self=shift;
725 my $text=shift;
726 my $type=shift;
727  
728 if($type eq 'A')
729 {
730 # domain name
731 if(lc($text) eq lc($self->{"CURRENT_ORIGIN"}))
732 {
733 return "@";
734 }
735 my $origin=lc($self->{"CURRENT_ORIGIN"});
736 if(($origin ne ".") && (length($origin)+1<length($text)) &&
737 (substr(lc($text),-length($origin)-1) eq ".".$origin))
738 {
739 $text=substr($text,0,-length($origin)-1);
740 }
741 if($origin eq ".")
742 {
743 $text=~s/\.$//;
744 }
745 return undef if($text!~/^(\.|([a-z0-9]([a-z0-9-]*[a-z0-9])?\.)+)$/i);
746 return $text;
747 }
748 elsif($type eq 'I')
749 {
750 # IP address
751 return undef if($text!~/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/);
752 return undef if(($1>255) || ($2>255) || ($3>255) || ($4>255));
753 return $text;
754 }
755 elsif($type eq 'M')
756 {
757 # mailbox name
758 my($lp,$dp)=split/\@/,$text;
759 $lp=~s/\./\\./sg;
760 $lp=~s/([\(\);"])/\\$1/sg;
761 # horrid hack below to make sure it prints leading '0's
762 $lp=~s/([^a-zA-Z0-9+=\.\(\);"-])/"\\".substr(1000+ord($1),1)/esg;
763 my $origin=lc($self->{"CURRENT_ORIGIN"});
764 if(($origin ne ".") && (length($origin)+1<length($dp)) &&
765 (substr(lc($dp),-length($origin)-1) eq ".".$origin))
766 {
767 $dp=substr($dp,0,-length($origin)-1);
768 }
769 if($origin eq ".")
770 {
771 $dp=~s/\.$//;
772 }
773 return undef if($dp!~/^(\.|([a-z0-9]([a-z0-9-]*[a-z0-9])?\.)+)$/i);
774 $text=$lp;
775 $text.=".".$dp if($dp);
776 return $text;
777 }
778 elsif($type eq 'N')
779 {
780 # number
781 return undef if($text!~/^\d+$/);
782 return $text;
783 }
784 elsif($type eq 'S')
785 {
786 # string
787 $text=~s/"/\\"/sg;
788 return "\"".$text."\"";
789 }
790 elsif($type eq 'T')
791 {
792 return undef if($text!~/^\d+$/);
793 my $orig=$text;
794 my %thash=();
795 while($text>604800)
796 {
797 $text-=604800;
798 $thash{"W"}++;
799 }
800 while($text>86400)
801 {
802 $text-=86400;
803 $thash{"D"}++;
804 }
805 while($text>3600)
806 {
807 $text-=3600;
808 $thash{"H"}++;
809 }
810 while($text>60)
811 {
812 $text-=60;
813 $thash{"M"}++;
814 }
815 $thash{"S"}=$text;
816 my $tstr="";
817 $tstr.=$thash{"W"}."W" if($thash{"W"});
818 $tstr.=$thash{"D"}."D" if($thash{"D"});
819 $tstr.=$thash{"H"}."H" if($thash{"H"});
820 $tstr.=$thash{"M"}."M" if($thash{"M"});
821 $tstr.=$thash{"S"}."S" if($thash{"S"});
822 if(length($tstr)<length($orig))
823 {
824 return $tstr;
825 }
826 else
827 {
828 return $orig;
829 }
830 }
831 return undef;
832 }
833  
834 =head1 COMMENTS
835  
836 I have been recommended to release this bit of code unfinished
837 onto CPAN by some people - yes Greg, you know who you are - I'm
838 fully aware that this doesn't abstract enough yet.
839  
840 Hopefully doing this will enable me to write it quicker.
841  
842 Version: 0.95
843  
844 =head1 AUTHOR
845  
846 Matthew Byng-Maddick C<<matthew@codix.net>>
847  
848 =head1 SEE ALSO
849  
850 L<bind(8)>
851  
852 =cut
853  
854 1;