Files
yggmail/internal/imapserver/user.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

107 lines
2.2 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"
"errors"
"fmt"
"log"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/backend"
)
type User struct {
backend *Backend
username string
conn *imap.ConnInfo
log *log.Logger
}
func (u *User) Username() string {
return hex.EncodeToString(u.backend.Config.PublicKey)
}
func (u *User) ListMailboxes(subscribed bool) (mailboxes []backend.Mailbox, err error) {
names, err := u.backend.Storage.MailboxList(subscribed)
if err != nil {
return nil, err
}
for _, mailbox := range names {
mailboxes = append(mailboxes, &Mailbox{
backend: u.backend,
user: u,
name: mailbox,
})
}
return
}
func (u *User) GetMailbox(name string) (mailbox backend.Mailbox, err error) {
if name == "" {
return &Mailbox{
backend: u.backend,
user: u,
name: "",
}, nil
}
ok, _ := u.backend.Storage.MailboxSelect(name)
if !ok {
return nil, fmt.Errorf("mailbox %q not found", name)
}
return &Mailbox{
backend: u.backend,
user: u,
name: name,
}, nil
}
func (u *User) CreateMailbox(name string) error {
u.log.Printf("Creating mailbox '%s'...\n", name)
if e := u.backend.Storage.MailboxCreate(name); e != nil {
u.log.Printf("Error creating mailbox '%s': %v\n", name, e);
return e;
}
u.log.Printf("Created mailbox '%s'\n", name);
return nil;
}
func (u *User) DeleteMailbox(name string) error {
switch name {
case "INBOX", "Outbox", "Sent":
return errors.New("Cannot delete " + name)
default:
if e := u.backend.Storage.MailboxDelete(name); e != nil {
u.log.Printf("Error deleting mailbox '%s': %v\n", name, e)
return e;
} else {
u.log.Printf("Deleted mailbox '%s'\n", name)
return e;
}
}
}
func (u *User) RenameMailbox(existingName, newName string) error {
switch existingName {
case "INBOX", "Outbox", "Sent":
return errors.New("Cannot rename " + existingName)
default:
return u.backend.Storage.MailboxRename(existingName, newName)
}
}
func (u *User) Logout() error {
return nil
}