proton-bridge/internal/transfer/provider_imap.go

124 lines
3.4 KiB
Go
Raw Normal View History

2021-01-04 10:55:15 +00:00
// Copyright (c) 2021 Proton Technologies AG
2020-05-14 13:22:29 +00:00
//
// This file is part of ProtonMail Bridge.
//
// ProtonMail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ProtonMail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
package transfer
import (
"net"
"strings"
2020-11-06 13:23:38 +00:00
"github.com/emersion/go-imap"
"github.com/emersion/go-sasl"
2020-05-14 13:22:29 +00:00
)
2020-11-06 13:23:38 +00:00
type IMAPClientProvider interface {
Capability() (map[string]bool, error)
2021-04-08 06:00:39 +00:00
Support(capability string) (bool, error)
2020-11-06 13:23:38 +00:00
State() imap.ConnState
SupportAuth(mech string) (bool, error)
Authenticate(auth sasl.Client) error
Login(username, password string) error
List(ref, name string, ch chan *imap.MailboxInfo) error
Select(name string, readOnly bool) (*imap.MailboxStatus, error)
Fetch(seqset *imap.SeqSet, items []imap.FetchItem, ch chan *imap.Message) error
UidFetch(seqset *imap.SeqSet, items []imap.FetchItem, ch chan *imap.Message) error
}
2020-05-14 13:22:29 +00:00
// IMAPProvider implements export from IMAP server.
type IMAPProvider struct {
username string
password string
addr string
2020-11-06 13:23:38 +00:00
clientDialer func(addr string) (IMAPClientProvider, error)
client IMAPClientProvider
2020-09-11 13:09:37 +00:00
timeIt *timeIt
2020-05-14 13:22:29 +00:00
}
// NewIMAPProvider returns new IMAPProvider.
func NewIMAPProvider(username, password, host, port string) (*IMAPProvider, error) {
2020-11-06 13:23:38 +00:00
return newIMAPProvider(imapClientDial, username, password, host, port)
}
func newIMAPProvider(clientDialer func(string) (IMAPClientProvider, error), username, password, host, port string) (*IMAPProvider, error) {
2020-05-14 13:22:29 +00:00
p := &IMAPProvider{
username: username,
password: password,
addr: net.JoinHostPort(host, port),
2020-09-11 13:09:37 +00:00
timeIt: newTimeIt("imap"),
2020-11-06 13:23:38 +00:00
clientDialer: clientDialer,
2020-05-14 13:22:29 +00:00
}
if err := p.auth(); err != nil {
return nil, err
}
return p, nil
}
// ID is used for generating transfer ID by combining source and target ID.
// We want to keep the same rules for import from any IMAP server, therefore
// it returns constant.
func (p *IMAPProvider) ID() string {
return "imap"
}
// Mailboxes returns all available folder names from root of EML files.
// In case the same folder name is used more than once (for example root/a/foo
// and root/b/foo), it's treated as the same folder.
2020-08-12 11:56:49 +00:00
func (p *IMAPProvider) Mailboxes(includeEmpty, includeAllMail bool) ([]Mailbox, error) {
2020-05-14 13:22:29 +00:00
mailboxesInfo, err := p.list()
if err != nil {
return nil, err
}
mailboxes := []Mailbox{}
for _, mailbox := range mailboxesInfo {
hasNoSelect := false
for _, attrib := range mailbox.Attributes {
if strings.ToLower(attrib) == "\\noselect" {
hasNoSelect = true
break
}
}
2020-08-12 11:56:49 +00:00
if hasNoSelect {
2020-05-14 13:22:29 +00:00
continue
}
2020-08-12 11:56:49 +00:00
if !includeEmpty || true {
2020-05-14 13:22:29 +00:00
mailboxStatus, err := p.selectIn(mailbox.Name)
if err != nil {
return nil, err
}
if mailboxStatus.Messages == 0 {
continue
}
}
mailboxes = append(mailboxes, Mailbox{
ID: "",
Name: mailbox.Name,
Color: "",
IsExclusive: false,
})
}
return mailboxes, nil
}