2019-03-02 15:22:20 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information
|
|
|
|
|
|
|
|
package simulate
|
|
|
|
|
|
|
|
import (
|
2019-06-04 12:55:38 +01:00
|
|
|
"context"
|
2019-03-02 15:22:20 +00:00
|
|
|
"net/http"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2019-11-08 20:40:39 +00:00
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
2019-03-02 15:22:20 +00:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/post"
|
2019-09-10 14:24:16 +01:00
|
|
|
"storj.io/storj/satellite/mailservice"
|
2019-03-02 15:22:20 +00:00
|
|
|
)
|
|
|
|
|
2019-06-04 12:55:38 +01:00
|
|
|
var mon = monkit.Package()
|
|
|
|
|
2019-09-10 14:24:16 +01:00
|
|
|
var _ mailservice.Sender = (*LinkClicker)(nil)
|
|
|
|
|
2019-03-02 15:22:20 +00:00
|
|
|
// LinkClicker is mailservice.Sender that click all links
|
|
|
|
// from html msg parts
|
2019-09-10 14:24:16 +01:00
|
|
|
//
|
|
|
|
// architecture: Service
|
2019-03-02 15:22:20 +00:00
|
|
|
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
|
2019-06-04 12:55:38 +01:00
|
|
|
func (clicker *LinkClicker) SendEmail(ctx context.Context, msg *post.Message) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-03-02 15:22:20 +00:00
|
|
|
// 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 {
|
2019-08-22 12:40:15 +01:00
|
|
|
response, err := http.Get(link)
|
2019-10-29 14:24:16 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2019-08-22 12:40:15 +01:00
|
|
|
sendError = errs.Combine(sendError, err, response.Body.Close())
|
2019-03-02 15:22:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|