2020-05-13 12:07:44 +00:00
|
|
|
package alpsbase
|
2019-12-16 11:51:42 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2020-01-24 16:49:50 +00:00
|
|
|
"bytes"
|
2019-12-16 11:51:42 +00:00
|
|
|
"fmt"
|
2020-02-25 09:46:55 +00:00
|
|
|
"net/url"
|
2019-12-16 11:51:42 +00:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-01-24 16:49:50 +00:00
|
|
|
"time"
|
2019-12-16 11:51:42 +00:00
|
|
|
|
|
|
|
"github.com/emersion/go-imap"
|
2020-01-24 16:49:50 +00:00
|
|
|
imapspecialuse "github.com/emersion/go-imap-specialuse"
|
2019-12-16 11:51:42 +00:00
|
|
|
imapclient "github.com/emersion/go-imap/client"
|
|
|
|
"github.com/emersion/go-message"
|
|
|
|
"github.com/emersion/go-message/textproto"
|
|
|
|
)
|
|
|
|
|
2020-03-27 09:45:43 +00:00
|
|
|
type MailboxInfo struct {
|
|
|
|
*imap.MailboxInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mbox *MailboxInfo) URL() *url.URL {
|
|
|
|
return &url.URL{
|
|
|
|
Path: fmt.Sprintf("/mailbox/%v", url.PathEscape(mbox.Name)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func listMailboxes(conn *imapclient.Client) ([]MailboxInfo, error) {
|
2019-12-16 11:51:42 +00:00
|
|
|
ch := make(chan *imap.MailboxInfo, 10)
|
|
|
|
done := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
done <- conn.List("", "*", ch)
|
|
|
|
}()
|
|
|
|
|
2020-03-27 09:45:43 +00:00
|
|
|
var mailboxes []MailboxInfo
|
2019-12-16 11:51:42 +00:00
|
|
|
for mbox := range ch {
|
2020-03-27 09:45:43 +00:00
|
|
|
mailboxes = append(mailboxes, MailboxInfo{mbox})
|
2019-12-16 11:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := <-done; err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to list mailboxes: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(mailboxes, func(i, j int) bool {
|
2020-03-27 12:19:26 +00:00
|
|
|
if mailboxes[i].Name == "INBOX" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if mailboxes[j].Name == "INBOX" {
|
|
|
|
return false
|
|
|
|
}
|
2019-12-16 11:51:42 +00:00
|
|
|
return mailboxes[i].Name < mailboxes[j].Name
|
|
|
|
})
|
|
|
|
return mailboxes, nil
|
|
|
|
}
|
|
|
|
|
2020-05-13 16:18:13 +00:00
|
|
|
type MailboxStatus struct {
|
|
|
|
*imap.MailboxStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mbox *MailboxStatus) URL() *url.URL {
|
|
|
|
return &url.URL{
|
|
|
|
Path: fmt.Sprintf("/mailbox/%v", url.PathEscape(mbox.Name)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getMailboxStatus(conn *imapclient.Client, name string) (*MailboxStatus, error) {
|
|
|
|
items := []imap.StatusItem{
|
|
|
|
imap.StatusMessages,
|
|
|
|
imap.StatusUidValidity,
|
|
|
|
imap.StatusUnseen,
|
|
|
|
}
|
|
|
|
status, err := conn.Status(name, items)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get mailbox status: %v", err)
|
|
|
|
}
|
|
|
|
return &MailboxStatus{status}, nil
|
|
|
|
}
|
|
|
|
|
2020-01-24 17:01:01 +00:00
|
|
|
type mailboxType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
mailboxSent mailboxType = iota
|
|
|
|
mailboxDrafts
|
|
|
|
)
|
|
|
|
|
2020-03-27 09:45:43 +00:00
|
|
|
func getMailboxByType(conn *imapclient.Client, mboxType mailboxType) (*MailboxInfo, error) {
|
2020-01-20 19:25:41 +00:00
|
|
|
ch := make(chan *imap.MailboxInfo, 10)
|
|
|
|
done := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
done <- conn.List("", "%", ch)
|
|
|
|
}()
|
|
|
|
|
2020-01-24 17:01:01 +00:00
|
|
|
// TODO: configurable fallback names?
|
|
|
|
var attr string
|
|
|
|
var fallbackNames []string
|
|
|
|
switch mboxType {
|
|
|
|
case mailboxSent:
|
|
|
|
attr = imapspecialuse.Sent
|
|
|
|
fallbackNames = []string{"Sent"}
|
|
|
|
case mailboxDrafts:
|
|
|
|
attr = imapspecialuse.Drafts
|
|
|
|
fallbackNames = []string{"Draft", "Drafts"}
|
|
|
|
}
|
|
|
|
|
|
|
|
var attrMatched bool
|
|
|
|
var best *imap.MailboxInfo
|
2020-01-20 19:25:41 +00:00
|
|
|
for mbox := range ch {
|
|
|
|
for _, a := range mbox.Attributes {
|
|
|
|
if attr == a {
|
2020-01-24 17:01:01 +00:00
|
|
|
best = mbox
|
|
|
|
attrMatched = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if attrMatched {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fallback := range fallbackNames {
|
|
|
|
if strings.EqualFold(fallback, mbox.Name) {
|
|
|
|
best = mbox
|
2020-01-20 19:25:41 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := <-done; err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get mailbox with attribute %q: %v", attr, err)
|
|
|
|
}
|
|
|
|
|
2020-05-18 16:33:06 +00:00
|
|
|
if best == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-03-27 09:45:43 +00:00
|
|
|
return &MailboxInfo{best}, nil
|
2020-01-20 19:25:41 +00:00
|
|
|
}
|
|
|
|
|
2019-12-16 11:51:42 +00:00
|
|
|
func ensureMailboxSelected(conn *imapclient.Client, mboxName string) error {
|
|
|
|
mbox := conn.Mailbox()
|
|
|
|
if mbox == nil || mbox.Name != mboxName {
|
|
|
|
if _, err := conn.Select(mboxName, false); err != nil {
|
|
|
|
return fmt.Errorf("failed to select mailbox: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-16 14:46:29 +00:00
|
|
|
type IMAPMessage struct {
|
2019-12-16 11:51:42 +00:00
|
|
|
*imap.Message
|
2020-02-25 09:46:55 +00:00
|
|
|
|
|
|
|
Mailbox string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *IMAPMessage) URL() *url.URL {
|
|
|
|
return &url.URL{
|
|
|
|
Path: fmt.Sprintf("/message/%v/%v", url.PathEscape(msg.Mailbox), msg.Uid),
|
|
|
|
}
|
2019-12-16 11:51:42 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 14:25:25 +00:00
|
|
|
func newIMAPPartNode(msg *IMAPMessage, path []int, part *imap.BodyStructure) *IMAPPartNode {
|
|
|
|
filename, _ := part.Filename()
|
|
|
|
return &IMAPPartNode{
|
|
|
|
Path: path,
|
|
|
|
MIMEType: strings.ToLower(part.MIMEType + "/" + part.MIMESubType),
|
|
|
|
Filename: filename,
|
|
|
|
Message: msg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *IMAPMessage) TextPart() *IMAPPartNode {
|
2020-01-28 10:15:10 +00:00
|
|
|
if msg.BodyStructure == nil {
|
2020-05-20 14:25:25 +00:00
|
|
|
return nil
|
2019-12-16 11:51:42 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 14:25:25 +00:00
|
|
|
var best *IMAPPartNode
|
2020-01-28 10:15:10 +00:00
|
|
|
isTextPlain := false
|
|
|
|
msg.BodyStructure.Walk(func(path []int, part *imap.BodyStructure) bool {
|
2019-12-16 11:51:42 +00:00
|
|
|
if !strings.EqualFold(part.MIMEType, "text") {
|
2020-01-28 10:15:10 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
if part.Disposition != "" && !strings.EqualFold(part.Disposition, "inline") {
|
|
|
|
return true
|
2019-12-16 11:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch strings.ToLower(part.MIMESubType) {
|
|
|
|
case "plain":
|
2020-01-28 10:15:10 +00:00
|
|
|
isTextPlain = true
|
2020-05-20 14:25:25 +00:00
|
|
|
best = newIMAPPartNode(msg, path, part)
|
2019-12-16 11:51:42 +00:00
|
|
|
case "html":
|
2020-01-28 10:15:10 +00:00
|
|
|
if !isTextPlain {
|
2020-05-20 14:25:25 +00:00
|
|
|
best = newIMAPPartNode(msg, path, part)
|
2020-01-28 10:15:10 +00:00
|
|
|
}
|
2019-12-16 11:51:42 +00:00
|
|
|
}
|
2020-01-28 10:15:10 +00:00
|
|
|
return true
|
|
|
|
})
|
2019-12-16 11:51:42 +00:00
|
|
|
|
2020-05-20 14:25:25 +00:00
|
|
|
return best
|
2020-02-25 09:46:55 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 11:30:07 +00:00
|
|
|
func (msg *IMAPMessage) Attachments() []IMAPPartNode {
|
|
|
|
if msg.BodyStructure == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var attachments []IMAPPartNode
|
|
|
|
msg.BodyStructure.Walk(func(path []int, part *imap.BodyStructure) bool {
|
|
|
|
if !strings.EqualFold(part.Disposition, "attachment") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-02-25 09:46:55 +00:00
|
|
|
attachments = append(attachments, *newIMAPPartNode(msg, path, part))
|
2020-01-28 11:30:07 +00:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
return attachments
|
|
|
|
}
|
|
|
|
|
2020-02-25 15:38:29 +00:00
|
|
|
func pathsEqual(a, b []int) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := range a {
|
|
|
|
if a[i] != b[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *IMAPMessage) PartByPath(path []int) *IMAPPartNode {
|
|
|
|
if msg.BodyStructure == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-03-18 18:44:18 +00:00
|
|
|
if len(path) == 0 {
|
|
|
|
return newIMAPPartNode(msg, nil, msg.BodyStructure)
|
|
|
|
}
|
2020-02-25 15:38:29 +00:00
|
|
|
|
|
|
|
var result *IMAPPartNode
|
|
|
|
msg.BodyStructure.Walk(func(p []int, part *imap.BodyStructure) bool {
|
|
|
|
if result == nil && pathsEqual(path, p) {
|
|
|
|
result = newIMAPPartNode(msg, p, part)
|
|
|
|
}
|
|
|
|
return result == nil
|
|
|
|
})
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-02-25 09:46:55 +00:00
|
|
|
func (msg *IMAPMessage) PartByID(id string) *IMAPPartNode {
|
|
|
|
if msg.BodyStructure == nil || id == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var result *IMAPPartNode
|
|
|
|
msg.BodyStructure.Walk(func(path []int, part *imap.BodyStructure) bool {
|
|
|
|
if result == nil && part.Id == "<"+id+">" {
|
|
|
|
result = newIMAPPartNode(msg, path, part)
|
|
|
|
}
|
|
|
|
return result == nil
|
|
|
|
})
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-12-16 11:51:42 +00:00
|
|
|
type IMAPPartNode struct {
|
|
|
|
Path []int
|
|
|
|
MIMEType string
|
|
|
|
Filename string
|
|
|
|
Children []IMAPPartNode
|
2020-02-25 09:46:55 +00:00
|
|
|
Message *IMAPMessage
|
2019-12-16 11:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (node IMAPPartNode) PathString() string {
|
|
|
|
l := make([]string, len(node.Path))
|
|
|
|
for i, partNum := range node.Path {
|
|
|
|
l[i] = strconv.Itoa(partNum)
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(l, ".")
|
|
|
|
}
|
|
|
|
|
2020-02-25 09:46:55 +00:00
|
|
|
func (node IMAPPartNode) URL(raw bool) *url.URL {
|
|
|
|
u := node.Message.URL()
|
|
|
|
if raw {
|
|
|
|
u.Path += "/raw"
|
|
|
|
}
|
|
|
|
q := u.Query()
|
|
|
|
q.Set("part", node.PathString())
|
|
|
|
u.RawQuery = q.Encode()
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
2019-12-16 11:51:42 +00:00
|
|
|
func (node IMAPPartNode) IsText() bool {
|
|
|
|
return strings.HasPrefix(strings.ToLower(node.MIMEType), "text/")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node IMAPPartNode) String() string {
|
|
|
|
if node.Filename != "" {
|
|
|
|
return fmt.Sprintf("%s (%s)", node.Filename, node.MIMEType)
|
|
|
|
} else {
|
|
|
|
return node.MIMEType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-25 09:46:55 +00:00
|
|
|
func imapPartTree(msg *IMAPMessage, bs *imap.BodyStructure, path []int) *IMAPPartNode {
|
2019-12-16 11:51:42 +00:00
|
|
|
if !strings.EqualFold(bs.MIMEType, "multipart") && len(path) == 0 {
|
|
|
|
path = []int{1}
|
|
|
|
}
|
|
|
|
|
|
|
|
filename, _ := bs.Filename()
|
|
|
|
|
|
|
|
node := &IMAPPartNode{
|
|
|
|
Path: path,
|
|
|
|
MIMEType: strings.ToLower(bs.MIMEType + "/" + bs.MIMESubType),
|
|
|
|
Filename: filename,
|
|
|
|
Children: make([]IMAPPartNode, len(bs.Parts)),
|
2020-02-25 09:46:55 +00:00
|
|
|
Message: msg,
|
2019-12-16 11:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, part := range bs.Parts {
|
|
|
|
num := i + 1
|
|
|
|
|
|
|
|
partPath := append([]int(nil), path...)
|
|
|
|
partPath = append(partPath, num)
|
|
|
|
|
2020-02-25 09:46:55 +00:00
|
|
|
node.Children[i] = *imapPartTree(msg, part, partPath)
|
2019-12-16 11:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
2019-12-16 14:46:29 +00:00
|
|
|
func (msg *IMAPMessage) PartTree() *IMAPPartNode {
|
2019-12-16 11:51:42 +00:00
|
|
|
if msg.BodyStructure == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-25 09:46:55 +00:00
|
|
|
return imapPartTree(msg, msg.BodyStructure, nil)
|
2019-12-16 11:51:42 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 11:42:04 +00:00
|
|
|
func (msg *IMAPMessage) HasFlag(flag string) bool {
|
|
|
|
for _, f := range msg.Flags {
|
|
|
|
if imap.CanonicalFlag(f) == flag {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-05-28 10:09:44 +00:00
|
|
|
func listMessages(conn *imapclient.Client, mbox *MailboxStatus, page, messagesPerPage int) ([]IMAPMessage, error) {
|
|
|
|
if err := ensureMailboxSelected(conn, mbox.Name); err != nil {
|
2019-12-16 11:51:42 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
to := int(mbox.Messages) - page*messagesPerPage
|
|
|
|
from := to - messagesPerPage + 1
|
|
|
|
if from <= 0 {
|
|
|
|
from = 1
|
|
|
|
}
|
|
|
|
if to <= 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-12-16 13:36:43 +00:00
|
|
|
var seqSet imap.SeqSet
|
2019-12-16 11:51:42 +00:00
|
|
|
seqSet.AddRange(uint32(from), uint32(to))
|
|
|
|
|
2020-01-13 18:59:36 +00:00
|
|
|
fetch := []imap.FetchItem{imap.FetchFlags, imap.FetchEnvelope, imap.FetchUid, imap.FetchBodyStructure}
|
2019-12-16 11:51:42 +00:00
|
|
|
|
|
|
|
ch := make(chan *imap.Message, 10)
|
|
|
|
done := make(chan error, 1)
|
|
|
|
go func() {
|
2019-12-16 13:36:43 +00:00
|
|
|
done <- conn.Fetch(&seqSet, fetch, ch)
|
2019-12-16 11:51:42 +00:00
|
|
|
}()
|
|
|
|
|
2019-12-16 14:46:29 +00:00
|
|
|
msgs := make([]IMAPMessage, 0, to-from)
|
2019-12-16 11:51:42 +00:00
|
|
|
for msg := range ch {
|
2020-05-28 10:09:44 +00:00
|
|
|
msgs = append(msgs, IMAPMessage{msg, mbox.Name})
|
2019-12-16 11:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := <-done; err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to fetch message list: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reverse list of messages
|
|
|
|
for i := len(msgs)/2 - 1; i >= 0; i-- {
|
|
|
|
opp := len(msgs) - 1 - i
|
|
|
|
msgs[i], msgs[opp] = msgs[opp], msgs[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
return msgs, nil
|
|
|
|
}
|
|
|
|
|
2020-02-05 10:37:01 +00:00
|
|
|
func searchCriteriaHeader(k, v string) *imap.SearchCriteria {
|
|
|
|
return &imap.SearchCriteria{
|
|
|
|
Header: map[string][]string{
|
|
|
|
k: []string{v},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-05 13:58:56 +00:00
|
|
|
func searchCriteriaOr(criteria ...*imap.SearchCriteria) *imap.SearchCriteria {
|
2020-02-05 10:37:01 +00:00
|
|
|
or := criteria[0]
|
|
|
|
for _, c := range criteria[1:] {
|
|
|
|
or = &imap.SearchCriteria{
|
|
|
|
Or: [][2]*imap.SearchCriteria{{or, c}},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return or
|
|
|
|
}
|
|
|
|
|
2020-01-28 19:04:33 +00:00
|
|
|
func searchMessages(conn *imapclient.Client, mboxName, query string, page, messagesPerPage int) (msgs []IMAPMessage, total int, err error) {
|
2019-12-16 13:36:43 +00:00
|
|
|
if err := ensureMailboxSelected(conn, mboxName); err != nil {
|
2019-12-17 15:23:51 +00:00
|
|
|
return nil, 0, err
|
2019-12-16 13:36:43 +00:00
|
|
|
}
|
|
|
|
|
2020-02-05 10:37:01 +00:00
|
|
|
// TODO: full-text search on demand (can be slow)
|
|
|
|
//criteria := &imap.SearchCriteria{Text: []string{query}}
|
|
|
|
criteria := searchCriteriaOr(
|
|
|
|
searchCriteriaHeader("From", query),
|
|
|
|
searchCriteriaHeader("To", query),
|
|
|
|
searchCriteriaHeader("Cc", query),
|
|
|
|
searchCriteriaHeader("Subject", query),
|
|
|
|
)
|
|
|
|
nums, err := conn.Search(criteria)
|
2019-12-16 13:36:43 +00:00
|
|
|
if err != nil {
|
2019-12-17 15:23:51 +00:00
|
|
|
return nil, 0, fmt.Errorf("UID SEARCH failed: %v", err)
|
2019-12-16 13:36:43 +00:00
|
|
|
}
|
2019-12-17 15:23:51 +00:00
|
|
|
total = len(nums)
|
|
|
|
|
2020-01-08 09:52:28 +00:00
|
|
|
from := page * messagesPerPage
|
2019-12-17 15:23:51 +00:00
|
|
|
to := from + messagesPerPage
|
|
|
|
if from >= len(nums) {
|
|
|
|
return nil, total, nil
|
2019-12-16 13:36:43 +00:00
|
|
|
}
|
2019-12-17 15:23:51 +00:00
|
|
|
if to > len(nums) {
|
|
|
|
to = len(nums)
|
|
|
|
}
|
|
|
|
nums = nums[from:to]
|
2019-12-16 13:36:43 +00:00
|
|
|
|
|
|
|
indexes := make(map[uint32]int)
|
|
|
|
for i, num := range nums {
|
|
|
|
indexes[num] = i
|
|
|
|
}
|
|
|
|
|
|
|
|
var seqSet imap.SeqSet
|
|
|
|
seqSet.AddNum(nums...)
|
|
|
|
|
|
|
|
fetch := []imap.FetchItem{imap.FetchEnvelope, imap.FetchUid, imap.FetchBodyStructure}
|
|
|
|
|
|
|
|
ch := make(chan *imap.Message, 10)
|
|
|
|
done := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
done <- conn.Fetch(&seqSet, fetch, ch)
|
|
|
|
}()
|
|
|
|
|
2019-12-17 15:23:51 +00:00
|
|
|
msgs = make([]IMAPMessage, len(nums))
|
2019-12-16 13:36:43 +00:00
|
|
|
for msg := range ch {
|
|
|
|
i, ok := indexes[msg.SeqNum]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2020-02-25 09:46:55 +00:00
|
|
|
msgs[i] = IMAPMessage{msg, mboxName}
|
2019-12-16 13:36:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := <-done; err != nil {
|
2019-12-17 15:23:51 +00:00
|
|
|
return nil, 0, fmt.Errorf("failed to fetch message list: %v", err)
|
2019-12-16 13:36:43 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 15:23:51 +00:00
|
|
|
return msgs, total, nil
|
2019-12-16 13:36:43 +00:00
|
|
|
}
|
|
|
|
|
2019-12-16 14:46:29 +00:00
|
|
|
func getMessagePart(conn *imapclient.Client, mboxName string, uid uint32, partPath []int) (*IMAPMessage, *message.Entity, error) {
|
2019-12-16 11:51:42 +00:00
|
|
|
if err := ensureMailboxSelected(conn, mboxName); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
seqSet := new(imap.SeqSet)
|
|
|
|
seqSet.AddNum(uid)
|
|
|
|
|
|
|
|
var partHeaderSection imap.BodySectionName
|
|
|
|
partHeaderSection.Peek = true
|
|
|
|
if len(partPath) > 0 {
|
|
|
|
partHeaderSection.Specifier = imap.MIMESpecifier
|
|
|
|
} else {
|
|
|
|
partHeaderSection.Specifier = imap.HeaderSpecifier
|
|
|
|
}
|
|
|
|
partHeaderSection.Path = partPath
|
|
|
|
|
|
|
|
var partBodySection imap.BodySectionName
|
|
|
|
if len(partPath) > 0 {
|
|
|
|
partBodySection.Specifier = imap.EntireSpecifier
|
|
|
|
} else {
|
|
|
|
partBodySection.Specifier = imap.TextSpecifier
|
|
|
|
}
|
|
|
|
partBodySection.Path = partPath
|
|
|
|
|
|
|
|
fetch := []imap.FetchItem{
|
|
|
|
imap.FetchEnvelope,
|
|
|
|
imap.FetchUid,
|
|
|
|
imap.FetchBodyStructure,
|
2019-12-17 11:42:04 +00:00
|
|
|
imap.FetchFlags,
|
2019-12-16 11:51:42 +00:00
|
|
|
partHeaderSection.FetchItem(),
|
|
|
|
partBodySection.FetchItem(),
|
|
|
|
}
|
|
|
|
|
|
|
|
ch := make(chan *imap.Message, 1)
|
|
|
|
if err := conn.UidFetch(seqSet, fetch, ch); err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("failed to fetch message: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := <-ch
|
|
|
|
if msg == nil {
|
|
|
|
return nil, nil, fmt.Errorf("server didn't return message")
|
|
|
|
}
|
|
|
|
|
|
|
|
headerReader := bufio.NewReader(msg.GetBody(&partHeaderSection))
|
|
|
|
h, err := textproto.ReadHeader(headerReader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("failed to read part header: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
part, err := message.New(message.Header{h}, msg.GetBody(&partBodySection))
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("failed to create message reader: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-02-25 09:46:55 +00:00
|
|
|
return &IMAPMessage{msg, mboxName}, part, nil
|
2019-12-16 11:51:42 +00:00
|
|
|
}
|
2020-01-20 19:25:41 +00:00
|
|
|
|
|
|
|
func markMessageAnswered(conn *imapclient.Client, mboxName string, uid uint32) error {
|
|
|
|
if err := ensureMailboxSelected(conn, mboxName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
seqSet := new(imap.SeqSet)
|
|
|
|
seqSet.AddNum(uid)
|
|
|
|
item := imap.FormatFlagsOp(imap.AddFlags, true)
|
|
|
|
flags := []interface{}{imap.AnsweredFlag}
|
|
|
|
return conn.UidStore(seqSet, item, flags, nil)
|
|
|
|
}
|
2020-01-24 16:49:50 +00:00
|
|
|
|
|
|
|
func appendMessage(c *imapclient.Client, msg *OutgoingMessage, mboxType mailboxType) (saved bool, err error) {
|
2020-01-24 17:01:01 +00:00
|
|
|
mbox, err := getMailboxByType(c, mboxType)
|
2020-01-24 16:49:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if mbox == nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IMAP needs to know in advance the final size of the message, so
|
|
|
|
// there's no way around storing it in a buffer here.
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := msg.WriteTo(&buf); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := []string{imap.SeenFlag}
|
|
|
|
if mboxType == mailboxDrafts {
|
|
|
|
flags = append(flags, imap.DraftFlag)
|
|
|
|
}
|
|
|
|
if err := c.Append(mbox.Name, flags, time.Now(), &buf); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
2020-01-24 19:27:05 +00:00
|
|
|
|
|
|
|
func deleteMessage(c *imapclient.Client, mboxName string, uid uint32) error {
|
|
|
|
if err := ensureMailboxSelected(c, mboxName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
seqSet := new(imap.SeqSet)
|
|
|
|
seqSet.AddNum(uid)
|
|
|
|
item := imap.FormatFlagsOp(imap.AddFlags, true)
|
|
|
|
flags := []interface{}{imap.DeletedFlag}
|
|
|
|
if err := c.UidStore(seqSet, item, flags, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Expunge(nil)
|
|
|
|
}
|