-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Major refactor to enable usage as a library
- Loading branch information
Bradley Kemp
committed
Jul 25, 2020
1 parent
5ecefae
commit 2c03eef
Showing
8 changed files
with
316 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"html/template" | ||
"io/ioutil" | ||
"net/url" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/bradleyjkemp/abwhose/matchers" | ||
) | ||
|
||
func emailTemplateConfigured() bool { | ||
_, configured := os.LookupEnv("ABWHOSE_MAILTO_TEMPLATE") | ||
return configured | ||
} | ||
|
||
func offerToSendEmail(u *url.URL, contact matchers.AbuseEmail) { | ||
emailTemplateFile, _ := os.LookupEnv("ABWHOSE_MAILTO_TEMPLATE") | ||
emailTemplateContents, err := ioutil.ReadFile(emailTemplateFile) | ||
if err != nil { | ||
fmt.Printf("Failed reading email template: %v\n", err) | ||
return | ||
} | ||
mailto := &bytes.Buffer{} | ||
err = template.Must(template.New("email").Parse(string(emailTemplateContents))).Execute(mailto, map[string]interface{}{ | ||
"domain": strings.Replace(u.Hostname(), ".", "[.]", -1), | ||
"url": strings.Replace(u.Hostname(), ".", "[.]", -1) + u.RawPath + u.RawQuery, | ||
"recipient": contact.Email, | ||
}) | ||
if err != nil { | ||
fmt.Printf("Error templating email: %v\n", err) | ||
return | ||
} | ||
fmt.Printf(" Send email to %s? [Y/n] ", contact.Email) | ||
if userSaysYes() { | ||
exec.Command("open", mailto.String()).Run() | ||
} | ||
} | ||
|
||
func userSaysYes() bool { | ||
var response string | ||
_, err := fmt.Scanln(&response) | ||
if err != nil && err.Error() != "unexpected newline" { | ||
panic(err) | ||
} | ||
okayResponses := map[string]bool{ | ||
"": true, | ||
"y": true, | ||
"yes": true, | ||
} | ||
if okayResponses[strings.ToLower(response)] { | ||
return true | ||
} | ||
return false | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.