Aviation.pm

filter: Statementstmt 13.6% 19 / 139 Branchbran 0.0% 0 / 104 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 35.2% 6 / 17 Podpod 20.0% 2 / 10 totaltotal 9.3% 27 / 289 CC 59 SCAR 46.4
CC
59
Cov
7%
tsd
306.0
said
90.0
greatcircle
72.0
CRAP 103.8
↑ Summary Weather.pm »
1 package Bot::BasicBot::Pluggable::Module::Aviation;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Bot::BasicBot::Pluggable::Module;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use base qw(Bot::BasicBot::Pluggable::Module);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Geo::WeatherNWS;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use LWP::Simple ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 0 ▶sub said {
Subroutine said: not called CC=9 SCAR=45.0
Pod documented
12 0 my ($self, $mess, $pri) = @_;
13  
14 0 my $line = $mess->{body};
15 0 my $who = $mess->{who};
16  
17 0 ▶ return unless ($pri == 2);
Branch: 1 branch
BranchTrueFalseTotal
unless $pri == 2 0 0 0
18  
19 0 ▶ if ($line =~ /^metar/i) { return $self->metar($line) }
Branch: 7 branches
BranchTrueFalseTotal
if ($line =~ /^metar/i) { } 0 0 0
elsif ($line =~ /^taf/i) { } 0 0 0
elsif ($line =~ /^great[-\s]?circle/i) { } 0 0 0
elsif ($line =~ /^tsd/i) { } 0 0 0
elsif ($line =~ /^zulutime/i) { } 0 0 0
elsif ($line =~ /^airport/i) { } 0 0 0
elsif ($line =~ /^aviation/i) { } 0 0 0
20 0 elsif ($line =~ /^taf/i) { return $self->taf($line) }
21 0 elsif ($line =~ /^great[-\s]?circle/i) { return $self->greatcircle($line) }
22 0 elsif ($line =~ /^tsd/i) { return $self->tsd($line) }
23 0 elsif ($line =~ /^zulutime/i) { return $self->zulutime($line) }
24 0 elsif ($line =~ /^airport/i) { return $self->airport($line) }
25 0 elsif ($line =~ /^aviation/i) { return $self->aviation($line) }
26  
27 0 return;
28  
29 }
30  
31 0 ▶sub help {
Subroutine help: not called CC=1 SCAR=6.9
Pod documented
32 0 return "My aviation-related functions are metar, taf, great-circle, tsd, zulutime, and airport. For help with any, ask me about '<function name> help";
33 }
34  
35 0 ▶sub _fix_icao {
Subroutine _fix_icao: not called CC=5 SCAR=34.0
36 0 my $site = uc(shift);
37  
38 # ICAO airport codes *can* contain numbers, despite earlier claims.
39 # Americans tend to use old FAA three-letter codes; luckily we can
40 # *usually* guess what they mean by prepending a 'K'. The original
41 # author, being Canadian apparently, displays similarl impeccable
42 # laziness.
43  
44 0 $site =~ s/[.?!]$//;
45 0 $site =~ s/\s+$//g;
46 return undef
47 0 ▶ unless $site =~ /^[\w\d]{3,4}$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $site =~ /^[\w\d]{3,4}$/ 0 0 0
48 0 ▶ $site = "C" . $site if length($site) == 3 && $site =~ /^Y/;
Branch: 1 branch
BranchTrueFalseTotal
if length $site == 3 and $site =~ /^Y/ 0 0 0
Condition0%0/3
length $site == 3 and $site =~ /^Y/
!ll&&!rl&&r
000
MC/DC0%0/2
length $site == 3 and $site =~ /^Y/
length $site == 3 $site =~ /^Y/
Truth table0%0/3
A and B
A= length $site == 3B= $site =~ /^Y/
ABresult
0X0
100
111
49 0 ▶ $site = "K" . $site if length($site) == 3;
Branch: 1 branch
BranchTrueFalseTotal
if length $site == 3 0 0 0
50  
51 0 return $site;
52 }
53  
54  
55 #
56 # METAR - current weather observation
57 #
58 0 ▶sub metar {
Subroutine metar: not called CC=5 SCAR=34.0
Pod undocumented
59 0 my ($self, $line) = @_;
60  
61 0 ▶ return unless $line =~ /\s*metar\s+(for\s+)?(.*)/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\s*metar\s+(for\s+)?(.*)/i 0 0 0
62
63 0 my $site = _fix_icao($2);
64 0 ▶ return "'$site' doesn't look like a valid ICAO airport identifier."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $site 0 0 0
65 unless defined $site;
66  
67 0 ▶ return "For observations, ask me 'metar <code>'. For information on decoding Aerodrome Weather Observations (METAR ), see http://www.avweb.com/toc/metartaf.html"
Branch: 1 branch
BranchTrueFalseTotal
if $site eq "HELP" 0 0 0
68 if ($site eq 'HELP');
69  
70 0 my $r = Geo::WeatherNWS->new();
71  
72 0 $r->getreport($site);
73  
74 0 ▶ return "Hrmm, I hit a problem - ".$r->{errortext} if $r->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"error"} 0 0 0
75  
76 0 return $r->{obs};
77  
78 }
79  
80 #
81 # TAF - terminal area (aerodrome) forecast
82 #
83 0 ▶sub taf {
Subroutine taf: not called CC=7 SCAR=40.3
Pod undocumented
84 0 my ($self, $line) = @_;
85  
86  
87 0 ▶ return unless $line =~ /\s*taf\s+(for\s+)?(.*)/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\s*taf\s+(for\s+)?(.*)/i 0 0 0
88  
89 0 my $site = _fix_icao($2);
90 0 ▶ return "'$site' doesn't look like a valid ICAO airport identifier."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $site 0 0 0
91 unless defined $site;
92  
93  
94 0 ▶ return "For a forecast, ask me 'taf <ICAO code>'. For information on decoding Terminal Area Forecasts, see http://www.avweb.com/toc/metartaf.html"
Branch: 1 branch
BranchTrueFalseTotal
if $site eq "HELP" 0 0 0
95 if ($site eq 'HELP');
96  
97 # god I hate CPAN some times, this code should be in GEO::Taf
98 0 my $content = LWP::Simple::get("http://weather.noaa.gov/cgi-bin/mgettaf.pl?cccc=$site");
99 0 ▶ return "I can't seem to retrieve data from weather.noaa.com right now."
Branch: 1 branch
BranchTrueFalseTotal
unless $content 0 0 0
100 unless $content;
101  
102 # extract TAF from equally verbose webpage
103 0 $content =~ m/($site( AMD)* \d+Z .*?)</s;
104 0 my $taf = $1;
105 0 $taf =~ s/\n//gm;
106 0 $taf =~ s/\s+/ /g;
107
108 0 my $taf_highlight_bold = $self->get("taf_highlight_bold");
109  
110
111 # Optionally highlight beginnings of parts of the forecast. Some
112 # find it useful, some find it obnoxious, so it's configurable. :-)
113 0 ▶ $taf =~ s/(FM\d+Z?|TEMPO \d+|BECMG \d+|PROB\d+)/\cB$1\cB/g if $taf_highlight_bold;
Branch: 1 branch
BranchTrueFalseTotal
if $taf_highlight_bold 0 0 0
114  
115  
116 # Sane?
117 0 ▶ return "I can't find any forecast for $site." if length($taf) < 10;
Branch: 1 branch
BranchTrueFalseTotal
if length $taf < 10 0 0 0
118  
119 0 return $taf;
120 }
121  
122 #
123 # greatcircle -- calculate great circle distance and heading between
124 #
125 0 ▶sub greatcircle {
Subroutine greatcircle: not called CC=8 SCAR=42.8
Pod undocumented
126 0 my ($self, $line) = @_;
127  
128 0 ▶ return "That doesn't look right. The 'great-circle' command takes two airport identifiers and returns the great circle distance and heading between them."
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^great-?circle\s+((from|between|for)\s+)?(\w+)\s+((and|to)\s)?(\w+)/i 0 0 0
129 unless $line =~ /^great-?circle\s+((from|between|for)\s+)?(\w+)\s+((and|to)\s)?(\w+)/i;
130  
131 # See metar part for explanation of this bit.
132 0 my $orig_apt = _fix_icao($3);
133 0 my $dest_apt = _fix_icao($6);
134  
135 0 ▶ return "$3 doesn't look like an ICAO code" unless $orig_apt;
Branch: 1 branch
BranchTrueFalseTotal
unless $orig_apt 0 0 0
136 0 ▶ return "$6 doesn't look like an ICAO code" unless $dest_apt;
Branch: 1 branch
BranchTrueFalseTotal
unless $dest_apt 0 0 0
137  
138 0 ▶ return "To get the great circle distance two airports ask me 'great-circle between <ICAO code> and <ICAO code>'"
Branch: 1 branch
BranchTrueFalseTotal
if $orig_apt eq "HELP" or $dest_apt eq "HELP" 0 0 0
Condition0%0/3
$orig_apt eq "HELP" or $dest_apt eq "HELP"
l!l&&r!l&&!r
000
MC/DC0%0/2
$orig_apt eq "HELP" or $dest_apt eq "HELP"
$orig_apt eq "HELP" $dest_apt eq "HELP"
Truth table0%0/3
A or B
A= $orig_apt eq "HELP"B= $dest_apt eq "HELP"
ABresult
1X1
011
000
139 if ($orig_apt eq 'HELP' or $dest_apt eq 'HELP');
140  
141 0 my $content = LWP::Simple::get("http://www8.landings.com/cgi-bin/nph-dist_apt?airport1=$orig_apt&airport2=$dest_apt");
142  
143 0 ▶ return "I can't seem to retrieve data from www.landings.com right now." unless $content;
Branch: 1 branch
BranchTrueFalseTotal
unless $content 0 0 0
144  
145 0 my $gcd;
146 0 ▶ if ($content =~ m/circle: ([.\d]+).*?, ([.\d]+).*?, ([.\d]+).*?heading: ([.\d]+)/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /circle: ([.\d]+).*?, ([.\d]+).*?, ([.\d]+).*?heading: ([.\d]+)/s) { } 0 0 0
147 0 $gcd = "Great-circle distance: $1 mi, $2 nm, $3 km, heading $4 degrees true";
148 } else {
149 0 $content =~ m/(No airport.*?database)/;
150 0 $gcd = $1;
151 }
152  
153 0 return $gcd;
154  
155  
156 }
157  
158 #
159 # tsd -- calculate time, speed, distance, given any two
160 #
161 0 ▶sub tsd {
Subroutine tsd: not called CC=17 SCAR=57.2
Pod undocumented
162 0 my ($self, $line) = @_;
163  
164 0 ▶ return "To solve time/speed/distance problems, substitute 'x' for " .
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /help/i 0 0 0
165 "the unknown value in 'tsd TIME SPEED DISTANCE'. For example, " .
166 "'tsd 3 x 200' will solve for the speed in at which you can travel " .
167 "200 mi in 3h." if $line =~ /help/i;
168  
169 0 my ($time, $speed, $distance) = ($line =~ /tsd\s+(\S+)\s+(\S+)\s+(\S+)/);
170  
171 0 my $error;
172 0 ▶ $error++ unless $time && $speed && $distance;
Branch: 1 branch
BranchTrueFalseTotal
unless $time and $speed and $distance 0 0 0
Condition0%0/6
$time and $speed and $distance
!ll&&!rl&&r
000
$time and $speed
!ll&&!rl&&r
000
MC/DC0%0/3
$time and $speed and $distance
$time $speed $distance
Truth table0%0/4
A and B and C
A= $timeB= $speedC= $distance
ABCresult
0XX0
10X0
1100
1111
173  
174 0 ▶ if ($time =~ /^[A-Za-z]$/) { # solve for time
Branch: 3 branches
BranchTrueFalseTotal
if ($time =~ /^[A-Za-z]$/) { } 0 0 0
elsif ($speed =~ /^[A-Za-z]$/) { } 0 0 0
elsif ($distance =~ /^[A-Za-z]$/) { } 0 0 0
175 0 ▶ $error++ unless $speed =~ /^[\d.]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $speed =~ /^[\d.]+$/ 0 0 0
176 0 ▶ $error++ unless $distance =~ /^[\d.]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $distance =~ /^[\d.]+$/ 0 0 0
177 0 ▶ return $distance / $speed unless $error;
Branch: 1 branch
BranchTrueFalseTotal
unless $error 0 0 0
178 }
179 elsif ($speed =~ /^[A-Za-z]$/) { # solve for speed
180 0 ▶ $error++ unless $time =~ /^[\d.]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $time =~ /^[\d.]+$/ 0 0 0
181 0 ▶ $error++ unless $distance =~ /^[\d.]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $distance =~ /^[\d.]+$/ 0 0 0
182 0 ▶ return $distance / $time unless $error;
Branch: 1 branch
BranchTrueFalseTotal
unless $error 0 0 0
183 }
184 elsif ($distance =~ /^[A-Za-z]$/) { # solve for distance
185 0 ▶ $error++ unless $speed =~ /^[\d.]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $speed =~ /^[\d.]+$/ 0 0 0
186 0 ▶ $error++ unless $time =~ /^[\d.]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $time =~ /^[\d.]+$/ 0 0 0
187 0 ▶ return $time * $speed unless $error;
Branch: 1 branch
BranchTrueFalseTotal
unless $error 0 0 0
188 }
189  
190 0 return "Your time/speed/distance problem looks incorrect. For help, try 'tsd help'.";
191  
192 }
193  
194 #
195 # zulutime -- return current UTC time
196 #
197 0 ▶sub zulutime {
Subroutine zulutime: not called CC=2 SCAR=17.9
Pod undocumented
198 0 my ($self, $line) = @_;
199 0 ▶ return "zulutime returns the time in DDHHMM format." if $line =~ /help/i;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /help/i 0 0 0
200 0 return sprintf('%02d%02d%02dZ', reverse((gmtime())[1..3]));
201 }
202  
203 0 ▶sub airport {
Subroutine airport: not called CC=4 SCAR=30.0
Pod undocumented
204 0 my ($self, $line) = @_;
205
206 0 ▶ return "That doesn't look right. Try 'airport code for CITY' or 'airport name for CODE' instead."
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /airport\s+(name|code|id)s?\s+(for\s+)?(.*)/i 0 0 0
207 unless $line =~ (/airport\s+(name|code|id)s?\s+(for\s+)?(.*)/i);
208  
209 0 ▶ my $function = lc($1); $function = 'code' if $function eq 'id';
Branch: 1 branch
BranchTrueFalseTotal
if $function eq "id" 0 0 0
210 0 my $query = $3;
211  
212 0 ▶ return $self->airport_name($query) if $function eq 'name';
Branch: 1 branch
BranchTrueFalseTotal
if $function eq "name" 0 0 0
213 0 return $self->airport_code($query);
214 }
215  
216  
217 0 ▶sub airport_name {
Subroutine airport_name: not called CC=6 SCAR=37.4
Pod undocumented
218 0 my ($self, $query) = @_;
219  
220 0 my $code = _fix_icao($query);
221  
222 0 ▶ return "That doesn't look like a valid ICAO airport identifier. (Perhaps you mean 'airport code for $query'?)"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 0 0
223 unless defined $code;
224  
225 0 my $content = LWP::Simple::get("http://www8.landings.com/cgi-bin/nph-search_apt?1=$code&max_ret=1");
226  
227 0 my @apt_lines = split (/\n/, $content);
228  
229 0 my $pnext = 0;
230 0 my $response = '';
231  
232 0 foreach (@apt_lines) {
233 # skip over entries without ICAO idents (ICAO: n/a)
234 0 ▶ if (/\(ICAO: <b>[^n]/) { $response .= "$_, "; $pnext = 1; }
Branch: 2 branches
BranchTrueFalseTotal
if (/\(ICAO: <b>[^n]/) { } 0 0 0
elsif ($pnext) { } 0 0 0
235 0 elsif ($pnext) { $response .= $_; $pnext = 0; }
236 }
237  
238 0 $response =~ s/(<.*?>)+/ /g; # naive, but works in *this* case.
239 0 $response =~ s/.*?\) //; # strip (ICAO: foo) bit
240 0 $response =~ s/\s+/ /g;
241 0 $response =~ s/ ,/,/g; # pet peeve.
242  
243 0 $response = decode_entities($response);
244  
245 0 ▶ return "I can't find an airport for $query." unless $response;
Branch: 1 branch
BranchTrueFalseTotal
unless $response 0 0 0
246 0 return "$query is $response";
247  
248 }
249  
250 0 ▶sub airport_code {
Subroutine airport_code: not called CC=5 SCAR=34.0
Pod undocumented
251 0 my ($self, $query) = @_;
252  
253 0 $query =~ s/[.?!]$//;
254 0 $query =~ s/\s+$//;
255  
256 # Grab airport data from Web.
257 0 my $url = "http://www8.landings.com/cgi-bin/nph-search_apt?6=$query&max_ret=100";
258 0 my $content = LWP::Simple::get($url);
259  
260 # If it can't find it, assume luser error :-)
261 0 ▶ return "I can't seem to access my airport data -- perhaps try again later."
Branch: 1 branch
BranchTrueFalseTotal
unless $content 0 0 0
262 unless $content;
263  
264 # extract csv-format airport data from incredibly and painfully verbose webpage
265 0 my @apt_lines = split (/\n/, $content);
266  
267 0 my $response = '';
268 0 foreach (@apt_lines) {
269 0 print "$_";
270 0 ▶ $response .= "$1 " if m|\(ICAO: <b>([^n]+?)</b>|;
Branch: 1 branch
BranchTrueFalseTotal
if m[\(ICAO: <b>([^n]+?)</b>] 0 0 0
271 }
272  
273 0 $response =~ s/(<.*?>)+/ /g; # naive, but works in *this* case.
274  
275 0 ▶ return "I can't find an airport code for $query." unless $response;
Branch: 1 branch
BranchTrueFalseTotal
unless $response 0 0 0
276 0 return "$query may be: $response";
277
278 }
279  
280  
281 1 1;
282  
283 =head1 NAME
284  
285 Bot::BasicBot::Pluggable::Module::Aviation - module for various flight-planning bits
286  
287  
288 =head1 IRC USAGE
289  
290 metar for <ICAO code>
291 taf for <ICAO code>
292 airport name for <ICAO code>
293 airport code for <airport name>
294 great-circle from <ICAO code> to <ICAO code>
295 zulutime
296  
297 =head1 AUTHOR
298  
299 Simon Wistow, <simon@thegestalt.org>
300  
301 based on code by
302  
303 Rich Lafferty <rich@alcor.concordia.ca> (original version)
304 Kevin Lenzo <lenzo@cs.cmu.edu> (fixes)
305 Lazarus Long <lazarus@frontiernet.net> (fixes)
306  
307  
308 =head1 COPYRIGHT
309  
310 Copyright 2005, Simon Wistow
311  
312 Distributed under the same terms as Perl itself.
313  
314 =head1 SEE ALSO
315  
316  
317 =cut
318  
↑ Summary Weather.pm »