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

Side by Side Diff: src/pkg/mail/message.go

Issue 4530079: code review 4530079: mail: new package. (Closed)
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:
View unified diff | Download patch
« no previous file with comments | « src/pkg/mail/Makefile ('k') | src/pkg/mail/message_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Package mail implements parsing of mail messages according to RFC 5322.
6 package mail
7
8 import (
9 "bufio"
10 "io"
11 "net/textproto"
12 "os"
13 "time"
14 )
15
16 // A Message represents a parsed mail message.
17 type Message struct {
18 Header Header
19 Body io.Reader
20 }
21
22 // ReadMessage reads a message from r.
23 // The headers are parsed, and the body of the message will be reading from r.
24 func ReadMessage(r io.Reader) (msg *Message, err os.Error) {
25 tp := textproto.NewReader(bufio.NewReader(r))
26
27 hdr, err := tp.ReadMIMEHeader()
28 if err != nil {
29 return nil, err
30 }
31
32 return &Message{
33 Header: Header(hdr),
34 Body: tp.R,
35 }, nil
36 }
37
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 }
OLDNEW
« no previous file with comments | « src/pkg/mail/Makefile ('k') | src/pkg/mail/message_test.go » ('j') | no next file with comments »

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