remove shadow $i
[spider.git] / perl / DXProtout.pm
1 #!/usr/bin/perl
2 #
3 # This module impliments the outgoing PCxx generation routines
4 #
5 # These are all the namespace of DXProt and are separated for "clarity"
6 #
7 # Copyright (c) 1998 Dirk Koopman G1TLH
8 #
9 # $Id$
10
11
12 package DXProt;
13
14 @ISA = qw(DXProt DXChannel);
15
16 use DXUtil;
17 use DXM;
18 use DXDebug;
19
20 use strict;
21
22 #
23 # All the PCxx generation routines
24 #
25
26 # create a talk string ($from, $to, $via, $text)
27 sub pc10
28 {
29         my ($from, $to, $via, $text) = @_;
30         my ($user1, $user2);
31         if ($via && $via ne $to) {
32                 $user1 = $via;
33                 $user2 = $to;
34         } else {
35                 $user2 = ' ';
36                 $user1 = $to;
37         }
38         $text = unpad($text);
39         $text = ' ' unless $text && length $text > 0;
40         $text =~ s/\^/%5E/g;
41         return "PC10^$from^$user1^$text^*^$user2^$main::mycall^~";  
42 }
43
44 # create a dx message (call, freq, dxcall, text) 
45 sub pc11
46 {
47         my ($mycall, $freq, $dxcall, $text) = @_;
48         my $hops = get_hops(11);
49         my $t = time;
50         $text = ' ' if !$text;
51         $text =~ s/\^/%5E/g;
52         return sprintf "PC11^%.1f^$dxcall^%s^%s^$text^$mycall^$main::mycall^$hops^~", $freq, cldate($t), ztime($t);
53 }
54
55 # create an announce message
56 sub pc12
57 {
58         my ($call, $text, $tonode, $sysop, $wx) = @_;
59         my $hops = get_hops(12);
60         $sysop = ' ' if !$sysop;
61         $text = ' ' if !$text;
62         $wx = '0' if !$wx;
63         $tonode = '*' if !$tonode;
64         $text =~ s/\^/%5E/g;
65         return "PC12^$call^$tonode^$text^$sysop^$main::mycall^$wx^$hops^~";
66 }
67
68 #
69 # add one or more users (I am expecting references that have 'call', 
70 # 'confmode' & 'here' method) 
71 #
72 # this will create a list of PC16 with up pc16_max_users in each
73 # called $self->pc16(..)
74 #
75 sub pc16
76 {
77         my $self = shift;
78         my @out;
79         my $i;
80
81         for ($i = 0; @_; ) {
82                 my $str = "PC16^$self->{call}";
83                 for ( ; @_ && $i < $DXProt::pc16_max_users; $i++) {
84                         my $ref = shift;
85                         $str .= sprintf "^%s %s %d", $ref->call, $ref->confmode ? '*' : '-', $ref->here;
86                 }
87                 $str .= sprintf "^%s^", get_hops(16);
88                 push @out, $str;
89                 $i = 0;
90         }
91         return (@out);
92 }
93
94 # remove a local user
95 sub pc17
96 {
97         my ($self, $ref) = @_;
98         my $hops = get_hops(17);
99         return "PC17^$ref->{call}^$self->{call}^$hops^";
100 }
101
102 # Request init string
103 sub pc18
104 {
105         return "PC18^DXSpider Version: $main::version Build: $main::build^$DXProt::myprot_version^";
106 }
107
108 #
109 # add one or more nodes 
110
111 sub pc19
112 {
113         my $self = shift;
114         my @out;
115         my $i;
116         
117
118         for ($i = 0; @_; ) {
119                 my $str = "PC19";
120                 for (; @_ && $i < $DXProt::pc19_max_nodes; $i++) {
121                         my $ref = shift;
122                         my $here = $ref->{here} ? '1' : '0';
123                         my $confmode = $ref->{confmode} ? '1' : '0';
124                         $str .= "^$here^$ref->{call}^$confmode^$ref->{pcversion}";
125                 }
126                 $str .= sprintf "^%s^", get_hops(19);
127                 push @out, $str;
128                 $i = 0;
129         }
130         return @out;
131 }
132
133 # end of Rinit phase
134 sub pc20
135 {
136         return 'PC20^';
137 }
138
139 # delete a node
140 sub pc21
141 {
142         my ($call, $reason) = @_;
143         my $hops = get_hops(21);
144         $reason = "Gone." if !$reason;
145         return "PC21^$call^$reason^$hops^";
146 }
147
148 # end of init phase
149 sub pc22
150 {
151         return 'PC22^';
152 }
153
154 # here status
155 sub pc24
156 {
157         my $self = shift;
158         my $call = $self->call;
159         my $flag = $self->here ? '1' : '0';
160         my $hops = get_hops(24);
161   
162         return "PC24^$call^$flag^$hops^";
163 }
164
165
166 # create a merged dx message (freq, dxcall, t, text, spotter, orig-node) 
167 sub pc26
168 {
169         my ($freq, $dxcall, $t, $text, $spotter, $orignode) = @_;
170         $text = ' ' unless $text;
171         $orignode = $main::mycall unless $orignode;
172         return sprintf "PC26^%.1f^$dxcall^%s^%s^$text^$spotter^$orignode^ ^~", $freq, cldate($t), ztime($t);
173 }
174
175 # create a merged WWV spot (logger, t, sfi, a, k, forecast, orig-node)
176 sub pc27
177 {
178         my ($logger, $t, $sfi, $a, $k, $forecast, $orignode) = @_;
179         return sprintf "PC27^%s^%-2.2s^$sfi^$a^$k^$forecast^$logger^$orignode^ ^~", cldate($t), ztime($t);
180 }
181
182 # message start (fromnode, tonode, to, from, t, private, subject, origin)
183 sub pc28
184 {
185         my ($tonode, $fromnode, $to, $from, $t, $private, $subject, $origin, $rr) = @_;
186         my $date = cldate($t);
187         my $time = ztime($t);
188         $private = $private ? '1' : '0';
189         $rr = $rr ? '1' : '0';
190         return "PC28^$tonode^$fromnode^$to^$from^$date^$time^$private^$subject^ ^5^$rr^ ^$origin^~";
191 }
192
193 # message text (from and to node same way round as pc29)
194 sub pc29 
195 {
196         my ($fromnode, $tonode, $stream, $text) = @_;
197         $text = ' ' unless $text && length $text > 0;
198         $text =~ s/\^/%5E/og;                   # remove ^
199         return "PC29^$fromnode^$tonode^$stream^$text^~";
200 }
201
202 # subject acknowledge (will have to and from node reversed to pc28)
203 sub pc30
204 {
205         my ($fromnode, $tonode, $stream) = @_;
206         return "PC30^$fromnode^$tonode^$stream^";
207 }
208
209 # acknowledge this tranche of lines (to and from nodes reversed to pc29 and pc28
210 sub pc31
211 {
212         my ($fromnode, $tonode, $stream) = @_;
213         return "PC31^$fromnode^$tonode^$stream^";
214 }
215
216 #  end of message from the sending end (pc28 node order)
217 sub pc32
218 {
219         my ($fromnode, $tonode, $stream) = @_;
220         return "PC32^$fromnode^$tonode^$stream^";
221 }
222
223 # acknowledge end of message from receiving end (opposite pc28 node order)
224 sub pc33
225 {
226         my ($fromnode, $tonode, $stream) = @_;
227         return "PC33^$fromnode^$tonode^$stream^";
228 }
229
230 # remote cmd send
231 sub pc34
232 {
233         my($fromnode, $tonode, $msg) = @_;
234         return "PC34^$tonode^$fromnode^$msg^~";
235 }
236
237 # remote cmd reply
238 sub pc35
239 {
240         my($fromnode, $tonode, $msg) = @_;
241         return "PC35^$tonode^$fromnode^$msg^~";
242 }
243
244 # send all the DX clusters I reckon are connected
245 sub pc38
246 {
247         my @nodes = map { ($_->dxchan && $_->dxchan->isolate) ? () : $_->call } DXNode->get_all();
248         return "PC38^" . join(',', @nodes) . "^~";
249 }
250
251 # tell the local node to discconnect
252 sub pc39
253 {
254         my ($call, $reason) = @_;
255         my $hops = get_hops(39);
256         $reason = "Gone." if !$reason;
257         return "PC39^$call^$reason^$hops^";
258 }
259
260 # cue up bulletin or file for transfer
261 sub pc40
262 {
263         my ($to, $from, $fn, $bull) = @_;
264         $bull = $bull ? '1' : '0';
265         return "PC40^$to^$from^$fn^$bull^5^";
266 }
267
268 # user info
269 sub pc41
270 {
271         my ($call, $sort, $info) = @_;
272         my $hops = get_hops(41);
273         $sort = $sort ? "$sort" : '0';
274         return "PC41^$call^$sort^$info^$hops^~";
275 }
276
277 # abort message
278 sub pc42
279 {
280         my ($fromnode, $tonode, $stream) = @_;
281         return "PC42^$fromnode^$tonode^$stream^";
282 }
283
284 # remote db request
285 sub pc44
286 {
287         my ($fromnode, $tonode, $stream, $db, $req, $call) = @_;
288         $db = uc $db;
289         return "PC44^$tonode^$fromnode^$stream^$db^$req^$call^";
290 }
291
292 # remote db data
293 sub pc45
294 {
295         my ($fromnode, $tonode, $stream, $data) = @_;
296         return "PC45^$tonode^$fromnode^$stream^$data^";
297 }
298
299 # remote db data complete
300 sub pc46
301 {
302         my ($fromnode, $tonode, $stream) = @_;
303         return "PC46^$tonode^$fromnode^$stream^";
304 }
305
306 # bull delete
307 sub pc49
308 {
309         my ($from, $subject) = @_;
310         my $hops = get_hops(49);
311         return "PC49^$from^$subject^$hops^~";
312 }
313
314 # periodic update of users, plus keep link alive device (always H99)
315 sub pc50
316 {
317         my $n = shift;
318         $n = 0 unless $n >= 0;
319         return "PC50^$main::mycall^$n^H99^";
320 }
321
322 # generate pings
323 sub pc51
324 {
325         my ($to, $from, $val) = @_;
326         return "PC51^$to^$from^$val^";
327 }
328
329 # clx remote cmd send
330 sub pc84
331 {
332         my($fromnode, $tonode, $call, $msg) = @_;
333         return "PC84^$tonode^$fromnode^$call^$msg^~";
334 }
335
336 # clx remote cmd reply
337 sub pc85
338 {
339         my($fromnode, $tonode, $call, $msg) = @_;
340         return "PC85^$tonode^$fromnode^$call^$msg^~";
341 }
342
343 1;
344 __END__
345
346
347