2020-07-31 11:48:37 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2020-08-02 16:20:50 +00:00
|
|
|
"crypto/tls"
|
2021-03-15 21:57:42 +00:00
|
|
|
"crypto/x509"
|
|
|
|
"errors"
|
2020-07-31 11:48:37 +00:00
|
|
|
"fmt"
|
|
|
|
"html/template"
|
2021-02-18 18:26:23 +00:00
|
|
|
"io"
|
2021-05-07 15:33:44 +00:00
|
|
|
"io/fs"
|
2024-08-09 20:11:18 +00:00
|
|
|
"net/http"
|
2023-06-18 18:38:09 +00:00
|
|
|
"net/url"
|
2021-03-15 21:57:42 +00:00
|
|
|
"os"
|
2021-04-02 14:56:34 +00:00
|
|
|
"strconv"
|
2020-07-31 11:48:37 +00:00
|
|
|
"strings"
|
2021-02-18 18:26:23 +00:00
|
|
|
textTemplate "text/template"
|
2020-07-31 11:48:37 +00:00
|
|
|
"time"
|
2020-08-16 12:36:54 +00:00
|
|
|
|
2021-02-18 14:58:53 +00:00
|
|
|
"github.com/gomarkdown/markdown"
|
|
|
|
"github.com/gomarkdown/markdown/html"
|
2023-10-05 11:32:25 +00:00
|
|
|
"github.com/hrfee/jfa-go/easyproxy"
|
2024-08-01 19:17:05 +00:00
|
|
|
lm "github.com/hrfee/jfa-go/logmessages"
|
2023-06-24 16:01:52 +00:00
|
|
|
"github.com/hrfee/mediabrowser"
|
2021-04-06 20:25:44 +00:00
|
|
|
"github.com/itchyny/timefmt-go"
|
2020-08-16 12:36:54 +00:00
|
|
|
"github.com/mailgun/mailgun-go/v4"
|
2023-06-24 16:01:52 +00:00
|
|
|
"github.com/timshannon/badgerhold/v4"
|
2021-10-07 11:01:42 +00:00
|
|
|
sMail "github.com/xhit/go-simple-mail/v2"
|
2020-07-31 11:48:37 +00:00
|
|
|
)
|
|
|
|
|
2023-06-20 20:43:25 +00:00
|
|
|
var markdownRenderer = html.NewRenderer(html.RendererOptions{Flags: html.Smartypants})
|
2021-05-29 20:05:12 +00:00
|
|
|
|
2021-07-24 18:49:08 +00:00
|
|
|
// EmailClient implements email sending, right now via smtp, mailgun or a dummy client.
|
2021-05-07 00:08:12 +00:00
|
|
|
type EmailClient interface {
|
|
|
|
Send(fromName, fromAddr string, message *Message, address ...string) error
|
2020-09-13 20:07:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
// Emailer contains the email sender, translations, and methods to construct messages.
|
2020-07-31 11:48:37 +00:00
|
|
|
type Emailer struct {
|
2020-09-13 20:07:15 +00:00
|
|
|
fromAddr, fromName string
|
2021-01-19 00:29:29 +00:00
|
|
|
lang emailLang
|
2021-05-07 00:08:12 +00:00
|
|
|
sender EmailClient
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
// Message stores content.
|
|
|
|
type Message struct {
|
2021-05-07 15:33:44 +00:00
|
|
|
Subject string `json:"subject"`
|
|
|
|
HTML string `json:"html"`
|
|
|
|
Text string `json:"text"`
|
|
|
|
Markdown string `json:"markdown"`
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 11:00:20 +00:00
|
|
|
func (emailer *Emailer) formatExpiry(expiry time.Time, tzaware bool, datePattern, timePattern string) (d, t, expiresIn string) {
|
2021-04-06 20:25:44 +00:00
|
|
|
d = timefmt.Format(expiry, datePattern)
|
|
|
|
t = timefmt.Format(expiry, timePattern)
|
2020-09-15 11:00:20 +00:00
|
|
|
currentTime := time.Now()
|
2020-07-31 11:48:37 +00:00
|
|
|
if tzaware {
|
2020-09-15 11:00:20 +00:00
|
|
|
currentTime = currentTime.UTC()
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
2020-09-15 11:00:20 +00:00
|
|
|
_, _, days, hours, minutes, _ := timeDiff(expiry, currentTime)
|
2020-07-31 11:48:37 +00:00
|
|
|
if days != 0 {
|
2021-04-02 14:56:34 +00:00
|
|
|
expiresIn += strconv.Itoa(days) + "d "
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
if hours != 0 {
|
2021-04-02 14:56:34 +00:00
|
|
|
expiresIn += strconv.Itoa(hours) + "h "
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
if minutes != 0 {
|
2021-04-02 14:56:34 +00:00
|
|
|
expiresIn += strconv.Itoa(minutes) + "m "
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
2020-09-15 11:00:20 +00:00
|
|
|
expiresIn = strings.TrimSuffix(expiresIn, " ")
|
2020-07-31 11:48:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-15 11:00:20 +00:00
|
|
|
// NewEmailer configures and returns a new emailer.
|
2020-09-13 20:07:15 +00:00
|
|
|
func NewEmailer(app *appContext) *Emailer {
|
|
|
|
emailer := &Emailer{
|
|
|
|
fromAddr: app.config.Section("email").Key("address").String(),
|
|
|
|
fromName: app.config.Section("email").Key("from").String(),
|
2021-01-19 00:29:29 +00:00
|
|
|
lang: app.storage.lang.Email[app.storage.lang.chosenEmailLang],
|
2020-09-13 20:07:15 +00:00
|
|
|
}
|
|
|
|
method := app.config.Section("email").Key("method").String()
|
|
|
|
if method == "smtp" {
|
2021-07-24 18:49:08 +00:00
|
|
|
sslTLS := false
|
2020-09-13 20:07:15 +00:00
|
|
|
if app.config.Section("smtp").Key("encryption").String() == "ssl_tls" {
|
2021-07-24 18:49:08 +00:00
|
|
|
sslTLS = true
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
2021-10-07 11:01:42 +00:00
|
|
|
username := app.config.Section("smtp").Key("username").MustString("")
|
|
|
|
password := app.config.Section("smtp").Key("password").String()
|
|
|
|
if username == "" && password != "" {
|
2020-10-11 21:32:51 +00:00
|
|
|
username = emailer.fromAddr
|
|
|
|
}
|
2023-10-05 11:32:25 +00:00
|
|
|
var proxyConf *easyproxy.ProxyConfig = nil
|
|
|
|
if app.proxyEnabled {
|
|
|
|
proxyConf = &app.proxyConfig
|
|
|
|
}
|
2023-10-14 12:28:12 +00:00
|
|
|
authType := sMail.AuthType(app.config.Section("smtp").Key("auth_type").MustInt(4))
|
|
|
|
err := emailer.NewSMTP(app.config.Section("smtp").Key("server").String(), app.config.Section("smtp").Key("port").MustInt(465), username, password, sslTLS, app.config.Section("smtp").Key("ssl_cert").MustString(""), app.config.Section("smtp").Key("hello_hostname").String(), app.config.Section("smtp").Key("cert_validation").MustBool(true), authType, proxyConf)
|
2021-03-15 21:57:42 +00:00
|
|
|
if err != nil {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.err.Printf(lm.FailedInitSMTP, err)
|
2021-03-15 21:57:42 +00:00
|
|
|
}
|
2020-09-13 20:07:15 +00:00
|
|
|
} else if method == "mailgun" {
|
2024-08-09 20:11:18 +00:00
|
|
|
emailer.NewMailgun(app.config.Section("mailgun").Key("api_url").String(), app.config.Section("mailgun").Key("api_key").String(), app.proxyTransport)
|
2021-04-08 13:20:13 +00:00
|
|
|
} else if method == "dummy" {
|
2021-05-07 00:08:12 +00:00
|
|
|
emailer.sender = &DummyClient{}
|
2020-09-13 20:07:15 +00:00
|
|
|
}
|
|
|
|
return emailer
|
|
|
|
}
|
|
|
|
|
2021-10-07 15:14:32 +00:00
|
|
|
// DummyClient just logs the email to the console for debugging purposes. It can be used by settings [email]/method to "dummy".
|
|
|
|
type DummyClient struct{}
|
|
|
|
|
|
|
|
func (dc *DummyClient) Send(fromName, fromAddr string, email *Message, address ...string) error {
|
|
|
|
fmt.Printf("FROM: %s <%s>\nTO: %s\nTEXT: %s\n", fromName, fromAddr, strings.Join(address, ", "), email.Text)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SMTP supports SSL/TLS and STARTTLS; implements EmailClient.
|
|
|
|
type SMTP struct {
|
|
|
|
Client *sMail.SMTPServer
|
2020-09-13 20:07:15 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 11:00:20 +00:00
|
|
|
// NewSMTP returns an SMTP emailClient.
|
2023-10-14 12:28:12 +00:00
|
|
|
func (emailer *Emailer) NewSMTP(server string, port int, username, password string, sslTLS bool, certPath string, helloHostname string, validateCertificate bool, authType sMail.AuthType, proxy *easyproxy.ProxyConfig) (err error) {
|
2021-10-07 11:01:42 +00:00
|
|
|
sender := &SMTP{}
|
|
|
|
sender.Client = sMail.NewSMTPClient()
|
|
|
|
if sslTLS {
|
|
|
|
sender.Client.Encryption = sMail.EncryptionSSLTLS
|
|
|
|
} else {
|
|
|
|
sender.Client.Encryption = sMail.EncryptionSTARTTLS
|
|
|
|
}
|
|
|
|
if username != "" || password != "" {
|
2023-10-14 12:28:12 +00:00
|
|
|
sender.Client.Authentication = authType
|
2021-10-07 11:01:42 +00:00
|
|
|
sender.Client.Username = username
|
|
|
|
sender.Client.Password = password
|
|
|
|
}
|
|
|
|
sender.Client.Helo = helloHostname
|
|
|
|
sender.Client.ConnectTimeout, sender.Client.SendTimeout = 15*time.Second, 15*time.Second
|
|
|
|
sender.Client.Host = server
|
|
|
|
sender.Client.Port = port
|
|
|
|
sender.Client.KeepAlive = false
|
2023-10-05 11:32:25 +00:00
|
|
|
|
2021-05-17 00:16:59 +00:00
|
|
|
// x509.SystemCertPool is unavailable on windows
|
|
|
|
if PLATFORM == "windows" {
|
2021-10-07 11:01:42 +00:00
|
|
|
sender.Client.TLSConfig = &tls.Config{
|
2021-11-09 20:18:54 +00:00
|
|
|
InsecureSkipVerify: !validateCertificate,
|
2021-10-07 11:01:42 +00:00
|
|
|
ServerName: server,
|
2021-08-26 20:03:02 +00:00
|
|
|
}
|
2023-10-05 11:32:25 +00:00
|
|
|
if proxy != nil {
|
|
|
|
sender.Client.CustomConn, err = easyproxy.NewConn(*proxy, fmt.Sprintf("%s:%d", server, port), sender.Client.TLSConfig)
|
|
|
|
}
|
2021-08-26 20:03:02 +00:00
|
|
|
emailer.sender = sender
|
2021-05-17 00:16:59 +00:00
|
|
|
return
|
|
|
|
}
|
2021-03-15 21:57:42 +00:00
|
|
|
rootCAs, err := x509.SystemCertPool()
|
|
|
|
if rootCAs == nil || err != nil {
|
|
|
|
rootCAs = x509.NewCertPool()
|
|
|
|
}
|
|
|
|
if certPath != "" {
|
|
|
|
var cert []byte
|
|
|
|
cert, err = os.ReadFile(certPath)
|
|
|
|
if rootCAs.AppendCertsFromPEM(cert) == false {
|
|
|
|
err = errors.New("Failed to append cert to pool")
|
|
|
|
}
|
|
|
|
}
|
2021-10-07 11:01:42 +00:00
|
|
|
sender.Client.TLSConfig = &tls.Config{
|
2021-11-09 20:18:54 +00:00
|
|
|
InsecureSkipVerify: !validateCertificate,
|
2021-10-07 11:01:42 +00:00
|
|
|
ServerName: server,
|
|
|
|
RootCAs: rootCAs,
|
2021-08-26 20:03:02 +00:00
|
|
|
}
|
2023-10-05 11:32:25 +00:00
|
|
|
if proxy != nil {
|
|
|
|
sender.Client.CustomConn, err = easyproxy.NewConn(*proxy, fmt.Sprintf("%s:%d", server, port), sender.Client.TLSConfig)
|
|
|
|
}
|
2021-08-26 20:03:02 +00:00
|
|
|
emailer.sender = sender
|
2021-03-15 21:57:42 +00:00
|
|
|
return
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
|
2021-10-07 15:14:32 +00:00
|
|
|
func (sm *SMTP) Send(fromName, fromAddr string, email *Message, address ...string) error {
|
|
|
|
from := fmt.Sprintf("%s <%s>", fromName, fromAddr)
|
|
|
|
var cli *sMail.SMTPClient
|
|
|
|
var err error
|
|
|
|
cli, err = sm.Client.Connect()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer cli.Close()
|
|
|
|
e := sMail.NewMSG()
|
|
|
|
e.SetFrom(from)
|
|
|
|
e.SetSubject(email.Subject)
|
|
|
|
e.AddTo(address...)
|
2021-11-10 20:12:31 +00:00
|
|
|
e.SetBody(sMail.TextPlain, email.Text)
|
|
|
|
if email.HTML != "" {
|
|
|
|
e.AddAlternative(sMail.TextHTML, email.HTML)
|
2021-10-07 15:14:32 +00:00
|
|
|
}
|
|
|
|
err = e.Send(cli)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mailgun client implements EmailClient.
|
|
|
|
type Mailgun struct {
|
|
|
|
client *mailgun.MailgunImpl
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMailgun returns a Mailgun emailClient.
|
2024-08-09 20:11:18 +00:00
|
|
|
func (emailer *Emailer) NewMailgun(url, key string, transport *http.Transport) {
|
2021-10-07 15:14:32 +00:00
|
|
|
sender := &Mailgun{
|
|
|
|
client: mailgun.NewMailgun(strings.Split(emailer.fromAddr, "@")[1], key),
|
|
|
|
}
|
2024-08-09 20:11:18 +00:00
|
|
|
if transport != nil {
|
|
|
|
cli := sender.client.Client()
|
|
|
|
cli.Transport = transport
|
|
|
|
}
|
2021-10-07 15:14:32 +00:00
|
|
|
// Mailgun client takes the base url, so we need to trim off the end (e.g 'v3/messages')
|
|
|
|
if strings.Contains(url, "messages") {
|
|
|
|
url = url[0:strings.LastIndex(url, "/")]
|
2021-12-30 02:55:00 +00:00
|
|
|
}
|
|
|
|
if strings.Contains(url, "v3") {
|
2021-10-07 15:14:32 +00:00
|
|
|
url = url[0:strings.LastIndex(url, "/")]
|
|
|
|
}
|
|
|
|
sender.client.SetAPIBase(url)
|
|
|
|
emailer.sender = sender
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mg *Mailgun) Send(fromName, fromAddr string, email *Message, address ...string) error {
|
|
|
|
message := mg.client.NewMessage(
|
|
|
|
fmt.Sprintf("%s <%s>", fromName, fromAddr),
|
|
|
|
email.Subject,
|
|
|
|
email.Text,
|
|
|
|
)
|
|
|
|
for _, a := range address {
|
|
|
|
// Adding variable tells mailgun to do a batch send, so users don't see other recipients.
|
|
|
|
message.AddRecipientAndVariables(a, map[string]interface{}{"unique_id": a})
|
|
|
|
}
|
|
|
|
message.SetHtml(email.HTML)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
|
|
|
defer cancel()
|
|
|
|
_, _, err := mg.client.Send(ctx, message)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-18 18:26:23 +00:00
|
|
|
type templ interface {
|
|
|
|
Execute(wr io.Writer, data interface{}) error
|
|
|
|
}
|
|
|
|
|
2021-05-07 15:33:44 +00:00
|
|
|
func (emailer *Emailer) construct(app *appContext, section, keyFragment string, data map[string]interface{}) (html, text, markdown string, err error) {
|
2021-02-18 18:26:23 +00:00
|
|
|
var tpl templ
|
2021-02-18 17:47:15 +00:00
|
|
|
if substituteStrings == "" {
|
|
|
|
data["jellyfin"] = "Jellyfin"
|
|
|
|
} else {
|
|
|
|
data["jellyfin"] = substituteStrings
|
|
|
|
}
|
2021-02-18 18:26:23 +00:00
|
|
|
var keys []string
|
2021-05-07 15:33:44 +00:00
|
|
|
plaintext := app.config.Section("email").Key("plaintext").MustBool(false)
|
|
|
|
if plaintext {
|
2021-05-21 20:35:25 +00:00
|
|
|
if telegramEnabled || discordEnabled {
|
2021-05-07 15:33:44 +00:00
|
|
|
keys = []string{"text"}
|
|
|
|
text, markdown = "", ""
|
|
|
|
} else {
|
|
|
|
keys = []string{"text"}
|
|
|
|
text = ""
|
|
|
|
}
|
2021-02-18 18:26:23 +00:00
|
|
|
} else {
|
2021-05-21 20:35:25 +00:00
|
|
|
if telegramEnabled || discordEnabled {
|
2021-05-07 15:33:44 +00:00
|
|
|
keys = []string{"html", "text", "markdown"}
|
|
|
|
} else {
|
|
|
|
keys = []string{"html", "text"}
|
|
|
|
}
|
2021-02-18 18:26:23 +00:00
|
|
|
}
|
|
|
|
for _, key := range keys {
|
2021-05-07 15:33:44 +00:00
|
|
|
var filesystem fs.FS
|
|
|
|
var fpath string
|
|
|
|
if key == "markdown" {
|
|
|
|
filesystem, fpath = app.GetPath(section, keyFragment+"text")
|
|
|
|
} else {
|
|
|
|
filesystem, fpath = app.GetPath(section, keyFragment+key)
|
|
|
|
}
|
2021-02-18 18:26:23 +00:00
|
|
|
if key == "html" {
|
|
|
|
tpl, err = template.ParseFS(filesystem, fpath)
|
|
|
|
} else {
|
|
|
|
tpl, err = textTemplate.ParseFS(filesystem, fpath)
|
|
|
|
}
|
2021-01-30 19:19:12 +00:00
|
|
|
if err != nil {
|
2021-02-02 18:09:02 +00:00
|
|
|
return
|
2021-01-30 19:19:12 +00:00
|
|
|
}
|
2021-05-07 15:33:44 +00:00
|
|
|
// For constructTemplate, if "md" is found in data it's used in stead of "text".
|
|
|
|
foundMarkdown := false
|
|
|
|
if key == "markdown" {
|
|
|
|
_, foundMarkdown = data["md"]
|
|
|
|
if foundMarkdown {
|
|
|
|
data["plaintext"], data["md"] = data["md"], data["plaintext"]
|
|
|
|
}
|
|
|
|
}
|
2021-01-30 19:19:12 +00:00
|
|
|
var tplData bytes.Buffer
|
2021-02-02 18:09:02 +00:00
|
|
|
err = tpl.Execute(&tplData, data)
|
2021-01-30 19:19:12 +00:00
|
|
|
if err != nil {
|
2021-02-02 18:09:02 +00:00
|
|
|
return
|
2021-01-30 19:19:12 +00:00
|
|
|
}
|
2021-05-07 15:33:44 +00:00
|
|
|
if foundMarkdown {
|
|
|
|
data["plaintext"], data["md"] = data["md"], data["plaintext"]
|
|
|
|
}
|
2021-01-30 19:19:12 +00:00
|
|
|
if key == "html" {
|
2021-02-02 18:09:02 +00:00
|
|
|
html = tplData.String()
|
2021-05-07 15:33:44 +00:00
|
|
|
} else if key == "text" {
|
2021-02-02 18:09:02 +00:00
|
|
|
text = tplData.String()
|
2021-05-07 15:33:44 +00:00
|
|
|
} else {
|
|
|
|
markdown = tplData.String()
|
2021-01-30 19:19:12 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-02 18:09:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-22 00:43:36 +00:00
|
|
|
func (emailer *Emailer) confirmationValues(code, username, key string, app *appContext, noSub bool) map[string]interface{} {
|
2021-02-19 21:38:20 +00:00
|
|
|
template := map[string]interface{}{
|
2021-02-02 18:09:02 +00:00
|
|
|
"clickBelow": emailer.lang.EmailConfirmation.get("clickBelow"),
|
|
|
|
"ifItWasNotYou": emailer.lang.Strings.get("ifItWasNotYou"),
|
|
|
|
"confirmEmail": emailer.lang.EmailConfirmation.get("confirmEmail"),
|
2021-02-19 21:38:20 +00:00
|
|
|
"message": "",
|
2021-02-20 00:22:40 +00:00
|
|
|
"username": username,
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
|
|
|
if noSub {
|
|
|
|
template["helloUser"] = emailer.lang.Strings.get("helloUser")
|
|
|
|
empty := []string{"confirmationURL"}
|
|
|
|
for _, v := range empty {
|
|
|
|
template[v] = "{" + v + "}"
|
|
|
|
}
|
|
|
|
} else {
|
2021-05-07 20:53:29 +00:00
|
|
|
message := app.config.Section("messages").Key("message").String()
|
2024-08-13 19:39:06 +00:00
|
|
|
inviteLink := app.ExternalURI
|
2023-06-18 18:38:09 +00:00
|
|
|
if code == "" { // Personal email change
|
|
|
|
inviteLink = fmt.Sprintf("%s/my/confirm/%s", inviteLink, url.PathEscape(key))
|
|
|
|
} else { // Invite email confirmation
|
2024-08-12 17:53:46 +00:00
|
|
|
inviteLink = fmt.Sprintf("%s/invite/%s?key=%s", inviteLink, code, url.PathEscape(key))
|
2021-08-26 17:53:22 +00:00
|
|
|
}
|
2021-02-19 21:38:20 +00:00
|
|
|
template["helloUser"] = emailer.lang.Strings.template("helloUser", tmpl{"username": username})
|
|
|
|
template["confirmationURL"] = inviteLink
|
|
|
|
template["message"] = message
|
|
|
|
}
|
2021-02-22 00:43:36 +00:00
|
|
|
return template
|
|
|
|
}
|
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
func (emailer *Emailer) constructConfirmation(code, username, key string, app *appContext, noSub bool) (*Message, error) {
|
|
|
|
email := &Message{
|
2021-02-22 00:43:36 +00:00
|
|
|
Subject: app.config.Section("email_confirmation").Key("subject").MustString(emailer.lang.EmailConfirmation.get("title")),
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
template := emailer.confirmationValues(code, username, key, app, noSub)
|
2023-06-25 18:40:54 +00:00
|
|
|
message := app.storage.MustGetCustomContentKey("EmailConfirmation")
|
|
|
|
if message.Enabled {
|
2021-04-15 14:34:17 +00:00
|
|
|
content := templateEmail(
|
2023-06-25 18:40:54 +00:00
|
|
|
message.Content,
|
|
|
|
message.Variables,
|
2021-04-15 14:34:17 +00:00
|
|
|
nil,
|
|
|
|
template,
|
|
|
|
)
|
2021-02-20 22:49:59 +00:00
|
|
|
email, err = emailer.constructTemplate(email.Subject, content, app)
|
2021-02-20 00:22:40 +00:00
|
|
|
} else {
|
2021-05-07 15:33:44 +00:00
|
|
|
email.HTML, email.Text, email.Markdown, err = emailer.construct(app, "email_confirmation", "email_", template)
|
2021-02-20 00:22:40 +00:00
|
|
|
}
|
2021-02-02 18:09:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-30 19:19:12 +00:00
|
|
|
return email, nil
|
|
|
|
}
|
|
|
|
|
2021-07-16 18:38:28 +00:00
|
|
|
// username is optional, but should only be passed once.
|
|
|
|
func (emailer *Emailer) constructTemplate(subject, md string, app *appContext, username ...string) (*Message, error) {
|
|
|
|
if len(username) != 0 {
|
|
|
|
md = templateEmail(md, []string{"{username}"}, nil, map[string]interface{}{"username": username[0]})
|
|
|
|
subject = templateEmail(subject, []string{"{username}"}, nil, map[string]interface{}{"username": username[0]})
|
|
|
|
}
|
2021-05-07 00:08:12 +00:00
|
|
|
email := &Message{Subject: subject}
|
2023-06-20 20:43:25 +00:00
|
|
|
html := markdown.ToHTML([]byte(md), nil, markdownRenderer)
|
2021-02-20 01:03:11 +00:00
|
|
|
text := stripMarkdown(md)
|
2021-05-07 20:53:29 +00:00
|
|
|
message := app.config.Section("messages").Key("message").String()
|
2021-02-18 14:58:53 +00:00
|
|
|
var err error
|
2021-07-16 18:46:05 +00:00
|
|
|
data := map[string]interface{}{
|
2021-02-18 18:26:23 +00:00
|
|
|
"text": template.HTML(html),
|
|
|
|
"plaintext": text,
|
|
|
|
"message": message,
|
2021-05-07 15:33:44 +00:00
|
|
|
"md": md,
|
2021-07-16 18:46:05 +00:00
|
|
|
}
|
|
|
|
if len(username) != 0 {
|
2021-07-16 19:57:11 +00:00
|
|
|
data["username"] = username[0]
|
2021-07-16 18:46:05 +00:00
|
|
|
}
|
|
|
|
email.HTML, email.Text, email.Markdown, err = emailer.construct(app, "template_email", "email_", data)
|
2021-02-18 14:58:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return email, nil
|
|
|
|
}
|
|
|
|
|
2021-02-22 00:43:36 +00:00
|
|
|
func (emailer *Emailer) inviteValues(code string, invite Invite, app *appContext, noSub bool) map[string]interface{} {
|
2020-07-31 11:48:37 +00:00
|
|
|
expiry := invite.ValidTill
|
2020-09-15 11:00:20 +00:00
|
|
|
d, t, expiresIn := emailer.formatExpiry(expiry, false, app.datePattern, app.timePattern)
|
2021-05-07 20:53:29 +00:00
|
|
|
message := app.config.Section("messages").Key("message").String()
|
2024-08-13 19:39:06 +00:00
|
|
|
inviteLink := fmt.Sprintf("%s/invite/%s", app.ExternalURI, code)
|
2021-02-19 21:38:20 +00:00
|
|
|
template := map[string]interface{}{
|
2021-02-02 18:09:02 +00:00
|
|
|
"hello": emailer.lang.InviteEmail.get("hello"),
|
|
|
|
"youHaveBeenInvited": emailer.lang.InviteEmail.get("youHaveBeenInvited"),
|
|
|
|
"toJoin": emailer.lang.InviteEmail.get("toJoin"),
|
|
|
|
"linkButton": emailer.lang.InviteEmail.get("linkButton"),
|
2021-02-19 21:38:20 +00:00
|
|
|
"message": "",
|
2021-02-20 00:22:40 +00:00
|
|
|
"date": d,
|
|
|
|
"time": t,
|
|
|
|
"expiresInMinutes": expiresIn,
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
|
|
|
if noSub {
|
|
|
|
template["inviteExpiry"] = emailer.lang.InviteEmail.get("inviteExpiry")
|
|
|
|
empty := []string{"inviteURL"}
|
|
|
|
for _, v := range empty {
|
|
|
|
template[v] = "{" + v + "}"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
template["inviteExpiry"] = emailer.lang.InviteEmail.template("inviteExpiry", tmpl{"date": d, "time": t, "expiresInMinutes": expiresIn})
|
|
|
|
template["inviteURL"] = inviteLink
|
|
|
|
template["message"] = message
|
|
|
|
}
|
2021-02-22 00:43:36 +00:00
|
|
|
return template
|
|
|
|
}
|
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
func (emailer *Emailer) constructInvite(code string, invite Invite, app *appContext, noSub bool) (*Message, error) {
|
|
|
|
email := &Message{
|
2021-05-02 11:50:04 +00:00
|
|
|
Subject: app.config.Section("invite_emails").Key("subject").MustString(emailer.lang.InviteEmail.get("title")),
|
2021-02-22 00:43:36 +00:00
|
|
|
}
|
|
|
|
template := emailer.inviteValues(code, invite, app, noSub)
|
2021-02-19 21:38:20 +00:00
|
|
|
var err error
|
2023-06-25 18:40:54 +00:00
|
|
|
message := app.storage.MustGetCustomContentKey("InviteEmail")
|
|
|
|
if message.Enabled {
|
2021-04-15 14:34:17 +00:00
|
|
|
content := templateEmail(
|
2023-06-25 18:40:54 +00:00
|
|
|
message.Content,
|
|
|
|
message.Variables,
|
2021-04-15 14:34:17 +00:00
|
|
|
nil,
|
|
|
|
template,
|
|
|
|
)
|
2021-02-20 22:49:59 +00:00
|
|
|
email, err = emailer.constructTemplate(email.Subject, content, app)
|
2021-02-20 00:22:40 +00:00
|
|
|
} else {
|
2021-05-07 15:33:44 +00:00
|
|
|
email.HTML, email.Text, email.Markdown, err = emailer.construct(app, "invite_emails", "email_", template)
|
2021-02-20 00:22:40 +00:00
|
|
|
}
|
2021-02-02 18:09:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
2020-09-13 20:18:47 +00:00
|
|
|
return email, nil
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 00:43:36 +00:00
|
|
|
func (emailer *Emailer) expiryValues(code string, invite Invite, app *appContext, noSub bool) map[string]interface{} {
|
2020-08-16 12:36:54 +00:00
|
|
|
expiry := app.formatDatetime(invite.ValidTill)
|
2021-02-19 21:38:20 +00:00
|
|
|
template := map[string]interface{}{
|
2021-02-02 18:09:02 +00:00
|
|
|
"inviteExpired": emailer.lang.InviteExpiry.get("inviteExpired"),
|
|
|
|
"notificationNotice": emailer.lang.InviteExpiry.get("notificationNotice"),
|
2021-02-20 00:22:40 +00:00
|
|
|
"code": "\"" + code + "\"",
|
|
|
|
"time": expiry,
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
|
|
|
if noSub {
|
|
|
|
template["expiredAt"] = emailer.lang.InviteExpiry.get("expiredAt")
|
|
|
|
} else {
|
2021-02-20 00:22:40 +00:00
|
|
|
template["expiredAt"] = emailer.lang.InviteExpiry.template("expiredAt", tmpl{"code": template["code"].(string), "time": template["time"].(string)})
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
2021-02-22 00:43:36 +00:00
|
|
|
return template
|
|
|
|
}
|
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
func (emailer *Emailer) constructExpiry(code string, invite Invite, app *appContext, noSub bool) (*Message, error) {
|
|
|
|
email := &Message{
|
2021-02-22 00:43:36 +00:00
|
|
|
Subject: emailer.lang.InviteExpiry.get("title"),
|
|
|
|
}
|
2021-02-19 21:38:20 +00:00
|
|
|
var err error
|
2021-02-22 00:43:36 +00:00
|
|
|
template := emailer.expiryValues(code, invite, app, noSub)
|
2023-06-25 18:40:54 +00:00
|
|
|
message := app.storage.MustGetCustomContentKey("InviteExpiry")
|
|
|
|
if message.Enabled {
|
2021-04-15 14:34:17 +00:00
|
|
|
content := templateEmail(
|
2023-06-25 18:40:54 +00:00
|
|
|
message.Content,
|
|
|
|
message.Variables,
|
2021-04-15 14:34:17 +00:00
|
|
|
nil,
|
|
|
|
template,
|
|
|
|
)
|
2021-02-20 22:49:59 +00:00
|
|
|
email, err = emailer.constructTemplate(email.Subject, content, app)
|
2021-02-20 00:22:40 +00:00
|
|
|
} else {
|
2021-05-07 15:33:44 +00:00
|
|
|
email.HTML, email.Text, email.Markdown, err = emailer.construct(app, "notifications", "expiry_", template)
|
2021-02-20 00:22:40 +00:00
|
|
|
}
|
2021-02-02 18:09:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
2020-09-13 20:18:47 +00:00
|
|
|
return email, nil
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 00:43:36 +00:00
|
|
|
func (emailer *Emailer) createdValues(code, username, address string, invite Invite, app *appContext, noSub bool) map[string]interface{} {
|
2021-02-19 21:38:20 +00:00
|
|
|
template := map[string]interface{}{
|
|
|
|
"nameString": emailer.lang.Strings.get("name"),
|
|
|
|
"addressString": emailer.lang.Strings.get("emailAddress"),
|
|
|
|
"timeString": emailer.lang.UserCreated.get("time"),
|
|
|
|
"notificationNotice": "",
|
2021-02-20 00:22:40 +00:00
|
|
|
"code": "\"" + code + "\"",
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
|
|
|
if noSub {
|
|
|
|
template["aUserWasCreated"] = emailer.lang.UserCreated.get("aUserWasCreated")
|
|
|
|
empty := []string{"name", "address", "time"}
|
|
|
|
for _, v := range empty {
|
|
|
|
template[v] = "{" + v + "}"
|
|
|
|
}
|
2020-07-31 11:48:37 +00:00
|
|
|
} else {
|
2021-02-19 21:38:20 +00:00
|
|
|
created := app.formatDatetime(invite.Created)
|
|
|
|
var tplAddress string
|
|
|
|
if app.config.Section("email").Key("no_username").MustBool(false) {
|
|
|
|
tplAddress = "n/a"
|
|
|
|
} else {
|
|
|
|
tplAddress = address
|
|
|
|
}
|
2021-02-20 00:22:40 +00:00
|
|
|
template["aUserWasCreated"] = emailer.lang.UserCreated.template("aUserWasCreated", tmpl{"code": template["code"].(string)})
|
2021-02-19 21:38:20 +00:00
|
|
|
template["name"] = username
|
|
|
|
template["address"] = tplAddress
|
|
|
|
template["time"] = created
|
|
|
|
template["notificationNotice"] = emailer.lang.UserCreated.get("notificationNotice")
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
2021-02-22 00:43:36 +00:00
|
|
|
return template
|
|
|
|
}
|
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
func (emailer *Emailer) constructCreated(code, username, address string, invite Invite, app *appContext, noSub bool) (*Message, error) {
|
|
|
|
email := &Message{
|
2021-02-22 00:43:36 +00:00
|
|
|
Subject: emailer.lang.UserCreated.get("title"),
|
|
|
|
}
|
|
|
|
template := emailer.createdValues(code, username, address, invite, app, noSub)
|
2021-02-02 18:09:02 +00:00
|
|
|
var err error
|
2023-06-25 18:40:54 +00:00
|
|
|
message := app.storage.MustGetCustomContentKey("UserCreated")
|
|
|
|
if message.Enabled {
|
2021-04-15 14:34:17 +00:00
|
|
|
content := templateEmail(
|
2023-06-25 18:40:54 +00:00
|
|
|
message.Content,
|
|
|
|
message.Variables,
|
2021-04-15 14:34:17 +00:00
|
|
|
nil,
|
|
|
|
template,
|
|
|
|
)
|
2021-02-20 22:49:59 +00:00
|
|
|
email, err = emailer.constructTemplate(email.Subject, content, app)
|
2021-02-20 00:22:40 +00:00
|
|
|
} else {
|
2021-05-07 15:33:44 +00:00
|
|
|
email.HTML, email.Text, email.Markdown, err = emailer.construct(app, "notifications", "created_", template)
|
2021-02-20 00:22:40 +00:00
|
|
|
}
|
2021-02-02 18:09:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
2020-09-13 20:18:47 +00:00
|
|
|
return email, nil
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 00:43:36 +00:00
|
|
|
func (emailer *Emailer) resetValues(pwr PasswordReset, app *appContext, noSub bool) map[string]interface{} {
|
2020-09-15 11:00:20 +00:00
|
|
|
d, t, expiresIn := emailer.formatExpiry(pwr.Expiry, true, app.datePattern, app.timePattern)
|
2021-05-07 20:53:29 +00:00
|
|
|
message := app.config.Section("messages").Key("message").String()
|
2021-02-19 21:38:20 +00:00
|
|
|
template := map[string]interface{}{
|
2021-02-02 18:09:02 +00:00
|
|
|
"someoneHasRequestedReset": emailer.lang.PasswordReset.get("someoneHasRequestedReset"),
|
|
|
|
"ifItWasNotYou": emailer.lang.Strings.get("ifItWasNotYou"),
|
2021-02-19 21:38:20 +00:00
|
|
|
"pinString": emailer.lang.PasswordReset.get("pin"),
|
2021-03-30 21:41:28 +00:00
|
|
|
"link_reset": false,
|
2021-02-19 21:38:20 +00:00
|
|
|
"message": "",
|
2021-02-20 00:22:40 +00:00
|
|
|
"username": pwr.Username,
|
|
|
|
"date": d,
|
|
|
|
"time": t,
|
|
|
|
"expiresInMinutes": expiresIn,
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
2021-03-30 21:41:28 +00:00
|
|
|
linkResetEnabled := app.config.Section("password_resets").Key("link_reset").MustBool(false)
|
|
|
|
if linkResetEnabled {
|
|
|
|
template["ifItWasYou"] = emailer.lang.PasswordReset.get("ifItWasYouLink")
|
|
|
|
} else {
|
|
|
|
template["ifItWasYou"] = emailer.lang.PasswordReset.get("ifItWasYou")
|
|
|
|
}
|
2021-02-19 21:38:20 +00:00
|
|
|
if noSub {
|
|
|
|
template["helloUser"] = emailer.lang.Strings.get("helloUser")
|
|
|
|
template["codeExpiry"] = emailer.lang.PasswordReset.get("codeExpiry")
|
|
|
|
empty := []string{"pin"}
|
|
|
|
for _, v := range empty {
|
|
|
|
template[v] = "{" + v + "}"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
template["helloUser"] = emailer.lang.Strings.template("helloUser", tmpl{"username": pwr.Username})
|
|
|
|
template["codeExpiry"] = emailer.lang.PasswordReset.template("codeExpiry", tmpl{"date": d, "time": t, "expiresInMinutes": expiresIn})
|
2021-03-30 21:41:28 +00:00
|
|
|
if linkResetEnabled {
|
2021-10-13 14:04:22 +00:00
|
|
|
pinLink, err := app.GenResetLink(pwr.Pin)
|
|
|
|
if err == nil {
|
2021-03-30 21:41:28 +00:00
|
|
|
// Strip /invite form end of this URL, ik its ugly.
|
|
|
|
template["link_reset"] = true
|
|
|
|
template["pin"] = pinLink
|
|
|
|
// Only used in html email.
|
|
|
|
template["pin_code"] = pwr.Pin
|
|
|
|
} else {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.info.Println(lm.FailedGeneratePWRLink, err)
|
2021-03-30 21:41:28 +00:00
|
|
|
template["pin"] = pwr.Pin
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
template["pin"] = pwr.Pin
|
|
|
|
}
|
2021-02-19 21:38:20 +00:00
|
|
|
template["message"] = message
|
|
|
|
}
|
2021-02-22 00:43:36 +00:00
|
|
|
return template
|
|
|
|
}
|
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
func (emailer *Emailer) constructReset(pwr PasswordReset, app *appContext, noSub bool) (*Message, error) {
|
|
|
|
email := &Message{
|
2021-02-22 00:43:36 +00:00
|
|
|
Subject: app.config.Section("password_resets").Key("subject").MustString(emailer.lang.PasswordReset.get("title")),
|
|
|
|
}
|
|
|
|
template := emailer.resetValues(pwr, app, noSub)
|
2021-02-19 21:38:20 +00:00
|
|
|
var err error
|
2023-06-25 18:40:54 +00:00
|
|
|
message := app.storage.MustGetCustomContentKey("PasswordReset")
|
|
|
|
if message.Enabled {
|
2021-04-15 14:34:17 +00:00
|
|
|
content := templateEmail(
|
2023-06-25 18:40:54 +00:00
|
|
|
message.Content,
|
|
|
|
message.Variables,
|
2021-04-15 14:34:17 +00:00
|
|
|
nil,
|
|
|
|
template,
|
|
|
|
)
|
2021-02-20 22:49:59 +00:00
|
|
|
email, err = emailer.constructTemplate(email.Subject, content, app)
|
2021-02-20 00:22:40 +00:00
|
|
|
} else {
|
2021-05-07 15:33:44 +00:00
|
|
|
email.HTML, email.Text, email.Markdown, err = emailer.construct(app, "password_resets", "email_", template)
|
2021-02-20 00:22:40 +00:00
|
|
|
}
|
2021-02-02 18:09:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-08-01 15:31:08 +00:00
|
|
|
}
|
2020-09-13 20:18:47 +00:00
|
|
|
return email, nil
|
2020-08-01 15:31:08 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 00:43:36 +00:00
|
|
|
func (emailer *Emailer) deletedValues(reason string, app *appContext, noSub bool) map[string]interface{} {
|
2021-02-19 21:38:20 +00:00
|
|
|
template := map[string]interface{}{
|
2021-04-12 20:28:36 +00:00
|
|
|
"yourAccountWas": emailer.lang.UserDeleted.get("yourAccountWasDeleted"),
|
|
|
|
"reasonString": emailer.lang.Strings.get("reason"),
|
|
|
|
"message": "",
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
|
|
|
if noSub {
|
|
|
|
empty := []string{"reason"}
|
|
|
|
for _, v := range empty {
|
|
|
|
template[v] = "{" + v + "}"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
template["reason"] = reason
|
2021-05-07 20:53:29 +00:00
|
|
|
template["message"] = app.config.Section("messages").Key("message").String()
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
2021-02-22 00:43:36 +00:00
|
|
|
return template
|
|
|
|
}
|
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
func (emailer *Emailer) constructDeleted(reason string, app *appContext, noSub bool) (*Message, error) {
|
|
|
|
email := &Message{
|
2021-02-22 00:43:36 +00:00
|
|
|
Subject: app.config.Section("deletion").Key("subject").MustString(emailer.lang.UserDeleted.get("title")),
|
|
|
|
}
|
2021-02-19 21:38:20 +00:00
|
|
|
var err error
|
2021-02-22 00:43:36 +00:00
|
|
|
template := emailer.deletedValues(reason, app, noSub)
|
2023-06-25 18:40:54 +00:00
|
|
|
message := app.storage.MustGetCustomContentKey("UserDeleted")
|
|
|
|
if message.Enabled {
|
2021-04-15 14:34:17 +00:00
|
|
|
content := templateEmail(
|
2023-06-25 18:40:54 +00:00
|
|
|
message.Content,
|
|
|
|
message.Variables,
|
2021-04-15 14:34:17 +00:00
|
|
|
nil,
|
|
|
|
template,
|
|
|
|
)
|
2021-02-20 22:49:59 +00:00
|
|
|
email, err = emailer.constructTemplate(email.Subject, content, app)
|
2021-02-20 00:22:40 +00:00
|
|
|
} else {
|
2021-05-07 15:33:44 +00:00
|
|
|
email.HTML, email.Text, email.Markdown, err = emailer.construct(app, "deletion", "email_", template)
|
2021-02-20 00:22:40 +00:00
|
|
|
}
|
2021-02-02 18:09:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-09-17 22:50:07 +00:00
|
|
|
}
|
|
|
|
return email, nil
|
|
|
|
}
|
|
|
|
|
2021-04-12 20:28:36 +00:00
|
|
|
func (emailer *Emailer) disabledValues(reason string, app *appContext, noSub bool) map[string]interface{} {
|
|
|
|
template := map[string]interface{}{
|
|
|
|
"yourAccountWas": emailer.lang.UserDisabled.get("yourAccountWasDisabled"),
|
|
|
|
"reasonString": emailer.lang.Strings.get("reason"),
|
|
|
|
"message": "",
|
|
|
|
}
|
|
|
|
if noSub {
|
|
|
|
empty := []string{"reason"}
|
|
|
|
for _, v := range empty {
|
|
|
|
template[v] = "{" + v + "}"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
template["reason"] = reason
|
2021-05-07 20:53:29 +00:00
|
|
|
template["message"] = app.config.Section("messages").Key("message").String()
|
2021-04-12 20:28:36 +00:00
|
|
|
}
|
|
|
|
return template
|
|
|
|
}
|
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
func (emailer *Emailer) constructDisabled(reason string, app *appContext, noSub bool) (*Message, error) {
|
|
|
|
email := &Message{
|
2021-04-12 20:28:36 +00:00
|
|
|
Subject: app.config.Section("disable_enable").Key("subject_disabled").MustString(emailer.lang.UserDisabled.get("title")),
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
template := emailer.disabledValues(reason, app, noSub)
|
2023-06-25 18:40:54 +00:00
|
|
|
message := app.storage.MustGetCustomContentKey("UserDisabled")
|
|
|
|
if message.Enabled {
|
2021-04-15 14:34:17 +00:00
|
|
|
content := templateEmail(
|
2023-06-25 18:40:54 +00:00
|
|
|
message.Content,
|
|
|
|
message.Variables,
|
2021-04-15 14:34:17 +00:00
|
|
|
nil,
|
|
|
|
template,
|
|
|
|
)
|
2021-04-12 20:28:36 +00:00
|
|
|
email, err = emailer.constructTemplate(email.Subject, content, app)
|
|
|
|
} else {
|
2021-05-07 15:33:44 +00:00
|
|
|
email.HTML, email.Text, email.Markdown, err = emailer.construct(app, "disable_enable", "disabled_", template)
|
2021-04-12 20:28:36 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return email, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (emailer *Emailer) enabledValues(reason string, app *appContext, noSub bool) map[string]interface{} {
|
|
|
|
template := map[string]interface{}{
|
|
|
|
"yourAccountWas": emailer.lang.UserEnabled.get("yourAccountWasEnabled"),
|
|
|
|
"reasonString": emailer.lang.Strings.get("reason"),
|
|
|
|
"message": "",
|
|
|
|
}
|
|
|
|
if noSub {
|
|
|
|
empty := []string{"reason"}
|
|
|
|
for _, v := range empty {
|
|
|
|
template[v] = "{" + v + "}"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
template["reason"] = reason
|
2021-05-07 20:53:29 +00:00
|
|
|
template["message"] = app.config.Section("messages").Key("message").String()
|
2021-04-12 20:28:36 +00:00
|
|
|
}
|
|
|
|
return template
|
|
|
|
}
|
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
func (emailer *Emailer) constructEnabled(reason string, app *appContext, noSub bool) (*Message, error) {
|
|
|
|
email := &Message{
|
2021-04-12 20:28:36 +00:00
|
|
|
Subject: app.config.Section("disable_enable").Key("subject_enabled").MustString(emailer.lang.UserEnabled.get("title")),
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
template := emailer.enabledValues(reason, app, noSub)
|
2023-06-25 18:40:54 +00:00
|
|
|
message := app.storage.MustGetCustomContentKey("UserEnabled")
|
|
|
|
if message.Enabled {
|
2021-04-15 14:34:17 +00:00
|
|
|
content := templateEmail(
|
2023-06-25 18:40:54 +00:00
|
|
|
message.Content,
|
|
|
|
message.Variables,
|
2021-04-15 14:34:17 +00:00
|
|
|
nil,
|
|
|
|
template,
|
|
|
|
)
|
2021-04-12 20:28:36 +00:00
|
|
|
email, err = emailer.constructTemplate(email.Subject, content, app)
|
|
|
|
} else {
|
2021-05-07 15:33:44 +00:00
|
|
|
email.HTML, email.Text, email.Markdown, err = emailer.construct(app, "disable_enable", "enabled_", template)
|
2021-04-12 20:28:36 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return email, nil
|
|
|
|
}
|
|
|
|
|
2024-07-28 15:53:27 +00:00
|
|
|
func (emailer *Emailer) expiryAdjustedValues(username string, expiry time.Time, reason string, app *appContext, noSub bool, custom bool) map[string]interface{} {
|
2024-07-28 15:02:47 +00:00
|
|
|
template := map[string]interface{}{
|
|
|
|
"yourExpiryWasAdjusted": emailer.lang.UserExpiryAdjusted.get("yourExpiryWasAdjusted"),
|
|
|
|
"ifPreviouslyDisabled": emailer.lang.UserExpiryAdjusted.get("ifPreviouslyDisabled"),
|
|
|
|
"reasonString": emailer.lang.Strings.get("reason"),
|
|
|
|
"newExpiry": "",
|
|
|
|
"message": "",
|
|
|
|
}
|
|
|
|
if noSub {
|
2024-07-28 15:53:27 +00:00
|
|
|
template["helloUser"] = emailer.lang.Strings.get("helloUser")
|
2024-07-28 15:02:47 +00:00
|
|
|
empty := []string{"reason", "newExpiry"}
|
|
|
|
for _, v := range empty {
|
|
|
|
template[v] = "{" + v + "}"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
template["reason"] = reason
|
|
|
|
template["message"] = app.config.Section("messages").Key("message").String()
|
2024-07-28 15:53:27 +00:00
|
|
|
template["helloUser"] = emailer.lang.Strings.template("helloUser", tmpl{"username": username})
|
2024-07-28 15:02:47 +00:00
|
|
|
exp := app.formatDatetime(expiry)
|
|
|
|
if !expiry.IsZero() {
|
|
|
|
if custom {
|
|
|
|
template["newExpiry"] = exp
|
|
|
|
} else if !expiry.IsZero() {
|
|
|
|
template["newExpiry"] = emailer.lang.UserExpiryAdjusted.template("newExpiry", tmpl{
|
|
|
|
"date": exp,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return template
|
|
|
|
}
|
|
|
|
|
2024-07-28 15:53:27 +00:00
|
|
|
func (emailer *Emailer) constructExpiryAdjusted(username string, expiry time.Time, reason string, app *appContext, noSub bool) (*Message, error) {
|
2024-07-28 15:02:47 +00:00
|
|
|
email := &Message{
|
|
|
|
Subject: app.config.Section("user_expiry").Key("adjustment_subject").MustString(emailer.lang.UserExpiryAdjusted.get("title")),
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
var template map[string]interface{}
|
|
|
|
message := app.storage.MustGetCustomContentKey("UserExpiryAdjusted")
|
|
|
|
if message.Enabled {
|
2024-07-28 15:53:27 +00:00
|
|
|
template = emailer.expiryAdjustedValues(username, expiry, reason, app, noSub, true)
|
2024-07-28 15:02:47 +00:00
|
|
|
} else {
|
2024-07-28 15:53:27 +00:00
|
|
|
template = emailer.expiryAdjustedValues(username, expiry, reason, app, noSub, false)
|
2024-07-28 15:02:47 +00:00
|
|
|
}
|
|
|
|
if noSub {
|
|
|
|
template["newExpiry"] = emailer.lang.UserExpiryAdjusted.template("newExpiry", tmpl{
|
|
|
|
"date": "{newExpiry}",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if message.Enabled {
|
|
|
|
content := templateEmail(
|
|
|
|
message.Content,
|
|
|
|
message.Variables,
|
|
|
|
nil,
|
|
|
|
template,
|
|
|
|
)
|
|
|
|
email, err = emailer.constructTemplate(email.Subject, content, app)
|
|
|
|
} else {
|
|
|
|
email.HTML, email.Text, email.Markdown, err = emailer.construct(app, "user_expiry", "adjustment_email_", template)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return email, nil
|
|
|
|
}
|
|
|
|
|
2021-04-08 13:20:13 +00:00
|
|
|
func (emailer *Emailer) welcomeValues(username string, expiry time.Time, app *appContext, noSub bool, custom bool) map[string]interface{} {
|
2021-02-19 21:38:20 +00:00
|
|
|
template := map[string]interface{}{
|
2021-05-02 13:44:19 +00:00
|
|
|
"welcome": emailer.lang.WelcomeEmail.get("welcome"),
|
|
|
|
"youCanLoginWith": emailer.lang.WelcomeEmail.get("youCanLoginWith"),
|
|
|
|
"jellyfinURLString": emailer.lang.WelcomeEmail.get("jellyfinURL"),
|
|
|
|
"usernameString": emailer.lang.Strings.get("username"),
|
|
|
|
"message": "",
|
|
|
|
"yourAccountWillExpire": "",
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
|
|
|
if noSub {
|
2021-04-08 13:20:13 +00:00
|
|
|
empty := []string{"jellyfinURL", "username", "yourAccountWillExpire"}
|
2021-02-19 21:38:20 +00:00
|
|
|
for _, v := range empty {
|
|
|
|
template[v] = "{" + v + "}"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
template["jellyfinURL"] = app.config.Section("jellyfin").Key("public_server").String()
|
|
|
|
template["username"] = username
|
2021-05-07 20:53:29 +00:00
|
|
|
template["message"] = app.config.Section("messages").Key("message").String()
|
2021-04-08 13:20:13 +00:00
|
|
|
exp := app.formatDatetime(expiry)
|
2021-04-15 14:34:17 +00:00
|
|
|
if !expiry.IsZero() {
|
|
|
|
if custom {
|
|
|
|
template["yourAccountWillExpire"] = exp
|
|
|
|
} else if !expiry.IsZero() {
|
|
|
|
template["yourAccountWillExpire"] = emailer.lang.WelcomeEmail.template("yourAccountWillExpire", tmpl{
|
|
|
|
"date": exp,
|
|
|
|
})
|
|
|
|
}
|
2021-04-08 13:20:13 +00:00
|
|
|
}
|
2021-02-19 21:38:20 +00:00
|
|
|
}
|
2021-02-22 00:43:36 +00:00
|
|
|
return template
|
|
|
|
}
|
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
func (emailer *Emailer) constructWelcome(username string, expiry time.Time, app *appContext, noSub bool) (*Message, error) {
|
|
|
|
email := &Message{
|
2021-02-22 00:43:36 +00:00
|
|
|
Subject: app.config.Section("welcome_email").Key("subject").MustString(emailer.lang.WelcomeEmail.get("title")),
|
|
|
|
}
|
2021-02-02 18:09:02 +00:00
|
|
|
var err error
|
2021-04-08 13:20:13 +00:00
|
|
|
var template map[string]interface{}
|
2023-06-25 18:40:54 +00:00
|
|
|
message := app.storage.MustGetCustomContentKey("WelcomeEmail")
|
|
|
|
if message.Enabled {
|
2021-04-08 13:20:13 +00:00
|
|
|
template = emailer.welcomeValues(username, expiry, app, noSub, true)
|
|
|
|
} else {
|
|
|
|
template = emailer.welcomeValues(username, expiry, app, noSub, false)
|
|
|
|
}
|
|
|
|
if noSub {
|
|
|
|
template["yourAccountWillExpire"] = emailer.lang.WelcomeEmail.template("yourAccountWillExpire", tmpl{
|
|
|
|
"date": "{yourAccountWillExpire}",
|
|
|
|
})
|
|
|
|
}
|
2023-06-25 18:40:54 +00:00
|
|
|
if message.Enabled {
|
2021-04-15 14:34:17 +00:00
|
|
|
content := templateEmail(
|
2023-06-25 18:40:54 +00:00
|
|
|
message.Content,
|
|
|
|
message.Variables,
|
|
|
|
message.Conditionals,
|
2021-04-15 14:34:17 +00:00
|
|
|
template,
|
|
|
|
)
|
2021-02-20 22:49:59 +00:00
|
|
|
email, err = emailer.constructTemplate(email.Subject, content, app)
|
2021-02-20 00:22:40 +00:00
|
|
|
} else {
|
2021-05-07 15:33:44 +00:00
|
|
|
email.HTML, email.Text, email.Markdown, err = emailer.construct(app, "welcome_email", "email_", template)
|
2021-02-20 00:22:40 +00:00
|
|
|
}
|
2021-02-02 18:09:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-01-24 15:19:58 +00:00
|
|
|
}
|
|
|
|
return email, nil
|
|
|
|
}
|
|
|
|
|
2021-02-28 15:41:06 +00:00
|
|
|
func (emailer *Emailer) userExpiredValues(app *appContext, noSub bool) map[string]interface{} {
|
|
|
|
template := map[string]interface{}{
|
|
|
|
"yourAccountHasExpired": emailer.lang.UserExpired.get("yourAccountHasExpired"),
|
|
|
|
"contactTheAdmin": emailer.lang.UserExpired.get("contactTheAdmin"),
|
|
|
|
"message": "",
|
|
|
|
}
|
|
|
|
if !noSub {
|
2021-05-07 20:53:29 +00:00
|
|
|
template["message"] = app.config.Section("messages").Key("message").String()
|
2021-02-28 15:41:06 +00:00
|
|
|
}
|
|
|
|
return template
|
|
|
|
}
|
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
func (emailer *Emailer) constructUserExpired(app *appContext, noSub bool) (*Message, error) {
|
|
|
|
email := &Message{
|
2021-02-28 15:41:06 +00:00
|
|
|
Subject: app.config.Section("user_expiry").Key("subject").MustString(emailer.lang.UserExpired.get("title")),
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
template := emailer.userExpiredValues(app, noSub)
|
2023-06-25 18:40:54 +00:00
|
|
|
message := app.storage.MustGetCustomContentKey("UserExpired")
|
|
|
|
if message.Enabled {
|
2021-04-15 14:34:17 +00:00
|
|
|
content := templateEmail(
|
2023-06-25 18:40:54 +00:00
|
|
|
message.Content,
|
|
|
|
message.Variables,
|
2021-04-15 14:34:17 +00:00
|
|
|
nil,
|
|
|
|
template,
|
|
|
|
)
|
2021-02-28 15:41:06 +00:00
|
|
|
email, err = emailer.constructTemplate(email.Subject, content, app)
|
|
|
|
} else {
|
2021-05-07 15:33:44 +00:00
|
|
|
email.HTML, email.Text, email.Markdown, err = emailer.construct(app, "user_expiry", "email_", template)
|
2021-02-28 15:41:06 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return email, nil
|
|
|
|
}
|
|
|
|
|
2020-09-15 11:00:20 +00:00
|
|
|
// calls the send method in the underlying emailClient.
|
2021-05-07 00:08:12 +00:00
|
|
|
func (emailer *Emailer) send(email *Message, address ...string) error {
|
|
|
|
return emailer.sender.Send(emailer.fromName, emailer.fromAddr, email, address...)
|
2020-07-31 11:48:37 +00:00
|
|
|
}
|
2021-05-07 15:06:47 +00:00
|
|
|
|
2023-06-22 11:04:40 +00:00
|
|
|
func (app *appContext) sendByID(email *Message, ID ...string) (err error) {
|
2021-05-07 15:06:47 +00:00
|
|
|
for _, id := range ID {
|
2023-06-20 11:19:24 +00:00
|
|
|
if tgChat, ok := app.storage.GetTelegramKey(id); ok && tgChat.Contact && telegramEnabled {
|
2021-05-07 15:06:47 +00:00
|
|
|
err = app.telegram.Send(email, tgChat.ChatID)
|
2023-06-22 11:04:40 +00:00
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
2021-05-21 20:35:25 +00:00
|
|
|
}
|
2023-06-20 11:19:24 +00:00
|
|
|
if dcChat, ok := app.storage.GetDiscordKey(id); ok && dcChat.Contact && discordEnabled {
|
2021-05-21 20:35:25 +00:00
|
|
|
err = app.discord.Send(email, dcChat.ChannelID)
|
2023-06-22 11:04:40 +00:00
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
2021-05-21 20:35:25 +00:00
|
|
|
}
|
2023-06-20 11:19:24 +00:00
|
|
|
if mxChat, ok := app.storage.GetMatrixKey(id); ok && mxChat.Contact && matrixEnabled {
|
2021-07-13 18:02:16 +00:00
|
|
|
err = app.matrix.Send(email, mxChat)
|
2023-06-22 11:04:40 +00:00
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
2021-05-29 20:05:12 +00:00
|
|
|
}
|
2023-06-20 11:19:24 +00:00
|
|
|
if address, ok := app.storage.GetEmailsKey(id); ok && address.Contact && emailEnabled {
|
2021-05-21 21:46:46 +00:00
|
|
|
err = app.email.send(email, address.Addr)
|
2023-06-22 11:04:40 +00:00
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
2021-05-07 15:06:47 +00:00
|
|
|
}
|
2023-06-22 11:04:40 +00:00
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
2021-05-07 15:06:47 +00:00
|
|
|
}
|
2023-06-22 11:04:40 +00:00
|
|
|
return
|
2021-05-07 15:06:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (app *appContext) getAddressOrName(jfID string) string {
|
2023-06-20 11:19:24 +00:00
|
|
|
if dcChat, ok := app.storage.GetDiscordKey(jfID); ok && dcChat.Contact && discordEnabled {
|
2023-06-12 15:32:40 +00:00
|
|
|
return RenderDiscordUsername(dcChat)
|
2021-05-21 20:35:25 +00:00
|
|
|
}
|
2023-06-20 11:19:24 +00:00
|
|
|
if tgChat, ok := app.storage.GetTelegramKey(jfID); ok && tgChat.Contact && telegramEnabled {
|
2021-05-07 15:06:47 +00:00
|
|
|
return "@" + tgChat.Username
|
|
|
|
}
|
2023-06-20 11:19:24 +00:00
|
|
|
if addr, ok := app.storage.GetEmailsKey(jfID); ok {
|
2021-05-21 21:46:46 +00:00
|
|
|
return addr.Addr
|
2021-05-07 15:06:47 +00:00
|
|
|
}
|
2023-06-22 11:04:40 +00:00
|
|
|
if mxChat, ok := app.storage.GetMatrixKey(jfID); ok && mxChat.Contact && matrixEnabled {
|
|
|
|
return mxChat.UserID
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2023-06-22 11:33:40 +00:00
|
|
|
// ReverseUserSearch returns the jellyfin ID of the user with the given username, email, or contact method username.
|
|
|
|
// returns "" if none found. returns only the first match, might be an issue if there are users with the same contact method usernames.
|
2023-12-20 18:18:39 +00:00
|
|
|
func (app *appContext) ReverseUserSearch(address string, matchUsername, matchEmail, matchContactMethod bool) (user mediabrowser.User, ok bool) {
|
2023-06-24 16:01:52 +00:00
|
|
|
ok = false
|
2023-12-20 18:18:39 +00:00
|
|
|
var err error = nil
|
|
|
|
if matchUsername {
|
2024-08-06 13:48:31 +00:00
|
|
|
user, err = app.jf.UserByName(address, false)
|
|
|
|
if err == nil {
|
2023-12-20 18:18:39 +00:00
|
|
|
ok = true
|
|
|
|
return
|
|
|
|
}
|
2023-06-22 11:33:40 +00:00
|
|
|
}
|
2023-12-20 18:18:39 +00:00
|
|
|
|
|
|
|
if matchEmail {
|
|
|
|
emailAddresses := []EmailAddress{}
|
|
|
|
err = app.storage.db.Find(&emailAddresses, badgerhold.Where("Addr").Eq(address))
|
|
|
|
if err == nil && len(emailAddresses) > 0 {
|
|
|
|
for _, emailUser := range emailAddresses {
|
2024-08-06 13:48:31 +00:00
|
|
|
user, err = app.jf.UserByID(emailUser.JellyfinID, false)
|
|
|
|
if err == nil {
|
2023-12-20 18:18:39 +00:00
|
|
|
ok = true
|
|
|
|
return
|
|
|
|
}
|
2023-06-24 16:01:52 +00:00
|
|
|
}
|
2023-06-22 11:04:40 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-20 18:18:39 +00:00
|
|
|
|
2023-06-24 16:01:52 +00:00
|
|
|
// Dont know how we'd use badgerhold when we need to render each username,
|
|
|
|
// Apart from storing the rendered name in the db.
|
2023-12-20 18:18:39 +00:00
|
|
|
if matchContactMethod {
|
|
|
|
for _, dcUser := range app.storage.GetDiscord() {
|
|
|
|
if RenderDiscordUsername(dcUser) == strings.ToLower(address) {
|
2024-08-06 13:48:31 +00:00
|
|
|
user, err = app.jf.UserByID(dcUser.JellyfinID, false)
|
|
|
|
if err == nil {
|
2023-12-20 18:18:39 +00:00
|
|
|
ok = true
|
|
|
|
return
|
|
|
|
}
|
2023-06-24 16:01:52 +00:00
|
|
|
}
|
2023-06-22 11:04:40 +00:00
|
|
|
}
|
2023-12-20 18:18:39 +00:00
|
|
|
tgUsername := strings.TrimPrefix(address, "@")
|
|
|
|
telegramUsers := []TelegramUser{}
|
|
|
|
err = app.storage.db.Find(&telegramUsers, badgerhold.Where("Username").Eq(tgUsername))
|
|
|
|
if err == nil && len(telegramUsers) > 0 {
|
|
|
|
for _, telegramUser := range telegramUsers {
|
2024-08-06 13:48:31 +00:00
|
|
|
user, err = app.jf.UserByID(telegramUser.JellyfinID, false)
|
|
|
|
if err == nil {
|
2023-12-20 18:18:39 +00:00
|
|
|
ok = true
|
|
|
|
return
|
|
|
|
}
|
2023-06-24 16:01:52 +00:00
|
|
|
}
|
2023-06-22 11:04:40 +00:00
|
|
|
}
|
2023-12-20 18:18:39 +00:00
|
|
|
matrixUsers := []MatrixUser{}
|
|
|
|
err = app.storage.db.Find(&matrixUsers, badgerhold.Where("UserID").Eq(address))
|
|
|
|
if err == nil && len(matrixUsers) > 0 {
|
|
|
|
for _, matrixUser := range matrixUsers {
|
2024-08-06 13:48:31 +00:00
|
|
|
user, err = app.jf.UserByID(matrixUser.JellyfinID, false)
|
|
|
|
if err == nil {
|
2023-12-20 18:18:39 +00:00
|
|
|
ok = true
|
|
|
|
return
|
|
|
|
}
|
2023-06-24 16:01:52 +00:00
|
|
|
}
|
2023-06-22 11:04:40 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-24 16:01:52 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// EmailAddressExists returns whether or not a user with the given email address exists.
|
|
|
|
func (app *appContext) EmailAddressExists(address string) bool {
|
|
|
|
c, err := app.storage.db.Count(&EmailAddress{}, badgerhold.Where("Addr").Eq(address))
|
|
|
|
return err != nil || c > 0
|
2021-05-07 15:06:47 +00:00
|
|
|
}
|