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