town/external/cmd/help/email.go

62 lines
1.2 KiB
Go
Raw Permalink Normal View History

2023-10-25 02:41:02 +00:00
package main
import (
2023-10-25 08:42:31 +00:00
"errors"
2023-10-25 02:41:02 +00:00
"fmt"
2023-10-25 08:42:31 +00:00
"os"
2024-04-16 00:44:55 +00:00
"strings"
2023-10-25 02:41:02 +00:00
"git.tilde.town/tildetown/town/email"
)
const emailText = `hello!
You (hopefully) requested to add a new public key to your tilde.town account.
If you didn't, feel free to ignore this email (or report it to an admin).
If you did, here is your auth code: %s
To use this code, please open a terminal and run:
ssh help@tilde.town
Follow the instructions there to add your new key and restore access to your account.
best,
~vilmibm`
2023-10-25 08:42:31 +00:00
func loadPassword() (string, error) {
f, err := os.Open("/town/docs/smtp_help.pw")
if err != nil {
return "", fmt.Errorf("could not open smtp password file: %w", err)
}
pw := make([]byte, 100)
n, err := f.Read(pw)
if err != nil {
return "", fmt.Errorf("could not read smtp password file: %w", err)
}
if n == 0 {
return "", errors.New("smtp password file was empty")
}
2024-04-16 00:44:55 +00:00
return strings.TrimSpace(string(pw[0:n])), nil
2023-10-25 08:42:31 +00:00
}
2023-10-25 08:34:58 +00:00
func sendAuthCodeEmail(ac AuthCode) error {
2023-10-25 08:42:31 +00:00
pw, err := loadPassword()
2023-10-25 02:41:02 +00:00
if err != nil {
return err
}
body := fmt.Sprintf(emailText, ac.Code)
mailer := email.NewExternalMailer(pw)
return mailer.Send(
ac.Email,
"Adding a new tilde.town public key",
body)
}