Merge branch 'master' into cross-signing

This commit is contained in:
Tulir Asokan
2020-11-12 00:21:11 +02:00
17 changed files with 185 additions and 160 deletions

View File

@ -19,10 +19,9 @@
package matrix
import (
"fmt"
"path/filepath"
"github.com/pkg/errors"
"maunium.net/go/mautrix/crypto"
"maunium.net/go/gomuks/debug"
@ -53,14 +52,14 @@ func isBadEncryptError(err error) bool {
func (c *Container) initCrypto() error {
cryptoStore, err := crypto.NewGobStore(filepath.Join(c.config.DataDir, "crypto.gob"))
if err != nil {
return errors.Wrap(err, "failed to open crypto store")
return fmt.Errorf("failed to open crypto store: %w", err)
}
crypt := crypto.NewOlmMachine(c.client, cryptoLogger{}, cryptoStore, c.config.Rooms)
crypt.AllowUnverifiedDevices = !c.config.SendToVerifiedOnly
c.crypto = crypt
err = c.crypto.Load()
if err != nil {
return errors.Wrap(err, "failed to create olm machine")
return fmt.Errorf("failed to create olm machine: %w", err)
}
return nil
}

View File

@ -32,8 +32,7 @@ import (
"runtime"
dbg "runtime/debug"
"time"
"github.com/pkg/errors"
"errors"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/crypto/attachment"
@ -115,7 +114,7 @@ func (c *Container) InitClient() error {
var err error
c.client, err = mautrix.NewClient(c.config.HS, mxid, accessToken)
if err != nil {
return errors.Wrap(err, "failed to create mautrix client")
return fmt.Errorf("failed to create mautrix client: %w", err)
}
c.client.UserAgent = fmt.Sprintf("gomuks %s (with mautrix-go %s)", c.gmx.Version(), mautrix.Version)
c.client.Logger = mxLogger{}
@ -123,13 +122,13 @@ func (c *Container) InitClient() error {
err = c.initCrypto()
if err != nil {
return errors.Wrap(err, "failed to initialize crypto")
return fmt.Errorf("failed to initialize crypto: %w", err)
}
if c.history == nil {
c.history, err = NewHistoryManager(c.config.HistoryPath)
if err != nil {
return errors.Wrap(err, "failed to initialize history")
return fmt.Errorf("failed to initialize history: %w", err)
}
}
@ -252,13 +251,14 @@ func (c *Container) Login(user, password string) error {
if err != nil {
return err
}
if len(resp.Flows) == 1 && resp.Flows[0].Type == "m.login.password" {
return c.PasswordLogin(user, password)
} else if len(resp.Flows) == 2 && resp.Flows[0].Type == "m.login.sso" && resp.Flows[1].Type == "m.login.token" {
return c.SingleSignOn()
} else {
return fmt.Errorf("no supported login flows")
for _, flow := range resp.Flows {
if flow.Type == "m.login.password" {
return c.PasswordLogin(user, password)
} else if flow.Type == "m.login.sso" {
return c.SingleSignOn()
}
}
return fmt.Errorf("no supported login flows")
}
// Logout revokes the access token, stops the syncer and calls the OnLogout() method of the UI.
@ -430,7 +430,8 @@ func (c *Container) Start() {
default:
if err := c.client.Sync(); err != nil {
if errors.Is(err, mautrix.MUnknownToken) {
debug.Print("Sync() errored with", err, "-> logging out")
debug.Print("Sync() errored with ", err, " -> logging out")
// TODO support soft logout
c.Logout()
} else {
debug.Print("Sync() errored", err)
@ -953,7 +954,7 @@ func (c *Container) UploadMedia(path string, encrypt bool) (*ifc.UploadedMediaIn
var err error
path, err = filepath.Abs(path)
if err != nil {
return nil, errors.Wrap(err, "failed to get absolute path")
return nil, fmt.Errorf("failed to get absolute path: %w", err)
}
msgtype, info, err := getMediaInfo(path)
@ -963,12 +964,12 @@ func (c *Container) UploadMedia(path string, encrypt bool) (*ifc.UploadedMediaIn
file, err := os.Open(path)
if err != nil {
return nil, errors.Wrap(err, "failed to open file")
return nil, fmt.Errorf("failed to open file: %w", err)
}
stat, err := file.Stat()
if err != nil {
return nil, errors.Wrap(err, "failed to get file info")
return nil, fmt.Errorf("failed to get file info: %w", err)
}
uploadFileName := stat.Name()

View File

@ -25,22 +25,22 @@ import (
"time"
"github.com/gabriel-vasile/mimetype"
"github.com/pkg/errors"
"gopkg.in/vansante/go-ffprobe.v2"
"maunium.net/go/gomuks/debug"
"maunium.net/go/mautrix/event"
"maunium.net/go/gomuks/debug"
)
func getImageInfo(path string) (event.FileInfo, error) {
var info event.FileInfo
file, err := os.Open(path)
if err != nil {
return info, errors.Wrap(err, "failed to open image to get info")
return info, fmt.Errorf("failed to open image to get info: %w", err)
}
cfg, _, err := image.DecodeConfig(file)
if err != nil {
return info, errors.Wrap(err, "failed to get image info")
return info, fmt.Errorf("failed to get image info: %w", err)
}
info.Width = cfg.Width
info.Height = cfg.Height
@ -53,7 +53,7 @@ func getFFProbeInfo(mimeClass, path string) (msgtype event.MessageType, info eve
var probedInfo *ffprobe.ProbeData
probedInfo, err = ffprobe.ProbeURL(ctx, path)
if err != nil {
err = errors.Wrap(err, fmt.Sprintf("failed to get %s info with ffprobe", mimeClass))
err = fmt.Errorf("failed to get %s info with ffprobe: %w", mimeClass, err)
return
}
if mimeClass == "audio" {
@ -78,7 +78,7 @@ func getMediaInfo(path string) (msgtype event.MessageType, info event.FileInfo,
var mime *mimetype.MIME
mime, err = mimetype.DetectFile(path)
if err != nil {
err = errors.Wrap(err, "failed to get content type")
err = fmt.Errorf("failed to get content type: %w", err)
return
}

View File

@ -19,16 +19,17 @@ package rooms
import (
"compress/gzip"
"encoding/gob"
"fmt"
"os"
"path/filepath"
"time"
"github.com/pkg/errors"
sync "github.com/sasha-s/go-deadlock"
"maunium.net/go/gomuks/debug"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
"maunium.net/go/gomuks/debug"
)
// RoomCache contains room state info in a hashmap and linked list.
@ -114,14 +115,14 @@ func (cache *RoomCache) LoadList() error {
if os.IsNotExist(err) {
return nil
}
return errors.Wrap(err, "failed to open room list file for reading")
return fmt.Errorf("failed to open room list file for reading: %w", err)
}
defer debugPrintError(file.Close, "Failed to close room list file after reading")
// Open gzip reader for room list file
cmpReader, err := gzip.NewReader(file)
if err != nil {
return errors.Wrap(err, "failed to read gzip room list")
return fmt.Errorf("failed to read gzip room list: %w", err)
}
defer debugPrintError(cmpReader.Close, "Failed to close room list gzip reader")
@ -131,7 +132,7 @@ func (cache *RoomCache) LoadList() error {
var size int
err = dec.Decode(&size)
if err != nil {
return errors.Wrap(err, "failed to read size of room list")
return fmt.Errorf("failed to read size of room list: %w", err)
}
// Read list
@ -167,7 +168,7 @@ func (cache *RoomCache) SaveList() error {
// Open room list file
file, err := os.OpenFile(cache.listPath, os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return errors.Wrap(err, "failed to open room list file for writing")
return fmt.Errorf("failed to open room list file for writing: %w", err)
}
defer debugPrintError(file.Close, "Failed to close room list file after writing")
@ -180,7 +181,7 @@ func (cache *RoomCache) SaveList() error {
// Write number of items in list
err = enc.Encode(len(cache.Map))
if err != nil {
return errors.Wrap(err, "failed to write size of room list")
return fmt.Errorf("failed to write size of room list: %w", err)
}
// Write list