Allow synonyms for localhost
[spider.git] / cmd / dx.pl
1 #
2 # the DX command
3 #
4 # this is where the fun starts!
5 #
6 # Copyright (c) 1998 Dirk Koopman G1TLH
7 #
8 #
9 #
10
11 my ($self, $line) = @_;
12 my @f = split /\s+/, $line, 3;
13 my $spotter = $self->call;
14 my $spotted;
15 my $freq;
16 my @out;
17 my $valid = 0;
18 my $localonly;
19 my $oline = $line;
20
21 #$DB::single=1;
22
23 return (1, $self->msg('e5')) if $self->remotecmd || $self->inscript;
24 return (1, $self->msg('e28')) unless $self->isregistered;
25
26
27 my $addr = DXCommandmode::alias_localhost($self->hostname || '127.0.0.1');
28
29 Log('cmd', "$self->{call}|$addr|dx|$line");
30
31 my @bad;
32 if (@bad = BadWords::check($line)) {    
33         $self->badcount(($self->badcount||0) + @bad);
34         LogDbg('DXCommand', "$self->{call} swore: $line (with words:" . join(',', @bad) . ")");
35         $localonly++; 
36 }
37
38 # do we have at least two args?
39 return (1, $self->msg('dx2')) unless @f >= 2;
40
41 # as a result of a suggestion by Steve K9AN, I am changing the syntax of
42 # 'spotted by' things to "dx by g1tlh <freq> <call>" <freq> and <call>
43 # can be in any order
44
45 if ($f[0] =~ /^by$/i) {
46         return (1, $self->msg('e5')) unless $main::allowdxby || $self->priv > 1;
47     $spotter = uc $f[1];
48     $line =~ s/^\s*$f[0]\s+$f[1]\s+//;
49         @f = split /\s+/, $line, 3; 
50         return (1, $self->msg('dx2')) unless @f >= 2;
51 }
52
53 my $ipaddr;
54 @f = split /\s+/, $line, 3;
55 if ($f[0] eq 'ip') {
56         return (1, $self->msg('e5')) unless $spotter &&  $self->priv > 1;
57         if (is_ipaddr($f[1])) {
58                 $ipaddr = $f[1];
59         } else {
60                 return (1, $self->msg('dx4', $f[1]));
61         }
62         $line =~ s/^\s*$f[0]\s+$f[1]\s+//;
63         @f = split /\s+/, $line, 3;
64 }
65
66
67 # get the freq and callsign either way round
68 if (is_freq($f[1]) && $f[0] =~ m{^[\w\d]+(?:/[\w\d]+){0,2}$}) {
69         $spotted = uc $f[0];
70         $freq = $f[1];
71 } elsif (is_freq($f[0]) && $f[1] =~ m{^[\w\d]+(?:/[\w\d]+){0,2}$}) {
72     $freq = $f[0];
73         $spotted = uc $f[1];
74 } else {
75         return (1, $self->msg('dx3'));
76 }
77 $line =~ s/^\s*$f[0]//;
78 $line =~ s/^\s*$f[1]//;
79 $line =~ unpad($line);
80 $line =~ s/\t+/ /g;                             # do this here because it needs to be stopped ASAP!
81 $line ||= ' ';
82
83 if ($self->conn && $self->conn->peerhost) {
84         $ipaddr ||= $addr; # force a PC61 
85 } elsif ($self->inscript) {
86         $ipaddr = "script";
87 }
88
89 # check some other things
90 # remove ssid from calls
91 my $spotternoid = basecall($spotter);
92 my $callnoid = basecall($self->{call});
93
94 #$DB::single = 1;
95
96 if ($DXProt::baddx->in($spotted)) {
97         $localonly++; 
98 }
99 if ($DXProt::badspotter->in($spotternoid) || $self->badip) { 
100         LogDbg('DXCommand', "badspotter $spotternoid as $spotter ($oline) from $addr");
101         $localonly++; 
102 }
103
104 dbg "spotter $spotternoid/$callnoid\n";
105
106 if (($spotted =~ /$spotternoid/ || $spotted =~ /$callnoid/) && $freq < $Spot::minselfspotqrg) {
107         LogDbg('DXCommand', "$spotternoid/$callnoid trying to self spot below ${Spot::minselfspotqrg}KHz ($oline) from $addr, not passed on to cluster");
108         $localonly++;
109 }
110
111 # bash down the list of bands until a valid one is reached
112 my $bandref;
113 my @bb;
114 my $i;
115
116 # first in KHz
117 L1:
118 foreach $bandref (Bands::get_all()) {
119         @bb = @{$bandref->band};
120         for ($i = 0; $i < @bb; $i += 2) {
121                 if ($freq >= $bb[$i] && $freq <= $bb[$i+1]) {
122                         $valid = 1;
123                         last L1;
124                 }
125         }
126 }
127
128 unless ($valid) {
129
130         # try again in MHZ 
131         $freq = $freq * 1000 if $freq;
132
133  L2:
134     foreach $bandref (Bands::get_all()) {
135                 @bb = @{$bandref->band};
136                 for ($i = 0; $i < @bb; $i += 2) {
137                         if ($freq >= $bb[$i] && $freq <= $bb[$i+1]) {
138                                 $valid = 1;
139                                 last L2;
140                         }
141                 }
142         }
143 }
144
145
146 push @out, $self->msg('dx1', $freq) unless $valid;
147
148 # check we have a callsign :-)
149 if ($spotted le ' ') {
150         push @out, $self->msg('dx2');
151         $valid = 0;
152 }
153
154 return (1, @out) unless $valid;
155
156 # Store it here (but only if it isn't baddx)
157 my $t = (int ($main::systime/60)) * 60;
158 return (1, $self->msg('dup')) if Spot::dup($freq, $spotted, $t, $line, $spotter, $main::mycall);
159 my @spot = Spot::prepare($freq, $spotted, $t, $line, $spotter, $main::mycall, $ipaddr);
160
161 #$DB::single = 1;
162
163 if ($freq =~ /^69/ || $localonly) {
164
165         # heaven forfend that we get a 69Mhz band :-)
166         if ($freq =~ /^69/) {
167                 $self->badcount(($self->badcount||0) + 1);
168         }
169
170         $self->dx_spot(undef, undef, @spot);
171
172         return (1);
173 } else {
174         # send orf to the users
175         $ipaddr ||= $main::mycall;      # emergency backstop
176         my $spot = DXProt::pc61($spotter, $freq, $spotted, unpad($line),  $ipaddr);
177         
178         $self->dx_spot(undef, undef, @spot);
179         if ($self->isslugged) {
180                 push @{$self->{sluggedpcs}}, [61, $spot, \@spot];
181         } else {
182                 # store in spots database 
183                 Spot::add(@spot);
184                 DXProt::send_dx_spot($self, $spot, @spot);
185         }
186 }
187
188 return (1, @out);
189
190
191