make sh/contest async
[spider.git] / cmd / show / qrz.pl
1 #
2 # Query the QRZ Database server for a callsign
3 #
4 # from an idea by Steve Franke K9AN and information from Angel EA7WA
5 # and finally (!) modified to use the XML interface
6 #
7 # Copyright (c) 2001-2009 Dirk Koopman G1TLH
8 #
9 my ($self, $line) = @_;
10 my @list = split /\s+/, $line;                # generate a list of callsigns
11 my $l;
12 my $call = $self->call;
13 my @out;
14
15 return (1, $self->msg('e24')) unless $Internet::allow;
16 return (1, "SHOW/QRZ <callsign>, e.g. SH/QRZ g1tlh") unless @list;
17 my $target = $Internet::http_proxy || $Internet::qrz_url || 'xml.qrz.com';
18 my $port = $Internet::http_proxy_port || 80;
19 my $url = '';
20 $url = 'http://' . ($Internet::qrz_url || 'xml.qrz.com') if $Internet::http_proxy;
21
22 foreach $l (@list) {
23
24         my $host = $url?$url:$target;
25         my $s = "$url/xml?callsign=$l;username=$Internet::qrz_uid;password=$Internet::qrz_pw;agent=dxspider";
26         if (isdbg('qrz')) {
27                 dbg("qrz: $host");
28                 dbg("qrz: $s");
29         }
30
31         Log('call', "$call: show/qrz \U$l");
32         push @out,  $self->msg('http1', 'qrz.com', "\U$l");
33
34         $self->http_get($host, $s, sub
35                                         {
36                                                 my ($response, $header, $body) = @_;
37                                                 my @out;
38
39                                                 if (isdbg('qrz')) {
40                                                         dbg("qrz response: $response");
41                                                         dbg("qrz body: $body");
42                                                 }
43                                                 if ($response =~ /^5/) {
44                                                         push @out, $self->msg('e18',"qrz.com $!");
45                                                 } else {
46                                                         Log('call', "$call: show/qrz \U$body");
47                                                         my $state = "blank";
48                                                         foreach my $result (split /\r?\n/, $body) {
49                                                                 dbg("qrz: $result") if isdbg('qrz') && $result;
50                                                                 if ($state eq 'blank' && $result =~ /^<Callsign>/i) {
51                                                                         $state = 'go';
52                                                                 } elsif ($state eq 'go') {
53                                                                         next if $result =~ m|<user>|;
54                                                                         next if $result =~ m|<u_views>|;
55                                                                         next if $result =~ m|<locref>|;
56                                                                         next if $result =~ m|<ccode>|;
57                                                                         next if $result =~ m|<dxcc>|;
58                                                                         last if $result =~ m|</Callsign>|;
59                                                                         my ($tag, $data) = $result =~ m|^\s*<(\w+)>(.*)</|;
60                                                                         push @out, sprintf "%10s: $data", $tag;
61                                                                 }
62                                                         }
63                                                         if (@out) {
64                                                                 unshift @out, $self->msg('http2', "show/qrz \U$l");
65                                                         } else {
66                                                                 push @out, $self->msg('e3', 'show/qrz', uc $l);
67                                                         }
68                                                 }
69                                                 $self->send_ans(@out);
70                                         }
71                                    );
72 }
73
74 return (1, @out);