Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(272)

Delta Between Two Patch Sets: src/pkg/mail/message.go

Issue 4530079: code review 4530079: mail: new package. (Closed)
Left Patch Set: diff -r c843ae19e7c2 https://go.googlecode.com/hg/ Created 13 years, 10 months ago
Right Patch Set: diff -r 484dac8ca33e https://go.googlecode.com/hg/ Created 13 years, 10 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « src/pkg/mail/Makefile ('k') | src/pkg/mail/message_test.go » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 // Package mail implements parsing of mail messages according to RFC 5322.
6 Package mail implements parsing of mail messages according to RFC 5322.
7
8 TODO(dsymonds): Message formatting.
9 */
10 package mail 6 package mail
11 7
12 import ( 8 import (
13 "bufio" 9 "bufio"
14 "io" 10 "io"
15 "net/textproto" 11 "net/textproto"
16 "os" 12 "os"
13 "time"
17 ) 14 )
18 15
19 // A Message represents a parsed mail message. 16 // A Message represents a parsed mail message.
20 type Message struct { 17 type Message struct {
21 » Header textproto.MIMEHeader 18 » Header Header
22 19 » Body io.Reader
23 » Body io.Reader
24 } 20 }
25 21
26 // ReadMessage reads and parses a message from b. 22 // ReadMessage reads a message from r.
23 // The headers are parsed, and the body of the message will be reading from r.
27 func ReadMessage(r io.Reader) (msg *Message, err os.Error) { 24 func ReadMessage(r io.Reader) (msg *Message, err os.Error) {
28 tp := textproto.NewReader(bufio.NewReader(r)) 25 tp := textproto.NewReader(bufio.NewReader(r))
29 26
30 » msg = new(Message) 27 » hdr, err := tp.ReadMIMEHeader()
31
32 » msg.Header, err = tp.ReadMIMEHeader()
33 if err != nil { 28 if err != nil {
34 return nil, err 29 return nil, err
35 } 30 }
36 31
37 » // Body is the remainder. 32 » return &Message{
38 » msg.Body = tp.R 33 » » Header: Header(hdr),
39 34 » » Body: tp.R,
40 » return msg, nil 35 » }, nil
41 } 36 }
42 37
43 // TODO(dsymonds): Parsers for specific headers such as Date, To, etc. 38 // Layouts suitable for passing to time.Parse.
39 // These are tried in order.
40 var dateLayouts []string
41
42 func init() {
43 » // Generate layouts based on RFC 5322, section 3.3.
44
45 » dows := [...]string{"", "Mon, "} // day-of-week
46 » days := [...]string{"2", "02"} // day = 1*2DIGIT
47 » years := [...]string{"2006", "06"} // year = 4*DIGIT / 2*DIGIT
48 » seconds := [...]string{":05", ""} // second
49 » zones := [...]string{"-0700", "MST"} // zone = (("+" / "-") 4DIGIT) / "G MT" / ...
50
51 » for _, dow := range dows {
52 » » for _, day := range days {
53 » » » for _, year := range years {
54 » » » » for _, second := range seconds {
55 » » » » » for _, zone := range zones {
56 » » » » » » s := dow + day + " Jan " + year + " 15:04" + second + " " + zone
57 » » » » » » dateLayouts = append(dateLayouts , s)
58 » » » » » }
59 » » » » }
60 » » » }
61 » » }
62 » }
63 }
64
65 func parseDate(date string) (*time.Time, os.Error) {
66 » for _, layout := range dateLayouts {
67 » » t, err := time.Parse(layout, date)
68 » » if err == nil {
69 » » » return t, nil
70 » » }
71 » }
72 » return nil, os.ErrorString("mail: header could not be parsed")
73 }
74
75 // TODO(dsymonds): Parsers for more specific headers such as To, From, etc.
76
77 // A Header represents the key-value pairs in a mail message header.
78 type Header map[string][]string
79
80 // Get gets the first value associated with the given key.
81 // If there are no values associated with the key, Get returns "".
82 func (h Header) Get(key string) string {
83 » return textproto.MIMEHeader(h).Get(key)
84 }
85
86 var ErrHeaderNotPresent = os.ErrorString("mail: header not in message")
87
88 // Date parses the Date header field.
89 func (h Header) Date() (*time.Time, os.Error) {
90 » hdr := h.Get("Date")
91 » if hdr == "" {
92 » » return nil, ErrHeaderNotPresent
93 » }
94 » return parseDate(hdr)
95 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b