gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

common.go (3674B)


      1 // GoToSocial
      2 // Copyright (C) GoToSocial Authors admin@gotosocial.org
      3 // SPDX-License-Identifier: AGPL-3.0-or-later
      4 //
      5 // This program is free software: you can redistribute it and/or modify
      6 // it under the terms of the GNU Affero General Public License as published by
      7 // the Free Software Foundation, either version 3 of the License, or
      8 // (at your option) any later version.
      9 //
     10 // This program is distributed in the hope that it will be useful,
     11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 // GNU Affero General Public License for more details.
     14 //
     15 // You should have received a copy of the GNU Affero General Public License
     16 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18 package email
     19 
     20 import (
     21 	"bytes"
     22 	"errors"
     23 	"fmt"
     24 	"net/smtp"
     25 	"os"
     26 	"path/filepath"
     27 	"strings"
     28 	"text/template"
     29 
     30 	"github.com/superseriousbusiness/gotosocial/internal/config"
     31 	"github.com/superseriousbusiness/gotosocial/internal/gtserror"
     32 )
     33 
     34 func (s *sender) sendTemplate(template string, subject string, data any, toAddresses ...string) error {
     35 	buf := &bytes.Buffer{}
     36 	if err := s.template.ExecuteTemplate(buf, template, data); err != nil {
     37 		return err
     38 	}
     39 
     40 	msg, err := assembleMessage(subject, buf.String(), s.from, toAddresses...)
     41 	if err != nil {
     42 		return err
     43 	}
     44 
     45 	if err := smtp.SendMail(s.hostAddress, s.auth, s.from, toAddresses, msg); err != nil {
     46 		return gtserror.SetType(err, gtserror.TypeSMTP)
     47 	}
     48 
     49 	return nil
     50 }
     51 
     52 func loadTemplates(templateBaseDir string) (*template.Template, error) {
     53 	if !filepath.IsAbs(templateBaseDir) {
     54 		cwd, err := os.Getwd()
     55 		if err != nil {
     56 			return nil, fmt.Errorf("error getting current working directory: %s", err)
     57 		}
     58 		templateBaseDir = filepath.Join(cwd, templateBaseDir)
     59 	}
     60 
     61 	// look for all templates that start with 'email_'
     62 	return template.ParseGlob(filepath.Join(templateBaseDir, "email_*"))
     63 }
     64 
     65 // assembleMessage assembles a valid email message following:
     66 //   - https://datatracker.ietf.org/doc/html/rfc2822
     67 //   - https://pkg.go.dev/net/smtp#SendMail
     68 func assembleMessage(mailSubject string, mailBody string, mailFrom string, mailTo ...string) ([]byte, error) {
     69 	if strings.ContainsAny(mailSubject, "\r\n") {
     70 		return nil, errors.New("email subject must not contain newline characters")
     71 	}
     72 
     73 	if strings.ContainsAny(mailFrom, "\r\n") {
     74 		return nil, errors.New("email from address must not contain newline characters")
     75 	}
     76 
     77 	for _, to := range mailTo {
     78 		if strings.ContainsAny(to, "\r\n") {
     79 			return nil, errors.New("email to address must not contain newline characters")
     80 		}
     81 	}
     82 
     83 	// Normalize the message body to use CRLF line endings
     84 	const CRLF = "\r\n"
     85 	mailBody = strings.ReplaceAll(mailBody, CRLF, "\n")
     86 	mailBody = strings.ReplaceAll(mailBody, "\n", CRLF)
     87 
     88 	msg := bytes.Buffer{}
     89 	switch {
     90 	case len(mailTo) == 1:
     91 		// Address email directly to the one recipient.
     92 		msg.WriteString("To: " + mailTo[0] + CRLF)
     93 	case config.GetSMTPDiscloseRecipients():
     94 		// Simply address To all recipients.
     95 		msg.WriteString("To: " + strings.Join(mailTo, ", ") + CRLF)
     96 	default:
     97 		// Address To anonymous group.
     98 		//
     99 		// Email will be sent to all recipients but we shouldn't include Bcc header.
    100 		//
    101 		// From the smtp.SendMail function: 'Sending "Bcc" messages is accomplished by
    102 		// including an email address in the to parameter but not including it in the
    103 		// msg headers.'
    104 		msg.WriteString("To: Undisclosed Recipients:;" + CRLF)
    105 	}
    106 	msg.WriteString("From: " + mailFrom + CRLF)
    107 	msg.WriteString("Subject: " + mailSubject + CRLF)
    108 	msg.WriteString(CRLF)
    109 	msg.WriteString(mailBody)
    110 	msg.WriteString(CRLF)
    111 
    112 	return msg.Bytes(), nil
    113 }