mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-10 04:36:43 +00:00
114 lines
3.6 KiB
Go
114 lines
3.6 KiB
Go
// Copyright (c) 2020 Proton Technologies AG
|
|
//
|
|
// 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 importexport provides core functionality of Import/Export app.
|
|
package importexport
|
|
|
|
import (
|
|
"github.com/ProtonMail/proton-bridge/internal/transfer"
|
|
"github.com/ProtonMail/proton-bridge/internal/users"
|
|
|
|
"github.com/ProtonMail/proton-bridge/pkg/listener"
|
|
logrus "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
var (
|
|
log = logrus.WithField("pkg", "importexport") //nolint[gochecknoglobals]
|
|
)
|
|
|
|
type ImportExport struct {
|
|
*users.Users
|
|
|
|
config Configer
|
|
panicHandler users.PanicHandler
|
|
clientManager users.ClientManager
|
|
}
|
|
|
|
func New(
|
|
config Configer,
|
|
panicHandler users.PanicHandler,
|
|
eventListener listener.Listener,
|
|
clientManager users.ClientManager,
|
|
credStorer users.CredentialsStorer,
|
|
) *ImportExport {
|
|
u := users.New(config, panicHandler, eventListener, clientManager, credStorer, &storeFactory{}, false)
|
|
return &ImportExport{
|
|
Users: u,
|
|
|
|
config: config,
|
|
panicHandler: panicHandler,
|
|
clientManager: clientManager,
|
|
}
|
|
}
|
|
|
|
// GetLocalImporter returns transferrer from local EML or MBOX structure to ProtonMail account.
|
|
func (ie *ImportExport) GetLocalImporter(address, path string) (*transfer.Transfer, error) {
|
|
source := transfer.NewLocalProvider(path)
|
|
target, err := ie.getPMAPIProvider(address)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return transfer.New(ie.panicHandler, ie.config.GetTransferDir(), source, target)
|
|
}
|
|
|
|
// GetRemoteImporter returns transferrer from remote IMAP to ProtonMail account.
|
|
func (ie *ImportExport) GetRemoteImporter(address, username, password, host, port string) (*transfer.Transfer, error) {
|
|
source, err := transfer.NewIMAPProvider(username, password, host, port)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
target, err := ie.getPMAPIProvider(address)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return transfer.New(ie.panicHandler, ie.config.GetTransferDir(), source, target)
|
|
}
|
|
|
|
// GetEMLExporter returns transferrer from ProtonMail account to local EML structure.
|
|
func (ie *ImportExport) GetEMLExporter(address, path string) (*transfer.Transfer, error) {
|
|
source, err := ie.getPMAPIProvider(address)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
target := transfer.NewEMLProvider(path)
|
|
return transfer.New(ie.panicHandler, ie.config.GetTransferDir(), source, target)
|
|
}
|
|
|
|
// GetMBOXExporter returns transferrer from ProtonMail account to local MBOX structure.
|
|
func (ie *ImportExport) GetMBOXExporter(address, path string) (*transfer.Transfer, error) {
|
|
source, err := ie.getPMAPIProvider(address)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
target := transfer.NewMBOXProvider(path)
|
|
return transfer.New(ie.panicHandler, ie.config.GetTransferDir(), source, target)
|
|
}
|
|
|
|
func (ie *ImportExport) getPMAPIProvider(address string) (*transfer.PMAPIProvider, error) {
|
|
user, err := ie.Users.GetUser(address)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
addressID, err := user.GetAddressID(address)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return transfer.NewPMAPIProvider(ie.clientManager, user.ID(), addressID)
|
|
}
|