removed password from login on client
[spider.git] / perl / DXUtil.pm
1 #
2 # various utilities which are exported globally
3 #
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
5 #
6 # $Id$
7 #
8
9 package DXUtil;
10
11 use Date::Parse;
12 use IO::File;
13 use Data::Dumper;
14
15 use Carp;
16
17 require Exporter;
18 @ISA = qw(Exporter);
19 @EXPORT = qw(atime ztime cldate cldatetime slat slong yesno promptf 
20                          parray parraypairs shellregex readfilestr writefilestr
21              print_all_fields cltounix iscallsign
22             );
23
24 @month = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
25 %patmap = (
26                    '*' => '.*',
27                    '?' => '.',
28                    '[' => '[',
29                    ']' => ']'
30 );
31
32 # a full time for logging and other purposes
33 sub atime
34 {
35         my $t = shift;
36         my ($sec,$min,$hour,$mday,$mon,$year) = gmtime((defined $t) ? $t : time);
37         $year += 1900;
38         my $buf = sprintf "%02d%s%04d\@%02d:%02d:%02d", $mday, $month[$mon], $year, $hour, $min, $sec;
39         return $buf;
40 }
41
42 # get a zulu time in cluster format (2300Z)
43 sub ztime
44 {
45         my $t = shift;
46         my ($sec,$min,$hour) = gmtime((defined $t) ? $t : time);
47         $year += 1900;
48         my $buf = sprintf "%02d%02dZ", $hour, $min;
49         return $buf;
50
51 }
52
53 # get a cluster format date (23-Jun-1998)
54 sub cldate
55 {
56         my $t = shift;
57         my ($sec,$min,$hour,$mday,$mon,$year) = gmtime((defined $t) ? $t : time);
58         $year += 1900;
59         my $buf = sprintf "%2d-%s-%04d", $mday, $month[$mon], $year;
60         return $buf;
61 }
62
63 # return a cluster style date time
64 sub cldatetime
65 {
66         my $t = shift;
67         my $date = cldate($t);
68         my $time = ztime($t);
69         return "$date $time";
70 }
71
72 # return a unix date from a cluster date and time
73 sub cltounix
74 {
75         my $date = shift;
76         my $time = shift;
77         my ($thisyear) = (gmtime)[5] + 1900;
78
79         return 0 unless $date =~ /^\s*(\d+)-(\w\w\w)-([12][90]\d\d)$/;
80         return 0 if $3 > 2036;
81         return 0 unless abs($thisyear-$3) <= 1;
82         $date = "$1 $2 $3";
83         return 0 unless $time =~ /^([012]\d)([012345]\d)Z$/;
84         $time = "$1:$2 +0000";
85         my $r = str2time("$date $time");
86         return $r unless $r;
87         return $r == -1 ? undef : $r;
88 }
89
90 # turn a latitude in degrees into a string
91 sub slat
92 {
93         my $n = shift;
94         my ($deg, $min, $let);
95         $let = $n >= 0 ? 'N' : 'S';
96         $n = abs $n;
97         $deg = int $n;
98         $min = int ((($n - $deg) * 60) + 0.5);
99         return "$deg $min $let";
100 }
101
102 # turn a longitude in degrees into a string
103 sub slong
104 {
105         my $n = shift;
106         my ($deg, $min, $let);
107         $let = $n >= 0 ? 'E' : 'W';
108         $n = abs $n;
109         $deg = int $n;
110         $min = int ((($n - $deg) * 60) + 0.5);
111         return "$deg $min $let";
112 }
113
114 # turn a true into 'yes' and false into 'no'
115 sub yesno
116 {
117         my $n = shift;
118         return $n ? $main::yes : $main::no;
119 }
120
121 # format a prompt with its current value and return it with its privilege
122 sub promptf
123 {
124         my ($line, $value) = @_;
125         my ($priv, $prompt, $action) = split ',', $line;
126
127         # if there is an action treat it as a subroutine and replace $value
128         if ($action) {
129                 my $q = qq{\$value = $action(\$value)};
130                 eval $q;
131         }
132         $prompt = sprintf "%15s: %s", $prompt, $value;
133         return ($priv, $prompt);
134 }
135
136 # take an arg as an array list and print it
137 sub parray
138 {
139         my $ref = shift;
140         return join(', ', @{$ref});
141 }
142
143 # take the arg as an array reference and print as a list of pairs
144 sub parraypairs
145 {
146         my $ref = shift;
147         my $i;
148         my $out;
149   
150         for ($i = 0; $i < @$ref; $i += 2) {
151                 my $r1 = @$ref[$i];
152                 my $r2 = @$ref[$i+1];
153                 $out .= "$r1-$r2, ";
154         }
155         chop $out;                                      # remove last space
156         chop $out;                                      # remove last comma
157         return $out;
158 }
159
160 # print all the fields for a record according to privilege
161 #
162 # The prompt record is of the format '<priv>,<prompt>[,<action>'
163 # and is expanded by promptf above
164 #
165 sub print_all_fields
166 {
167         my $self = shift;                       # is a dxchan
168         my $ref = shift;                        # is a thingy with field_prompt and fields methods defined
169         my @out;
170         my @fields = $ref->fields;
171         my $field;
172
173         foreach $field (sort {$ref->field_prompt($a) cmp $ref->field_prompt($b)} @fields) {
174                 if (defined $ref->{$field}) {
175                         my ($priv, $ans) = promptf($ref->field_prompt($field), $ref->{$field});
176                         push @out, $ans if ($self->priv >= $priv);
177                 }
178         }
179         return @out;
180 }
181
182 # generate a regex from a shell type expression 
183 # see 'perl cookbook' 6.9
184 sub shellregex
185 {
186         my $in = shift;
187         $in =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
188         return '^' . $in . "\$";
189 }
190
191 # start an attempt at determining whether this string might be a callsign
192 sub iscallsign
193 {
194         my $call = uc shift;
195         return 1 if $call =~ /^[A-Z]+\d+[A-Z]+/;
196         return 1 if $call =~ /^\d+[A-Z]\d+[A-Z]+/;
197         return undef;
198 }
199
200 # read in a file into a string and return it. 
201 # the filename can be split into a dir and file and the 
202 # file can be in upper or lower case.
203 # there can also be a suffix
204 sub readfilestr
205 {
206         my ($dir, $file, $suffix) = @_;
207         my $fn;
208         my $f;
209         if ($suffix) {
210                 $f = uc $file;
211                 $fn = "$dir/$f.$suffix";
212                 unless (-e $fn) {
213                         $f = lc $file;
214                         $fn = "$dir/$file.$suffix";
215                 }
216         } elsif ($file) {
217                 $f = uc $file;
218                 $fn = "$dir/$file";
219                 unless (-e $fn) {
220                         $f = lc $file;
221                         $fn = "$dir/$file";
222                 }
223         } else {
224                 $fn = $dir;
225         }
226
227         my $fh = new IO::File $fn;
228         my $s = undef;
229         if ($fh) {
230                 local $/ = undef;
231                 $s = <$fh>;
232                 $fh->close;
233         }
234         return $s;
235 }
236
237 # write out a file in the format required for reading
238 # in via readfilestr, it expects the same arguments 
239 # and a reference to an object
240 sub writefilestr
241 {
242         my $dir = shift;
243         my $file = shift;
244         my $suffix = shift;
245         my $obj = shift;
246         my $fn;
247         my $f;
248         
249         confess('no object to write in writefilestr') unless $obj;
250         confess('object not a reference in writefilestr') unless ref $obj;
251         
252         if ($suffix) {
253                 $f = uc $file;
254                 $fn = "$dir/$f.$suffix";
255                 unless (-e $fn) {
256                         $f = lc $file;
257                         $fn = "$dir/$file.$suffix";
258                 }
259         } elsif ($file) {
260                 $f = uc $file;
261                 $fn = "$dir/$file";
262                 unless (-e $fn) {
263                         $f = lc $file;
264                         $fn = "$dir/$file";
265                 }
266         } else {
267                 $fn = $dir;
268         }
269
270         my $fh = new IO::File ">$fn";
271         if ($fh) {
272                 my $dd = new Data::Dumper([ $obj ]);
273                 $dd->Indent(1);
274                 $dd->Terse(1);
275                 $dd->Quotekeys(0);
276                 #       $fh->print(@_) if @_ > 0;     # any header comments, lines etc
277                 $fh->print($dd->Dumpxs);
278                 $fh->close;
279         }
280 }