| OLD | NEW |
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package memory | 5 package memory |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 net_mail "net/mail" | 9 net_mail "net/mail" |
| 10 "net/textproto" | 10 "net/textproto" |
| 11 "path/filepath" | 11 "path/filepath" |
| 12 "strings" | 12 "strings" |
| 13 "sync" | 13 "sync" |
| 14 | 14 |
| 15 "github.com/luci/gae/service/mail" | 15 "github.com/luci/gae/service/mail" |
| 16 "github.com/luci/gae/service/user" | 16 "github.com/luci/gae/service/user" |
| 17 "golang.org/x/net/context" | 17 "golang.org/x/net/context" |
| 18 ) | 18 ) |
| 19 | 19 |
| 20 type mailData struct { | 20 type mailData struct { |
| 21 sync.Mutex | 21 sync.Mutex |
| 22 queue []*mail.TestMessage | 22 queue []*mail.TestMessage |
| 23 admins []string | 23 admins []string |
| 24 adminsPlain []string | 24 adminsPlain []string |
| 25 } | 25 } |
| 26 | 26 |
| 27 // mailImpl is a contextual pointer to the current mailData. | 27 // mailImpl is a contextual pointer to the current mailData. |
| 28 type mailImpl struct { | 28 type mailImpl struct { |
| 29 context.Context |
| 30 |
| 29 data *mailData | 31 data *mailData |
| 30 | |
| 31 c context.Context | |
| 32 } | 32 } |
| 33 | 33 |
| 34 var _ mail.Interface = (*mailImpl)(nil) | 34 var _ mail.RawInterface = (*mailImpl)(nil) |
| 35 | 35 |
| 36 // useMail adds a mail.Interface implementation to context, accessible | 36 // useMail adds a mail.RawInterface implementation to context, accessible |
| 37 // by mail.Get(c) | 37 // by mail.Raw(c) or the exported mail methods. |
| 38 func useMail(c context.Context) context.Context { | 38 func useMail(c context.Context) context.Context { |
| 39 data := &mailData{ | 39 data := &mailData{ |
| 40 admins: []string{"admin@example.com"}, | 40 admins: []string{"admin@example.com"}, |
| 41 adminsPlain: []string{"admin@example.com"}, | 41 adminsPlain: []string{"admin@example.com"}, |
| 42 } | 42 } |
| 43 | 43 |
| 44 » return mail.SetFactory(c, func(c context.Context) mail.Interface { | 44 » return mail.SetFactory(c, func(ic context.Context) mail.RawInterface { |
| 45 » » return &mailImpl{data, c} | 45 » » return &mailImpl{ic, data} |
| 46 }) | 46 }) |
| 47 } | 47 } |
| 48 | 48 |
| 49 func parseEmails(emails ...string) error { | 49 func parseEmails(emails ...string) error { |
| 50 for _, e := range emails { | 50 for _, e := range emails { |
| 51 if _, err := net_mail.ParseAddress(e); err != nil { | 51 if _, err := net_mail.ParseAddress(e); err != nil { |
| 52 return fmt.Errorf("invalid email (%q): %s", e, err) | 52 return fmt.Errorf("invalid email (%q): %s", e, err) |
| 53 } | 53 } |
| 54 } | 54 } |
| 55 return nil | 55 return nil |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 vals := msg.Headers[k] | 121 vals := msg.Headers[k] |
| 122 delete(msg.Headers, k) | 122 delete(msg.Headers, k) |
| 123 msg.Headers[canonK] = vals | 123 msg.Headers[canonK] = vals |
| 124 } | 124 } |
| 125 | 125 |
| 126 return nil | 126 return nil |
| 127 } | 127 } |
| 128 | 128 |
| 129 func (m *mailImpl) sendImpl(msg *mail.Message) error { | 129 func (m *mailImpl) sendImpl(msg *mail.Message) error { |
| 130 email := "" | 130 email := "" |
| 131 » userSvc := user.Get(m.c) | 131 » if u := user.Current(m); u != nil { |
| 132 » if u := userSvc.Current(); u != nil { | |
| 133 email = u.Email | 132 email = u.Email |
| 134 } | 133 } |
| 135 | 134 |
| 136 m.data.Lock() | 135 m.data.Lock() |
| 137 adminsPlain := m.data.adminsPlain[:] | 136 adminsPlain := m.data.adminsPlain[:] |
| 138 m.data.Unlock() | 137 m.data.Unlock() |
| 139 | 138 |
| 140 testMsg := &mail.TestMessage{Message: *msg} | 139 testMsg := &mail.TestMessage{Message: *msg} |
| 141 | 140 |
| 142 if err := checkMessage(testMsg, adminsPlain, email); err != nil { | 141 if err := checkMessage(testMsg, adminsPlain, email); err != nil { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 157 m.data.Lock() | 156 m.data.Lock() |
| 158 ads := m.data.admins[:] | 157 ads := m.data.admins[:] |
| 159 m.data.Unlock() | 158 m.data.Unlock() |
| 160 | 159 |
| 161 msg.To = make([]string, len(ads)) | 160 msg.To = make([]string, len(ads)) |
| 162 copy(msg.To, ads) | 161 copy(msg.To, ads) |
| 163 | 162 |
| 164 return m.sendImpl(msg) | 163 return m.sendImpl(msg) |
| 165 } | 164 } |
| 166 | 165 |
| 167 func (m *mailImpl) Testable() mail.Testable { | 166 func (m *mailImpl) GetTestable() mail.Testable { return m } |
| 168 » return m | |
| 169 } | |
| 170 | 167 |
| 171 func (m *mailImpl) SetAdminEmails(emails ...string) { | 168 func (m *mailImpl) SetAdminEmails(emails ...string) { |
| 172 adminsPlain := make([]string, len(emails)) | 169 adminsPlain := make([]string, len(emails)) |
| 173 for i, e := range emails { | 170 for i, e := range emails { |
| 174 adr, err := net_mail.ParseAddress(e) | 171 adr, err := net_mail.ParseAddress(e) |
| 175 if err != nil { | 172 if err != nil { |
| 176 panic(fmt.Errorf("invalid email (%q): %s", e, err)) | 173 panic(fmt.Errorf("invalid email (%q): %s", e, err)) |
| 177 } | 174 } |
| 178 adminsPlain[i] = adr.Address | 175 adminsPlain[i] = adr.Address |
| 179 } | 176 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 194 ret[i] = m.Copy() | 191 ret[i] = m.Copy() |
| 195 } | 192 } |
| 196 return ret | 193 return ret |
| 197 } | 194 } |
| 198 | 195 |
| 199 func (m *mailImpl) Reset() { | 196 func (m *mailImpl) Reset() { |
| 200 m.data.Lock() | 197 m.data.Lock() |
| 201 m.data.queue = nil | 198 m.data.queue = nil |
| 202 m.data.Unlock() | 199 m.data.Unlock() |
| 203 } | 200 } |
| OLD | NEW |