-
Notifications
You must be signed in to change notification settings - Fork 402
/
linkclicker.go
86 lines (71 loc) · 1.76 KB
/
linkclicker.go
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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information
package simulate
import (
"context"
"net/http"
"regexp"
"strings"
"github.com/spacemonkeygo/monkit/v3"
"github.com/zeebo/errs"
"storj.io/storj/private/post"
"storj.io/storj/satellite/mailservice"
)
var mon = monkit.Package()
var _ mailservice.Sender = (*LinkClicker)(nil)
// LinkClicker is mailservice.Sender that click all links
// from html msg parts
//
// architecture: Service
type LinkClicker struct{}
// FromAddress return empty mail address.
func (clicker *LinkClicker) FromAddress() post.Address {
return post.Address{}
}
// SendEmail click all links from email html parts.
func (clicker *LinkClicker) SendEmail(ctx context.Context, msg *post.Message) (err error) {
defer mon.Task()(&ctx)(&err)
// dirty way to find links without pulling in a html dependency
regx := regexp.MustCompile(`href="([^\s])+"`)
// collect all links
var links []string
for _, part := range msg.Parts {
tags := findLinkTags(part.Content)
for _, tag := range tags {
href := regx.FindString(tag)
if href == "" {
continue
}
links = append(links, href[len(`href="`):len(href)-1])
}
}
// click all links
var sendError error
for _, link := range links {
response, err := http.Get(link)
if err != nil {
continue
}
sendError = errs.Combine(sendError, err, response.Body.Close())
}
return sendError
}
func findLinkTags(body string) []string {
var tags []string
Loop:
for {
stTag := strings.Index(body, "<a")
if stTag < 0 {
break Loop
}
stripped := body[stTag:]
endTag := strings.Index(stripped, "</a>")
if endTag < 0 {
break Loop
}
offset := endTag + len("</a>") + 1
body = stripped[offset:]
tags = append(tags, stripped[:offset])
}
return tags
}