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
|
package feed
import (
"bytes"
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"mime"
"net/url"
"path"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/emersion/go-message"
"github.com/emersion/go-message/mail"
"github.com/gabriel-vasile/mimetype"
"github.com/Necoro/feed2imap-go/internal/feed/template"
"github.com/Necoro/feed2imap-go/internal/http"
"github.com/Necoro/feed2imap-go/internal/msg"
"github.com/Necoro/feed2imap-go/pkg/config"
"github.com/Necoro/feed2imap-go/pkg/log"
"github.com/Necoro/feed2imap-go/pkg/version"
)
func address(name, address string) []*mail.Address {
return []*mail.Address{{Name: name, Address: address}}
}
func (item *item) fromAddress() []*mail.Address {
switch {
case item.Author != nil && item.Author.Email != "":
return address(item.Author.Name, item.Author.Email)
case item.Author != nil && item.Author.Name != "":
return address(item.Author.Name, item.defaultEmail())
case item.Feed.Author != nil && item.Feed.Author.Email != "":
return address(item.Feed.Author.Name, item.Feed.Author.Email)
case item.Feed.Author != nil && item.Feed.Author.Name != "":
return address(item.Feed.Author.Name, item.defaultEmail())
default:
return address(item.feed.Name, item.defaultEmail())
}
}
func (item *item) toAddress() []*mail.Address {
return address(item.feed.Name, item.defaultEmail())
}
func (item *item) writeHtml(writer io.Writer) error {
return template.Html.Execute(writer, item)
}
func (item *item) buildHeader() message.Header {
var h mail.Header
h.SetContentType("multipart/alternative", nil)
h.SetAddressList("From", item.fromAddress())
h.SetAddressList("To", item.toAddress())
h.Set("Message-Id", item.messageId())
h.Set(msg.VersionHeader, version.Version())
h.Set(msg.ReasonHeader, strings.Join(item.reasons, ","))
h.Set(msg.IdHeader, item.id())
if item.GUID != "" {
h.Set(msg.GuidHeader, item.GUID)
}
{ // date
date := item.DateParsed()
if date == nil {
now := time.Now()
date = &now
}
h.SetDate(*date)
}
{ // subject
subject := item.Title
if subject == "" {
subject = item.Date()
}
if subject == "" {
subject = item.Link
}
h.SetSubject(subject)
}
return h.Header
}
func (item *item) writeHtmlPart(w *message.Writer) error {
var ih message.Header
ih.SetContentType("text/html", map[string]string{"charset": "utf-8"})
ih.SetContentDisposition("inline", nil)
ih.Set("Content-Transfer-Encoding", "8bit")
partW, err := w.CreatePart(ih)
if err != nil {
return err
}
defer partW.Close()
if err = item.writeHtml(w); err != nil {
return fmt.Errorf("writing html part: %w", err)
}
return nil
}
func (img *feedImage) writeImagePart(w *message.Writer, cid string) error {
var ih message.Header
ih.SetContentType(img.mime, nil)
ih.SetContentDisposition("inline", nil)
ih.Set("Content-Transfer-Encoding", "base64")
ih.SetText("Content-ID", fmt.Sprintf("<%s>", cid))
imgW, err := w.CreatePart(ih)
if err != nil {
return err
}
defer imgW.Close()
if _, err = imgW.Write(img.image); err != nil {
return err
}
return nil
}
func (item *item) writeToBuffer(b *bytes.Buffer) error {
h := item.buildHeader()
writer, err := message.CreateWriter(b, h)
if err != nil {
return err
}
defer writer.Close()
if item.feed.Global.WithPartHtml() {
item.buildBody()
var relWriter *message.Writer
if len(item.images) > 0 {
var rh message.Header
rh.SetContentType("multipart/related", map[string]string{"type": "text/html"})
if relWriter, err = writer.CreatePart(rh); err != nil {
return err
}
defer relWriter.Close()
} else {
relWriter = writer
}
if err = item.writeHtmlPart(relWriter); err != nil {
return err
}
for idx, img := range item.images {
cid := cidNr(idx + 1)
if err = img.writeImagePart(relWriter, cid); err != nil {
return err
}
}
item.clearImages() // safe memory
}
return nil
}
func (item *item) message() (msg.Message, error) {
var b bytes.Buffer
if err := item.writeToBuffer(&b); err != nil {
return msg.Message{}, err
}
msg := msg.Message{
Content: b.String(),
IsUpdate: item.updateOnly,
ID: item.id(),
}
return msg, nil
}
func (feed *Feed) Messages() (msg.Messages, error) {
var (
err error
mails = make([]msg.Message, len(feed.items))
)
for idx := range feed.items {
if mails[idx], err = feed.items[idx].message(); err != nil {
return nil, fmt.Errorf("creating mails for %s: %w", feed.Name, err)
}
}
return mails, nil
}
func getImage(src string, timeout int, disableTLS bool) ([]byte, string, error) {
resp, cancel, err := http.Get(src, timeout, disableTLS)
if err != nil {
return nil, "", fmt.Errorf("fetching from '%s': %w", src, err)
}
defer cancel()
img, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, "", fmt.Errorf("reading from '%s': %w", src, err)
}
var mimeStr string
ext := path.Ext(src)
if ext == "" {
mimeStr = mimetype.Detect(img).String()
} else {
mimeStr = mime.TypeByExtension(ext)
}
return img, mimeStr, nil
}
func cidNr(idx int) string {
return fmt.Sprintf("cid_%d", idx)
}
func getBody(content, description string, bodyCfg config.Body) string {
switch bodyCfg {
case "default":
if content != "" {
return content
}
return description
case "description":
return description
case "content":
return content
case "both":
return description + content
default:
panic(fmt.Sprintf("Unknown value for Body: %v", bodyCfg))
}
}
func (item *item) buildBody() {
feed := item.feed
feedUrl, err := url.Parse(feed.Url)
if err != nil {
panic(fmt.Sprintf("URL '%s' of feed '%s' is not a valid URL. How have we ended up here?", feed.Url, feed.Name))
}
body := getBody(item.Content, item.Description, feed.Body)
if !feed.InclImages {
item.Body = body
return
}
doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
if err != nil {
log.Errorf("Feed %s: Item %s: Error while parsing html content: %s", feed.Name, item.Link, err)
item.Body = body
return
}
doneAnything := true
nodes := doc.Find("img")
nodes.Each(func(i int, selection *goquery.Selection) {
const attr = "src"
src, ok := selection.Attr(attr)
if !ok {
return
}
srcUrl, err := url.Parse(src)
if err != nil {
log.Errorf("Feed %s: Item %s: Error parsing URL '%s' embedded in item: %s",
feed.Name, item.Link, src, err)
return
}
imgUrl := feedUrl.ResolveReference(srcUrl)
img, mime, err := getImage(imgUrl.String(), feed.Global.Timeout, feed.NoTLS)
if err != nil {
log.Errorf("Feed %s: Item %s: Error fetching image: %s",
feed.Name, item.Link, err)
return
}
if img == nil {
return
}
if feed.EmbedImages {
imgStr := "data:" + mime + ";base64," + base64.StdEncoding.EncodeToString(img)
selection.SetAttr(attr, imgStr)
} else {
idx := item.addImage(img, mime)
cid := "cid:" + cidNr(idx)
selection.SetAttr(attr, cid)
}
doneAnything = true
})
if doneAnything {
html, err := doc.Find("body").Html()
if err != nil {
item.clearImages()
log.Errorf("Feed %s: Item %s: Error during rendering HTML: %s",
feed.Name, item.Link, err)
} else {
body = html
}
}
item.Body = body
}
|