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