format using go fmt

This commit is contained in:
Mohamad Damaj 2023-07-23 12:28:10 +00:00
parent 7d4a9f82c3
commit 93b5066af8

88
main.go
View file

@ -4,10 +4,15 @@ import (
cryptorand "crypto/rand" cryptorand "crypto/rand"
"database/sql" "database/sql"
"fmt" "fmt"
"github.com/akyoto/cache"
"github.com/hugmouse/maddy-password-reset/templates"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"html/template" "html/template"
"io" "io"
"log" "log"
"math/big" "math/big"
_ "modernc.org/sqlite"
"net/http" "net/http"
"net/mail" "net/mail"
"net/smtp" "net/smtp"
@ -15,11 +20,6 @@ import (
"strconv" "strconv"
"strings" "strings"
"time" "time"
_ "modernc.org/sqlite"
"github.com/akyoto/cache"
"github.com/hugmouse/maddy-password-reset/templates"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
) )
const ( const (
@ -182,49 +182,49 @@ func main() {
if err != nil { if err != nil {
log.Println("[AddressParser]: Invalid mail address: ", err) log.Println("[AddressParser]: Invalid mail address: ", err)
return err return err
} }
go func() { go func() {
// Check if there is already a password reset // Check if there is already a password reset
_, exists := passwordResetCache.Get(mail) _, exists := passwordResetCache.Get(mail)
if exists { if exists {
log.Printf("[Cache] Mail %q already exists in cache, ignoring\n", mail) log.Printf("[Cache] Mail %q already exists in cache, ignoring\n", mail)
return return
} }
// Check if it's exists in Maddy db // Check if it's exists in Maddy db
// It will return an error is there is no user found // It will return an error is there is no user found
var password string var password string
err = db.QueryRow("SELECT value FROM passwords WHERE key = ?", mail).Scan(&password) err = db.QueryRow("SELECT value FROM passwords WHERE key = ?", mail).Scan(&password)
if err != nil {
log.Println("[Sqlite] An error occurred while trying to get password from Maddy database:", err)
return
}
// Generating an unique key
random := randomString(10)
passwordResetCache.Set(random, mail, CacheTime)
// Connect to the server, authenticate, set the sender and recipient,
// and send the email all in one step.
to := []string{mail}
if !DebugBypassMailSending {
msg := strings.ReplaceAll(EmailTemplate, "$TO", mail)
msg = strings.ReplaceAll(msg, "$FROM", EmailFrom)
msg = strings.ReplaceAll(msg, "$SUBJECT", EmailSubject)
msg = strings.ReplaceAll(msg, "$MESSAGE", EmailMessage)
msg = strings.ReplaceAll(msg, "$RESET_LINK", HostingURL+"reset/"+random)
err := smtp.SendMail(MXServer, auth, SMTPMailUsername, to, []byte(msg))
if err != nil { if err != nil {
log.Println("[Sqlite] An error occurred while trying to get password from Maddy database:", err) log.Println("[SMTP] Failed to send mail - ", err)
return return
} }
} else {
// Generating an unique key log.Println("[SMTP] Debug mode enabled, not sending email")
random := randomString(10) log.Println("[SMTP] Reset link:", HostingURL+"reset/"+random)
passwordResetCache.Set(random, mail, CacheTime) }
}()
// Connect to the server, authenticate, set the sender and recipient,
// and send the email all in one step.
to := []string{mail}
if !DebugBypassMailSending {
msg := strings.ReplaceAll(EmailTemplate, "$TO", mail)
msg = strings.ReplaceAll(msg, "$FROM", EmailFrom)
msg = strings.ReplaceAll(msg, "$SUBJECT", EmailSubject)
msg = strings.ReplaceAll(msg, "$MESSAGE", EmailMessage)
msg = strings.ReplaceAll(msg, "$RESET_LINK", HostingURL+"reset/"+random)
err := smtp.SendMail(MXServer, auth, SMTPMailUsername, to, []byte(msg))
if err != nil {
log.Println("[SMTP] Failed to send mail - ", err)
return
}
} else {
log.Println("[SMTP] Debug mode enabled, not sending email")
log.Println("[SMTP] Reset link:", HostingURL+"reset/"+random)
}
}()
return c.Render(http.StatusOK, "reset.gohtml", map[string]any{ return c.Render(http.StatusOK, "reset.gohtml", map[string]any{
"Sent": true, "Sent": true,