lotus/imap/client.go

145 lines
3.0 KiB
Go
Raw Normal View History

2023-08-13 03:04:16 +01:00
package imap
import (
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/client"
2023-08-21 00:26:22 +01:00
"time"
2023-08-13 03:04:16 +01:00
)
var imapStatusFlags = []imap.StatusItem{
imap.StatusMessages,
imap.StatusRecent,
imap.StatusUidNext,
imap.StatusUidValidity,
imap.StatusUnseen,
}
type Client struct {
ic *client.Client
}
2023-09-11 17:23:44 +01:00
func (c *Client) HandleWS(action string, args []string) (map[string]any, error) {
switch action {
case "copy":
// TODO: implementation
case "create":
// TODO: implementation
case "delete":
// TODO: implementation
case "select":
// TODO: implementation
case "fetch":
// TODO: implementation
case "list":
a := make([]*imap.MailboxInfo, 0)
b := make(chan *imap.MailboxInfo, 10)
go func() {
for info := range b {
a = append(a, info)
}
}()
err := c.ic.List(args[0], args[1], b)
if err != nil {
return nil, err
}
2023-09-11 17:45:37 +01:00
return map[string]any{"info": a}, nil
2023-09-11 17:23:44 +01:00
case "move":
// TODO: implementation
case "rename":
// TODO: implementation
case "search":
// TODO: implementation
case "status":
// TODO: implementation
}
_ = args
return map[string]any{"Error": "Not implemented"}, nil
}
2023-08-21 00:26:22 +01:00
func (c *Client) Append(name string, flags []string, date time.Time, msg imap.Literal) error {
return c.ic.Append(name, flags, date, msg)
}
func (c *Client) Copy(seqset *imap.SeqSet, dest string) error {
return c.ic.Copy(seqset, dest)
}
func (c *Client) Create(name string) error {
return c.ic.Create(name)
}
func (c *Client) Delete(name string) error {
return c.ic.Delete(name)
2023-08-13 03:04:16 +01:00
}
func (c *Client) Fetch(folder string, start, end, limit uint32) ([]*imap.Message, error) {
// select the mailbox
mbox, err := c.ic.Select(folder, false)
if err != nil {
return nil, err
}
// setup fetch range
if end > mbox.Messages {
end = mbox.Messages
}
if end-start > limit {
start = end - (limit - 1)
}
seqSet := new(imap.SeqSet)
seqSet.AddRange(start, end)
messages := make(chan *imap.Message, limit)
done := make(chan error, 1)
go func() {
done <- c.ic.Fetch(seqSet, []imap.FetchItem{imap.FetchEnvelope}, messages)
}()
2023-08-21 00:26:22 +01:00
out := make([]*imap.Message, 0, limit)
2023-08-13 03:04:16 +01:00
for msg := range messages {
2023-08-21 00:26:22 +01:00
out = append(out, msg)
}
if err := <-done; err != nil {
return nil, err
}
return out, nil
}
func (c *Client) List(ref, name string) ([]*imap.MailboxInfo, error) {
infos := make(chan *imap.MailboxInfo, 1)
done := make(chan error, 1)
go func() {
done <- c.ic.List(ref, name, infos)
}()
out := make([]*imap.MailboxInfo, 0)
for info := range infos {
out = append(out, info)
2023-08-13 03:04:16 +01:00
}
if err := <-done; err != nil {
return nil, err
}
2023-08-21 00:26:22 +01:00
return out, nil
}
func (c *Client) Move(seqset *imap.SeqSet, dest string) error {
return c.ic.Move(seqset, dest)
}
func (c *Client) Noop() error {
return c.ic.Noop()
}
func (c *Client) Rename(existingName, newName string) error {
return c.ic.Rename(existingName, newName)
}
func (c *Client) Search(criteria *imap.SearchCriteria) ([]uint32, error) {
return c.ic.Search(criteria)
}
func (c *Client) Status(name string) (*imap.MailboxStatus, error) {
mbox, err := c.ic.Status(name, imapStatusFlags)
return mbox, err
2023-08-13 03:04:16 +01:00
}