LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 /* | 5 /* |
6 Package mail implements parsing of mail messages. | 6 Package mail implements parsing of mail messages. |
7 | 7 |
8 For the most part, this package follows the syntax as specified by RFC 5322. | 8 For the most part, this package follows the syntax as specified by RFC 5322. |
9 Notable divergences: | 9 Notable divergences: |
10 * Obsolete address formats are not parsed, including addresses with | 10 * Obsolete address formats are not parsed, including addresses with |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 } | 118 } |
119 return parseDate(hdr) | 119 return parseDate(hdr) |
120 } | 120 } |
121 | 121 |
122 // AddressList parses the named header field as a list of addresses. | 122 // AddressList parses the named header field as a list of addresses. |
123 func (h Header) AddressList(key string) ([]*Address, error) { | 123 func (h Header) AddressList(key string) ([]*Address, error) { |
124 hdr := h.Get(key) | 124 hdr := h.Get(key) |
125 if hdr == "" { | 125 if hdr == "" { |
126 return nil, ErrHeaderNotPresent | 126 return nil, ErrHeaderNotPresent |
127 } | 127 } |
128 » return newAddrParser(hdr).parseAddressList() | 128 » return ParseAddressList(hdr) |
129 } | 129 } |
130 | 130 |
131 // Address represents a single mail address. | 131 // Address represents a single mail address. |
132 // An address such as "Barry Gibbs <bg@example.com>" is represented | 132 // An address such as "Barry Gibbs <bg@example.com>" is represented |
133 // as Address{Name: "Barry Gibbs", Address: "bg@example.com"}. | 133 // as Address{Name: "Barry Gibbs", Address: "bg@example.com"}. |
134 type Address struct { | 134 type Address struct { |
135 Name string // Proper name; may be empty. | 135 Name string // Proper name; may be empty. |
136 Address string // user@domain | 136 Address string // user@domain |
| 137 } |
| 138 |
| 139 // Parses a single RFC 5322 address, e.g. "Barry Gibbs <bg@example.com>" |
| 140 func ParseAddress(address string) (*Address, error) { |
| 141 return newAddrParser(address).parseAddress() |
| 142 } |
| 143 |
| 144 // ParseAddressList parses the given string as a list of addresses. |
| 145 func ParseAddressList(list string) ([]*Address, error) { |
| 146 return newAddrParser(list).parseAddressList() |
137 } | 147 } |
138 | 148 |
139 // String formats the address as a valid RFC 5322 address. | 149 // String formats the address as a valid RFC 5322 address. |
140 // If the address's name contains non-ASCII characters | 150 // If the address's name contains non-ASCII characters |
141 // the name will be rendered according to RFC 2047. | 151 // the name will be rendered according to RFC 2047. |
142 func (a *Address) String() string { | 152 func (a *Address) String() string { |
143 s := "<" + a.Address + ">" | 153 s := "<" + a.Address + ">" |
144 if a.Name == "" { | 154 if a.Name == "" { |
145 return s | 155 return s |
146 } | 156 } |
(...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
515 return false | 525 return false |
516 } | 526 } |
517 return '!' <= c && c <= '~' | 527 return '!' <= c && c <= '~' |
518 } | 528 } |
519 | 529 |
520 // isVchar returns true if c is an RFC 5322 VCHAR character. | 530 // isVchar returns true if c is an RFC 5322 VCHAR character. |
521 func isVchar(c byte) bool { | 531 func isVchar(c byte) bool { |
522 // Visible (printing) characters. | 532 // Visible (printing) characters. |
523 return '!' <= c && c <= '~' | 533 return '!' <= c && c <= '~' |
524 } | 534 } |
LEFT | RIGHT |