proton-bridge/internal/frontend/types/types.go

144 lines
4.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 types provides interfaces used in frontend packages.
package types
import (
"github.com/ProtonMail/proton-bridge/internal/bridge"
"github.com/ProtonMail/proton-bridge/internal/importexport"
"github.com/ProtonMail/proton-bridge/internal/transfer"
"github.com/ProtonMail/proton-bridge/pkg/pmapi"
"github.com/ProtonMail/proton-bridge/pkg/updates"
)
// PanicHandler is an interface of a type that can be used to gracefully handle panics which occur.
type PanicHandler interface {
HandlePanic()
}
// Updater is an interface for handling Bridge upgrades.
type Updater interface {
CheckIsUpToDate() (isUpToDate bool, latestVersion updates.VersionInfo, err error)
GetDownloadLink() string
GetLocalVersion() updates.VersionInfo
StartUpgrade(currentStatus chan<- updates.Progress)
}
type NoEncConfirmator interface {
ConfirmNoEncryption(string, bool)
}
// UserManager is an interface of users needed by frontend.
type UserManager interface {
Login(username, password string) (pmapi.Client, *pmapi.Auth, error)
FinishLogin(client pmapi.Client, auth *pmapi.Auth, mailboxPassword string) (User, error)
GetUsers() []User
GetUser(query string) (User, error)
DeleteUser(userID string, clearCache bool) error
ClearData() error
CheckConnection() error
}
// User is an interface of user needed by frontend.
type User interface {
ID() string
Username() string
IsConnected() bool
IsCombinedAddressMode() bool
GetPrimaryAddress() string
GetAddresses() []string
GetBridgePassword() string
SwitchAddressMode() error
Logout() error
}
// Bridger is an interface of bridge needed by frontend.
type Bridger interface {
UserManager
GetCurrentClient() string
SetCurrentOS(os string)
ReportBug(osType, osVersion, description, accountName, address, emailClient string) error
AllowProxy()
DisallowProxy()
}
type bridgeWrap struct {
*bridge.Bridge
}
// NewBridgeWrap wraps bridge struct into local bridgeWrap to implement local interface.
// The problem is that Bridge returns the bridge package's User type.
// Every method which returns User therefore has to be overridden to fulfill the interface.
func NewBridgeWrap(bridge *bridge.Bridge) *bridgeWrap { //nolint[golint]
return &bridgeWrap{Bridge: bridge}
}
func (b *bridgeWrap) FinishLogin(client pmapi.Client, auth *pmapi.Auth, mailboxPassword string) (User, error) {
return b.Bridge.FinishLogin(client, auth, mailboxPassword)
}
func (b *bridgeWrap) GetUsers() (users []User) {
for _, user := range b.Bridge.GetUsers() {
users = append(users, user)
}
return
}
func (b *bridgeWrap) GetUser(query string) (User, error) {
return b.Bridge.GetUser(query)
}
// ImportExporter is an interface of import/export needed by frontend.
type ImportExporter interface {
UserManager
GetLocalImporter(string, string) (*transfer.Transfer, error)
GetRemoteImporter(string, string, string, string, string) (*transfer.Transfer, error)
GetEMLExporter(string, string) (*transfer.Transfer, error)
GetMBOXExporter(string, string) (*transfer.Transfer, error)
}
type importExportWrap struct {
*importexport.ImportExport
}
// NewImportExportWrap wraps import/export struct into local importExportWrap
// to implement local interface.
// The problem is that Import/Export returns the importexport package's User
// type. Every method which returns User therefore has to be overridden to
// fulfill the interface.
func NewImportExportWrap(ie *importexport.ImportExport) *importExportWrap { //nolint[golint]
return &importExportWrap{ImportExport: ie}
}
func (b *importExportWrap) FinishLogin(client pmapi.Client, auth *pmapi.Auth, mailboxPassword string) (User, error) {
return b.ImportExport.FinishLogin(client, auth, mailboxPassword)
}
func (b *importExportWrap) GetUsers() (users []User) {
for _, user := range b.ImportExport.GetUsers() {
users = append(users, user)
}
return
}
func (b *importExportWrap) GetUser(query string) (User, error) {
return b.ImportExport.GetUser(query)
}