From 9e170f3d040ed8dce732b61cf6a2554a3b86d29e Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Mon, 21 Jan 2019 23:55:16 +0200 Subject: [PATCH] Add a bunch of new logging --- database/message.go | 3 +-- database/portal.go | 6 ++---- database/puppet.go | 6 ++---- database/user.go | 6 ++---- go.mod | 4 ++++ main.go | 2 +- portal.go | 25 ++++++++++++++++++------- puppet.go | 21 +++++++++++++++------ user.go | 10 ++++++++-- 9 files changed, 53 insertions(+), 30 deletions(-) diff --git a/database/message.go b/database/message.go index 904e8b0..c6a2790 100644 --- a/database/message.go +++ b/database/message.go @@ -129,10 +129,9 @@ func (msg *Message) encodeBinaryContent() []byte { return buf.Bytes() } -func (msg *Message) Insert() error { +func (msg *Message) Insert() { _, err := msg.db.Exec("INSERT INTO message VALUES (?, ?, ?, ?, ?, ?)", msg.Chat.JID, msg.Chat.Receiver, msg.JID, msg.MXID, msg.Sender, msg.encodeBinaryContent()) if err != nil { msg.log.Warnfln("Failed to insert %s@%s: %v", msg.Chat, msg.JID, err) } - return err } diff --git a/database/portal.go b/database/portal.go index ec40fac..60eb486 100644 --- a/database/portal.go +++ b/database/portal.go @@ -142,16 +142,15 @@ func (portal *Portal) mxidPtr() *string { return nil } -func (portal *Portal) Insert() error { +func (portal *Portal) Insert() { _, err := portal.db.Exec("INSERT INTO portal VALUES (?, ?, ?, ?, ?, ?)", portal.Key.JID, portal.Key.Receiver, portal.mxidPtr(), portal.Name, portal.Topic, portal.Avatar) if err != nil { portal.log.Warnfln("Failed to insert %s: %v", portal.Key, err) } - return err } -func (portal *Portal) Update() error { +func (portal *Portal) Update() { var mxid *string if len(portal.MXID) > 0 { mxid = &portal.MXID @@ -161,5 +160,4 @@ func (portal *Portal) Update() error { if err != nil { portal.log.Warnfln("Failed to update %s: %v", portal.Key, err) } - return err } diff --git a/database/puppet.go b/database/puppet.go index afccdc2..3e1b169 100644 --- a/database/puppet.go +++ b/database/puppet.go @@ -92,20 +92,18 @@ func (puppet *Puppet) Scan(row Scannable) *Puppet { return puppet } -func (puppet *Puppet) Insert() error { +func (puppet *Puppet) Insert() { _, err := puppet.db.Exec("INSERT INTO puppet VALUES (?, ?, ?, ?)", puppet.JID, puppet.Avatar, puppet.Displayname, puppet.NameQuality) if err != nil { puppet.log.Warnfln("Failed to insert %s: %v", puppet.JID, err) } - return err } -func (puppet *Puppet) Update() error { +func (puppet *Puppet) Update() { _, err := puppet.db.Exec("UPDATE puppet SET displayname=?, name_quality=?, avatar=? WHERE jid=?", puppet.Displayname, puppet.NameQuality, puppet.Avatar, puppet.JID) if err != nil { puppet.log.Warnfln("Failed to update %s->%s: %v", puppet.JID, err) } - return err } diff --git a/database/user.go b/database/user.go index f7ccba2..090d70a 100644 --- a/database/user.go +++ b/database/user.go @@ -148,7 +148,7 @@ func (user *User) sessionUnptr() (sess whatsapp.Session) { return } -func (user *User) Insert() error { +func (user *User) Insert() { sess := user.sessionUnptr() _, err := user.db.Exec("INSERT INTO user VALUES (?, ?, ?, ?, ?, ?, ?, ?)", user.MXID, user.jidPtr(), user.ManagementRoom, @@ -156,10 +156,9 @@ func (user *User) Insert() error { if err != nil { user.log.Warnfln("Failed to insert %s: %v", user.MXID, err) } - return err } -func (user *User) Update() error { +func (user *User) Update() { sess := user.sessionUnptr() _, err := user.db.Exec("UPDATE user SET jid=?, management_room=?, client_id=?, client_token=?, server_token=?, enc_key=?, mac_key=? WHERE mxid=?", user.jidPtr(), user.ManagementRoom, @@ -168,5 +167,4 @@ func (user *User) Update() error { if err != nil { user.log.Warnfln("Failed to update %s: %v", user.MXID, err) } - return err } diff --git a/go.mod b/go.mod index 134b468..8679b92 100644 --- a/go.mod +++ b/go.mod @@ -14,3 +14,7 @@ require ( maunium.net/go/mautrix v0.1.0-alpha.3 maunium.net/go/mautrix-appservice v0.1.0-alpha.3 ) + +replace maunium.net/go/mautrix => ../mautrix-go + +replace maunium.net/go/mautrix-appservice => ../mautrix-appservice-go diff --git a/main.go b/main.go index f18cf34..22ee7a9 100644 --- a/main.go +++ b/main.go @@ -33,7 +33,7 @@ import ( ) var configPath = flag.MakeFull("c", "config", "The path to your config file.", "config.yaml").String() -var baseConfigPath = flag.MakeFull("b", "base-config", "The path to the example config file.", "example-config.yaml").String() +//var baseConfigPath = flag.MakeFull("b", "base-config", "The path to the example config file.", "example-config.yaml").String() var registrationPath = flag.MakeFull("r", "registration", "The path where to save the appservice registration.", "registration.yaml").String() var generateRegistration = flag.MakeFull("g", "generate-registration", "Generate registration and quit.", "false").Bool() var wantHelp, _ = flag.MakeHelpFlag() diff --git a/portal.go b/portal.go index 97c0d37..ea8a306 100644 --- a/portal.go +++ b/portal.go @@ -222,13 +222,19 @@ func (portal *Portal) SyncParticipants(metadata *whatsappExt.GroupInfo) { } for _, participant := range metadata.Participants { puppet := portal.bridge.GetPuppetByJID(participant.JID) - puppet.Intent().EnsureJoined(portal.MXID) + err := puppet.Intent().EnsureJoined(portal.MXID) + if err != nil { + portal.log.Warnfln("Failed to make puppet of %s join %s: %v", participant.JID, portal.MXID, err) + } user := portal.bridge.GetUserByJID(participant.JID) if user != nil && !portal.bridge.AS.StateStore.IsInvited(portal.MXID, user.MXID) { - portal.MainIntent().InviteUser(portal.MXID, &mautrix.ReqInviteUser{ + _, err = portal.MainIntent().InviteUser(portal.MXID, &mautrix.ReqInviteUser{ UserID: user.MXID, }) + if err != nil { + portal.log.Warnfln("Failed to invite %s to %s: %v", user.MXID, portal.MXID, err) + } } expectedLevel := 0 @@ -272,14 +278,14 @@ func (portal *Portal) UpdateAvatar(user *User, avatar *whatsappExt.ProfilePicInf data, err := avatar.DownloadBytes() if err != nil { - portal.log.Errorln("Failed to download avatar:", err) + portal.log.Warnln("Failed to download avatar:", err) return false } mimeType := http.DetectContentType(data) resp, err := portal.MainIntent().UploadBytes(data, mimeType) if err != nil { - portal.log.Errorln("Failed to upload avatar:", err) + portal.log.Warnln("Failed to upload avatar:", err) return false } @@ -354,7 +360,10 @@ func (portal *Portal) Sync(user *User, contact whatsapp.Contact) { return } } else { - portal.MainIntent().EnsureInvited(portal.MXID, user.MXID) + err := portal.MainIntent().EnsureInvited(portal.MXID, user.MXID) + if err != nil { + portal.log.Warnfln("Failed to ensure %s is invited to %s: %v", user.MXID, portal.MXID, err) + } } update := false @@ -569,7 +578,7 @@ func (portal *Portal) HandleTextMessage(source *User, message whatsapp.TextMessa portal.bridge.Formatter.ParseWhatsApp(content) portal.SetReply(content, message.Info) - intent.UserTyping(portal.MXID, false, 0) + _, _ = intent.UserTyping(portal.MXID, false, 0) resp, err := intent.SendMassagedMessageEvent(portal.MXID, mautrix.EventMessage, content, int64(message.Info.Timestamp*1000)) if err != nil { portal.log.Errorfln("Failed to handle message %s: %v", message.Info.Id, err) @@ -653,7 +662,7 @@ func (portal *Portal) HandleMediaMessage(source *User, download func() ([]byte, content.MsgType = mautrix.MsgFile } - intent.UserTyping(portal.MXID, false, 0) + _, _ = intent.UserTyping(portal.MXID, false, 0) ts := int64(info.Timestamp * 1000) resp, err := intent.SendMassagedMessageEvent(portal.MXID, mautrix.EventMessage, content, ts) if err != nil { @@ -768,6 +777,7 @@ func (portal *Portal) HandleMatrixMessage(sender *User, evt *mautrix.Event) { if portal.IsPrivateChat() && sender.JID != portal.Key.Receiver { return } + portal.log.Debugfln("Received event %s", evt.ID) ts := uint64(evt.Timestamp / 1000) status := waProto.WebMessageInfo_ERROR @@ -880,6 +890,7 @@ func (portal *Portal) HandleMatrixMessage(sender *User, evt *mautrix.Event) { return } portal.markHandled(sender, info, evt.ID) + portal.log.Debugln("Sending event", evt.ID, "to WhatsApp") err = sender.Conn.Send(info) if err != nil { portal.log.Errorfln("Error handling Matrix event %s: %v", evt.ID, err) diff --git a/puppet.go b/puppet.go index cb654f8..70ab56d 100644 --- a/puppet.go +++ b/puppet.go @@ -131,7 +131,7 @@ func (puppet *Puppet) UpdateAvatar(source *User, avatar *whatsappExt.ProfilePicI var err error avatar, err = source.Conn.GetProfilePicThumb(puppet.JID) if err != nil { - puppet.log.Errorln(err) + puppet.log.Warnln("Failed to get avatar:", err) return false } } @@ -141,31 +141,40 @@ func (puppet *Puppet) UpdateAvatar(source *User, avatar *whatsappExt.ProfilePicI } if len(avatar.URL) == 0 { - puppet.Intent().SetAvatarURL("") + err := puppet.Intent().SetAvatarURL("") + if err != nil { + puppet.log.Warnln("Failed to remove avatar:", err) + } puppet.Avatar = avatar.Tag return true } data, err := avatar.DownloadBytes() if err != nil { - puppet.log.Errorln("Failed to download avatar:", err) + puppet.log.Warnln("Failed to download avatar:", err) return false } mime := http.DetectContentType(data) resp, err := puppet.Intent().UploadBytes(data, mime) if err != nil { - puppet.log.Errorln("Failed to upload avatar:", err) + puppet.log.Warnln("Failed to upload avatar:", err) return false } - puppet.Intent().SetAvatarURL(resp.ContentURI) + err = puppet.Intent().SetAvatarURL(resp.ContentURI) + if err != nil { + puppet.log.Warnln("Failed to set avatar:", err) + } puppet.Avatar = avatar.Tag return true } func (puppet *Puppet) Sync(source *User, contact whatsapp.Contact) { - puppet.Intent().EnsureRegistered() + err := puppet.Intent().EnsureRegistered() + if err != nil { + puppet.log.Errorln("Failed to ensure registered:", err) + } if contact.Jid == source.JID { contact.Notify = source.Conn.Info.Pushname diff --git a/user.go b/user.go index eebf70e..40ac6b9 100644 --- a/user.go +++ b/user.go @@ -192,7 +192,10 @@ func (user *User) Login(roomID types.MatrixRoomID) { return } - bot.SendImage(roomID, string(code), resp.ContentURI) + _, err = bot.SendImage(roomID, string(code), resp.ContentURI) + if err != nil { + user.log.Errorln("Failed to send QR code to user:", err) + } }() session, err := user.Conn.Login(qrChan) if err != nil { @@ -204,7 +207,10 @@ func (user *User) Login(roomID types.MatrixRoomID) { user.JID = strings.Replace(user.Conn.Info.Wid, whatsappExt.OldUserSuffix, whatsappExt.NewUserSuffix, 1) user.Session = &session user.Update() - bot.SendNotice(roomID, "Successfully logged in. Now, you may ask for `import contacts`.") + _, err = bot.SendNotice(roomID, "Successfully logged in. Now, you may ask for `import contacts`.") + if err != nil { + user.log.Warnln("Failed to send login confirmation to user:", err) + } } func (user *User) HandleError(err error) {