From 7ad214a9addc1ac0749dc14947015cf885c829f1 Mon Sep 17 00:00:00 2001 From: Otto Hollmann Date: Sat, 2 Apr 2022 12:04:27 +0200 Subject: [PATCH 1/9] Fix login,logout commands. --- commands.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/commands.go b/commands.go index 974fb8a..4212ff5 100644 --- a/commands.go +++ b/commands.go @@ -220,6 +220,10 @@ func (handler *CommandHandler) CommandLogin(ce *CommandEvent) { ce.Reply("**Usage:** `login username password`") return } + if ce.User.Conn != nil && ce.User.Conn.LoggedIn == true { + ce.Reply("You're already logged into Skype.") + return + } leavePortals(ce) if !ce.User.Connect(true) { ce.User.log.Debugln("Connect() returned false, assuming error was logged elsewhere and canceling login.") @@ -234,7 +238,7 @@ func (handler *CommandHandler) CommandLogin(ce *CommandEvent) { const cmdLogoutHelp = `logout - Logout from Skype` func (handler *CommandHandler) CommandLogout(ce *CommandEvent) { - if ce.User.Conn == nil { + if ce.User.Conn == nil || ce.User.Conn.LoggedIn == false { ce.Reply("You're not logged into Skype.") return } From e002f32e7e88745b9fecfdde39fe3084b9556b54 Mon Sep 17 00:00:00 2001 From: Otto Hollmann Date: Sat, 2 Apr 2022 15:02:33 +0200 Subject: [PATCH 2/9] Add password column into database. --- .../2022-04-02-add-username-and-password.go | 18 ++++++++++++++++++ database/upgrades/upgrades.go | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 database/upgrades/2022-04-02-add-username-and-password.go diff --git a/database/upgrades/2022-04-02-add-username-and-password.go b/database/upgrades/2022-04-02-add-username-and-password.go new file mode 100644 index 0000000..0dc240d --- /dev/null +++ b/database/upgrades/2022-04-02-add-username-and-password.go @@ -0,0 +1,18 @@ +package upgrades + +import ( + "database/sql" +) + +func init() { + upgrades[20] = upgrade{"Add password columns to user table.", func(tx *sql.Tx, c context) error { + if c.dialect == Postgres { + _, err := tx.Exec(`ALTER TABLE "user" ADD COLUMN password VARCHAR(255)`) + if err != nil { + return err + } + } + + return nil + }} +} diff --git a/database/upgrades/upgrades.go b/database/upgrades/upgrades.go index e224593..af54f6c 100644 --- a/database/upgrades/upgrades.go +++ b/database/upgrades/upgrades.go @@ -39,7 +39,7 @@ type upgrade struct { fn upgradeFunc } -const NumberOfUpgrades = 20 +const NumberOfUpgrades = 21 var upgrades [NumberOfUpgrades]upgrade From c7fb0d0ed5adc7526ce6eefb85edb28c069b99c8 Mon Sep 17 00:00:00 2001 From: Otto Hollmann Date: Sat, 2 Apr 2022 16:23:06 +0200 Subject: [PATCH 3/9] Add save-password and remove-password commands. --- commands.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ database/user.go | 5 +++++ 2 files changed, 52 insertions(+) diff --git a/commands.go b/commands.go index 4212ff5..3a59976 100644 --- a/commands.go +++ b/commands.go @@ -101,6 +101,10 @@ func (handler *CommandHandler) CommandMux(ce *CommandEvent) { handler.CommandPing(ce) case "logout": handler.CommandLogout(ce) + case "save-password": + handler.CommandSavePassword(ce) + case "remove-password": + handler.CommandRemovePassword(ce) case "login-matrix", "sync", "list", "open", "pm", "invite", "kick", "leave", "join", "create", "share": if !ce.User.HasSession() { ce.Reply("You're not logged in. Use the `login` command to log into Skype.") @@ -281,6 +285,47 @@ func (handler *CommandHandler) CommandLogout(ce *CommandEvent) { } } +const cmdSavePasswordHelp = `save-password - save user password into database` + +// CommandSavePassword handles save-password command +func (handler *CommandHandler) CommandSavePassword(ce *CommandEvent) { + var ret bool + if len(ce.Args) > 0 { + ce.Reply("**Usage:** `save-password`") + return + } + + if ce.User.Conn == nil || ce.User.Conn.LoggedIn == false { + ce.Reply("You're not logged into Skype.") + return + } + + ret = ce.User.bridge.DB.User.SetPassByMXID(ce.User.Conn.LoginInfo.Password, ce.User.MXID) + if ret == true { + ce.Reply("Your password was successfully saved into database.") + } else { + ce.Reply("An error occurred while saving your password into database. Try it again.") + } +} + +const cmdRemovePasswordHelp = `remove-password - remove user password from database` + +// CommandRemovePassword handles remove-password command +func (handler *CommandHandler) CommandRemovePassword(ce *CommandEvent) { + var ret bool + if len(ce.Args) > 0 { + ce.Reply("**Usage:** `remove-password`") + return + } + + ret = ce.User.bridge.DB.User.SetPassByMXID("", ce.User.MXID) + if ret == true { + ce.Reply("Your password was successfully removed from database.") + } else { + ce.Reply("An error occurred while removing your password from database. Try it again.") + } +} + func leavePortals(ce *CommandEvent) { portals := ce.User.GetPortals() //newPortals := ce.User.GetPortalsNew() @@ -353,6 +398,8 @@ func (handler *CommandHandler) CommandHelp(ce *CommandEvent) { cmdPrefix + cmdHelpHelp, cmdPrefix + cmdLoginHelp, cmdPrefix + cmdLogoutHelp, + cmdPrefix + cmdSavePasswordHelp, + cmdPrefix + cmdRemovePasswordHelp, cmdPrefix + cmdPingHelp, //cmdPrefix + cmdLoginMatrixHelp, //cmdPrefix + cmdLogoutMatrixHelp, diff --git a/database/user.go b/database/user.go index 45ac771..73f5228 100644 --- a/database/user.go +++ b/database/user.go @@ -54,6 +54,11 @@ func (uq *UserQuery) GetByJID(userID types.SkypeID) *User { return uq.New().Scan(row) } +func (uq *UserQuery) SetPassByMXID(password string, userID id.UserID) bool { + row := uq.db.QueryRow(`UPDATE "user" SET password=$1 WHERE mxid=$2`, password, userID) + return row != nil +} + type User struct { db *Database log log.Logger From 3f6b71450680fbeb3e4123024b3a0746651f4c3d Mon Sep 17 00:00:00 2001 From: Otto Hollmann Date: Sat, 2 Apr 2022 23:59:47 +0200 Subject: [PATCH 4/9] Improve ping,logout commands. --- commands.go | 10 ++++++++++ database/user.go | 9 +++++++++ user.go | 2 +- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/commands.go b/commands.go index 3a59976..6686599 100644 --- a/commands.go +++ b/commands.go @@ -283,6 +283,11 @@ func (handler *CommandHandler) CommandLogout(ce *CommandEvent) { } else { leavePortals(ce) } + var pass string; + ret := ce.User.bridge.DB.User.GetPassByMXID(ce.User.MXID, &pass) + if ret && pass != "" { + ce.Reply("WARNING, your password is stored in database. Use command `remove-password` to remove it.") + } } const cmdSavePasswordHelp = `save-password - save user password into database` @@ -383,6 +388,11 @@ func (handler *CommandHandler) CommandPing(ce *CommandEvent) { } ce.Reply("You're logged in as @" + username + ", orgid is " + orgId) } + var pass string; + ret := ce.User.bridge.DB.User.GetPassByMXID(ce.User.MXID, &pass) + if ret && pass != "" { + ce.Reply("WARNING, your password is stored in database. Use command `remove-password` to remove it.") + } } const cmdHelpHelp = `help - Prints this help` diff --git a/database/user.go b/database/user.go index 73f5228..4d04b5b 100644 --- a/database/user.go +++ b/database/user.go @@ -54,6 +54,15 @@ func (uq *UserQuery) GetByJID(userID types.SkypeID) *User { return uq.New().Scan(row) } +func (uq *UserQuery) GetPassByMXID(userID id.UserID, password *string) bool { + row := uq.db.QueryRow(`SELECT password FROM "user" WHERE mxid=$1`, userID) + if row == nil { + return false + } + err := row.Scan(password) + return err == nil +} + func (uq *UserQuery) SetPassByMXID(password string, userID id.UserID) bool { row := uq.db.QueryRow(`UPDATE "user" SET password=$1 WHERE mxid=$2`, password, userID) return row != nil diff --git a/user.go b/user.go index f9db40d..2f5f763 100644 --- a/user.go +++ b/user.go @@ -396,7 +396,7 @@ func (user *User) monitorSession(ce *CommandEvent) { if x > 0 { user.SetSession(user.Conn.LoginInfo) } else { - ce.Reply("Session expired") + ce.Reply("Session expired\nStore your password into database with command `save-password` to resolve this issue.") close(user.Conn.Refresh) leavePortals(ce) } From 5f9ece8e0a7f3a053aa15f707c1169eb2f5d4ccf Mon Sep 17 00:00:00 2001 From: Otto Hollmann Date: Sun, 10 Apr 2022 11:43:12 +0200 Subject: [PATCH 5/9] Rename database upgrade. --- ...-add-username-and-password.go => 2022-04-02-add-password.go} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename database/upgrades/{2022-04-02-add-username-and-password.go => 2022-04-02-add-password.go} (69%) diff --git a/database/upgrades/2022-04-02-add-username-and-password.go b/database/upgrades/2022-04-02-add-password.go similarity index 69% rename from database/upgrades/2022-04-02-add-username-and-password.go rename to database/upgrades/2022-04-02-add-password.go index 0dc240d..174843b 100644 --- a/database/upgrades/2022-04-02-add-username-and-password.go +++ b/database/upgrades/2022-04-02-add-password.go @@ -5,7 +5,7 @@ import ( ) func init() { - upgrades[20] = upgrade{"Add password columns to user table.", func(tx *sql.Tx, c context) error { + upgrades[20] = upgrade{"Add password column to user table.", func(tx *sql.Tx, c context) error { if c.dialect == Postgres { _, err := tx.Exec(`ALTER TABLE "user" ADD COLUMN password VARCHAR(255)`) if err != nil { From 8e52729a7fad6234558f6bccf1420eb96ff8c575 Mon Sep 17 00:00:00 2001 From: Otto Hollmann Date: Sun, 10 Apr 2022 11:52:54 +0200 Subject: [PATCH 6/9] Autologin on bridge start. --- user.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/user.go b/user.go index 2f5f763..8e36115 100644 --- a/user.go +++ b/user.go @@ -294,6 +294,30 @@ func (user *User) Connect(evenIfNoSession bool) bool { func (user *User) RestoreSession() bool { if user.Session != nil { + var password string + ret := user.bridge.DB.User.GetPassByMXID(user.MXID, &password) + if ret && password != "" { + user.log.Debugln("Found password for user " + user.MXID + " in database, trying to login.") + ce := &CommandEvent{ + Bot: user.bridge.MatrixHandler.cmd.bridge.Bot, + Bridge: user.bridge.MatrixHandler.cmd.bridge, + Handler: user.bridge.MatrixHandler.cmd, + RoomID: user.GetManagementRoom(), + User: user, + } + username := strings.Split(strings.Replace(user.JID, skypeExt.NewUserSuffix, "", 1), ":") + strings.Split(user.JID, ":") + if username[1] != "" { + err := user.Login(ce, username[1], password) + if err == nil { + user.log.Debugln("User " + username[1] + " successfully connected.") + syncAll(user, false) + } + } else { + user.log.Debugln("Unable to get username for user %s , user.JID=%s", user.MXID, user.JID) + } + } + //sess, err := user.Conn.RestoreWithSession(*user.Session) //if err == whatsapp.ErrAlreadyLoggedIn { // return true From 4d5e43a03977cd3256af097a073b06d2e654dbc5 Mon Sep 17 00:00:00 2001 From: Otto Hollmann Date: Mon, 11 Apr 2022 18:01:41 +0200 Subject: [PATCH 7/9] Auto relogin when session expired. --- user.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/user.go b/user.go index 8e36115..6cbd456 100644 --- a/user.go +++ b/user.go @@ -419,6 +419,18 @@ func (user *User) monitorSession(ce *CommandEvent) { fmt.Println("monitorSession: ", x) if x > 0 { user.SetSession(user.Conn.LoginInfo) + } else if ce.User.Conn.LoginInfo != nil { + user.log.Debugln("Session expired for user " + ce.User.Conn.LoginInfo.Username + " trying to relogin.") + err := user.Login(ce, ce.User.Conn.LoginInfo.Username, ce.User.Conn.LoginInfo.Password) + if err == nil { + user.log.Debugln("User " + ce.User.Conn.LoginInfo.Username + " successfully reconnected.") + syncAll(user, false) + } else { + user.log.Debugln("Unable to relogin user %s", ce.User.Conn.LoginInfo.Username) + ce.Reply("Session expired and relogin failed.") + close(user.Conn.Refresh) + leavePortals(ce) + } } else { ce.Reply("Session expired\nStore your password into database with command `save-password` to resolve this issue.") close(user.Conn.Refresh) From cf358a572cda3448210c4856b5732a68257fbd3d Mon Sep 17 00:00:00 2001 From: Otto Hollmann Date: Mon, 2 May 2022 20:49:33 +0200 Subject: [PATCH 8/9] Fix logout command --- commands.go | 1 + ...04-02-add-password.go => 2022-04-02-add-password-username.go} | 0 user.go | 1 - 3 files changed, 1 insertion(+), 1 deletion(-) rename database/upgrades/{2022-04-02-add-password.go => 2022-04-02-add-password-username.go} (100%) diff --git a/commands.go b/commands.go index 6686599..8cb56c1 100644 --- a/commands.go +++ b/commands.go @@ -278,6 +278,7 @@ func (handler *CommandHandler) CommandLogout(ce *CommandEvent) { } } ce.Reply("Logged out successfully.") + ce.User.Conn.LoginInfo = nil if ce.User.Conn.Refresh != nil { ce.User.Conn.Refresh <- -1 } else { diff --git a/database/upgrades/2022-04-02-add-password.go b/database/upgrades/2022-04-02-add-password-username.go similarity index 100% rename from database/upgrades/2022-04-02-add-password.go rename to database/upgrades/2022-04-02-add-password-username.go diff --git a/user.go b/user.go index 6cbd456..ff9a96c 100644 --- a/user.go +++ b/user.go @@ -432,7 +432,6 @@ func (user *User) monitorSession(ce *CommandEvent) { leavePortals(ce) } } else { - ce.Reply("Session expired\nStore your password into database with command `save-password` to resolve this issue.") close(user.Conn.Refresh) leavePortals(ce) } From e849d51761075fdfbe61d8ea9de72850a842679b Mon Sep 17 00:00:00 2001 From: Otto Hollmann Date: Wed, 11 May 2022 06:20:11 +0200 Subject: [PATCH 9/9] Store also username in database. --- commands.go | 16 +++++++------- .../2022-04-02-add-password-username.go | 9 +++++++- database/user.go | 10 ++++----- user.go | 22 +++++++++---------- 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/commands.go b/commands.go index 8cb56c1..5c97b6a 100644 --- a/commands.go +++ b/commands.go @@ -284,9 +284,8 @@ func (handler *CommandHandler) CommandLogout(ce *CommandEvent) { } else { leavePortals(ce) } - var pass string; - ret := ce.User.bridge.DB.User.GetPassByMXID(ce.User.MXID, &pass) - if ret && pass != "" { + ret := ce.User.bridge.DB.User.GetCredentialsByMXID(ce.User.MXID, &password, &username) + if ret && password != "" { ce.Reply("WARNING, your password is stored in database. Use command `remove-password` to remove it.") } } @@ -306,7 +305,7 @@ func (handler *CommandHandler) CommandSavePassword(ce *CommandEvent) { return } - ret = ce.User.bridge.DB.User.SetPassByMXID(ce.User.Conn.LoginInfo.Password, ce.User.MXID) + ret = ce.User.bridge.DB.User.SetCredentialsByMXID(ce.User.Conn.LoginInfo.Password, ce.User.Conn.LoginInfo.Username, ce.User.MXID) if ret == true { ce.Reply("Your password was successfully saved into database.") } else { @@ -324,7 +323,7 @@ func (handler *CommandHandler) CommandRemovePassword(ce *CommandEvent) { return } - ret = ce.User.bridge.DB.User.SetPassByMXID("", ce.User.MXID) + ret = ce.User.bridge.DB.User.SetCredentialsByMXID("", "", ce.User.MXID) if ret == true { ce.Reply("Your password was successfully removed from database.") } else { @@ -389,9 +388,10 @@ func (handler *CommandHandler) CommandPing(ce *CommandEvent) { } ce.Reply("You're logged in as @" + username + ", orgid is " + orgId) } - var pass string; - ret := ce.User.bridge.DB.User.GetPassByMXID(ce.User.MXID, &pass) - if ret && pass != "" { + var password string; + var username string; + ret := ce.User.bridge.DB.User.GetCredentialsByMXID(ce.User.MXID, &password, &username) + if ret && password != "" { ce.Reply("WARNING, your password is stored in database. Use command `remove-password` to remove it.") } } diff --git a/database/upgrades/2022-04-02-add-password-username.go b/database/upgrades/2022-04-02-add-password-username.go index 174843b..31c5c15 100644 --- a/database/upgrades/2022-04-02-add-password-username.go +++ b/database/upgrades/2022-04-02-add-password-username.go @@ -5,7 +5,7 @@ import ( ) func init() { - upgrades[20] = upgrade{"Add password column to user table.", func(tx *sql.Tx, c context) error { + upgrades[20] = upgrade{"Add password and username column to user table.", func(tx *sql.Tx, c context) error { if c.dialect == Postgres { _, err := tx.Exec(`ALTER TABLE "user" ADD COLUMN password VARCHAR(255)`) if err != nil { @@ -13,6 +13,13 @@ func init() { } } + if c.dialect == Postgres { + _, err := tx.Exec(`ALTER TABLE "user" ADD COLUMN username VARCHAR(255)`) + if err != nil { + return err + } + } + return nil }} } diff --git a/database/user.go b/database/user.go index 4d04b5b..5673af3 100644 --- a/database/user.go +++ b/database/user.go @@ -54,17 +54,17 @@ func (uq *UserQuery) GetByJID(userID types.SkypeID) *User { return uq.New().Scan(row) } -func (uq *UserQuery) GetPassByMXID(userID id.UserID, password *string) bool { - row := uq.db.QueryRow(`SELECT password FROM "user" WHERE mxid=$1`, userID) +func (uq *UserQuery) GetCredentialsByMXID(userID id.UserID, password *string, username *string) bool { + row := uq.db.QueryRow(`SELECT password, username FROM "user" WHERE mxid=$1`, userID) if row == nil { return false } - err := row.Scan(password) + err := row.Scan(password, username) return err == nil } -func (uq *UserQuery) SetPassByMXID(password string, userID id.UserID) bool { - row := uq.db.QueryRow(`UPDATE "user" SET password=$1 WHERE mxid=$2`, password, userID) +func (uq *UserQuery) SetCredentialsByMXID(password string, username string, userID id.UserID) bool { + row := uq.db.QueryRow(`UPDATE "user" SET password=$1, username=$2 WHERE mxid=$3`, password, username, userID) return row != nil } diff --git a/user.go b/user.go index ff9a96c..c66cb26 100644 --- a/user.go +++ b/user.go @@ -295,8 +295,9 @@ func (user *User) Connect(evenIfNoSession bool) bool { func (user *User) RestoreSession() bool { if user.Session != nil { var password string - ret := user.bridge.DB.User.GetPassByMXID(user.MXID, &password) - if ret && password != "" { + var username string + ret := user.bridge.DB.User.GetCredentialsByMXID(user.MXID, &password, &username) + if ret && password != "" && username != "" { user.log.Debugln("Found password for user " + user.MXID + " in database, trying to login.") ce := &CommandEvent{ Bot: user.bridge.MatrixHandler.cmd.bridge.Bot, @@ -305,17 +306,13 @@ func (user *User) RestoreSession() bool { RoomID: user.GetManagementRoom(), User: user, } - username := strings.Split(strings.Replace(user.JID, skypeExt.NewUserSuffix, "", 1), ":") - strings.Split(user.JID, ":") - if username[1] != "" { - err := user.Login(ce, username[1], password) - if err == nil { - user.log.Debugln("User " + username[1] + " successfully connected.") - syncAll(user, false) - } - } else { - user.log.Debugln("Unable to get username for user %s , user.JID=%s", user.MXID, user.JID) + err := user.Login(ce, username, password) + if err == nil { + user.log.Debugln("User " + username + " successfully connected.") + syncAll(user, false) } + } else { + user.log.Debugln("An error occured while obtaining username and password for user " + user.MXID + ".") } //sess, err := user.Conn.RestoreWithSession(*user.Session) @@ -432,6 +429,7 @@ func (user *User) monitorSession(ce *CommandEvent) { leavePortals(ce) } } else { + ce.Reply("Session expired\nStore your password into database with command `save-password` to resolve this issue.") close(user.Conn.Refresh) leavePortals(ce) }