Browse Source

Move repo to own git and add drone CI

master
Jan-Lukas Else 4 years ago
parent
commit
d9e221a019
5 changed files with 44 additions and 25 deletions
  1. +20
    -0
      .drone.yml
  2. +1
    -1
      go.mod
  3. +15
    -15
      mail.go
  4. +6
    -7
      spamcheck.go
  5. +2
    -2
      spamcheck_test.go

+ 20
- 0
.drone.yml View File

@ -0,0 +1,20 @@
kind: pipeline
name: default
steps:
- name: publish
image: plugins/docker
settings:
username:
from_secret: docker_username
password:
from_secret: docker_password
repo: quay.io/jlelse/mailygo
registry: quay.io
tags: latest
when:
branch:
- master
event:
exclude:
- pull_request

+ 1
- 1
go.mod View File

@ -1,4 +1,4 @@
module codeberg.org/jlelse/mailygo
module git.jlel.se/jlelse/mailygo
go 1.14


+ 15
- 15
mail.go View File

@ -16,20 +16,20 @@ func sendForm(values *FormValues) {
}
func buildMessage(recipient string, date time.Time, values *FormValues) string {
var msgBuffer bytes.Buffer
_, _ = fmt.Fprintf(&msgBuffer, "From: Forms <%s>", appConfig.Sender)
_, _ = fmt.Fprintln(&msgBuffer)
_, _ = fmt.Fprintf(&msgBuffer, "To: %s", recipient)
_, _ = fmt.Fprintln(&msgBuffer)
msgBuffer := &bytes.Buffer{}
_, _ = fmt.Fprintf(msgBuffer, "From: Forms <%s>", appConfig.Sender)
_, _ = fmt.Fprintln(msgBuffer)
_, _ = fmt.Fprintf(msgBuffer, "To: %s", recipient)
_, _ = fmt.Fprintln(msgBuffer)
if replyTo := findReplyTo(values); replyTo != "" {
_, _ = fmt.Fprintf(&msgBuffer, "Reply-To: %s", replyTo)
_, _ = fmt.Fprintln(&msgBuffer)
_, _ = fmt.Fprintf(msgBuffer, "Reply-To: %s", replyTo)
_, _ = fmt.Fprintln(msgBuffer)
}
_, _ = fmt.Fprintf(&msgBuffer, "Date: %s", date.Format(time.RFC1123Z))
_, _ = fmt.Fprintln(&msgBuffer)
_, _ = fmt.Fprintf(&msgBuffer, "Subject: New submission on %s", findFormName(values))
_, _ = fmt.Fprintln(&msgBuffer)
_, _ = fmt.Fprintln(&msgBuffer)
_, _ = fmt.Fprintf(msgBuffer, "Date: %s", date.Format(time.RFC1123Z))
_, _ = fmt.Fprintln(msgBuffer)
_, _ = fmt.Fprintf(msgBuffer, "Subject: New submission on %s", findFormName(values))
_, _ = fmt.Fprintln(msgBuffer)
_, _ = fmt.Fprintln(msgBuffer)
bodyValues := removeMetaValues(values)
var keys []string
for key := range *bodyValues {
@ -37,9 +37,9 @@ func buildMessage(recipient string, date time.Time, values *FormValues) string {
}
sort.Strings(keys)
for _, key := range keys {
_, _ = fmt.Fprint(&msgBuffer, key)
_, _ = fmt.Fprint(&msgBuffer, ": ")
_, _ = fmt.Fprintln(&msgBuffer, strings.Join((*bodyValues)[key], ", "))
_, _ = fmt.Fprint(msgBuffer, key)
_, _ = fmt.Fprint(msgBuffer, ": ")
_, _ = fmt.Fprintln(msgBuffer, strings.Join((*bodyValues)[key], ", "))
}
return msgBuffer.String()
}


+ 6
- 7
spamcheck.go View File

@ -22,25 +22,24 @@ func checkValues(values *FormValues) bool {
}
}
}
return checkBlacklist(&allValues) || checkUrls(&urlsToCheck)
return checkBlacklist(allValues) || checkUrls(urlsToCheck)
}
func checkBlacklist(values *[]string) bool {
for _, value := range *values {
func checkBlacklist(values []string) bool {
for _, value := range values {
for _, blacklistedString := range appConfig.Blacklist {
if strings.Contains(strings.ToLower(value), strings.ToLower(blacklistedString)) {
return true
}
}
}
return false
}
// Only tests when GOOGLE_API_KEY is set
// Returns true when it spam
func checkUrls(urlsToCheck *[]string) bool {
if len(appConfig.GoogleApiKey) < 1 || len(*urlsToCheck) == 0 {
func checkUrls(urlsToCheck []string) bool {
if len(appConfig.GoogleApiKey) < 1 || len(urlsToCheck) == 0 {
return false
}
sb, err := safebrowsing.NewSafeBrowser(safebrowsing.Config{
@ -50,7 +49,7 @@ func checkUrls(urlsToCheck *[]string) bool {
if err != nil {
return false
}
allThreats, err := sb.LookupURLs(*urlsToCheck)
allThreats, err := sb.LookupURLs(urlsToCheck)
if err != nil {
return false
}


+ 2
- 2
spamcheck_test.go View File

@ -13,13 +13,13 @@ func Test_checkBlacklist(t *testing.T) {
}
t.Run("Allowed values", func(t *testing.T) {
prepare()
if checkBlacklist(&[]string{"Hello", "How are you?"}) == true {
if checkBlacklist([]string{"Hello", "How are you?"}) == true {
t.Error()
}
})
t.Run("Forbidden values", func(t *testing.T) {
prepare()
if checkBlacklist(&[]string{"How are you?", "Hello TeSt1"}) == false {
if checkBlacklist([]string{"How are you?", "Hello TeSt1"}) == false {
t.Error()
}
})


Loading…
Cancel
Save