move stuff around and try to get pc59 handling/generation more correct
[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 use vars qw($VERSION $BRANCH);
23 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
24 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/  || (0,0));
25 $main::build += $VERSION;
26 $main::branch += $BRANCH;
27
28 use vars qw($sentencelth $use_newroute);
29
30 $sentencelth = 180;
31 $use_newroute = 1;
32  
33 #
34 # All the PCxx generation routines
35 #
36
37 # create a talk string ($from, $to, $via, $text)
38 sub pc10
39 {
40         my ($from, $to, $via, $text, $origin) = @_;
41         my ($user1, $user2);
42         if ($via && $via ne $to) {
43                 $user1 = $via;
44                 $user2 = $to;
45         } else {
46                 $user2 = ' ';
47                 $user1 = $to;
48         }
49         $origin ||= $main::mycall;
50         $text = unpad($text);
51         $text = ' ' unless $text && length $text > 0;
52         $text =~ s/\^/%5E/g;
53         return "PC10^$from^$user1^$text^*^$user2^$origin^~";  
54 }
55
56 # create a dx message (call, freq, dxcall, text) 
57 sub pc11
58 {
59         my ($mycall, $freq, $dxcall, $text) = @_;
60         my $hops = get_hops(11);
61         my $t = time;
62         $text = ' ' if !$text;
63         $text =~ s/\^/%5E/g;
64         return sprintf "PC11^%.1f^$dxcall^%s^%s^$text^$mycall^$main::mycall^$hops^~", $freq, cldate($t), ztime($t);
65 }
66
67 # create an announce message
68 sub pc12
69 {
70         my ($call, $text, $tonode, $sysop, $wx, $origin) = @_;
71         my $hops = get_hops(12);
72         $origin ||= $main::mycall;
73         $sysop ||= ' ';
74         $text ||= ' ';
75         $wx ||= '0';
76         $tonode ||= '*';
77         $text =~ s/\^/%5E/g;
78         return "PC12^$call^$tonode^$text^$sysop^$origin^$wx^$hops^~";
79 }
80
81 #
82 # add one or more users (I am expecting references that have 'call', 
83 # 'conf' & 'here' method) 
84 #
85 # this will create a list of PC16 with up pc16_max_users in each
86 # called $self->pc16(..)
87 #
88 sub pc16
89 {
90         my $node = shift;
91         my $ncall = $node->call;
92         my @out;
93
94         my $s = "";
95         for (@_) {
96                 next unless $_;
97                 my $ref = $_;
98                 my $str = sprintf "^%s %s %d", $ref->call, $ref->conf ? '*' : '-', $ref->here;
99                 if (length($s) + length($str) > $sentencelth) {
100                         push @out, "PC16^$ncall" . $s . sprintf "^%s^", get_hops(16);
101                         $s = "";
102                 }
103                 $s .= $str;
104         }
105         push @out, "PC16^$ncall" . $s . sprintf "^%s^", get_hops(16);
106         return @out;
107 }
108
109 # remove a local user
110 sub pc17
111 {
112         my @out;
113         while (@_) {
114                 my $node = shift;
115                 my $ref = shift;
116                 my $hops = get_hops(17);
117                 my $ncall = $node->call;
118                 my $ucall = $ref->call;
119                 push @out, "PC17^$ucall^$ncall^$hops^"; 
120         }
121         return @out;
122 }
123
124 # Request init string
125 sub pc18
126 {
127         my $nr = $use_newroute ? ' !NRt' : '';
128         return "PC18^DXSpider Version: $main::version Build: $main::build$nr^$DXProt::myprot_version^";
129 }
130
131 #
132 # add one or more nodes 
133
134 sub pc19
135 {
136         my @out;
137         my @in;
138
139         my $s = "";
140         for (@_) {
141                 next unless $_;
142                 my $ref = $_;
143                 my $call = $ref->call;
144                 my $here = $ref->here;
145                 my $conf = $ref->conf;
146                 my $version = $ref->version;
147                 my $str = "^$here^$call^$conf^$version";
148                 if (length($s) + length($str) > $sentencelth) {
149                         push @out, "PC19" . $s . sprintf "^%s^", get_hops(19);
150                         $s = "";
151                 }
152                 $s .= $str;
153         }
154         push @out, "PC19" . $s . sprintf "^%s^", get_hops(19);
155         return @out;
156 }
157
158 # end of Rinit phase
159 sub pc20
160 {
161         return 'PC20^';
162 }
163
164 # delete a node
165 sub pc21
166 {
167         my @out;
168         while (@_) {
169                 my $node = shift;
170                 my $hops = get_hops(21);
171                 my $call = $node->call;
172                 push @out, "PC21^$call^Gone^$hops^";
173         }
174         return @out;
175 }
176
177 # end of init phase
178 sub pc22
179 {
180         return 'PC22^';
181 }
182
183 # here status
184 sub pc24
185 {
186         my $self = shift;
187         my $call = $self->call;
188         my $flag = $self->here ? '1' : '0';
189         my $hops = shift || get_hops(24);
190   
191         return "PC24^$call^$flag^$hops^";
192 }
193
194
195 # create a merged dx message (freq, dxcall, t, text, spotter, orig-node) 
196 sub pc26
197 {
198         my ($freq, $dxcall, $t, $text, $spotter, $orignode) = @_;
199         $text = ' ' unless $text;
200         $orignode = $main::mycall unless $orignode;
201         return sprintf "PC26^%.1f^$dxcall^%s^%s^$text^$spotter^$orignode^ ^~", $freq, cldate($t), ztime($t);
202 }
203
204 # create a merged WWV spot (logger, t, sfi, a, k, forecast, orig-node)
205 sub pc27
206 {
207         my ($logger, $t, $sfi, $a, $k, $forecast, $orignode) = @_;
208         return sprintf "PC27^%s^%-2.2s^$sfi^$a^$k^$forecast^$logger^$orignode^ ^~", cldate($t), ztime($t);
209 }
210
211 # message start (fromnode, tonode, to, from, t, private, subject, origin)
212 sub pc28
213 {
214         my ($tonode, $fromnode, $to, $from, $t, $private, $subject, $origin, $rr) = @_;
215         my $date = cldate($t);
216         my $time = ztime($t);
217         $private = $private ? '1' : '0';
218         $rr = $rr ? '1' : '0';
219         $subject ||= ' ';
220         return "PC28^$tonode^$fromnode^$to^$from^$date^$time^$private^$subject^ ^5^$rr^ ^$origin^~";
221 }
222
223 # message text (from and to node same way round as pc29)
224 sub pc29 
225 {
226         my ($fromnode, $tonode, $stream, $text) = @_;
227         $text = ' ' unless defined $text && length $text > 0;
228         $text =~ s/\^/%5E/og;                   # remove ^
229         return "PC29^$fromnode^$tonode^$stream^$text^~";
230 }
231
232 # subject acknowledge (will have to and from node reversed to pc28)
233 sub pc30
234 {
235         my ($fromnode, $tonode, $stream) = @_;
236         return "PC30^$fromnode^$tonode^$stream^";
237 }
238
239 # acknowledge this tranche of lines (to and from nodes reversed to pc29 and pc28
240 sub pc31
241 {
242         my ($fromnode, $tonode, $stream) = @_;
243         return "PC31^$fromnode^$tonode^$stream^";
244 }
245
246 #  end of message from the sending end (pc28 node order)
247 sub pc32
248 {
249         my ($fromnode, $tonode, $stream) = @_;
250         return "PC32^$fromnode^$tonode^$stream^";
251 }
252
253 # acknowledge end of message from receiving end (opposite pc28 node order)
254 sub pc33
255 {
256         my ($fromnode, $tonode, $stream) = @_;
257         return "PC33^$fromnode^$tonode^$stream^";
258 }
259
260 # remote cmd send
261 sub pc34
262 {
263         my($fromnode, $tonode, $msg) = @_;
264         return "PC34^$tonode^$fromnode^$msg^~";
265 }
266
267 # remote cmd reply
268 sub pc35
269 {
270         my($fromnode, $tonode, $msg) = @_;
271         return "PC35^$tonode^$fromnode^$msg^~";
272 }
273
274 # send all the DX clusters I reckon are connected
275 sub pc38
276 {
277         return join '^', "PC38", map {$_->call} Route::Node::get_all();
278 }
279
280 # tell the local node to discconnect
281 sub pc39
282 {
283         my ($call, $reason) = @_;
284         my $hops = get_hops(39);
285         $reason = "Gone." if !$reason;
286         return "PC39^$call^$reason^$hops^";
287 }
288
289 # cue up bulletin or file for transfer
290 sub pc40
291 {
292         my ($to, $from, $fn, $bull) = @_;
293         $bull = $bull ? '1' : '0';
294         return "PC40^$to^$from^$fn^$bull^5^";
295 }
296
297 # user info
298 sub pc41
299 {
300         my $call = shift;
301         $call = shift if ref $call;
302         
303         my $sort = shift || '0';
304         my $info = shift || ' ';
305         my $hops = shift || get_hops(41);
306         return "PC41^$call^$sort^$info^$hops^~";
307 }
308
309 # abort message
310 sub pc42
311 {
312         my ($fromnode, $tonode, $stream) = @_;
313         return "PC42^$fromnode^$tonode^$stream^";
314 }
315
316 # remote db request
317 sub pc44
318 {
319         my ($fromnode, $tonode, $stream, $db, $req, $call) = @_;
320         $db = uc $db;
321         return "PC44^$tonode^$fromnode^$stream^$db^$req^$call^";
322 }
323
324 # remote db data
325 sub pc45
326 {
327         my ($fromnode, $tonode, $stream, $data) = @_;
328         return "PC45^$tonode^$fromnode^$stream^$data^";
329 }
330
331 # remote db data complete
332 sub pc46
333 {
334         my ($fromnode, $tonode, $stream) = @_;
335         return "PC46^$tonode^$fromnode^$stream^";
336 }
337
338 # bull delete
339 sub pc49
340 {
341         my ($from, $subject) = @_;
342         my $hops = get_hops(49);
343         return "PC49^$from^$subject^$hops^~";
344 }
345
346 # periodic update of users, plus keep link alive device (always H99)
347 sub pc50
348 {
349         my $self = shift;
350         my $call = $self->call;
351         my $n = shift || '0';
352         my $hops = shift || 'H99';
353         return "PC50^$call^$n^$hops^";
354 }
355
356 # generate pings
357 sub pc51
358 {
359         my ($to, $from, $val) = @_;
360         return "PC51^$to^$from^$val^";
361 }
362
363 my $hexlasttime = 0;
364 my $hexlastlet = 'A';
365 my $hexoverflow = '';
366
367 sub hexstamp
368 {
369         my $t = shift || $main::systime;
370         if ($t ne $hexlasttime) {
371                 $hexlasttime = $t;
372                 $hexoverflow = '';
373                 $hexlastlet = 'A';
374         } else {
375                 do {
376                         $hexlastlet = chr(ord($hexlastlet) + 1);
377                         if ($hexlastlet ge '~') {
378                                 $hexlastlet = 'A';
379                                 $hexoverflow ||= '@';
380                                 do {
381                                         $hexoverflow = chr(ord($hexoverflow) + 1);
382                                 } while ($hexoverflow eq '^');
383                         }
384                 } while ($hexlastlet eq '^');
385         }
386         return sprintf "%08X%s%s", $hexlasttime, $hexoverflow, $hexlastlet;
387 }
388
389 sub pc58
390 {
391         my $sort = shift;
392         my $hexstamp = shift || hexstamp();
393         my $from = shift;
394         my $to = shift;
395         my $text = unpad(shift);
396         $text = ' ' if !$text;
397         $text =~ s/\^/%5E/g;
398         return "PC58^$sort^$hexstamp^$from^$to^$text" . sprintf "^%s^", get_hops(58);
399 }
400
401 sub pc59
402 {
403         my @out;
404         my $sort = shift;
405         my $hexstamp = shift || hexstamp();
406         my $node = shift;
407         
408         my $s = "PC59^$sort^$hexstamp^$node";
409         for (@_) {
410                 next unless $_;
411                 my $ref = $_;
412                 my $call = $ref->call;
413                 my $here = $ref->here;
414                 $s .= '^' . $ref->enc_pc59;
415         }
416         push @out, sprintf "$s^%s^", get_hops(59);
417         return @out;
418 }
419
420 sub PC59c
421 {
422         return PC59('C', @_);
423 }
424
425 sub PC59a
426 {
427         return PC59('A', @_);
428 }
429
430 sub PC59d
431 {
432         return PC59('D', @_);
433 }
434
435 # clx remote cmd send
436 sub pc84
437 {
438         my($fromnode, $tonode, $call, $msg) = @_;
439         return "PC84^$tonode^$fromnode^$call^$msg^~";
440 }
441
442 # clx remote cmd reply
443 sub pc85
444 {
445         my($fromnode, $tonode, $call, $msg) = @_;
446         return "PC85^$tonode^$fromnode^$call^$msg^~";
447 }
448
449 # spider route broadcast
450 sub pc90
451 {
452 }
453
454 1;
455 __END__
456
457
458