From 6950ba7109df1a6344c88cb8e1105cd1a5a565a6 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Tue, 24 Oct 2023 18:58:34 +0000 Subject: [PATCH] send auth code email --- cmd/help/main.go | 9 ++++++--- cmd/review/email.go | 23 +---------------------- email/email.go | 21 +++++++++++++++++++++ 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/cmd/help/main.go b/cmd/help/main.go index 60087ef..4355d56 100644 --- a/cmd/help/main.go +++ b/cmd/help/main.go @@ -172,13 +172,16 @@ func collectEmail(db *sql.DB, cs colorScheme, tty *tty.TTY) error { Email: email, } - err = ac.Insert(db) - if err != nil { + if err = ac.Insert(db); err != nil { + // TODO log + return err + } + + if err = sendAuthCodeEmail(*ac); err != nil { // TODO log return err } - // TODO send email return nil } diff --git a/cmd/review/email.go b/cmd/review/email.go index bcd1166..f5a3da0 100644 --- a/cmd/review/email.go +++ b/cmd/review/email.go @@ -1,9 +1,7 @@ package main import ( - "errors" "fmt" - "os" "git.tilde.town/tildetown/town/email" "git.tilde.town/tildetown/town/invites" @@ -28,27 +26,8 @@ If you end up stuck, e-mail root@tilde.town with any questions. See you on the server, ~vilmibm` -func loadPassword() (string, error) { - f, err := os.Open("/town/docs/smtp.pw") - if err != nil { - return "", err - } - - pw := make([]byte, 100) - - n, err := f.Read(pw) - if err != nil { - return "", err - } - if n == 0 { - return "", errors.New("read nothing") - } - - return string(pw[0:n]), nil -} - func sendInviteEmail(invite invites.Invite) error { - pw, err := loadPassword() + pw, err := email.LoadPassword() if err != nil { return fmt.Errorf("could not read password: %w", err) } diff --git a/email/email.go b/email/email.go index 8588dc2..0acc8df 100644 --- a/email/email.go +++ b/email/email.go @@ -3,8 +3,10 @@ package email import ( "bytes" "crypto/tls" + "errors" "fmt" "net/smtp" + "os" "os/exec" ) @@ -14,6 +16,25 @@ const ( SMTPPort = 465 ) +func LoadPassword() (string, error) { + f, err := os.Open("/town/docs/smtp.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") + } + + return string(pw[0:n]), nil +} + func SendLocalEmail(username, subject, body string) error { cmd := exec.Command("/usr/sbin/sendmail", username) cmd.Stdin = bytes.NewBufferString(fmt.Sprintf("Subject: %s\n\n%s", subject, body))