Add a bunch of new logging

This commit is contained in:
Tulir Asokan 2019-01-21 23:55:16 +02:00
parent 79bd35964f
commit 9e170f3d04
9 changed files with 53 additions and 30 deletions

View file

@ -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
}

View file

@ -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
}

View file

@ -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
}

View file

@ -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
}

4
go.mod
View file

@ -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

View file

@ -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()

View file

@ -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)

View file

@ -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

10
user.go
View file

@ -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) {