2021-07-18 23:03:28 +01:00
|
|
|
/*
|
|
|
|
|
* 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/.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-07-07 18:15:07 +01:00
|
|
|
package imapserver
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2025-12-20 16:30:37 +02:00
|
|
|
"log"
|
2021-07-07 18:15:07 +01:00
|
|
|
|
2021-07-10 11:42:55 +01:00
|
|
|
"github.com/emersion/go-imap"
|
2021-07-07 18:15:07 +01:00
|
|
|
"github.com/emersion/go-imap/backend"
|
2026-02-14 02:34:09 +05:00
|
|
|
"github.com/neilalexander/yggmail/internal/account"
|
|
|
|
|
"github.com/neilalexander/yggmail/internal/storage"
|
2021-07-07 18:15:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type User struct {
|
2026-02-14 02:34:09 +05:00
|
|
|
//backend *Backend
|
|
|
|
|
storage storage.Storage
|
2021-07-07 18:15:07 +01:00
|
|
|
username string
|
2026-02-14 02:34:09 +05:00
|
|
|
session *account.Session
|
2021-07-10 11:42:55 +01:00
|
|
|
conn *imap.ConnInfo
|
2026-02-14 02:34:09 +05:00
|
|
|
log *log.Logger
|
2021-07-07 18:15:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (u *User) Username() string {
|
2026-02-14 02:34:09 +05:00
|
|
|
return u.session.Username()
|
2021-07-07 18:15:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (u *User) ListMailboxes(subscribed bool) (mailboxes []backend.Mailbox, err error) {
|
2026-02-14 02:34:09 +05:00
|
|
|
names, err := u.storage.MailboxList(u.username, subscribed)
|
2021-07-07 18:15:07 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, mailbox := range names {
|
|
|
|
|
mailboxes = append(mailboxes, &Mailbox{
|
2026-02-14 02:34:09 +05:00
|
|
|
storage: u.storage,
|
2021-07-07 18:15:07 +01:00
|
|
|
user: u,
|
|
|
|
|
name: mailbox,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (u *User) GetMailbox(name string) (mailbox backend.Mailbox, err error) {
|
|
|
|
|
if name == "" {
|
|
|
|
|
return &Mailbox{
|
2026-02-14 02:34:09 +05:00
|
|
|
storage: u.storage,
|
2021-07-07 18:15:07 +01:00
|
|
|
user: u,
|
|
|
|
|
name: "",
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
2026-02-14 02:34:09 +05:00
|
|
|
ok, _ := u.storage.MailboxSelect(u.username, name)
|
2021-07-07 18:15:07 +01:00
|
|
|
if !ok {
|
|
|
|
|
return nil, fmt.Errorf("mailbox %q not found", name)
|
|
|
|
|
}
|
|
|
|
|
return &Mailbox{
|
2026-02-14 02:34:09 +05:00
|
|
|
storage: u.storage,
|
2021-07-07 18:15:07 +01:00
|
|
|
user: u,
|
|
|
|
|
name: name,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (u *User) CreateMailbox(name string) error {
|
2025-12-20 16:30:37 +02:00
|
|
|
u.log.Printf("Creating mailbox '%s'...\n", name)
|
2026-02-14 02:34:09 +05:00
|
|
|
|
|
|
|
|
if e := u.storage.MailboxCreate(u.username, name); e != nil {
|
|
|
|
|
u.log.Printf("Error creating mailbox '%s': %v\n", name, e)
|
|
|
|
|
return e
|
2025-12-20 16:30:37 +02:00
|
|
|
}
|
2026-02-14 02:34:09 +05:00
|
|
|
|
|
|
|
|
u.log.Printf("Created mailbox '%s'\n", name)
|
|
|
|
|
return nil
|
2021-07-07 18:15:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (u *User) DeleteMailbox(name string) error {
|
2021-07-09 19:38:05 +01:00
|
|
|
switch name {
|
2025-12-20 16:30:37 +02:00
|
|
|
case "INBOX", "Outbox", "Sent":
|
2021-07-09 19:38:05 +01:00
|
|
|
return errors.New("Cannot delete " + name)
|
|
|
|
|
default:
|
2026-02-14 02:34:09 +05:00
|
|
|
if e := u.storage.MailboxDelete(u.username, name); e != nil {
|
|
|
|
|
u.log.Printf("Error deleting mailbox '%s': %v\n", name, e)
|
|
|
|
|
return e
|
2025-12-20 16:30:37 +02:00
|
|
|
} else {
|
|
|
|
|
u.log.Printf("Deleted mailbox '%s'\n", name)
|
2026-02-14 02:34:09 +05:00
|
|
|
return e
|
2025-12-20 16:30:37 +02:00
|
|
|
}
|
2021-07-07 18:15:07 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (u *User) RenameMailbox(existingName, newName string) error {
|
2021-07-09 19:38:05 +01:00
|
|
|
switch existingName {
|
2025-12-20 16:30:37 +02:00
|
|
|
case "INBOX", "Outbox", "Sent":
|
2021-07-09 19:38:05 +01:00
|
|
|
return errors.New("Cannot rename " + existingName)
|
|
|
|
|
default:
|
2026-02-14 02:34:09 +05:00
|
|
|
return u.storage.MailboxRename(u.username, existingName, newName)
|
2021-07-07 18:15:07 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (u *User) Logout() error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|