Files
yggmail/internal/imapserver/backend.go
Tristan B. Velloza Kildaire 8bf3ba5f47 Onboarding, Sent box and Outbox (#45)
* main

- Set

* Working

* Welcome

- Added welcome message generation

* Smtpsender

- On successful SMTP send create the "Sent" box and then
try move from "Outbox" to "Sent"

* Sent box

- Create the mailbox in `main.go` and not every time we try move from `Outbox` to `Sent`

* Use logegr

* USer

- Added logger pointer (and made use of it)
- Disallow renaming or deletion of 'Sent'

* When creating a new user set it up with logger

* Encoded message

* Added tests

* Send a welcome mail on startup (soon to mke it only happen once)

* try set flags

* Onboarding flag set

* Sender
- Removed testing code

* Welcome

- Moved welcomer code

* Cleaned up

* Added more

* renamed package

* Removed comment

* welcome

- FIxed variable names

* welcome

- Removed semi-colons
- Fixed imports

* welcome

- Ran `gofmt`

* welcome test

- Fixed up

* h

* main

- Ran `gofmt`

* Main

- Fxied

* Welcome

- Foxed name

* Added `.gitignore`

* Mailbox

- Disabled print logging

* Fixed

* fixedg

* fixe and use `%v`
2025-12-20 14:30:37 +00:00

74 lines
2.0 KiB
Go

/*
* Copyright (c) 2021 Neil Alexander
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package imapserver
import (
"encoding/hex"
"fmt"
"log"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/backend"
"github.com/neilalexander/yggmail/internal/config"
"github.com/neilalexander/yggmail/internal/storage"
"github.com/neilalexander/yggmail/internal/utils"
)
type Backend struct {
Config *config.Config
Log *log.Logger
Storage storage.Storage
Server *IMAPServer
}
func (b *Backend) Login(conn *imap.ConnInfo, username, password string) (backend.User, error) {
// If our username is email-like, then take just the localpart
if pk, err := utils.ParseAddress(username); err == nil {
if !pk.Equal(b.Config.PublicKey) {
b.Log.Println("Failed to authenticate IMAP user due to wrong domain", hex.EncodeToString(pk), hex.EncodeToString(b.Config.PublicKey))
return nil, fmt.Errorf("failed to authenticate: wrong domain in username")
}
}
username = hex.EncodeToString(b.Config.PublicKey)
if authed, err := b.Storage.ConfigTryPassword(password); err != nil {
b.Log.Printf("Failed to authenticate IMAP user %q due to error: %s", username, err)
return nil, fmt.Errorf("failed to authenticate: %w", err)
} else if !authed {
b.Log.Printf("Failed to authenticate IMAP user %q\n", username)
return nil, backend.ErrInvalidCredentials
}
defer b.Log.Printf("Authenticated IMAP user from %s as %q\n", conn.RemoteAddr.String(), username)
user := &User{
backend: b,
username: username,
conn: conn,
log: b.Log,
}
return user, nil
}
/*
func (b *Backend) NotifyNew(id int) error {
b.Server.server.ForEachConn(func(conn server.Conn) {
notify := false
for _, cap := range conn.Capabilities() {
if cap == "NOTIFY" {
notify = true
}
}
if !notify {
return
}
conn.WaitReady()
conn.WriteResp()
})
return nil
}
*/