summaryrefslogtreecommitdiff
path: root/internal/mailer/mailer.go
blob: d6dbd17f0a49710999cb961c8a3fefdfd17f3840 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
package mailer

import (
	"crypto/rand"
	"database/sql"
	"encoding/base64"
	"encoding/binary"
	"fmt"
	"html/template"
	"log/slog"
	"slices"
	"strconv"
	"strings"
	"sync"
	"time"

	"github.com/SayaAndy/saya-today-web/internal/b2"
	"github.com/SayaAndy/saya-today-web/internal/templatemanager"
	"github.com/SayaAndy/saya-today-web/locale"
	"github.com/dgraph-io/ristretto/v2"
	"github.com/gofiber/fiber/v2"
	"github.com/wneessen/go-mail"
	"golang.org/x/crypto/argon2"
)

type Mailer struct {
	verificationCodes *ristretto.Cache[uint64, string]
	unsubscribeCodes  *ristretto.Cache[uint64, []byte]
	db                *sql.DB
	tm                *templatemanager.TemplateManager
	mailClient        *mail.Client
	clientHost        string
	mailAddress       string
	publicName        string
	salt              []byte

	lostMailMap map[string]struct {
		Dur        time.Duration
		End        time.Time
		CodeExpiry time.Time
	}
	lostMailMapMutex sync.RWMutex

	hashMap      map[string][]byte
	hashMapMutex sync.RWMutex

	l map[string]*locale.LocaleConfig
}

type SubscriptionType int

const (
	All SubscriptionType = iota
	None
	Specific
)

func NewMailer(db *sql.DB, clientHost string, mailHost string, publicName string, mailAddress string, username string, password string, salt []byte, localization map[string]*locale.LocaleConfig) (*Mailer, error) {
	verificationCodes, err := ristretto.NewCache(&ristretto.Config[uint64, string]{
		NumCounters:            10000,
		MaxCost:                1 << 20, // 1 MB
		BufferItems:            64,
		TtlTickerDurationInSec: 3600, // 1 hour
	})
	if err != nil {
		return nil, fmt.Errorf("fail to initialize cache for verification codes: %w", err)
	}

	unsubscribeCodes, err := ristretto.NewCache(&ristretto.Config[uint64, []byte]{
		NumCounters:            10000,
		MaxCost:                1 << 20, // 1 MB
		BufferItems:            64,
		TtlTickerDurationInSec: 86400, // 1 day
	})
	if err != nil {
		return nil, fmt.Errorf("fail to initialize cache for verification codes: %w", err)
	}

	tm, err := templatemanager.NewTemplateManager(templatemanager.TemplateManagerTemplates{
		Name:  "new-post",
		Files: []string{"views/layouts/general-mail.html", "views/messages/new-post.html"},
	}, templatemanager.TemplateManagerTemplates{
		Name:  "verify-email",
		Files: []string{"views/layouts/general-mail.html", "views/messages/verify-email.html"},
	})
	if err != nil {
		return nil, fmt.Errorf("fail to initialize template manager for message templating: %w", err)
	}

	mailClient, err := mail.NewClient(mailHost,
		mail.WithSMTPAuth(mail.SMTPAuthAutoDiscover), mail.WithTLSPortPolicy(mail.TLSMandatory),
		mail.WithUsername(username), mail.WithPassword(password),
	)
	if err != nil {
		return nil, fmt.Errorf("fail to initialize mail client: %w", err)
	}

	return &Mailer{
		verificationCodes: verificationCodes,
		unsubscribeCodes:  unsubscribeCodes,
		db:                db,
		clientHost:        clientHost,
		tm:                tm,
		mailClient:        mailClient,
		mailAddress:       mailAddress,
		publicName:        publicName,
		salt:              salt,
		hashMap:           make(map[string][]byte),
		lostMailMap: make(map[string]struct {
			Dur        time.Duration
			End        time.Time
			CodeExpiry time.Time
		}, 0),
		l: localization}, nil
}

func (m *Mailer) GetHash(id string) []byte {
	m.hashMapMutex.RLock()
	if val, ok := m.hashMap[id]; ok {
		m.hashMapMutex.RUnlock()
		slog.Debug("gave an old hash", slog.String("hash", base64.RawStdEncoding.EncodeToString(val)))
		return val
	}
	m.hashMapMutex.RUnlock()

	m.hashMapMutex.Lock()
	defer m.hashMapMutex.Unlock()

	if val, ok := m.hashMap[id]; ok {
		slog.Debug("gave a newly generated hash", slog.String("hash", base64.RawStdEncoding.EncodeToString(val)))
		return val
	}

	m.hashMap[id] = argon2.IDKey([]byte(id), m.salt, 1, 64*1024, 4, 32)
	slog.Debug("generated hash", slog.String("hash", base64.RawStdEncoding.EncodeToString(m.hashMap[id])))
	return m.hashMap[id]
}

func (m *Mailer) IsAllowedToRetryVerification(userId string) (retryAllowed bool, whenAllowed time.Time, codeExpiry time.Time) {
	m.lostMailMapMutex.RLock()
	defer m.lostMailMapMutex.RUnlock()
	previous, ok := m.lostMailMap[userId]
	if ok && previous.End.After(time.Now()) {
		return false, previous.End, previous.CodeExpiry
	}
	return true, time.Time{}, previous.CodeExpiry
}

func (m *Mailer) MailIsTaken(email string) (bool, error) {
	tx, err := m.db.Begin()
	if err != nil {
		return false, fmt.Errorf("failed to initialize transaction with db: %s", err)
	}

	slog.Debug("began db transaction", slog.String("method", "MailIsTaken"))
	defer func(tx *sql.Tx) {
		if err = tx.Commit(); err != nil {
			tx.Rollback()
		}
		slog.Debug("ended db transaction", slog.String("method", "MailIsTaken"))
	}(tx)

	var rows *sql.Rows
	if rows, err = tx.Query(`SELECT email FROM user_email_table WHERE email=? LIMIT 1;`, email); err != nil {
		tx.Rollback()

		return false, fmt.Errorf("failed to query user-email settings in db: %s", err)
	}
	defer rows.Close()

	isTaken := rows.Next()
	return isTaken, nil
}

func (m *Mailer) GetInfo(userIdHash []byte) (email string, lang string, err error) {
	tx, err := m.db.Begin()
	if err != nil {
		return "", "", fmt.Errorf("failed to initialize transaction with db: %s", err)
	}

	slog.Debug("began db transaction", slog.String("method", "GetInfo"))
	defer func(tx *sql.Tx) {
		if err = tx.Commit(); err != nil {
			tx.Rollback()
		}
		slog.Debug("ended db transaction", slog.String("method", "GetInfo"))
	}(tx)

	var rows *sql.Rows
	if rows, err = tx.Query(`SELECT email, lang FROM user_email_table WHERE user_id=? LIMIT 1;`, userIdHash); err != nil {
		tx.Rollback()
		return "", "", fmt.Errorf("failed to query user-email settings in db: %s", err)
	}
	defer rows.Close()

	if !rows.Next() {
		return "", "", nil
	}

	if err = rows.Scan(&email, &lang); err != nil {
		return "", "", fmt.Errorf("failed to scan the result from user-email settings query: %s", err)
	}
	return
}

func (m *Mailer) Unsubscribe(unsubscribeCodeString string) (clientError error, serverError error) {
	unsubscribeCode, err := strconv.ParseUint(unsubscribeCodeString, 16, 64)
	if err != nil {
		return fmt.Errorf("invalid unsubscribe code: %s", err), nil
	}

	userId, _ := m.unsubscribeCodes.Get(unsubscribeCode)
	if len(userId) == 0 {
		return fmt.Errorf("invalid unsubscribe code: have no information about it"), nil
	}

	if err = m.Subscribe(userId, None); err != nil {
		return nil, fmt.Errorf("failed to unsubscribe: %s", err)
	}

	return nil, nil
}

func (m *Mailer) SendVerificationCode(userId string, address string, lang string) error {
	message := mail.NewMsg()

	if err := message.EnvelopeFrom(m.mailAddress); err != nil {
		return fmt.Errorf("failed to set ENVELOPE FROM address: %w", err)
	}
	if err := message.FromFormat(m.publicName, m.mailAddress); err != nil {
		return fmt.Errorf("failed to set formatted FROM address: %w", err)
	}
	if err := message.To(address); err != nil {
		return fmt.Errorf("failed to set TO address: %w", err)
	}

	message.SetMessageID()
	message.SetDate()
	message.SetBulk()

	dur := 1 * time.Minute
	m.lostMailMapMutex.Lock()
	if previous, ok := m.lostMailMap[userId]; ok {
		if time.Now().Before(previous.End) {
			m.lostMailMapMutex.Unlock()
			return fmt.Errorf("user is not allowed to send another verification code until %s", previous.End)
		}
		dur = 2 * m.lostMailMap[userId].Dur
	}
	m.lostMailMap[userId] = struct {
		Dur        time.Duration
		End        time.Time
		CodeExpiry time.Time
	}{Dur: dur, End: time.Now().Add(dur), CodeExpiry: time.Now().Add(time.Hour)}
	m.lostMailMapMutex.Unlock()

	verificationCodeBytes := make([]byte, 8)
	rand.Read(verificationCodeBytes)
	verificationCode := binary.LittleEndian.Uint64(verificationCodeBytes)

	verificationInfo := fmt.Sprintf("%s.%s", base64.RawStdEncoding.EncodeToString([]byte(userId)), base64.RawStdEncoding.EncodeToString([]byte(address)))
	m.verificationCodes.Set(verificationCode, verificationInfo, int64(len(verificationInfo)+8))

	message.Subject(m.l[lang].Mail.VerifyEmail.Subject)

	msg, err := m.tm.Render("verify-email", fiber.Map{
		"L":                m.l[lang],
		"Lang":             lang,
		"VerificationCode": fmt.Sprintf("%X", verificationCode),
		"ClientHost":       m.clientHost,
	})
	if err != nil {
		return fmt.Errorf("failed to render message body: %w", err)
	}

	message.SetBodyString(mail.TypeTextHTML, string(msg))
	if err := m.mailClient.DialAndSend(message); err != nil {
		return fmt.Errorf("failed to send verification code message: %w", err)
	}
	slog.Debug("verification code message successfully delivered", slog.String("address", address), slog.String("user_id", userId))
	return nil
}

func (m *Mailer) Verify(verificationCodeEncoded string, lang string) error {
	verificationCode, err := strconv.ParseUint(verificationCodeEncoded, 16, 64)
	if err != nil {
		return fmt.Errorf("failed to decode verification code from 8-byte hex: %w", err)
	}

	verificationInfo, _ := m.verificationCodes.Get(verificationCode)
	if verificationInfo == "" {
		return fmt.Errorf("failed to get verification info by its code (might be absent, might be empty)")
	}

	verificationSegments := strings.Split(verificationInfo, ".")
	if len(verificationSegments) != 2 {
		m.verificationCodes.Del(verificationCode)
		return fmt.Errorf("invalid format of verification info: expected %d segments, got %d", 2, len(verificationSegments))
	}

	userId, err := base64.RawStdEncoding.DecodeString(verificationSegments[0])
	if err != nil {
		m.verificationCodes.Del(verificationCode)
		delete(m.lostMailMap, verificationSegments[0])
		return fmt.Errorf("could not decode user id: %s", err)
	}

	address, err := base64.RawStdEncoding.DecodeString(verificationSegments[1])
	if err != nil {
		m.verificationCodes.Del(verificationCode)
		delete(m.lostMailMap, verificationSegments[0])
		return fmt.Errorf("could not decode address: %s", err)
	}

	tx, err := m.db.Begin()
	if err != nil {
		return fmt.Errorf("failed to initialize transaction with db: %s", err)
	}

	slog.Debug("began db transaction", slog.String("method", "Verify"))
	if _, err = tx.Exec(`INSERT INTO user_email_table(user_id, email, lang) VALUES(?, ?, ?)
  ON CONFLICT(user_id) DO UPDATE SET
  	email=excluded.email,
	lang=excluded.lang;`, m.GetHash(string(userId)), address, lang); err != nil {
		tx.Rollback()
		slog.Debug("ended db transaction", slog.String("method", "Verify"))
		return fmt.Errorf("failed to configure user-email settings in db: %s", err)
	}

	if err = tx.Commit(); err != nil {
		tx.Rollback()
		slog.Debug("ended db transaction", slog.String("method", "Verify"))
		return fmt.Errorf("failed to commit transaction to db: %s", err)
	}
	slog.Debug("ended db transaction", slog.String("method", "Verify"))

	m.verificationCodes.Del(verificationCode)
	delete(m.lostMailMap, verificationSegments[0])
	return nil
}

func (m *Mailer) GetSubscriptions(userId string) (subscriptionType SubscriptionType, tags []string, err error) {
	tx, err := m.db.Begin()
	if err != nil {
		return None, nil, fmt.Errorf("failed to initialize transaction with db: %s", err)
	}

	slog.Debug("began db transaction", slog.String("method", "GetSubscriptions"))
	defer func(tx *sql.Tx) {
		if err = tx.Commit(); err != nil {
			tx.Rollback()
		}
		slog.Debug("ended db transaction", slog.String("method", "GetSubscriptions"))
	}(tx)

	hash := m.GetHash(userId)

	var rows *sql.Rows
	if rows, err = tx.Query(`SELECT tags FROM subscription_user_to_tags_table WHERE user_id=? LIMIT 1;`, hash); err != nil {
		tx.Rollback()
		return None, nil, fmt.Errorf("failed to query user-to-tags table in db for the user: %s", err)
	}
	defer rows.Close()

	if !rows.Next() {
		return None, nil, nil
	}

	tagsString := ""
	if err = rows.Scan(&tagsString); err != nil {
		return None, nil, fmt.Errorf("failed to scan the result from user-to-tags query: %s", err)
	}

	switch tagsString {
	case "":
		return None, nil, nil
	case "_all":
		return All, nil, nil
	default:
		return Specific, strings.Split(tagsString, ","), nil
	}
}

func (m *Mailer) Subscribe(userIdHash []byte, subscriptionType SubscriptionType, tags ...string) error {
	tx, err := m.db.Begin()
	if err != nil {
		return fmt.Errorf("failed to initialize transaction with db: %s", err)
	}

	slog.Debug("began db transaction", slog.String("method", "Subscribe"))

	slices.Sort(tags)
	tagsOutput := ""
	switch subscriptionType {
	case All:
		tagsOutput = "_all"
	case None:
		tagsOutput = ""
	case Specific:
		tagsOutput = strings.Join(tags, ",")
	}

	if _, err = tx.Exec(`INSERT INTO subscription_user_to_tags_table(user_id, tags) VALUES(?, ?)
  ON CONFLICT(user_id) DO UPDATE SET
  	tags=excluded.tags;`, userIdHash, tagsOutput); err != nil {
		tx.Rollback()
		slog.Debug("ended db transaction", slog.String("method", "Subscribe"))
		return fmt.Errorf("failed to configure user-to-tags table in db for the user: %s", err)
	}

	if err = tx.Commit(); err != nil {
		tx.Rollback()
		slog.Debug("ended db transaction", slog.String("method", "Subscribe"))
		return fmt.Errorf("failed to commit transaction to db: %s", err)
	}
	slog.Debug("ended db transaction", slog.String("method", "Subscribe"))

	return nil
}

func (m *Mailer) NewPost(post *b2.BlogPage) error {
	tx, err := m.db.Begin()
	if err != nil {
		return fmt.Errorf("failed to initialize transaction with db: %s", err)
	}

	slog.Debug("began db transaction", slog.String("method", "NewPost"))
	var rows *sql.Rows
	if rows, err = tx.Query(`SELECT user_id, tags FROM subscription_user_to_tags_table;`); err != nil {
		tx.Rollback()
		slog.Debug("ended db transaction", slog.String("method", "NewPost"))
		return fmt.Errorf("failed to query user-to-tags table in db: %s", err)
	}

	var userId []byte
	usersToSend := make([]struct {
		userId []byte
		email  string
	}, 0)
	var tagsString string
	i := -1

rowLoop:
	for rows.Next() {
		i++
		if err = rows.Scan(&userId, &tagsString); err != nil {
			slog.Warn("failed to scan a row in user-to-tags table", slog.String("error", err.Error()), slog.Int("index", i), slog.String("user_id", base64.RawStdEncoding.EncodeToString(userId)))
			continue
		}

		if tagsString == "" {
			continue
		}

		if tagsString == "_all" {
			usersToSend = append(usersToSend, struct {
				userId []byte
				email  string
			}{userId, ""})
			continue
		}

		pageTags := strings.Split(tagsString, ",")
		for _, tag := range pageTags {
			if _, found := slices.BinarySearch(post.Metadata.Tags, tag); found {
				usersToSend = append(usersToSend, struct {
					userId []byte
					email  string
				}{userId, ""})
				continue rowLoop
			}
		}
	}

	tx.Commit()
	rows.Close()
	slog.Debug("ended db transaction", slog.String("method", "NewPost"))

	for i := range usersToSend {
		email, lang, err := m.GetInfo(usersToSend[i].userId)
		if err != nil {
			slog.Warn("failed to get info about the user", slog.String("error", err.Error()), slog.Int("index", i), slog.String("user_id", base64.RawStdEncoding.EncodeToString(userId)))
			continue
		}

		if lang != post.Lang {
			continue
		}

		usersToSend[i].email = email
	}

	messages := make([]*mail.Msg, 0, len(usersToSend))
	for _, user := range usersToSend {
		if user.email == "" {
			continue
		}
		unsubscribeCodeBytes := make([]byte, 8)
		rand.Read(unsubscribeCodeBytes)
		unsubscribeCode := binary.LittleEndian.Uint64(unsubscribeCodeBytes)

		unsubscribeFooter := strings.Replace(m.l[post.Lang].Mail.UnsubscribeFooter, "{}", fmt.Sprintf(`<a style="color: #273de1 !important;" href="https://%s/%s/user/unsubscribe?code=%X">`, m.clientHost, post.Lang, unsubscribeCode), 1)
		unsubscribeFooter = strings.Replace(unsubscribeFooter, "{/}", "</a>", 1)

		msgBody, err := m.tm.Render("new-post", fiber.Map{
			"L":                 m.l[post.Lang],
			"Lang":              post.Lang,
			"Post":              post,
			"ClientHost":        m.clientHost,
			"UnsubscribeFooter": template.HTML(unsubscribeFooter),
		})

		if err != nil {
			return fmt.Errorf("failed to render message body: %w", err)
		}

		message := mail.NewMsg()

		if err := message.EnvelopeFrom(m.mailAddress); err != nil {
			return fmt.Errorf("failed to set ENVELOPE FROM address: %w", err)
		}
		if err := message.FromFormat(m.publicName, m.mailAddress); err != nil {
			return fmt.Errorf("failed to set formatted FROM address: %w", err)
		}
		if err := message.To(user.email); err != nil {
			return fmt.Errorf("failed to set TO address: %w", err)
		}

		message.SetMessageID()
		message.SetDate()
		message.SetBulk()
		message.Subject(m.l[post.Lang].Mail.NewPost.Subject)
		message.SetBodyString(mail.TypeTextHTML, string(msgBody))

		m.unsubscribeCodes.Set(unsubscribeCode, user.userId, 40)

		messages = append(messages, message)
	}

	if err := m.mailClient.DialAndSend(messages...); err != nil {
		return fmt.Errorf("failed to send new post notifications: %w", err)
	}
	return nil
}