2 # various utilities which are exported globally
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
20 use vars qw(@month %patmap $pi $d2r $r2d @ISA @EXPORT);
24 @EXPORT = qw(atime ztime cldate cldatetime slat slong yesno promptf
25 parray parraypairs phex phash shellregex readfilestr writefilestr
27 print_all_fields cltounix unpad is_callsign is_latlong
28 is_qra is_freq is_digits is_pctext is_pcflag insertitem deleteitem
29 is_prefix dd is_ipaddr $pi $d2r $r2d localdata localdata_mv
33 @month = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
46 # a full time for logging and other purposes
50 my ($sec,$min,$hour,$mday,$mon,$year) = gmtime((defined $t) ? $t : time);
52 my $buf = sprintf "%02d%s%04d\@%02d:%02d:%02d", $mday, $month[$mon], $year, $hour, $min, $sec;
56 # get a zulu time in cluster format (2300Z)
60 $t = defined $t ? $t : time;
62 my ($sec,$min,$hour) = $dst ? localtime($t): gmtime($t);
63 my $buf = sprintf "%02d%02d%s", $hour, $min, ($dst) ? '' : 'Z';
67 # get a cluster format date (23-Jun-1998)
71 $t = defined $t ? $t : time;
73 my ($sec,$min,$hour,$mday,$mon,$year) = $dst ? localtime($t) : gmtime($t);
75 my $buf = sprintf "%2d-%s-%04d", $mday, $month[$mon], $year;
79 # return a cluster style date time
84 my $date = cldate($t, $dst);
85 my $time = ztime($t, $dst);
89 # return a unix date from a cluster date and time
94 my ($thisyear) = (gmtime)[5] + 1900;
96 return 0 unless $date =~ /^\s*(\d+)-(\w\w\w)-([12][90]\d\d)$/;
97 return 0 if $3 > 2036;
98 return 0 unless abs($thisyear-$3) <= 1;
100 return 0 unless $time =~ /^([012]\d)([012345]\d)Z$/;
101 $time = "$1:$2 +0000";
102 my $r = str2time("$date $time");
104 return $r == -1 ? undef : $r;
107 # turn a latitude in degrees into a string
111 my ($deg, $min, $let);
112 $let = $n >= 0 ? 'N' : 'S';
115 $min = int ((($n - $deg) * 60) + 0.5);
116 return "$deg $min $let";
119 # turn a longitude in degrees into a string
123 my ($deg, $min, $let);
124 $let = $n >= 0 ? 'E' : 'W';
127 $min = int ((($n - $deg) * 60) + 0.5);
128 return "$deg $min $let";
131 # turn a true into 'yes' and false into 'no'
135 return $n ? $main::yes : $main::no;
138 # provide a data dumpered version of the object passed
142 my $dd = new Data::Dumper([$value]);
145 $dd->Quotekeys($] < 5.005 ? 1 : 0);
146 $value = $dd->Dumpxs;
147 $value =~ s/([\r\n\t])/sprintf("%%%02X", ord($1))/eg;
148 $value =~ s/^\s*\[//;
149 $value =~ s/\]\s*$//;
154 # format a prompt with its current value and return it with its privilege
157 my ($line, $value) = @_;
158 my ($priv, $prompt, $action) = split ',', $line;
160 # if there is an action treat it as a subroutine and replace $value
162 my $q = qq{\$value = $action(\$value)};
164 } elsif (ref $value) {
167 $prompt = sprintf "%15s: %s", $prompt, $value;
168 return ($priv, $prompt);
171 # turn a hex field into printed hex
175 return sprintf '%X', $val;
178 # take an arg as a hash of call=>time pairs and print it
183 for (sort keys %$ref) {
184 $out .= "$_=$ref->{$_}, ";
191 # take an arg as an array list and print it
195 return ref $ref ? join(', ', @{$ref}) : $ref;
198 # take the arg as an array reference and print as a list of pairs
205 for ($i = 0; $i < @$ref; $i += 2) {
207 my $r2 = @$ref[$i+1];
210 chop $out; # remove last space
211 chop $out; # remove last comma
215 # take the arg as a hash reference and print it out as such
221 while (my ($k,$v) = each %$ref) {
222 $out .= "${k}=>$v, ";
224 chop $out; # remove last space
225 chop $out; # remove last comma
232 my @a = split /,/, $ref->field_prompt(shift);
233 my @b = split /,/, $ref->field_prompt(shift);
234 return lc $a[1] cmp lc $b[1];
237 # print all the fields for a record according to privilege
239 # The prompt record is of the format '<priv>,<prompt>[,<action>'
240 # and is expanded by promptf above
244 my $self = shift; # is a dxchan
245 my $ref = shift; # is a thingy with field_prompt and fields methods defined
247 my @fields = $ref->fields;
249 my $width = $self->width - 1;
252 foreach $field (sort {_sort_fields($ref, $a, $b)} @fields) {
253 if (defined $ref->{$field}) {
254 my ($priv, $ans) = promptf($ref->field_prompt($field), $ref->{$field});
256 if (length $ans > $width) {
257 my ($p, $a) = split /: /, $ans, 2;
258 my $l = (length $p) + 2;
259 my $al = ($width - 1) - $l;
261 while (length $a > $al ) {
262 ($bit, $a) = unpack "A$al A*", $a;
263 push @tmp, "$p: $bit";
266 push @tmp, "$p: $a" if length $a;
270 push @out, @tmp if ($self->priv >= $priv);
276 # generate a regex from a shell type expression
277 # see 'perl cookbook' 6.9
281 $in =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
282 return '^' . $in . "\$";
285 # read in a file into a string and return it.
286 # the filename can be split into a dir and file and the
287 # file can be in upper or lower case.
288 # there can also be a suffix
291 my ($dir, $file, $suffix) = @_;
296 $fn = "$dir/$f.$suffix";
299 $fn = "$dir/$file.$suffix";
312 my $fh = new IO::File $fn;
322 # write out a file in the format required for reading
323 # in via readfilestr, it expects the same arguments
324 # and a reference to an object
334 confess('no object to write in writefilestr') unless $obj;
335 confess('object not a reference in writefilestr') unless ref $obj;
339 $fn = "$dir/$f.$suffix";
342 $fn = "$dir/$file.$suffix";
355 my $fh = new IO::File ">$fn";
357 my $dd = new Data::Dumper([ $obj ]);
361 # $fh->print(@_) if @_ > 0; # any header comments, lines etc
362 $fh->print($dd->Dumpxs);
369 copy(@_) or return $!;
372 # remove leading and trailing spaces from an input string
381 # check that a field only has callsign characters in it
385 (?:\d?[A-Z]{1,2}\d*/)? # out of area prefix /
386 (?:\d?[A-Z]{1,2}\d+) # main prefix one (required)
387 [A-Z]{1,5} # callsign letters (required)
388 (?:-(?:\d{1,2}|\#))? # - nn possibly (eg G8BPQ-8) or -# (an RBN spot)
389 (?:/[0-9A-Z]{1,7})? # / another prefix, callsign or special label (including /MM, /P as well as /EURO or /LGT) possibly
392 # longest callign allowed is 1X11/1Y11XXXXX-11/XXXXXXX
397 return $_[0] =~ m!^(?:[A-Z]{1,2}\d+ | \d[A-Z]{1,2}}\d+)!x # basic prefix
401 # check that a PC protocol field is valid text
404 return undef unless length $_[0];
405 return undef if $_[0] =~ /[\x00-\x08\x0a-\x1f\x80-\x9f]/;
409 # check that a PC prot flag is fairly valid (doesn't check the difference between 1/0 and */-)
412 return $_[0] =~ /^[01\*\-]+$/;
415 # check that a thing is a frequency
418 return $_[0] =~ /^\d+(?:\.\d+)?$/;
421 # check that a thing is just digits
424 return $_[0] =~ /^[\d]+$/;
427 # does it look like a qra locator?
430 return unless length $_[0] == 4 || length $_[0] == 6;
431 return $_[0] =~ /^[A-Ra-r][A-Ra-r]\d\d(?:[A-Xa-x][A-Xa-x])?$/;
434 # does it look like a valid lat/long
437 return $_[0] =~ /^\s*\d{1,2}\s+\d{1,2}\s*[NnSs]\s+1?\d{1,2}\s+\d{1,2}\s*[EeWw]\s*$/;
440 # is it an ip address?
443 return $_[0] =~ /^\d+\.\d+\.\d+\.\d+$/ || $_[0] =~ /^[0-9a-f:,]+$/;
446 # insert an item into a list if it isn't already there returns 1 if there 0 if not
452 return 1 if grep {$_ eq $item } @$list;
457 # delete an item from a list if it is there returns no deleted
464 @$list = grep {$_ ne $item } @$list;
468 # find the correct local_data directory
469 # basically, if there is a local_data directory with this filename and it is younger than the
470 # equivalent one in the (system) data directory then return that name rather than the system one
474 my $ofn = "$main::data/$ifn";
477 if (-e "$main::local_data") {
478 $tfn = "$main::local_data/$ifn";
479 if (-e $tfn && -e $ofn) {
480 $ofn = $tfn if -M $tfn < -M $ofn;
489 # move a file or a directory from data -> local_data if isn't there already
493 if (-e "$main::data/$ifn" ) {
494 unless (-e "$main::local_data/$ifn") {
495 move("$main::data/$ifn", "$main::local_data/$ifn") or die "localdata_mv: cannot move $ifn from '$main::data' -> '$main::local_data' $!\n";