Fix/change things

This commit is contained in:
Tulir Asokan 2019-05-23 20:09:13 +03:00
parent 94ba2244b0
commit f9c3e99d10
5 changed files with 56 additions and 18 deletions

View file

@ -76,6 +76,7 @@ func (handler *CommandHandler) Handle(roomID types.MatrixRoomID, user *User, mes
User: user, User: user,
Args: args[1:], Args: args[1:],
} }
handler.log.Debugfln("%s sent '%s' in %s", user.MXID, message, roomID)
switch cmd { switch cmd {
case "login": case "login":
handler.CommandLogin(ce) handler.CommandLogin(ce)
@ -83,11 +84,15 @@ func (handler *CommandHandler) Handle(roomID types.MatrixRoomID, user *User, mes
handler.CommandHelp(ce) handler.CommandHelp(ce)
case "reconnect": case "reconnect":
handler.CommandReconnect(ce) handler.CommandReconnect(ce)
case "disconnect":
handler.CommandDisconnect(ce)
case "delete-connection":
handler.CommandDeleteConnection(ce)
case "delete-session": case "delete-session":
handler.CommandDeleteSession(ce) handler.CommandDeleteSession(ce)
case "delete-portal": case "delete-portal":
handler.CommandDeletePortal(ce) handler.CommandDeletePortal(ce)
case "logout", "disconnect", "sync", "list", "open", "pm": case "logout", "sync", "list", "open", "pm":
if ce.User.Conn == nil { if ce.User.Conn == nil {
ce.Reply("You are not logged in. Use the `login` command to log into WhatsApp.") ce.Reply("You are not logged in. Use the `login` command to log into WhatsApp.")
return return
@ -99,8 +104,6 @@ func (handler *CommandHandler) Handle(roomID types.MatrixRoomID, user *User, mes
switch cmd { switch cmd {
case "logout": case "logout":
handler.CommandLogout(ce) handler.CommandLogout(ce)
case "disconnect":
handler.CommandDisconnect(ce)
case "sync": case "sync":
handler.CommandSync(ce) handler.CommandSync(ce)
case "list": case "list":
@ -147,6 +150,7 @@ func (handler *CommandHandler) CommandLogout(ce *CommandEvent) {
ce.User.log.Warnln("Error while disconnecting after logout:", err) ce.User.log.Warnln("Error while disconnecting after logout:", err)
} }
ce.User.Connected = false ce.User.Connected = false
ce.User.Conn.RemoveHandlers()
ce.User.Conn = nil ce.User.Conn = nil
ce.User.SetSession(nil) ce.User.SetSession(nil)
ce.Reply("Logged out successfully.") ce.Reply("Logged out successfully.")
@ -163,6 +167,7 @@ func (handler *CommandHandler) CommandDeleteSession(ce *CommandEvent) {
ce.User.Connected = false ce.User.Connected = false
if ce.User.Conn != nil { if ce.User.Conn != nil {
_, _ = ce.User.Conn.Disconnect() _, _ = ce.User.Conn.Disconnect()
ce.User.Conn.RemoveHandlers()
ce.User.Conn = nil ce.User.Conn = nil
} }
ce.Reply("Session information purged") ce.Reply("Session information purged")
@ -171,6 +176,11 @@ func (handler *CommandHandler) CommandDeleteSession(ce *CommandEvent) {
const cmdReconnectHelp = `reconnect - Reconnect to WhatsApp` const cmdReconnectHelp = `reconnect - Reconnect to WhatsApp`
func (handler *CommandHandler) CommandReconnect(ce *CommandEvent) { func (handler *CommandHandler) CommandReconnect(ce *CommandEvent) {
if ce.User.Conn == nil {
ce.Reply("No existing connection, creating one...")
ce.User.Connect(false)
return
}
err := ce.User.Conn.Restore() err := ce.User.Conn.Restore()
if err == whatsapp.ErrInvalidSession { if err == whatsapp.ErrInvalidSession {
if ce.User.Session != nil { if ce.User.Session != nil {
@ -211,9 +221,28 @@ func (handler *CommandHandler) CommandReconnect(ce *CommandEvent) {
go ce.User.PostLogin() go ce.User.PostLogin()
} }
func (handler *CommandHandler) CommandDeleteConnection(ce *CommandEvent) {
if ce.User.Conn == nil {
ce.Reply("You don't have a WhatsApp connection.")
return
}
sess, err := ce.User.Conn.Disconnect()
if err == nil && len(sess.Wid) > 0 {
ce.User.SetSession(&sess)
}
ce.User.Connected = false
ce.User.Conn.RemoveHandlers()
ce.User.Conn = nil
ce.Reply("Successfully disconnected. Use the `reconnect` command to reconnect.")
}
const cmdDisconnectHelp = `disconnect - Disconnect from WhatsApp (without logging out)` const cmdDisconnectHelp = `disconnect - Disconnect from WhatsApp (without logging out)`
func (handler *CommandHandler) CommandDisconnect(ce *CommandEvent) { func (handler *CommandHandler) CommandDisconnect(ce *CommandEvent) {
if ce.User.Conn == nil {
ce.Reply("You don't have a WhatsApp connection.")
return
}
sess, err := ce.User.Conn.Disconnect() sess, err := ce.User.Conn.Disconnect()
ce.User.Connected = false ce.User.Connected = false
if err == whatsapp.ErrNotConnected { if err == whatsapp.ErrNotConnected {

View file

@ -326,11 +326,7 @@ func (portal *Portal) UpdateAvatar(user *User, avatar *whatsappExt.ProfilePicInf
} }
} }
if avatar.Status != 0 && avatar.Status != 404 { if avatar.Status != 0 {
// 401s can happen a lot so let's not log those.
if avatar.Status != 401 {
portal.log.Warnln("Unexpected avatar update status code, not updating.", avatar)
}
return false return false
} }

View file

@ -136,11 +136,7 @@ func (puppet *Puppet) UpdateAvatar(source *User, avatar *whatsappExt.ProfilePicI
} }
} }
if avatar.Status != 0 && avatar.Status != 404 { if avatar.Status != 0 {
// 401s can happen a lot so let's not log those.
if avatar.Status != 401 {
puppet.log.Warnln("Unexpected avatar update status code, not updating.", avatar)
}
return false return false
} }

View file

@ -37,11 +37,6 @@ const (
MessageChat JSONMessageType = "Chat" MessageChat JSONMessageType = "Chat"
) )
func (ext *ExtendedConn) AddHandler(handler whatsapp.Handler) {
ext.Conn.AddHandler(handler)
ext.handlers = append(ext.handlers, handler)
}
func (ext *ExtendedConn) HandleError(error) {} func (ext *ExtendedConn) HandleError(error) {}
type UnhandledJSONMessageHandler interface { type UnhandledJSONMessageHandler interface {

View file

@ -46,6 +46,28 @@ func ExtendConn(conn *whatsapp.Conn) *ExtendedConn {
return ext return ext
} }
func (ext *ExtendedConn) AddHandler(handler whatsapp.Handler) {
ext.Conn.AddHandler(handler)
ext.handlers = append(ext.handlers, handler)
}
func (ext *ExtendedConn) RemoveHandler(handler whatsapp.Handler) bool {
ext.Conn.RemoveHandler(handler)
for i, v := range ext.handlers {
if v == handler {
ext.handlers = append(ext.handlers[:i], ext.handlers[i+1:]...)
return true
}
}
return false
}
func (ext *ExtendedConn) RemoveHandlers() {
ext.Conn.RemoveHandlers()
ext.handlers = make([]whatsapp.Handler, 0)
}
func (ext *ExtendedConn) shouldCallSynchronously(handler whatsapp.Handler) bool { func (ext *ExtendedConn) shouldCallSynchronously(handler whatsapp.Handler) bool {
sh, ok := handler.(whatsapp.SyncHandler) sh, ok := handler.(whatsapp.SyncHandler)
return ok && sh.ShouldCallSynchronously() return ok && sh.ShouldCallSynchronously()