2 # various utilities which are exported globally
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
18 use vars qw(@month %patmap @ISA @EXPORT);
22 @EXPORT = qw(atime ztime cldate cldatetime slat slong yesno promptf
23 parray parraypairs phex phash shellregex readfilestr writefilestr
25 print_all_fields cltounix unpad is_callsign is_long_callsign is_latlong
26 is_qra is_freq is_digits is_pctext is_pcflag insertitem deleteitem
27 is_prefix dd is_ipaddr
31 @month = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
39 # a full time for logging and other purposes
43 my ($sec,$min,$hour,$mday,$mon,$year) = gmtime((defined $t) ? $t : time);
45 my $buf = sprintf "%02d%s%04d\@%02d:%02d:%02d", $mday, $month[$mon], $year, $hour, $min, $sec;
49 # get a zulu time in cluster format (2300Z)
53 $t = defined $t ? $t : time;
55 my ($sec,$min,$hour) = $dst ? localtime($t): gmtime($t);
56 my $buf = sprintf "%02d%02d%s", $hour, $min, ($dst) ? '' : 'Z';
60 # get a cluster format date (23-Jun-1998)
64 $t = defined $t ? $t : time;
66 my ($sec,$min,$hour,$mday,$mon,$year) = $dst ? localtime($t) : gmtime($t);
68 my $buf = sprintf "%2d-%s-%04d", $mday, $month[$mon], $year;
72 # return a cluster style date time
77 my $date = cldate($t, $dst);
78 my $time = ztime($t, $dst);
82 # return a unix date from a cluster date and time
87 my ($thisyear) = (gmtime)[5] + 1900;
89 return 0 unless $date =~ /^\s*(\d+)-(\w\w\w)-([12][90]\d\d)$/;
90 return 0 if $3 > 2036;
91 return 0 unless abs($thisyear-$3) <= 1;
93 return 0 unless $time =~ /^([012]\d)([012345]\d)Z$/;
94 $time = "$1:$2 +0000";
95 my $r = str2time("$date $time");
97 return $r == -1 ? undef : $r;
100 # turn a latitude in degrees into a string
104 my ($deg, $min, $let);
105 $let = $n >= 0 ? 'N' : 'S';
108 $min = int ((($n - $deg) * 60) + 0.5);
109 return "$deg $min $let";
112 # turn a longitude in degrees into a string
116 my ($deg, $min, $let);
117 $let = $n >= 0 ? 'E' : 'W';
120 $min = int ((($n - $deg) * 60) + 0.5);
121 return "$deg $min $let";
124 # turn a true into 'yes' and false into 'no'
128 return $n ? $main::yes : $main::no;
131 # provide a data dumpered version of the object passed
135 my $dd = new Data::Dumper([$value]);
138 $dd->Quotekeys($] < 5.005 ? 1 : 0);
139 $value = $dd->Dumpxs;
140 $value =~ s/([\r\n\t])/sprintf("%%%02X", ord($1))/eg;
141 $value =~ s/^\s*\[//;
142 $value =~ s/\]\s*$//;
147 # format a prompt with its current value and return it with its privilege
150 my ($line, $value) = @_;
151 my ($priv, $prompt, $action) = split ',', $line;
153 # if there is an action treat it as a subroutine and replace $value
155 my $q = qq{\$value = $action(\$value)};
157 } elsif (ref $value) {
160 $prompt = sprintf "%15s: %s", $prompt, $value;
161 return ($priv, $prompt);
164 # turn a hex field into printed hex
168 return sprintf '%X', $val;
171 # take an arg as a hash of call=>time pairs and print it
176 for (sort keys %$ref) {
177 $out .= "$_=$ref->{$_}, ";
184 # take an arg as an array list and print it
188 return ref $ref ? join(', ', @{$ref}) : $ref;
191 # take the arg as an array reference and print as a list of pairs
198 for ($i = 0; $i < @$ref; $i += 2) {
200 my $r2 = @$ref[$i+1];
203 chop $out; # remove last space
204 chop $out; # remove last comma
208 # take the arg as a hash reference and print it out as such
214 while (my ($k,$v) = each %$ref) {
215 $out .= "${k}=>$v, ";
217 chop $out; # remove last space
218 chop $out; # remove last comma
225 my @a = split /,/, $ref->field_prompt(shift);
226 my @b = split /,/, $ref->field_prompt(shift);
227 return lc $a[1] cmp lc $b[1];
230 # print all the fields for a record according to privilege
232 # The prompt record is of the format '<priv>,<prompt>[,<action>'
233 # and is expanded by promptf above
237 my $self = shift; # is a dxchan
238 my $ref = shift; # is a thingy with field_prompt and fields methods defined
240 my @fields = $ref->fields;
242 my $width = $self->width - 1;
245 foreach $field (sort {_sort_fields($ref, $a, $b)} @fields) {
246 if (defined $ref->{$field}) {
247 my ($priv, $ans) = promptf($ref->field_prompt($field), $ref->{$field});
249 if (length $ans > $width) {
250 my ($p, $a) = split /: /, $ans, 2;
251 my $l = (length $p) + 2;
252 my $al = ($width - 1) - $l;
254 while (length $a > $al ) {
255 ($bit, $a) = unpack "A$al A*", $a;
256 push @tmp, "$p: $bit";
259 push @tmp, "$p: $a" if length $a;
263 push @out, @tmp if ($self->priv >= $priv);
269 # generate a regex from a shell type expression
270 # see 'perl cookbook' 6.9
274 $in =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
275 return '^' . $in . "\$";
278 # read in a file into a string and return it.
279 # the filename can be split into a dir and file and the
280 # file can be in upper or lower case.
281 # there can also be a suffix
284 my ($dir, $file, $suffix) = @_;
289 $fn = "$dir/$f.$suffix";
292 $fn = "$dir/$file.$suffix";
305 my $fh = new IO::File $fn;
315 # write out a file in the format required for reading
316 # in via readfilestr, it expects the same arguments
317 # and a reference to an object
327 confess('no object to write in writefilestr') unless $obj;
328 confess('object not a reference in writefilestr') unless ref $obj;
332 $fn = "$dir/$f.$suffix";
335 $fn = "$dir/$file.$suffix";
348 my $fh = new IO::File ">$fn";
350 my $dd = new Data::Dumper([ $obj ]);
354 # $fh->print(@_) if @_ > 0; # any header comments, lines etc
355 $fh->print($dd->Dumpxs);
362 copy(@_) or return $!;
365 # remove leading and trailing spaces from an input string
374 # check that a field only has callsign characters in it
377 return $_[0] =~ m!^(?:[A-Z]{1,2}\d+ | \d[A-Z]{1,2}\d+) # basic prefix
378 (?:/(?:[A-Z]{1,2}\d+ | \d[A-Z]{1,2}\d+))? # / another one (possibly)
379 [A-Z]{1,4} # callsign letters
380 (?:/(?:[A-Z]{1,2}\d+ | \d[A-Z]{1,2}\d+))? # / another prefix possibly
381 (?:/[0-9A-Z]{1,2})? # /0-9A-Z+ possibly
382 (?:-\d{1,2})? # - nn possibly
386 # check that a field only has callsign characters in it but has more than the standard 3 callsign letters
389 return $_[0] =~ m!^(?:[A-Z]{1,2}\d+ | \d[A-Z]{1,2}\d+) # basic prefix
390 (?:/(?:[A-Z]{1,2}\d+ | \d[A-Z]{1,2}\d+))? # / another one (possibly)
391 [A-Z]{1,5} # callsign letters
392 (?:/(?:[A-Z]{1,2}\d+ | \d[A-Z]{1,2}\d+))? # / another prefix possibly
393 (?:/[0-9A-Z]{1,2})? # /0-9A-Z+ possibly
394 (?:-\d{1,2})? # - nn possibly
400 return $_[0] =~ m!^(?:[A-Z]{1,2}\d+ | \d[A-Z]{1,2}\d+)!x # basic prefix
404 # check that a PC protocol field is valid text
407 return undef unless length $_[0];
408 return undef if $_[0] =~ /[\x00-\x08\x0a-\x1f\x80-\x9f]/;
412 # check that a PC prot flag is fairly valid (doesn't check the difference between 1/0 and */-)
415 return $_[0] =~ /^[01\*\-]+$/;
418 # check that a thing is a frequency
421 return $_[0] =~ /^\d+(?:\.\d+)?$/;
424 # check that a thing is just digits
427 return $_[0] =~ /^[\d]+$/;
430 # does it look like a qra locator?
433 return $_[0] =~ /^[A-Ra-r][A-Ra-r]\d\d[A-Xa-x][A-Xa-x]$/;
436 # does it look like a valid lat/long
439 return $_[0] =~ /^\s*\d{1,2}\s+\d{1,2}\s*[NnSs]\s+1?\d{1,2}\s+\d{1,2}\s*[EeWw]\s*$/;
442 # is it an ip address?
445 return $_[0] =~ /^\d+\.\d+\.\d+\.\d+$/ || $_[0] =~ /^[0-9a-f:]+$/;
448 # insert an item into a list if it isn't already there returns 1 if there 0 if not
454 return 1 if grep {$_ eq $item } @$list;
459 # delete an item from a list if it is there returns no deleted
466 @$list = grep {$_ ne $item } @$list;