-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
473 lines (372 loc) · 8.6 KB
/
query.go
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
package finger
import (
"sourcecode.social/reiver/go-erorr"
"fmt"
"strings"
)
// A Query represents a parsed finger-protocol target.
//
// For example, consider the following finger-protocol request:
//
// "/W dariush@reiver.link@example.com\r\n"
//
// When left as just an uninterpreted string, the finger-protocol target is:
//
// "dariush@reiver.link@example.com"
//
// Interpretting this finger-protocol target string, and separating out the:
//
// • actor (i.e,. "dariush")
//
// • addresses (i.e., "reiver.link" and "example.com")
//
// Is what makes it a finger-protocol query.
type Query struct {
actor Actor
path Path
addresses []Address
}
func EmptyQuery() Query {
return Query{}
}
func AssembleQueryAddresses(addresses ...Address) Query {
return Query{
addresses: addresses,
}
}
func AssembleQueryActor(actor Actor) Query {
return Query{
actor: actor,
}
}
func AssembleQueryActorPath(actor Actor, path Path) Query {
return Query{
actor: actor,
path: path,
}
}
func AssembleQueryActorPathAddresses(actor Actor, path Path, addresses ...Address) Query {
return Query{
actor: actor,
path: path,
addresses: addresses,
}
}
func AssembleQueryActorAddresses(actor Actor, addresses ...Address) Query {
return Query{
actor: actor,
addresses: addresses,
}
}
func CreateQueryHost(host string) Query {
return Query{
addresses: []Address{
CreateAddressHost(host),
},
}
}
func CreateQueryHostPort(host string, port uint16) Query {
return Query{
addresses: []Address{
CreateAddress(host, port),
},
}
}
func CreateQueryHosts(hosts ...string) Query {
var addresses []Address
for _, hostString := range hosts {
var address Address
if "" != hostString {
address = CreateAddressHost(hostString)
}
addresses = append(addresses, address)
}
return Query{
addresses: addresses,
}
}
func CreateQueryActor(actor string) Query {
return Query{
actor: CreateActor(actor),
}
}
func CreateQueryActorHost(actor string, host string) Query {
return Query{
actor: CreateActor(actor),
addresses: []Address{
CreateAddressHost(host),
},
}
}
func CreateQueryActorHosts(actor string, hosts ...string) Query {
var addresses []Address
for _, hostString := range hosts {
var address Address
if "" != hostString {
address = CreateAddressHost(hostString)
}
addresses = append(addresses, address)
}
return Query{
actor: CreateActor(actor),
addresses: addresses,
}
}
func CreateQueryActorPath(actor string, path string) Query {
return Query{
actor: CreateActor(actor),
path: CreatePath(path),
}
}
func CreateQueryActorPathHost(actor string, path string, host string) Query {
return Query{
actor: CreateActor(actor),
path: CreatePath(path),
addresses: []Address{
CreateAddressHost(host),
},
}
}
func CreateQueryActorPathHosts(actor string, path string, hosts ...string) Query {
var addresses []Address
for _, hostString := range hosts {
var address Address
if "" != hostString {
address = CreateAddressHost(hostString)
}
addresses = append(addresses, address)
}
return Query{
actor: CreateActor(actor),
path: CreatePath(path),
addresses: addresses,
}
}
func CreateQueryActorHostPort(actor string, host string, port uint16) Query {
return Query{
actor: CreateActor(actor),
addresses: []Address{
CreateAddress(host, port),
},
}
}
func CreateQueryActorPathHostPort(actor string, path string, host string, port uint16) Query {
return Query{
actor: CreateActor(actor),
path: CreatePath(path),
addresses: []Address{
CreateAddress(host, port),
},
}
}
// ParseQuery parses a (target) string for a finger-protocol query.
func ParseQuery(query string) (Query, error) {
if "" == query {
return EmptyQuery(), nil
}
var q Query
{
var query0 byte = query[0]
if '/' != query0 && '@' != query0 {
var indexSolidus int = strings.IndexRune(query, '/')
var indexAt int = strings.IndexRune(query, '@')
// Ex: "joeblow"
if indexSolidus < 0 && indexAt < 0 {
q.actor = CreateActor(query)
/////////////////////////////// RETURN
return q, nil
}
var index int
switch {
// Ex: "joeblow/once/twice/thrice/fource"
case 0 <= indexSolidus && indexAt < 0 && "" != query:
index = indexSolidus
// Ex: "joeblow@example.om"
case indexSolidus < 0 && 0 <= indexAt && "" != query:
index = indexAt
// Ex: "joeblow@example.com"
case 0 <= indexSolidus && 0 <= indexAt && "" != query:
index = indexSolidus
if indexAt < index {
index = indexAt
}
}
q.actor = CreateActor(query[:index])
query = query[index:]
}
}
if "" == query {
return q, nil
}
{
var query0 byte = query[0]
if '/' == query0 {
var indexAt int = strings.IndexRune(query, '@')
if indexAt < 0 {
q.path = CreatePath(query)
/////////////////////////////// RETURN
return q, nil
}
var index int = indexAt
q.path = CreatePath(query[:index])
query = query[index:]
}
}
if "" == query {
return q, nil
}
{
for {
if "" == query {
break
}
if '@' != query[0] {
break
}
query = query[1:]
var index int = strings.IndexRune(query, '@')
var address Address
{
var s string
switch {
case index < 0:
s = query
query = ""
default:
s = query[:index]
query = query[index:]
}
var err error
address, err = ParseAddress(s)
if nil != err {
return Query{}, fmt.Errorf("problem parsing finger-protocol query: %w", err)
}
}
q.addresses = append(q.addresses, address)
}
}
return q, nil
}
// ParseQueryFromTarget parses a target for a finger-protocol query.
func ParseQueryFromTarget(target Target) (Query, error) {
value, something := target.Unwrap()
if !something {
return EmptyQuery(), erorr.Error("problem parsing finger-protocol query from target: empty target")
}
return ParseQuery(value)
}
// ParseRFC1288Query parses a (target) string for an older style finger-protocol query as defined IETF RFC-1288.
func ParseRFC1288Query(query string) (Query, error) {
if "" == query {
return Query{}, nil
}
var q Query
{
if '@' != query[0] {
var index int = strings.IndexRune(query, '@')
switch {
case index < 0 && "" != query:
q.actor = CreateActor(query)
default:
q.actor = CreateActor(query[:index])
query = query[index:]
}
}
}
{
for {
if "" == query {
break
}
if '@' != query[0] {
break
}
query = query[1:]
var index int = strings.IndexRune(query, '@')
var address Address
{
var s string
switch {
case index < 0:
s = query
query = ""
default:
s = query[:index]
query = query[index:]
}
var err error
address, err = ParseAddress(s)
if nil != err {
return Query{}, fmt.Errorf("problem parsing finger-protocol query: %w", err)
}
}
q.addresses = append(q.addresses, address)
}
}
return q, nil
}
// ParseRFC1288Query parses a target for an older style finger-protocol query as defined IETF RFC-1288.
func ParseRFC1288QueryFromTarget(target Target) (Query, error) {
value, something := target.Unwrap()
if !something {
return EmptyQuery(), erorr.Error("problem parsing finger-protocol query from target: empty target")
}
return ParseRFC1288Query(value)
}
// ClientParameters returns the information need for a finger-protocol client
// to make a finger-protocol request.
func (receiver Query) ClientParameters() (Address, Query) {
var addresses []Address = receiver.addresses
length := len(addresses)
if length < 1 {
return DefaultAddress(), receiver
}
return addresses[length-1], Query{
actor: receiver.actor,
path: receiver.path,
addresses: addresses[:length-1],
}
}
func (receiver Query) isEmpty() bool {
return EmptyActor() == receiver.actor &&
EmptyPath() == receiver.path &&
len(receiver.addresses) < 1
}
func (receiver Query) LenAddresses() int {
return len(receiver.addresses)
}
func (receiver Query) String() string {
var buffer strings.Builder
{
actor, actorIsSomething := receiver.actor.Unwrap()
if actorIsSomething {
buffer.WriteString(actor)
}
}
{
path, pathIsSomething := receiver.path.Unwrap()
if pathIsSomething {
buffer.WriteString(path)
}
}
{
for _, address := range receiver.addresses {
buffer.WriteRune('@')
buffer.WriteString(address.String())
}
}
return buffer.String()
}
// Targets returns the equivalent finger.Target to finger.Query.
func (receiver Query) Target() Target {
if receiver.isEmpty() {
return EmptyTarget()
}
return CreateTarget(receiver.String())
}
func (receiver Query) Actor() Actor {
return receiver.actor
}
func (receiver Query) Path() Path {
return receiver.path
}