-
Notifications
You must be signed in to change notification settings - Fork 5
/
addresses.go
1324 lines (1264 loc) · 30.9 KB
/
addresses.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
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package mail
import (
"bytes"
"fmt"
"net"
"strings"
)
type AddressType int
const (
NormalAddressType AddressType = iota
BounceAddressType
EmptyGroupAddressType
LocalAddressType
InvalidAddressType
)
type Address struct {
id int
name string
Localpart string
Domain string
t AddressType
err error
}
func NewAddress(name, localpart, domain string) Address {
addr := Address{
name: name,
Localpart: localpart,
Domain: domain,
t: InvalidAddressType,
}
if domain != "" {
addr.t = NormalAddressType
} else if localpart != "" {
addr.t = LocalAddressType
} else if name != "" {
addr.t = EmptyGroupAddressType
} else if name == "" && localpart == "" && domain == "" {
addr.t = BounceAddressType
}
return addr
}
// Returns the name stored in this Address. The name is the RFC 2822
// display-part, or in case of memberless groups, the display-name of the
// group.
//
// A memberless group is stored as an Address whose localpart() and domain()
// are both empty.
func (a *Address) Name(avoidUTF8 bool) string {
atom := true
ascii := true
i := 0
for i < len(a.name) {
c := a.name[i]
// source: 2822 section 3.2.4
if (c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') ||
c == '!' || c == '#' || c == '$' || c == '%' ||
c == '&' || c == '\'' || c == '*' || c == '+' ||
c == '-' || c == '/' || c == '=' || c == '?' ||
c == '^' || c == '_' || c == '`' || c == '{' ||
c == '|' || c == '}' || c == '~' ||
// extra
c == ' ' {
// still an atom
} else if c >= 128 {
ascii = false
if avoidUTF8 {
atom = false
}
} else {
atom = false
}
i++
}
if atom || i == 0 {
return a.name
}
if ascii || !avoidUTF8 {
return quote(a.name, '"', '\\')
}
return encodePhrase(a.name)
}
// Returns the localpart and domain as a EString. Returns toString() if the
// type() isn't Normal or Local.
func (a *Address) lpdomain() string {
var r string
if a.t == NormalAddressType || a.t == LocalAddressType {
if a.localpartIsSensible() {
r = a.Localpart
} else {
r = quote(a.Localpart, '"', '\'')
}
}
if a.t == NormalAddressType {
r += "@" + a.Domain
}
if r == "" {
r = a.toString(false)
}
return r
}
func (a *Address) String() string {
return a.toString(false)
}
// Returns an RFC 2822 representation of this address. If \a avoidUTF8 is
// present and true (the default is false), toString() returns an address which
// avoids UTF-8 at all costs, even if that loses information.
func (a *Address) toString(avoidUTF8 bool) string {
var r string
switch a.t {
case InvalidAddressType:
r = ""
case BounceAddressType:
r = "<>"
case EmptyGroupAddressType:
r = a.Name(true) + ":;"
case LocalAddressType:
if avoidUTF8 && a.needsUnicode() {
r = "this-address@needs-unicode.invalid"
} else if a.localpartIsSensible() {
r = a.Localpart
} else {
r = quote(a.Localpart, '"', '\'')
}
case NormalAddressType:
if avoidUTF8 && a.needsUnicode() {
r = "this-address@needs-unicode.invalid"
} else {
postfix := ""
var buf bytes.Buffer
if a.name != "" {
buf.WriteString(a.Name(avoidUTF8))
buf.WriteString(" <")
postfix = ">"
}
if a.localpartIsSensible() {
buf.WriteString(a.Localpart)
} else {
buf.WriteString(quote(a.Localpart, '"', '\''))
}
buf.WriteByte('@')
buf.WriteString(a.Domain)
buf.WriteString(postfix)
r = buf.String()
}
}
return r
}
// Returns true if this is a sensible-looking localpart, and false if it needs
// quoting. We should never permit one of our users to need quoting, but we
// must permit foreign addresses that do.
func (a *Address) localpartIsSensible() bool {
if a.Localpart == "" {
return false
}
i := 0
for i < len(a.Localpart) {
c := a.Localpart[i]
if c == '.' {
if a.Localpart[i+1] == '.' {
return false
}
} else if !((c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') ||
c == '!' || c == '#' ||
c == '$' || c == '%' ||
c == '&' || c == '\'' ||
c == '*' || c == '+' ||
c == '-' || c == '/' ||
c == '=' || c == '?' ||
c == '^' || c == '_' ||
c == '`' || c == '{' ||
c == '|' || c == '}' ||
c == '~' || c >= 161) {
return false
}
i++
}
return true
}
// Returns true if this message needs unicode address support, and false if it
// can be transmitted over plain old SMTP.
//
// Note that the display-name can require unicode even if the address does not.
func (a *Address) needsUnicode() bool {
if isAscii(a.Localpart) && isAscii(a.Domain) {
return false
}
return true
}
type Addresses []Address
// The AddressParser class helps parse email addresses and lists.
//
// In the interests of simplicity, AddressParser parses everything as
// if it were a list of addresses - either of the mailbox-list or
// address-list productions in RFC 2822. The user of this class must
// check that the supplied addresses fit the (often more specific)
// requirements.
//
// AddressParser supports most of RFC 822 and 2822, but mostly omits
// address groups. An empty address group is translated into a single
// Address, a nonempty group is translated into the equivalent number
// of addresses.
//
// AddressParser does not attempt to canonicalize the addresses
// parsed or get rid of duplicates (To: ams@oryx.com, ams@ory.com),
// it only parses.
//
// The first error seen is stored and can be accessed using error().
type AddressParser struct {
s string
firstError error
recentError error
Addresses Addresses
lastComment string
}
/*
address = mailbox / group
mailbox = name-addr / addr-spec
name-addr = [display-name] angle-addr
angle-addr = [CFWS] "<" addr-spec ">" [CFWS] / obs-angle-addr
group = display-name ":" [mailbox-list / CFWS] ";"
[CFWS]
display-name = phrase
mailbox-list = (mailbox *("," mailbox)) / obs-mbox-list
address-list = (address *("," address)) / obs-addr-list
addr-spec = local-part "@" Domain
local-part = dot-atom / quoted-string / obs-local-part
Domain = dot-atom / Domain-literal / obs-Domain
Domain-literal = [CFWS] "[" *([FWS] dcontent) [FWS] "]" [CFWS]
dcontent = dtext / quoted-pair
dtext = NO-WS-CTL / ; Non white space controls
%d33-90 / ; The rest of the US-ASCII
%d94-126 ; characters not including "[",
; "]", or "\"
obs-angle-addr = [CFWS] "<" [obs-route] addr-spec ">" [CFWS]
obs-route = [CFWS] obs-Domain-list ":" [CFWS]
obs-Domain-list = "@" Domain *(*(CFWS / "," ) [CFWS] "@" Domain)
obs-local-part = word *("." word)
obs-Domain = atom *("." atom)
obs-mbox-list = 1*([mailbox] [CFWS] "," [CFWS]) [mailbox]
obs-addr-list = 1*([address] [CFWS] "," [CFWS]) [address]
*/
// Constructs an Address Parser parsing \a s. After construction, addresses()
// and error() may be accessed immediately.
func NewAddressParser(s string) AddressParser {
p := AddressParser{s: s}
i := len(s) - 1
j := i + 1
colon := strings.Contains(s, ":")
for i >= 0 && i < j {
j = i
i = p.address(i)
for i < j && i >= 0 &&
(s[i] == ',' ||
(!colon && s[i] == ';')) {
i--
i = p.space(i)
}
}
p.Addresses.Uniquify()
if i < 0 && p.firstError == nil {
return p
}
// Plan B: Look for '@' signs and scan for addresses around
// them. Use what's there.
p.Addresses = nil
leftBorder := 0
atsign := strings.IndexByte(s, '@')
for atsign >= 0 {
nextAtsign := strings.IndexByte(s[atsign+1:], '@')
rightBorder := 0
if nextAtsign < 0 {
rightBorder = len(s)
} else {
nextAtsign += atsign + 1
rightBorder = p.findBorder(atsign+1, nextAtsign-1)
}
if leftBorder > 0 &&
(s[leftBorder] == '.' || s[leftBorder] == '>') {
leftBorder++
}
end := atsign + 1
for end < rightBorder && s[end] == ' ' {
end++
}
for end < rightBorder &&
((s[end] >= 'a' && s[end] <= 'z') ||
(s[end] >= 'A' && s[end] <= 'Z') ||
(s[end] >= '0' && s[end] <= '9') ||
s[end] == '.' ||
s[end] == '-') {
end++
}
start := atsign
for start > leftBorder && s[start-1] == ' ' {
start--
}
for start > leftBorder &&
((s[start-1] >= 'a' && s[start-1] <= 'z') ||
(s[start-1] >= 'A' && s[start-1] <= 'Z') ||
(s[start-1] >= '0' && s[start-1] <= '9') ||
s[start-1] == '.' ||
s[start-1] == '-') {
start--
}
lp := simplify(s[start:atsign])
dom := simplify(s[atsign+1 : end])
if lp != "" && dom != "" {
addr := NewAddress("", lp, dom)
p.Addresses = append(p.Addresses, addr)
}
atsign = nextAtsign
leftBorder = rightBorder
}
if len(p.Addresses) > 0 {
p.firstError = nil
p.recentError = nil
p.Addresses.Uniquify()
return p
}
// Plan C: Is it an attempt at group syntax by someone who should
// rather be filling shelves at a supermarket?
if strings.Contains(s, ":;") && !strings.Contains(s, "@") {
ix := strings.Index(s, ":;")
n := simplify(s[:ix])
j := 0
bad := false
var buf bytes.Buffer
for j < len(n) {
if (n[j] >= 'a' && n[j] <= 'z') ||
(n[j] >= 'A' && n[j] <= 'Z') ||
(n[j] >= '0' && n[j] <= '9') {
buf.WriteByte(n[j])
} else if n[j] == ' ' || n[j] == '_' || n[j] == '-' {
buf.WriteByte('-')
} else {
bad = true
}
j++
}
if !bad {
p.firstError = nil
p.recentError = nil
addr := NewAddress(n, "", "") // FIXME: should this be buf.String() instead of n?
p.Addresses = []Address{addr}
}
}
return p
}
// Finds the point between \a left and \a right which is most likely to be the
// border between two addresses. Mucho heuristics. Never used for correct
// addresses, only when we're grasping at straws.
//
// Both \a left and \a right are considered to be possible borders, but a
// border between the extremes is preferred if possible.
func (p *AddressParser) findBorder(left, right int) int {
// if there's only one chance, that _is_ the border.
if right <= left {
return left
}
// comma?
b := left + strings.IndexByte(p.s[left:], ',')
if b >= left && b <= right {
return b
}
// semicolon? perhaps we should also guard against a dot?
b = left + strings.IndexByte(p.s[left:], ';')
if b >= left && b <= right {
return b
}
// less-than or greater-than? To: <asdf@asdf.asdf><asdf@asdf.asdf>
b = left + strings.IndexByte(p.s[left:], '<')
if b >= left && b <= right {
return b
}
b = left + strings.IndexByte(p.s[left:], '>')
if b >= left && b <= right {
return b
}
// whitespace?
b = left
for b <= right &&
p.s[b] != ' ' && p.s[b] != '\t' &&
p.s[b] != '\r' && p.s[b] != '\n' {
b++
}
if b >= left && b <= right {
return b
}
// try to scan for end of the presumed right-hand-side domain
b = left
dot := b
for b <= right {
any := false
for b <= right &&
((p.s[b] >= 'a' && p.s[b] <= 'z') ||
(p.s[b] >= 'A' && p.s[b] <= 'Z') ||
(p.s[b] >= '0' && p.s[b] <= '9') ||
p.s[b] == '-') {
any = true
b++
}
// did we see a domain component at all?
if !any {
if b > left && p.s[b-1] == '.' {
return b - 1 // no, but we just saw a dot, make that the border
}
return b // no, and no dot, so put the border here
}
if b <= right {
// if we don't see a dot here, the domain cannot go on
if p.s[b] != '.' {
return b
}
dot = b
b++
// see if the next domain component is a top-level domain
for _, tld := range tlds {
l := len(tld)
if b+l <= right {
c := p.s[b+l]
if !(c >= 'a' && c <= 'z') &&
!(c >= 'A' && c <= 'Z') &&
!(c >= '0' && c <= '9') {
if strings.ToLower(p.s[b:l]) == tld {
return b + l
}
}
}
}
}
}
// the entire area is legal in a domain, but we have to draw the
// line somewhere, so if we've seen one or more dots in the
// middle, we use the rightmost dot.
if dot > left && dot < right {
return dot
}
// the entire area is a single word. what can we do?
if right+1 >= len(p.s) {
return right
}
return left
}
// Asserts that addresses() should return a list of a single regular
// fully-qualified address. error() will return an error message if that isn't
// the case.
func (p *AddressParser) assertSingleAddress() {
normal := 0
for _, a := range p.Addresses {
if a.t == NormalAddressType {
normal++
if normal > 1 {
a.err = fmt.Errorf("This is address no. %d of 1 allowed", normal)
}
} else {
a.err = fmt.Errorf("Expected normal email address (whatever@example.com), got %s", a.toString(false))
}
}
for _, a := range p.Addresses {
if a.err != nil {
p.setError(a.err.Error(), 0)
}
}
if len(p.Addresses) == 0 {
p.setError("No address supplied", 0)
}
}
// This private helper adds the address with \a name, \a localpart and \a
// domain to the list, unless it's there already.
//
// \a name is adjusted heuristically.
func (p *AddressParser) add(name, localpart, domain string) {
// if the localpart is too long, reject the add()
if len(localpart) > 256 {
p.recentError = fmt.Errorf("localpart too long (%d characters, RFC 2821's maximum is 64): %s@%s", len(localpart), localpart, domain)
if p.firstError == nil {
p.firstError = p.recentError
}
return
}
// anti-outlook hackery, step 1: remove extra surrounding quotes
i := 0
for i < len(name)-1 &&
(name[i] == name[len(name)-1-i] &&
(name[i] == '\'' || name[i] == '"')) {
i++
}
if i > 0 {
name = name[i : len(name)-i]
}
// for names, we treat all whitespace equally. "a b" == " a b "
name = simplify(name)
// sometimes a@b (c) is munged as (c) <a@b>, let's unmunge that.
if len(name) > 1 && name[0] == '(' && name[len(name)-1] == ')' {
name = simplify(name[1 : len(name)-1])
}
// anti-outlook, step 2: if the name is the same as the address,
// just kill it.
an := strings.ToTitle(name)
if len(an) == len(localpart)+1+len(domain) &&
an == strings.ToTitle(localpart)+"@"+strings.ToTitle(domain) {
name = ""
}
a := NewAddress(name, localpart, domain)
a.err = p.recentError
// Prepend, since addresses are detected in reverse
p.Addresses = append([]Address{a}, p.Addresses...)
}
// This private function parses an address ending at position \a i and adds it
// to the list.
func (p *AddressParser) address(i int) int {
// we're presumably looking at an address
p.lastComment = ""
p.recentError = nil
i = p.comment(i)
s := p.s
for i > 0 && s[i] == ',' {
i--
i = p.comment(i)
}
for i > 1 && s[i] == '>' && s[i-1] == '>' {
i--
}
if i < 0 {
// nothing there. error of some sort.
} else if i > 0 && s[i-1] == '<' && s[i] == '>' {
// the address is <>. whether that's legal is another matter.
p.add("", "", "")
i -= 2
if i >= 0 && s[i] == '<' {
i--
}
_, i = p.phrase(i)
} else if i > 2 && s[i] == '>' && s[i-1] == ';' && s[i-2] == ':' {
// it's a microsoft-broken '<Unknown-Recipient:;>'
i -= 3
var name string
name, i = p.phrase(i)
p.add(name, "", "")
if i >= 0 && s[i] == '<' {
i--
}
} else if i > 2 && s[i] == '>' && s[i-1] == ';' &&
strings.Contains(s[:i], ":@") {
// it may be a sendmail-broken '<Unknown-Recipient:@x.y;>'
x := i
i -= 2
_, i = p.domain(i)
if i > 1 && s[i] == '@' && s[i-1] == ':' {
i -= 2
var name string
name, i = p.phrase(i)
p.add(name, "", "")
if i >= 0 && s[i] == '<' {
i--
}
} else {
i = x
}
} else if s[i] == '>' {
// name-addr
i--
var dom string
dom, i = p.domain(i)
var lp, name string
if s[i] == '<' {
lp = dom
dom = ""
} else {
if s[i] == '@' {
i--
for i > 0 && s[i] == '@' {
i--
}
aftercomment := i
i = p.comment(i)
if i >= 1 && s[i] == ';' {
j := i - 1
for j > 0 && p.s[j] == ' ' {
j--
}
if p.s[j] == ':' {
// <unlisted-recipients:; (no To-header on input)@do.ma.in>
j--
n, j := p.phrase(j)
if n != "" {
lp = ""
dom = ""
name = n
i = j
}
}
} else if aftercomment > i && i < 0 {
// To: <(Recipient list suppressed)@localhost>
n := simplify(p.lastComment)
lp = ""
dom = ""
name = ""
j := 0
var buf bytes.Buffer
for j < len(n) {
if (n[j] >= 'a' && n[j] <= 'z') ||
(n[j] >= 'A' && n[j] <= 'Z') ||
(n[j] >= '0' && n[j] <= '9') {
buf.WriteByte(n[j])
} else if n[j] == ' ' || n[j] == '_' || n[j] == '-' {
buf.WriteByte('-')
} else {
p.setError("Localpart contains parenthesis", i)
}
j++
}
name = buf.String()
} else {
lp, i = p.localpart(i)
if s[i] != '<' {
j := i
for j >= 0 &&
((s[j] >= 'a' && s[j] <= 'z') ||
(s[j] >= 'A' && s[j] <= 'Z') ||
s[j] == ' ') {
j--
}
if j >= 0 && s[j] == '<' {
tmp := s[j+1 : i+1]
if s[i+1] == ' ' {
tmp += " "
}
lp = tmp + lp
i = j
}
}
}
}
i = p.route(i)
}
if i >= 0 && s[i] == '<' {
i--
for i >= 0 && s[i] == '<' {
i--
}
var n string
n, i = p.phrase(i)
for i >= 0 && (s[i] == '@' || s[i] == '<') {
// we're looking at an unencoded 8-bit name, or at
// 'lp@domain<lp@domain>', or at 'x<y<z@domain>'. we
// react to that by ignoring the display-name.
i--
_, i = p.phrase(i)
n = ""
}
if n != "" {
name = n
}
}
// if the display-name contains unknown-8bit or the
// undisplayable marker control characters, we drop the
// display-name.
j := 0
for j < len(name) && (name[j] >= 32 && name[j] < 127) {
j++
}
if j < len(name) {
name = ""
}
p.add(name, lp, dom)
} else if i > 1 && s[i] == '=' && s[i-1] == '?' && s[i-2] == '>' {
// we're looking at "=?charset?q?safdsafsdfs<a@b>?=". how ugly.
i -= 3
var dom string
dom, i = p.domain(i)
if s[i] == '@' {
i--
for i > 0 && s[i] == '@' {
i--
}
var lp string
lp, i = p.localpart(i)
if s[i] == '<' {
i--
_, i = p.atom(i) // discard the "supplied" display-name
p.add("", lp, dom)
} else {
p.setError("Expected '<' while in =?...?...<Localpart@Domain>?=", i)
return i
}
} else {
p.setError("Expected '@' while in =?...?...<Localpart@Domain>?=", i)
return i
}
} else if s[i] == ';' && strings.Contains(s[:i], ":") {
// group
empty := true
i--
p.comment(i)
for i > 0 && s[i] != ':' {
j := i
i = p.address(i)
empty = false
if i == j {
p.setError("Parsing stopped while in group parser", i)
return i
}
if s[i] == ',' {
i--
} else if s[i] != ':' {
p.setError("Expected : or ',' while parsing group", i)
return i
}
}
if s[i] == ':' {
i--
var name string
name, i = p.phrase(i)
if empty {
p.add(name, "", "")
}
}
} else if s[i] == '"' && strings.Contains(s[:i], "%\"") {
// quite likely we're looking at x%"y@z", as once used on vms
x := i
x--
dom, x := p.domain(x)
if x > 0 && s[x] == '@' {
x--
lp, x := p.localpart(x)
if x > 2 && s[x] == '"' && s[x-1] == '%' {
x -= 2
_, x := p.domain(x)
p.add("", lp, dom)
i = x
}
}
} else if s[i] == '"' && strings.Contains(s[:i], "::") {
// we may be looking at A::B "display-name"
b := i - 1
for b > 0 && s[b] != '"' {
b--
}
name := ""
if s[b] == '"' {
var err error
name, err = decode(s[b+1:i], "us-ascii")
i = b - 1
if err != nil { // FIXME: should really check well-formedness instead
name = ""
}
name = "" // do it anyway: we don't want name <localpart>
}
var lp string
lp, i = p.atom(i)
if i > 2 && s[i] == ':' && s[i-1] == ':' {
i -= 2
var a string
a, i = p.atom(i)
lp = a + "::" + lp
p.add(name, lp, "")
} else {
p.setError("Expected NODE::USER while parsing VMS address", i)
}
} else if i > 10 && s[i] >= '0' && s[i] <= '9' && s[i-2] == '.' &&
strings.Contains(s, "\"") && strings.Contains(s, "-19") {
// we may be looking at A::B "display-name" date
x := i
for x > 0 && s[x] != '"' {
x--
}
date := simplify(strings.ToLower(s[x+1 : i]))
dp := 0
c := date[0]
for dp < len(date) &&
((c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') ||
c == ' ' || c == '-' ||
c == ':' || c == '.') {
dp++
c = date[dp]
}
if dp == len(date) && strings.Contains(date, "-19") {
// at least it resembles the kind of date field we skip
i = x
}
} else if isQuoted(s, '"', '\'') && strings.Contains(s, "@") {
wrapped := NewAddressParser(unquote(s, '"', '\''))
if wrapped.firstError == nil {
p.Addresses = append(p.Addresses, wrapped.Addresses...)
i = -1
} else {
p.setError("Unexpected quote character", i)
}
} else {
// addr-spec
name, err := decode(p.lastComment, "us-ascii")
if err != nil || strings.Contains(p.lastComment, "=?") {
name = ""
}
var dom string
dom, i = p.domain(i)
lp := ""
if i >= 0 && s[i] == '@' {
i--
for i > 0 && s[i] == '@' {
i--
}
aftercomment := i
i = p.comment(i)
if i >= 1 && s[i] == ';' {
j := i - 1
for j > 0 && p.s[j] == ' ' {
j--
}
if p.s[j] == ':' {
// unlisted-recipients:; (no To-header on input)@do.ma.in
j--
n, j := p.phrase(j)
if n != "" {
lp = ""
dom = ""
name = n
i = j
}
}
} else if aftercomment > i && i < 0 {
// To: (Recipient list suppressed)@localhost
n := simplify(p.lastComment)
lp = ""
dom = ""
name = ""
j := 0
var buf bytes.Buffer
for j < len(n) {
if (n[j] >= 'a' && n[j] <= 'z') ||
(n[j] >= 'A' && n[j] <= 'Z') ||
(n[j] >= '0' && n[j] <= '9') {
buf.WriteByte(n[j])
} else if n[j] == ' ' || n[j] == '_' || n[j] == '-' {
buf.WriteByte('-')
} else {
p.setError("Localpart contains parenthesis", i)
}
j++
}
} else {
lp, i = p.localpart(i)
}
} else {
lp = dom
dom = ""
}
i = p.route(i)
i = p.comment(i)
if lp != "" || dom != "" || name != "" {
p.add(name, lp, dom)
}
}
i = p.comment(i)
return i
}
// This private function skips past space at position \a i, or past nothing.
// Nothing is perfectly okay.
func (p *AddressParser) space(i int) int {
for i >= 0 && (p.s[i] == 32 || p.s[i] == 9 ||
p.s[i] == 13 || p.s[i] == 10) {
i--
}
return i
}
// This private function skips past a sequence of spaces and comments at \a i,
// or past nothing. Nothing is perfectly okay.
func (p *AddressParser) comment(i int) int {
i = p.space(i)
for i > 0 && p.s[i] == ')' {
j := i
// ctext = NO-WS-CTL / ; Non white space controls
//
// %d33-39 / ; The rest of the US-ASCII
// %d42-91 / ; characters not including "(",
// %d93-126 ; ")", or "\"
//
// ccontent = ctext / quoted-pair / comment
//
// comment = "(" *([FWS] ccontent) [FWS] ")"
i--
i = p.ccontent(i)
if p.s[i] != '(' {
p.setError("Unbalanced comment: ", i)
} else {
ep := newParser(p.s[i : j+1])
p.lastComment = ep.Comment()
}
if i > 0 {
i--
i = p.space(i)
}
}
return i
}
// This very private helper helps comment() handle nested comments. It advances
// \a i to the start of a comment (where it points to '(').
func (p *AddressParser) ccontent(i int) int {
for {
if i > 0 && p.s[i-1] == '\\' {
i--
} else if p.s[i] == ')' {
i = p.comment(i)
} else if p.s[i] == '(' {
return i
}
if i == 0 {
return i
}
i--
}
}
// This static helper removes quoted-pair from \a s and turns all sequences of
// spaces into a single space. It returns the result.
func unqp(s string) string {
sp := false
var buf bytes.Buffer
j := 0
for j < len(s) {
if s[j] == ' ' || s[j] == 9 ||
s[j] == 10 || s[j] == 13 {
sp = true
for s[j] == ' ' || s[j] == 9 ||
s[j] == 10 || s[j] == 13 {
j++
}
} else {
if sp {
buf.WriteByte(' ')
sp = false
}
if s[j] == '\\' {
j++
buf.WriteByte(s[j])
j++
} else {
buf.WriteByte(s[j])
j++
}
}
}
return buf.String()
}
// This private function picks up a domain ending at \a i and returns it as a
// string. The validity of the domain is not checked (and should not be - it
// may come from an old mail message) only its syntactical validity.
func (p *AddressParser) domain(i int) (string, int) {