forked from aler9/dctk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol_nmdc.go
829 lines (713 loc) · 20 KB
/
protocol_nmdc.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
package dctoolkit
import (
"fmt"
"net"
"regexp"
"strings"
)
const (
// client <-> hub features
nmdcFeatureUserCommands = "UserCommand"
nmdcFeatureNoGetInfo = "NoGetINFO"
nmdcFeatureNoHello = "NoHello"
nmdcFeatureUserIp = "UserIP2"
nmdcFeatureTTHSearch = "TTHSearch"
nmdcFeatureZlibFull = "ZPipe0"
nmdcFeatureTls = "TLS"
// client <-> client features
nmdcFeatureMiniSlots = "MiniSlots"
nmdcFeatureFileListBzip = "XmlBZList"
nmdcFeatureAdcGet = "ADCGet"
nmdcFeatureTTHLeaves = "TTHL"
nmdcFeatureTTHDownload = "TTHF"
nmdcFeatureZlibGet = "ZLIG"
)
var reNmdcCommand = regexp.MustCompile("(?s)^\\$([a-zA-Z0-9]+)( (.+))?$")
var reNmdcPublicChat = regexp.MustCompile("(?s)^<(" + reStrNick + "|.+?)> (.+)$") // some very bad hubs also use spaces in public message authors
var reNmdcPrivateChat = regexp.MustCompile("(?s)^\\$To: (" + reStrNick + ") From: (" + reStrNick + ") \\$<(" + reStrNick + ")> (.+)$")
var reNmdcCmdConnectToMe = regexp.MustCompile("^(" + reStrNick + ") (" + reStrIp + "):(" + reStrPort + ")(S?)$")
var reNmdcCmdDirection = regexp.MustCompile("^(Download|Upload) ([0-9]+)$")
var reNmdcCmdForceMove = regexp.MustCompile("^(" + reStrAddress + ")(:(" + reStrPort + "))?$")
var reNmdcCmdInfo = regexp.MustCompile("^\\$ALL (" + reStrNick + ") (.*?)(<(.*?) V:(.+?),M:(A|P),H:([0-9]+)/([0-9]+)/([0-9]+),S:([0-9]+)>)?\\$ \\$(.*?)(.)\\$(.*?)\\$([0-9]+)\\$$")
var reNmdcCmdLock = regexp.MustCompile("^([^ ]+)( Pk=(.+?)(Ref=(.+?))?)?$")
var reNmdcCmdRevConnectToMe = regexp.MustCompile("^(" + reStrNick + ") (" + reStrNick + ")$")
var reNmdcCmdSearchReqActive = regexp.MustCompile("^(" + reStrIp + "):(" + reStrPort + ") (F|T)\\?(F|T)\\?([0-9]+)\\?([0-9])\\?(.+)$")
var reNmdcCmdSearchReqPassive = regexp.MustCompile("^Hub:(" + reStrNick + ") (F|T)\\?(F|T)\\?([0-9]+)\\?([0-9])\\?(.+)$")
var reNmdcCmdSearchResult = regexp.MustCompile("^(" + reStrNick + ") ([^\x05]+?)(\x05([0-9]+))? ([0-9]+)/([0-9]+)\x05(TTH:(" + reStrTTH + ")|(.+?)) \\((" + reStrIp + "):(" + reStrPort + ")\\)$")
var reNmdcCmdUserCommand = regexp.MustCompile("^([0-9]{1,3}) ([0-9]{1,2}) (.*?)$")
var reNmdcCmdUserIP = regexp.MustCompile("^(" + reStrNick + ") (" + reStrIp + ")$")
// http://nmdc.sourceforge.net/Versions/NMDC-1.3.html#_key
// https://web.archive.org/web/20150529002427/http://wiki.gusari.org/index.php?title=LockToKey%28%29
func nmdcComputeKey(lock []byte) []byte {
// the key has exactly as many characters as the lock
key := make([]byte, len(lock))
// Except for the first, each key character is computed from the corresponding lock character and the one before it
key[0] = 0
for n := 1; n < len(key); n++ {
key[n] = lock[n] ^ lock[n-1]
}
// The first key character is calculated from the first lock character and the last two lock characters
key[0] = lock[0] ^ lock[len(lock)-1] ^ lock[len(lock)-2] ^ 5
// Next, every character in the key must be nibble-swapped
for n := 0; n < len(key); n++ {
key[n] = ((key[n] << 4) & 240) | ((key[n] >> 4) & 15)
}
// the characters with the decimal ASCII values of 0, 5, 36, 96, 124, and 126
// cannot be sent to the server. Each character with this value must be
// substituted with the string /%DCN000%/, /%DCN005%/, /%DCN036%/, /%DCN096%/, /%DCN124%/, or /%DCN126%/
var res []byte
for _, byt := range key {
if byt == 0 || byt == 5 || byt == 36 || byt == 96 || byt == 124 || byt == 126 {
res = append(res, []byte(fmt.Sprintf("/%%DCN%.3d%%/", byt))...)
} else {
res = append(res, byt)
}
}
return res
}
func nmdcCommandEncode(key string, args string) string {
return "$" + key + " " + args + "|"
}
type protocolNmdc struct {
*protocolBase
}
func newProtocolNmdc(remoteLabel string, nconn net.Conn,
applyReadTimeout bool, applyWriteTimeout bool) protocol {
p := &protocolNmdc{
protocolBase: newProtocolBase(remoteLabel,
nconn, applyReadTimeout, applyWriteTimeout, '|'),
}
return p
}
func (p *protocolNmdc) Read() (msgDecodable, error) {
if p.readBinary == false {
msgStr, err := p.ReadMessage()
if err != nil {
return nil, err
}
msg, err := func() (msgDecodable, error) {
if len(msgStr) == 0 {
return &msgNmdcKeepAlive{}, nil
}
if matches := reNmdcCommand.FindStringSubmatch(msgStr); matches != nil {
key, args := matches[1], matches[3]
cmd := func() msgNmdcCommandDecodable {
switch key {
case "ADCGET":
return &msgNmdcGetFile{}
case "ADCSND":
return &msgNmdcSendFile{}
case "BadPass":
return &msgNmdcBadPassword{}
case "BotList":
return &msgNmdcBotList{}
case "ConnectToMe":
return &msgNmdcConnectToMe{}
case "Direction":
return &msgNmdcDirection{}
case "Error":
return &msgNmdcError{}
case "ForceMove":
return &msgNmdcForceMove{}
case "GetPass":
return &msgNmdcGetPass{}
case "Hello":
return &msgNmdcHello{}
case "HubName":
return &msgNmdcHubName{}
case "HubIsFull":
return &msgNmdcHubIsFull{}
case "HubTopic":
return &msgNmdcHubTopic{}
case "Key":
return &msgNmdcKey{}
case "Lock":
return &msgNmdcLock{}
case "LogedIn":
return &msgNmdcLoggedIn{}
case "MaxedOut":
return &msgNmdcMaxedOut{}
case "MyINFO":
return &msgNmdcMyInfo{}
case "MyNick":
return &msgNmdcMyNick{}
case "OpList":
return &msgNmdcOpList{}
case "Quit":
return &msgNmdcQuit{}
case "RevConnectToMe":
return &msgNmdcRevConnectToMe{}
case "Search":
return &msgNmdcSearchRequest{}
case "SR":
return &msgNmdcSearchResult{}
case "Supports":
return &msgNmdcSupports{}
case "UserCommand":
return &msgNmdcUserCommand{}
case "UserIP":
return &msgNmdcUserIp{}
case "ValidateDenide":
return &msgNmdcValidateDenide{}
case "ZOn":
return &msgNmdcZon{}
}
return nil
}()
if cmd == nil {
return nil, fmt.Errorf("unrecognized command")
}
err := cmd.NmdcDecode(args)
if err != nil {
return nil, fmt.Errorf("unable to decode arguments")
}
return cmd, nil
}
if matches := reNmdcPublicChat.FindStringSubmatch(msgStr); matches != nil {
return &msgNmdcPublicChat{Author: matches[1], Content: matches[2]}, nil
}
if matches := reNmdcPrivateChat.FindStringSubmatch(msgStr); matches != nil {
return &msgNmdcPrivateChat{Author: matches[3], Content: matches[4]}, nil
}
return nil, fmt.Errorf("unknown sequence")
}()
if err != nil {
return nil, fmt.Errorf("Unable to parse: %s (%s)", err, msgStr)
}
dolog(LevelDebug, "[%s->c] %T %+v", p.remoteLabel, msg, msg)
return msg, nil
} else {
buf, err := p.ReadBinary()
if err != nil {
return nil, err
}
return &msgBinary{buf}, nil
}
}
func (p *protocolNmdc) Write(msg msgEncodable) {
nmdc, ok := msg.(msgNmdcEncodable)
if !ok {
panic(fmt.Errorf("command not fit for nmdc (%T)", msg))
}
dolog(LevelDebug, "[c->%s] %T %+v", p.remoteLabel, msg, msg)
p.protocolBase.Write([]byte(nmdc.NmdcEncode()))
}
type msgNmdcCommandDecodable interface {
NmdcDecode(args string) error
}
type msgNmdcEncodable interface {
NmdcEncode() string
}
type msgNmdcGetFile struct {
Query string
Start uint64
Length int64
Compressed bool
}
func (m *msgNmdcGetFile) NmdcDecode(args string) error {
matches := reSharedCmdAdcGet.FindStringSubmatch(args)
if matches == nil {
return errorArgsFormat
}
m.Query, m.Start, m.Length, m.Compressed = matches[1], atoui64(matches[4]),
atoi64(matches[5]), (matches[6] != "")
return nil
}
func (m *msgNmdcGetFile) NmdcEncode() string {
return nmdcCommandEncode("ADCGET", fmt.Sprintf("%s %d %d%s",
m.Query, m.Start, m.Length,
func() string {
if m.Compressed == true {
return " ZL1"
}
return ""
}()))
}
type msgNmdcSendFile struct {
Query string
Start uint64
Length uint64
Compressed bool
}
func (m *msgNmdcSendFile) NmdcDecode(args string) error {
matches := reSharedCmdAdcSnd.FindStringSubmatch(args)
if matches == nil {
return errorArgsFormat
}
m.Query, m.Start, m.Length, m.Compressed = matches[1], atoui64(matches[4]),
atoui64(matches[5]), (matches[6] != "")
return nil
}
func (m *msgNmdcSendFile) NmdcEncode() string {
return nmdcCommandEncode("ADCSND", fmt.Sprintf("%s %d %d%s",
m.Query, m.Start, m.Length,
func() string {
if m.Compressed {
return " ZL1"
}
return ""
}()))
}
type msgNmdcBadPassword struct{}
func (m *msgNmdcBadPassword) NmdcDecode(args string) error {
return nil
}
type msgNmdcBotList struct {
Bots map[string]struct{}
}
func (m *msgNmdcBotList) NmdcDecode(args string) error {
m.Bots = make(map[string]struct{})
for _, bot := range strings.Split(strings.TrimSuffix(args, "$$"), "$$") {
m.Bots[bot] = struct{}{}
}
return nil
}
type msgNmdcConnectToMe struct {
Target string
Ip string
Port uint
Encrypted bool
}
func (m *msgNmdcConnectToMe) NmdcDecode(args string) error {
matches := reNmdcCmdConnectToMe.FindStringSubmatch(args)
if matches == nil {
return errorArgsFormat
}
m.Target, m.Ip, m.Port, m.Encrypted = matches[1], matches[2], atoui(matches[3]),
(matches[4] != "")
return nil
}
func (m *msgNmdcConnectToMe) NmdcEncode() string {
return nmdcCommandEncode("ConnectToMe", fmt.Sprintf("%s %s:%d%s",
m.Target, m.Ip, m.Port,
func() string {
if m.Encrypted {
return "S"
}
return ""
}()))
}
type msgNmdcDirection struct {
Direction string
Bet uint
}
func (m *msgNmdcDirection) NmdcDecode(args string) error {
matches := reNmdcCmdDirection.FindStringSubmatch(args)
if matches == nil {
return errorArgsFormat
}
m.Direction, m.Bet = matches[1], atoui(matches[2])
return nil
}
func (m *msgNmdcDirection) NmdcEncode() string {
return nmdcCommandEncode("Direction", fmt.Sprintf("%s %d", m.Direction, m.Bet))
}
type msgNmdcError struct {
Error string
}
func (m *msgNmdcError) NmdcDecode(args string) error {
m.Error = args
return nil
}
func (m *msgNmdcError) NmdcEncode() string {
return nmdcCommandEncode("Error", m.Error)
}
type msgNmdcForceMove struct {
Address string
Port uint
}
func (m *msgNmdcForceMove) NmdcDecode(args string) error {
matches := reNmdcCmdForceMove.FindStringSubmatch(args)
if matches == nil {
return errorArgsFormat
}
m.Address, m.Port = matches[1], atoui(matches[3])
return nil
}
type msgNmdcGetNickList struct{}
func (m *msgNmdcGetNickList) NmdcEncode() string {
return nmdcCommandEncode("GetNickList", "")
}
type msgNmdcGetPass struct{}
func (m *msgNmdcGetPass) NmdcDecode(args string) error {
return nil
}
type msgNmdcKeepAlive struct{}
func (m *msgNmdcKeepAlive) NmdcEncode() string {
return "|"
}
type msgNmdcHello struct{}
func (m *msgNmdcHello) NmdcDecode(args string) error {
return nil
}
type msgNmdcHubIsFull struct{}
func (m *msgNmdcHubIsFull) NmdcDecode(args string) error {
return nil
}
type msgNmdcHubName struct {
Content string
}
func (m *msgNmdcHubName) NmdcDecode(args string) error {
m.Content = args
return nil
}
type msgNmdcHubTopic struct {
Content string
}
func (m *msgNmdcHubTopic) NmdcDecode(args string) error {
m.Content = args
return nil
}
type msgNmdcKey struct {
Key []byte
}
func (m *msgNmdcKey) NmdcDecode(args string) error {
m.Key = []byte(args)
return nil
}
func (m *msgNmdcKey) NmdcEncode() string {
return nmdcCommandEncode("Key", string(m.Key))
}
type msgNmdcLock struct {
Lock string
Pk string
Ref string
}
func (m *msgNmdcLock) NmdcDecode(args string) error {
matches := reNmdcCmdLock.FindStringSubmatch(args)
if matches == nil {
return errorArgsFormat
}
m.Lock, m.Pk, m.Ref = matches[1], matches[3], matches[5]
return nil
}
func (m *msgNmdcLock) NmdcEncode() string {
ret := m.Lock
if m.Pk != "" {
ret += " Pk=" + m.Pk
if m.Ref != "" {
ret += "Ref=" + m.Ref
}
}
return nmdcCommandEncode("Lock", ret)
}
type msgNmdcLoggedIn struct{}
func (m *msgNmdcLoggedIn) NmdcDecode(args string) error {
return nil
}
type msgNmdcMaxedOut struct{}
func (m *msgNmdcMaxedOut) NmdcDecode(args string) error {
return nil
}
func (m *msgNmdcMaxedOut) NmdcEncode() string {
return nmdcCommandEncode("MaxedOut", "")
}
type msgNmdcMyInfo struct {
Nick string
Description string
Client string
Version string
Mode string
HubUnregisteredCount uint
HubRegisteredCount uint
HubOperatorCount uint
UploadSlots uint
Connection string
StatusByte byte
Email string
ShareSize uint64
}
func (m *msgNmdcMyInfo) NmdcDecode(args string) error {
matches := reNmdcCmdInfo.FindStringSubmatch(args)
if matches == nil {
return errorArgsFormat
}
m.Nick, m.Description, m.Client, m.Version, m.Mode, m.HubUnregisteredCount,
m.HubRegisteredCount, m.HubOperatorCount, m.UploadSlots, m.Connection,
m.StatusByte, m.Email, m.ShareSize = matches[1], matches[2], matches[4],
matches[5], matches[6], atoui(matches[7]), atoui(matches[8]),
atoui(matches[9]), atoui(matches[10]), matches[11],
[]byte(matches[12])[0], matches[13], atoui64(matches[14])
return nil
}
func (m *msgNmdcMyInfo) NmdcEncode() string {
return nmdcCommandEncode("MyINFO", fmt.Sprintf(
"$ALL %s %s<%s V:%s,M:%s,H:%d/%d/%d,S:%d>$ $%s%s$%s$%d$",
m.Nick, m.Description, m.Client, m.Version, m.Mode,
m.HubUnregisteredCount, m.HubRegisteredCount, m.HubOperatorCount,
m.UploadSlots, m.Connection,
string([]byte{m.StatusByte}), m.Email, m.ShareSize))
}
type msgNmdcMyNick struct {
Nick string
}
func (m *msgNmdcMyNick) NmdcDecode(args string) error {
m.Nick = args
return nil
}
func (m *msgNmdcMyNick) NmdcEncode() string {
return nmdcCommandEncode("MyNick", m.Nick)
}
type msgNmdcMyPass struct {
Pass string
}
func (m *msgNmdcMyPass) NmdcEncode() string {
return nmdcCommandEncode("MyPass", m.Pass)
}
type msgNmdcOpList struct {
Ops map[string]struct{}
}
func (m *msgNmdcOpList) NmdcDecode(args string) error {
m.Ops = make(map[string]struct{})
for _, op := range strings.Split(strings.TrimSuffix(args, "$$"), "$$") {
m.Ops[op] = struct{}{}
}
return nil
}
type msgNmdcPrivateChat struct {
Author string
Dest string
Content string
}
func (c *msgNmdcPrivateChat) NmdcEncode() string {
return fmt.Sprintf("$To: %s From: %s $<%s> %s|", c.Dest, c.Author, c.Author, c.Content)
}
type msgNmdcPublicChat struct {
Author string
Content string
}
func (c *msgNmdcPublicChat) NmdcEncode() string {
return fmt.Sprintf("<%s> %s|", c.Author, c.Content)
}
type msgNmdcQuit struct {
Nick string
}
func (m *msgNmdcQuit) NmdcDecode(args string) error {
m.Nick = args
return nil
}
type msgNmdcRevConnectToMe struct {
Author string
Target string
}
func (m *msgNmdcRevConnectToMe) NmdcDecode(args string) error {
matches := reNmdcCmdRevConnectToMe.FindStringSubmatch(args)
if matches == nil {
return errorArgsFormat
}
m.Author, m.Target = matches[1], matches[2]
return nil
}
func (m *msgNmdcRevConnectToMe) NmdcEncode() string {
return nmdcCommandEncode("RevConnectToMe", fmt.Sprintf("%s %s", m.Author, m.Target))
}
type msgNmdcSearchRequest struct {
Type nmdcSearchType
MinSize uint64
MaxSize uint64
Query string
IsActive bool
Ip string // active only
UdpPort uint // active only
Nick string // passive only
}
func (m *msgNmdcSearchRequest) NmdcDecode(args string) error {
if matches := reNmdcCmdSearchReqActive.FindStringSubmatch(args); matches != nil {
m.IsActive = true
m.Ip, m.UdpPort = matches[1], atoui(matches[2])
m.MaxSize = func() uint64 {
if matches[3] == "T" && matches[4] == "T" {
return atoui64(matches[5])
}
return 0
}()
m.MinSize = func() uint64 {
if matches[3] == "T" && matches[4] == "F" {
return atoui64(matches[5])
}
return 0
}()
m.Type = nmdcSearchType(atoi(matches[6]))
m.Query = nmdcSearchUnescape(matches[7])
} else if matches := reNmdcCmdSearchReqPassive.FindStringSubmatch(args); matches != nil {
m.IsActive = false
m.Nick = matches[1]
m.MaxSize = func() uint64 {
if matches[2] == "T" && matches[3] == "T" {
return atoui64(matches[4])
}
return 0
}()
m.MinSize = func() uint64 {
if matches[2] == "T" && matches[3] == "F" {
return atoui64(matches[4])
}
return 0
}()
m.Type = nmdcSearchType(atoi(matches[5]))
m.Query = nmdcSearchUnescape(matches[6])
} else {
return fmt.Errorf("invalid args")
}
return nil
}
func (m *msgNmdcSearchRequest) NmdcEncode() string {
// <sizeRestricted>?<isMaxSize>?<size>?<fileType>?<searchPattern>
return nmdcCommandEncode("Search", fmt.Sprintf("%s %s?%s?%d?%d?%s",
func() string {
if m.IsActive {
return fmt.Sprintf("%s:%d", m.Ip, m.UdpPort)
}
return fmt.Sprintf("Hub:%s", m.Nick)
}(),
func() string {
if m.MinSize != 0 || m.MaxSize != 0 {
return "T"
}
return "F"
}(),
func() string {
if m.MaxSize != 0 {
return "T"
}
return "F"
}(),
func() uint64 {
if m.MaxSize != 0 {
return m.MaxSize
}
return m.MinSize
}(),
m.Type,
func() string {
if m.Type == nmdcSearchTypeTTH {
return "TTH:" + m.Query
}
return nmdcSearchEscape(m.Query)
}()))
}
type msgNmdcSearchResult struct {
Nick string
Path string
IsDir bool
Size uint64 // file only, also directory in ADC
SlotAvail uint
SlotCount uint
TTH TigerHash // file only
HubName string // sometimes provided instead of TTH
HubIp string
HubPort uint
TargetNick string // send only, passive only
}
func (m *msgNmdcSearchResult) NmdcDecode(args string) error {
matches := reNmdcCmdSearchResult.FindStringSubmatch(args)
if matches == nil {
return errorArgsFormat
}
if matches[8] != "" {
tth, err := TigerHashFromBase32(matches[8])
if err != nil {
return err
}
m.TTH = tth
} else {
m.HubName = matches[9]
}
m.IsDir = (matches[3] == "")
m.Nick, m.Path, m.Size, m.SlotAvail, m.SlotCount, m.HubIp, m.HubPort =
matches[1],
"/"+strings.Replace(matches[2], "\\", "/", -1),
func() uint64 {
if matches[3] != "" {
return atoui64(matches[4])
}
return 0
}(),
atoui(matches[5]),
atoui(matches[6]),
matches[10],
atoui(matches[11])
return nil
}
func (m *msgNmdcSearchResult) NmdcEncode() string {
return nmdcCommandEncode("SR", fmt.Sprintf("%s %s%s %d/%d\x05TTH:%s (%s:%d)%s",
m.Nick,
strings.Replace(m.Path[1:], "/", "\\", -1), // skip first slash
func() string {
if m.IsDir == false {
return fmt.Sprintf("\x05%d", m.Size)
}
return ""
}(),
m.SlotAvail, m.SlotCount,
func() string {
if m.IsDir == true {
return dirTTH
}
return m.TTH.String()
}(),
m.HubIp, m.HubPort,
func() string {
if m.TargetNick != "" {
return "\x05" + m.TargetNick
}
return ""
}()))
}
type msgNmdcSupports struct {
Features map[string]struct{}
}
func (m *msgNmdcSupports) NmdcDecode(args string) error {
m.Features = make(map[string]struct{})
for _, feat := range strings.Split(args, " ") {
m.Features[feat] = struct{}{}
}
return nil
}
func (m *msgNmdcSupports) NmdcEncode() string {
var ret []string
for feat := range m.Features {
ret = append(ret, feat)
}
return nmdcCommandEncode("Supports", strings.Join(ret, " "))
}
type msgNmdcUserCommand struct{}
func (m *msgNmdcUserCommand) NmdcDecode(args string) error {
matches := reNmdcCmdUserCommand.FindStringSubmatch(args)
if matches == nil {
return errorArgsFormat
}
return nil
}
type msgNmdcUserIp struct {
Ips map[string]string
}
func (m *msgNmdcUserIp) NmdcDecode(args string) error {
m.Ips = make(map[string]string)
for _, ipstr := range strings.Split(strings.TrimSuffix(args, "$$"), "$$") {
matches := reNmdcCmdUserIP.FindStringSubmatch(ipstr)
if matches == nil {
return errorArgsFormat
}
m.Ips[matches[1]] = matches[2]
}
return nil
}
type msgNmdcValidateNick struct {
Nick string
}
func (m *msgNmdcValidateNick) NmdcEncode() string {
return nmdcCommandEncode("ValidateNick", m.Nick)
}
type msgNmdcVersion struct{}
func (m *msgNmdcVersion) NmdcEncode() string {
return nmdcCommandEncode("Version", "1,0091")
}
type msgNmdcValidateDenide struct{}
func (m *msgNmdcValidateDenide) NmdcDecode(args string) error {
return nil
}
type msgNmdcZon struct{}
func (m *msgNmdcZon) NmdcDecode(args string) error {
return nil
}