update sh/qrz and start get/keps
[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 # Then made asyncronous...
8 #
9 # Copyright (c) 2001-2013 Dirk Koopman G1TLH
10 #
11
12 use vars qw (%allowed);
13
14 %allowed = qw(call 1 fname 1 name 1 addr2 1 state 1 country 1 lat 1 lon 1 county 1 moddate 1 qslmgr 1 grid 1 );
15
16 sub _send
17 {
18         my $conn = shift;
19         my $msg = shift;
20         my $dxchan = shift;
21
22         my ($tag, $data) = $msg =~ m|^\s*<(\w+)>(.*)</|;
23         if ($allowed{$tag}) {
24                 my $prefix = $conn->{prefix} || ' ';
25                 $dxchan->send($prefix . sprintf("%-10s: $data", $tag));
26         }
27 }
28
29 sub _on_disc
30 {
31         my $conn = shift;
32         my $dxchan = shift;
33         $dxchan->send("Data provided by www.qrz.com");
34 }
35
36 sub filter
37 {
38         my $conn = shift;
39         my $msg = shift;
40         my $dxchan = shift;
41
42         my $state = $conn->{state};
43         
44         dbg("qrz: $state $msg") if isdbg('qrz');
45
46         if ($state eq 'blank') { 
47                 if ($msg =~ /^<Callsign>/) {
48                         $conn->{state} = 'go';
49                 } elsif ($msg =~ /^<Error>/) {
50                         _send($conn, $msg, $dxchan);
51                 }
52         } elsif ($state eq 'go') {
53             if ($msg =~ m|</Callsign>|) {
54                         $conn->{state} = 'skip';
55                         return;
56                 }
57 #               $DB::single = 1;
58                 _send($conn, $msg, $dxchan);
59         }
60 }
61
62 sub handle
63 {
64         my ($self, $line) = @_;
65         my $call = $self->call;
66         my @out;
67
68         return (1, $self->msg('e24')) unless $Internet::allow;
69         return (1, "SHOW/QRZ <callsign>, e.g. SH/QRZ g1tlh") unless $line;
70         my $target = $Internet::qrz_url || 'xmldata.qrz.com';
71         my $port = 80;
72         my $path = qq{/xml/current/?callsign=$line;username=$Internet::qrz_uid;password=$Internet::qrz_pw;agent=dxspider};
73         dbg("qrz: $path") if isdbg('qrz');
74
75         Log('call', "$call: show/qrz \U$line");
76         my $conn = AsyncMsg->get($self, $target, $port, $path, filter=>\&filter, prefix=>'qrz> ', on_disc=>\&_on_disc);
77         if ($conn) {
78                 $conn->{state} = 'blank';
79                 push @out, $self->msg('m21', "show/qrz");
80         } else {
81                 push @out, $self->msg('e18', 'QRZ.com');
82         }
83
84         return (1, @out);
85 }