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,
Args: args[1:],
}
handler.log.Debugfln("%s sent '%s' in %s", user.MXID, message, roomID)
switch cmd {
case "login":
handler.CommandLogin(ce)
@ -83,11 +84,15 @@ func (handler *CommandHandler) Handle(roomID types.MatrixRoomID, user *User, mes
handler.CommandHelp(ce)
case "reconnect":
handler.CommandReconnect(ce)
case "disconnect":
handler.CommandDisconnect(ce)
case "delete-connection":
handler.CommandDeleteConnection(ce)
case "delete-session":
handler.CommandDeleteSession(ce)
case "delete-portal":
handler.CommandDeletePortal(ce)
case "logout", "disconnect", "sync", "list", "open", "pm":
case "logout", "sync", "list", "open", "pm":
if ce.User.Conn == nil {
ce.Reply("You are not logged in. Use the `login` command to log into WhatsApp.")
return
@ -99,8 +104,6 @@ func (handler *CommandHandler) Handle(roomID types.MatrixRoomID, user *User, mes
switch cmd {
case "logout":
handler.CommandLogout(ce)
case "disconnect":
handler.CommandDisconnect(ce)
case "sync":
handler.CommandSync(ce)
case "list":
@ -147,6 +150,7 @@ func (handler *CommandHandler) CommandLogout(ce *CommandEvent) {
ce.User.log.Warnln("Error while disconnecting after logout:", err)
}
ce.User.Connected = false
ce.User.Conn.RemoveHandlers()
ce.User.Conn = nil
ce.User.SetSession(nil)
ce.Reply("Logged out successfully.")
@ -163,6 +167,7 @@ func (handler *CommandHandler) CommandDeleteSession(ce *CommandEvent) {
ce.User.Connected = false
if ce.User.Conn != nil {
_, _ = ce.User.Conn.Disconnect()
ce.User.Conn.RemoveHandlers()
ce.User.Conn = nil
}
ce.Reply("Session information purged")
@ -171,6 +176,11 @@ func (handler *CommandHandler) CommandDeleteSession(ce *CommandEvent) {
const cmdReconnectHelp = `reconnect - Reconnect to WhatsApp`
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()
if err == whatsapp.ErrInvalidSession {
if ce.User.Session != nil {
@ -211,9 +221,28 @@ func (handler *CommandHandler) CommandReconnect(ce *CommandEvent) {
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)`
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()
ce.User.Connected = false
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 {
// 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)
}
if avatar.Status != 0 {
return false
}

View file

@ -136,11 +136,7 @@ func (puppet *Puppet) UpdateAvatar(source *User, avatar *whatsappExt.ProfilePicI
}
}
if avatar.Status != 0 && avatar.Status != 404 {
// 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)
}
if avatar.Status != 0 {
return false
}

View file

@ -37,11 +37,6 @@ const (
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) {}
type UnhandledJSONMessageHandler interface {

View file

@ -46,6 +46,28 @@ func ExtendConn(conn *whatsapp.Conn) *ExtendedConn {
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 {
sh, ok := handler.(whatsapp.SyncHandler)
return ok && sh.ShouldCallSynchronously()