forked from jaredquinn/ogbbsguides
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nl2binkd
executable file
·391 lines (338 loc) · 8.02 KB
/
nl2binkd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#!/bin/bash
#
# nl2binkd v1.01
# (c) 2012 by Markus Reschke
# 1.01 changes by Jared Quinn
#
# 1.01 changes may break standards compliance however it creates a binkd
# nodelist from the FTSC fidonet list that is closer to the version
# distributed in the fidonet I-BINKD file echo.
#
# This script extracts nodes with binkd services from the standard Fidonet
# nodelist and creates a special nodelist for binkd.
#
# prepare variables
FLAG=false
STAT=1 # exit status
# exit hook for cleaning up
trap 'exit $STAT' 0 1 2 15
# print usage if no arguments provided
case "$*" in
"") echo "Usage: `basename $0` <nodelist> <binkd nodelist> [domain]";
echo "Example: `basename $0` NODELIST.006 binkp.nl fidonet";
echo "Purpose: extract binkp nodes from nodelist";
exit;;
esac
# check arguments
if [ ! "$2" ]; then
echo "argument error: please run `basename $0` for usage."
exit
fi
# save arguments
NODELIST="$1"
BINKP_NL="$2"
DOMAIN="$3"
# check nodelist
if [ -r "$NODELIST" ]; then
TEST=`head -1 "$NODELIST" | awk '/Day/ {print $1}'`
if [ "$TEST" != ";A" ]; then
echo "File doesn't seem to be a nodelist!"
exit
fi
else
echo "Can't read nodelist!"
exit
fi
# move binkd nodelist
if [ -w "$BINKP_NL" ]; then
mv "$BINKP_NL" "$BINKP_NL-old"
fi
# write header for new binkd nodelist
TEST=`head -1 "$NODELIST" | sed 's/;A //' | tr -d '\r'`
echo -e "#\n# binkd nodelist generated by nl2binkd\n# $TEST\n#" > "$BINKP_NL"
# a monster awk script :-)
awk -v binkd_nl="$BINKP_NL" -v domain="$DOMAIN" '
#
# functions
#
# return IP address/DNS name or port number
function get_value(conn, type) {
value = ""
x = split(conn, token, ":")
if (x == 1)
{
# might be addr or port
if (type == "addr")
{
if (token[1] ~ /^[0-9a-zA-Z.-]+\.[a-zA-Z]+$/) value = token[1]
else if (token[1] ~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/) value = token[1]
}
else if (type == "port")
{
if (token[1] ~ /^[1-9][0-9]*$/) value = token[1]
}
}
else if (x == 2)
{
# should be addr:port
if (type == "addr")
{
if (token[1] ~ /^[0-9a-zA-Z.-]+\.[a-zA-Z]+$/) value = token[1]
else if (token[1] ~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/) value = token[1]
}
else if (type == "port")
{
if (token[2] ~ /^[1-9][0-9]*$/) value = token[2]
}
}
else if (x > 2)
{
# to many parts
return value
}
# cleanup
for (y in token)
delete token[y]
return value
}
#
# begin
#
begin {
# global variables
zone = ""
net = ""
node = ""
address = ""
}
#
# main
#
{
# reset variables
node = ""
address = ""
run_flag = "FALSE"
addr_flag = "FALSE"
# remove CR
sub("\\r", "")
# check for non-comments
if ($1 !~ /^;[AS]$/)
{
# prepare data parsing
# split line into array (data separated by ",")
n = split($0, data, ",")
if (n > 1) run_flag = "TRUE"
}
# extract FTS address
if (run_flag == "TRUE")
{
run_flag = "FALSE"
# get/update all parts of the FTS address
if (data[1] ~ /^Zone$/)
{
# ZC entry
zone = data[2]
net = zone
node = "0"
}
else if (data[1] ~ /^Region$/)
{
# RC entry
net = data[2]
node = "0"
}
else if (data[1] ~ /^Host$/)
{
# NC entry
net = data[2]
node = "0"
}
else
{
# node entry
node = data[2]
}
# build FTS address
if (zone != "" && net != "" && node != "")
{
address = zone ":" net "/" node
if (domain != "") address = address "@" domain
run_flag = "TRUE"
}
}
# search for IBN flags (=binkd)
if (run_flag == "TRUE")
{
run_flag = "FALSE"
count = 0
for (i in data)
{
if (data[i] ~ /^IBN/ || data[i] ~ /^BND/)
{
run_flag = "TRUE"
# save IP details if available
n = length(data[i]);
if (n > 4)
{
count++;
ip_conn[count] = substr(data[i], 5)
test = get_value(ip_conn[count], "addr")
if (test != "")
{
addr_flag = "TRUE"
}
}
}
if (data[i] ~ /^ON2/)
{
# save Onion address if available
n = length(data[i]);
if (n == 20)
{
test = get_value(substr(data[i] ".onion", 5), "addr")
if (test != "")
{
run_flag = "TRUE"
count++;
ip_addr[count] = test
}
}
}
}
}
# search for hidden address in system name
if (run_flag == "TRUE" && addr_flag == "FALSE")
{
count = 0
# third field is system name
test = get_value(data[3], "addr")
if (test != "")
{
count++
ip_addr[count] = test
}
}
# search for hidden address in phone number
if (run_flag == "TRUE" && addr_flag == "FALSE")
{
# sixth field is phone number
if (data[6] ~ /^000-[0-9]+-[0-9]+-[0-9]+-[0-9]+$/)
{
if (data[6] !~ /^000-0+-0+-0+-0+$/)
{
count++
ip_addr[count] = data[6]
}
}
}
# search for addresses in INA flags
if (run_flag == "TRUE" && addr_flag == "FALSE")
{
for (i in data)
{
if (data[i] ~ /^INA/)
{
# save IP address if available
n = length(data[i]);
if (n > 4)
{
test = get_value(substr(data[i], 5), "addr")
if (test != "")
{
count++;
ip_addr[count] = test
}
}
}
}
}
# search for addresses in IP flags
if (run_flag == "TRUE" && addr_flag == "FALSE")
{
for (i in data)
{
if (data[i] ~ /^IP/)
{
# save IP address if available
n = length(data[i]);
if (n > 3)
{
test = get_value(substr(data[i], 4), "addr")
if (test != "")
{
count++;
ip_addr[count] = test
}
}
}
}
}
# de-duplication of hidden addresses
if (run_flag == "TRUE")
{
for (i = 1; i <= count; i++)
{
if (ip_addr[i] != "")
{
for (j = i+1; j <= count; j++)
{
if (ip_addr[i] == ip_addr[j]) delete ip_addr[j]
}
}
}
}
# output node
if (run_flag == "TRUE")
{
conns = ""
if (addr_flag == "TRUE")
{
# take data from IBN only
for (i in ip_conn)
{
a_addr = get_value(ip_conn[i], "addr")
a_port = get_value(ip_conn[i], "port")
if (conns != "" && a_addr != "") conns = conns ";"
if (a_addr != "" && a_port != "") conns = conns a_addr ":" a_port
else if (a_addr != "") conns = conns a_addr
}
}
else
{
# consider hidden addresses
for (j in ip_addr)
{
test = "FALSE"
if (ip_addr[j] != "")
{
if (conns != "") conns = conns ";"
# combine address with ports from IBN
for (i in ip_conn)
{
test = "TRUE"
a_port = get_value(ip_conn[i], "port")
if (a_port != "") conns = conns ip_addr[j] ":" a_port
}
# no ports from IBN
if (test == "FALSE")
{
conns = conns ip_addr[j]
}
}
}
}
# write node data in binkd format
if (conns != "") print "Node " address " " conns " -" >> binkd_nl
}
# clean up
for (i in data)
delete data[i]
for (i in ip_conn)
delete ip_conn[i]
for (i in ip_addr)
delete ip_addr[i]
}
' "$NODELIST"
# the end
STAT=0 # exit status