morpheus

A Matrix client written in Go-QT
git clone git://archive.git.mtrnord.blog/Nordgedanken/morpheus.git
Log | Files | Refs | README | LICENSE

commit 8ed3fff7cf76f8dc3103d66c3c32f532725e5f39
parent 2de791fda69003f0f7d548b07f5706ff3b5211a1
Author: MTRNord <mtrnord1@gmail.com>
Date:   Thu, 24 Aug 2017 20:28:29 +0200

Add autologin on startup

Diffstat:
Mmain.go | 56+++++++++++++++++++++++++++++++++++++++++++++++++++++---
Mmatrix/login.go | 30++++++++++++++++++++++--------
Mtags | 80+++++++++++++++++++++++++++++++++++++++++++------------------------------------
Muis.go | 56+++++++++++++++++++++++++++++++++++++++++++-------------
4 files changed, 162 insertions(+), 60 deletions(-)

diff --git a/main.go b/main.go @@ -7,10 +7,12 @@ import ( "github.com/Nordgedanken/Neo/matrix" "github.com/Nordgedanken/Neo/util" "github.com/therecipe/qt/widgets" + "github.com/tidwall/buntdb" ) var window *widgets.QMainWindow var localLog *log.Logger +var db *buntdb.DB func main() { var file *os.File @@ -18,7 +20,7 @@ func main() { localLog, file = util.StartFileLog(localLog) defer file.Close() - db := matrix.OpenDB() + db = matrix.OpenDB() defer db.Close() localLog.Println("Starting Neo") @@ -38,8 +40,56 @@ func main() { loginUI.Resize2(windowWidth, windowHeight) loginUI.SetMinimumSize2(windowWidth, windowHeight) - //Show loginUI - window.SetCentralWidget(loginUI) + var accessToken string + var homeserverURL string + var userID string + + // Get cache + db.View(func(tx *buntdb.Tx) error { + var QueryErr error + QueryErr = tx.AscendKeys("user:accessToken", + func(key, value string) bool { + accessToken = value + return true + }) + QueryErr = tx.AscendKeys("user:homeserverURL", + func(key, value string) bool { + homeserverURL = value + return true + }) + QueryErr = tx.AscendKeys("user:userID", + func(key, value string) bool { + userID = value + return true + }) + if QueryErr != nil { + return QueryErr + } + return nil + }) + + if accessToken != "" && homeserverURL != "" && userID != "" { + localLog.Println("Starting Auto Login Sequenze in background") + results := make(chan *matrix.Client) + + wg.Add(1) + go DoLogin("", "", homeserverURL, userID, accessToken, localLog, results) + + go func() { + wg.Wait() // wait for each execTask to return + close(results) // then close the results channel + }() + + //Show MainUI + for result := range results { + //TODO Don't switch screen on wrong login data. + MainUI := NewMainUI(windowWidth, windowHeight, result) + window.SetCentralWidget(MainUI) + } + } else { + //Show loginUI + window.SetCentralWidget(loginUI) + } window.Show() //enter the main event loop diff --git a/matrix/login.go b/matrix/login.go @@ -2,22 +2,18 @@ package matrix import ( "strings" - "sync" "github.com/matrix-org/gomatrix" "github.com/tidwall/buntdb" ) var clientInstance *Client -var once sync.Once -//getClient returns a Client -func getClient(homeserverURL, userID, accessToken string) (*Client, error) { +//GetClient returns a Client +func GetClient(homeserverURL, userID, accessToken string) (*Client, error) { var err error var client *gomatrix.Client - once.Do(func() { - client, err = gomatrix.NewClient(homeserverURL, userID, accessToken) - }) + client, err = gomatrix.NewClient(homeserverURL, userID, accessToken) if err != nil { return nil, err } @@ -40,7 +36,15 @@ func getClient(homeserverURL, userID, accessToken string) (*Client, error) { func LoginUser(username, password string) (*Client, error) { usernameSplit := strings.Split(username, ":") homeserverURL := usernameSplit[1] - cli, cliErr := getClient("https://"+homeserverURL, "", "") + var cli *Client + var cliErr error + if strings.HasPrefix(homeserverURL, "https://") { + cli, cliErr = GetClient(homeserverURL, "", "") + } else if strings.HasPrefix(homeserverURL, "http://") { + cli, cliErr = GetClient(homeserverURL, "", "") + } else { + cli, cliErr = GetClient("https://"+homeserverURL, "", "") + } if cliErr != nil { return nil, cliErr } @@ -54,5 +58,15 @@ func LoginUser(username, password string) (*Client, error) { return nil, err } cli.SetCredentials(resp.UserID, resp.AccessToken) + DBerr := db.Update(func(tx *buntdb.Tx) error { + tx.Set("user:accessToken", resp.AccessToken, nil) + tx.Set("user:deviceID", resp.DeviceID, nil) + tx.Set("user:homeserverURL", resp.HomeServer, nil) + tx.Set("user:userID", resp.UserID, nil) + return nil + }) + if DBerr != nil { + return nil, DBerr + } return cli, nil } diff --git a/tags b/tags @@ -5,66 +5,74 @@ !_TAG_PROGRAM_URL https://github.com/jstemmer/gotags !_TAG_PROGRAM_VERSION 1.4.1 /go1.8.3/ *gomatrix.Client matrix\types.go 9;" e access:public ctype:Client language:Go line:9 type:*gomatrix.Client -C rcc_cgo_windows_windows_386.go 11;" i language:Go line:11 Client matrix\types.go 8;" t access:public language:Go line:8 type:struct -DisplayName matrix\ownUser.go 16;" w access:public ctype:RespUserDisplayName language:Go line:16 type:string -GetOwnUserAvatar matrix\types.go 19;" m access:public ctype:Client language:Go line:19 signature:() type:*gui.QPixmap -GetUserDisplayName matrix\types.go 13;" m access:public ctype:Client language:Go line:13 signature:(mxid string) type:*RespUserDisplayName, error -Logger util\logger.go 15;" f access:public language:Go line:15 signature:() type:*log.Logger -LoginUser matrix\login.go 41;" f access:public language:Go line:41 signature:(username, password string) type:*Client, error -NewLoginUI uis.go 16;" f access:public language:Go line:16 signature:(windowWidth, windowHeight int) type:*widgets.QWidget -NewMainUI uis.go 74;" f access:public language:Go line:74 signature:(windowWidth, windowHeight int, cli *matrix.Client) type:*widgets.QWidget -OpenDB matrix\db.go 12;" f access:public language:Go line:12 signature:() type:*buntdb.DB -RespUserDisplayName matrix\ownUser.go 15;" t access:public language:Go line:15 type:struct -StartFileLog util\logger.go 23;" f access:public language:Go line:23 signature:(localLog *log.Logger) type:*log.Logger, *os.File -clientInstance matrix\login.go 11;" v access:private language:Go line:11 type:*Client -db matrix\db.go 10;" v access:private language:Go line:10 type:*buntdb.DB -fmt uis.go 4;" i language:Go line:4 -getClient matrix\login.go 15;" f access:private language:Go line:15 signature:(homeserverURL, userID, accessToken string) type:*Client, error -getOwnUserAvatar matrix\ownUser.go 27;" f access:private language:Go line:27 signature:(cli *Client) type:*gui.QPixmap -getUserDisplayName matrix\ownUser.go 20;" f access:private ctype:RespUserDisplayName language:Go line:20 signature:(mxid string, cli *Client) type:*RespUserDisplayName, error +DisplayName matrix\types.go 14;" w access:public ctype:RespUserDisplayName language:Go line:14 type:string +DoLogin uis.go 18;" f access:public language:Go line:18 signature:(username, password, homeserverURL, userID, accessToken string, localLog *log.Logger, results chan *matrix.Client) +GetClient matrix\login.go 13;" f access:public language:Go line:13 signature:(homeserverURL, userID, accessToken string) type:*Client, error +GetOwnUserAvatar matrix\types.go 24;" m access:public ctype:Client language:Go line:24 signature:() type:*gui.QPixmap +GetUserDisplayName matrix\types.go 18;" m access:public ctype:Client language:Go line:18 signature:(mxid string) type:string, error +Logger util\logger.go 18;" f access:public language:Go line:18 signature:() type:*log.Logger +LoginUser matrix\login.go 36;" f access:public language:Go line:36 signature:(username, password string) type:*Client, error +NewLoginUI uis.go 46;" f access:public language:Go line:46 signature:(windowWidth, windowHeight int) type:*widgets.QWidget +NewMainUI uis.go 141;" f access:public language:Go line:141 signature:(windowWidth, windowHeight int, cli *matrix.Client) type:*widgets.QWidget +OpenDB matrix\db.go 14;" f access:public language:Go line:14 signature:() type:*buntdb.DB +RespUserDisplayName matrix\types.go 13;" t access:public language:Go line:13 type:struct +StartFileLog util\logger.go 26;" f access:public language:Go line:26 signature:(localLog *log.Logger) type:*log.Logger, *os.File +clientInstance matrix\login.go 10;" v access:private language:Go line:10 type:*Client +db main.go 15;" v access:private language:Go line:15 type:*buntdb.DB +db matrix\db.go 12;" v access:private language:Go line:12 type:*buntdb.DB +getOwnUserAvatar matrix\ownUser.go 55;" f access:private language:Go line:55 signature:(cli *Client) type:*gui.QPixmap +getUserDisplayName matrix\ownUser.go 23;" f access:private language:Go line:23 signature:(mxid string, cli *Client) type:string, error github.com/Nordgedanken/Neo/matrix main.go 7;" i language:Go line:7 -github.com/Nordgedanken/Neo/matrix uis.go 6;" i language:Go line:6 +github.com/Nordgedanken/Neo/matrix uis.go 8;" i language:Go line:8 github.com/Nordgedanken/Neo/util main.go 8;" i language:Go line:8 github.com/Nordgedanken/Neo/util matrix\ownUser.go 8;" i language:Go line:8 -github.com/matrix-org/gomatrix matrix\login.go 7;" i language:Go line:7 +github.com/matrix-org/gomatrix matrix\login.go 6;" i language:Go line:6 github.com/matrix-org/gomatrix matrix\types.go 4;" i language:Go line:4 -github.com/therecipe/qt/core uis.go 7;" i language:Go line:7 +github.com/shibukawa/configdir matrix\db.go 8;" i language:Go line:8 +github.com/shibukawa/configdir util\logger.go 11;" i language:Go line:11 +github.com/therecipe/qt/core uis.go 9;" i language:Go line:9 github.com/therecipe/qt/gui matrix\ownUser.go 9;" i language:Go line:9 github.com/therecipe/qt/gui matrix\types.go 5;" i language:Go line:5 -github.com/therecipe/qt/uitools uis.go 8;" i language:Go line:8 github.com/therecipe/qt/widgets main.go 9;" i language:Go line:9 -github.com/therecipe/qt/widgets uis.go 9;" i language:Go line:9 -github.com/tidwall/buntdb matrix\db.go 7;" i language:Go line:7 -github.com/tidwall/buntdb matrix\login.go 8;" i language:Go line:8 +github.com/therecipe/qt/widgets uis.go 10;" i language:Go line:10 +github.com/tidwall/buntdb main.go 10;" i language:Go line:10 +github.com/tidwall/buntdb matrix\db.go 9;" i language:Go line:9 +github.com/tidwall/buntdb matrix\login.go 7;" i language:Go line:7 +github.com/tidwall/buntdb matrix\ownUser.go 10;" i language:Go line:10 +github.com/tidwall/buntdb uis.go 11;" i language:Go line:11 +init matrix\ownUser.go 15;" f access:private language:Go line:15 signature:() io util\logger.go 4;" i language:Go line:4 -localLog main.go 12;" v access:private language:Go line:12 type:*log.Logger -localLog matrix\ownUser.go 12;" v access:private language:Go line:12 type:*log.Logger +localLog main.go 14;" v access:private language:Go line:14 type:*log.Logger +localLog matrix\ownUser.go 13;" v access:private language:Go line:13 type:*log.Logger log main.go 4;" i language:Go line:4 log matrix\db.go 4;" i language:Go line:4 log matrix\ownUser.go 4;" i language:Go line:4 +log uis.go 4;" i language:Go line:4 log util\logger.go 5;" i language:Go line:5 -logInstance util\logger.go 11;" v access:private language:Go line:11 type:*log.Logger -main main.go 15;" f access:private language:Go line:15 signature:() +logInstance util\logger.go 14;" v access:private language:Go line:14 type:*log.Logger +main main.go 17;" f access:private language:Go line:17 signature:() main main.go 1;" p language:Go line:1 -main rcc_cgo_windows_windows_386.go 1;" p language:Go line:1 main uis.go 1;" p language:Go line:1 matrix matrix\db.go 1;" p language:Go line:1 matrix matrix\login.go 1;" p language:Go line:1 matrix matrix\ownUser.go 1;" p language:Go line:1 matrix matrix\types.go 1;" p language:Go line:1 -once matrix\login.go 12;" v access:private language:Go line:12 type:sync.Once -once util\logger.go 12;" v access:private language:Go line:12 type:sync.Once +once util\logger.go 15;" v access:private language:Go line:15 type:sync.Once os main.go 5;" i language:Go line:5 os matrix\db.go 5;" i language:Go line:5 os matrix\ownUser.go 5;" i language:Go line:5 os util\logger.go 6;" i language:Go line:6 -password uis.go 13;" v access:private language:Go line:13 type:string +password uis.go 15;" v access:private language:Go line:15 type:string +path/filepath matrix\db.go 6;" i language:Go line:6 +path/filepath util\logger.go 7;" i language:Go line:7 strings matrix\login.go 4;" i language:Go line:4 strings matrix\ownUser.go 6;" i language:Go line:6 -sync matrix\login.go 5;" i language:Go line:5 -sync util\logger.go 7;" i language:Go line:7 -time util\logger.go 8;" i language:Go line:8 -username uis.go 12;" v access:private language:Go line:12 type:string +strings uis.go 5;" i language:Go line:5 +sync uis.go 6;" i language:Go line:6 +sync util\logger.go 8;" i language:Go line:8 +time util\logger.go 9;" i language:Go line:9 +username uis.go 14;" v access:private language:Go line:14 type:string util util\logger.go 1;" p language:Go line:1 +wg uis.go 16;" v access:private language:Go line:16 type:sync.WaitGroup window main.go 13;" v access:private language:Go line:13 type:*widgets.QMainWindow diff --git a/uis.go b/uis.go @@ -2,15 +2,45 @@ package main import ( "log" + "strings" "sync" "github.com/Nordgedanken/Neo/matrix" "github.com/therecipe/qt/core" "github.com/therecipe/qt/widgets" + "github.com/tidwall/buntdb" ) var username string var password string +var wg sync.WaitGroup + +func DoLogin(username, password, homeserverURL, userID, accessToken string, localLog *log.Logger, results chan<- *matrix.Client) { + defer wg.Done() + var cli *matrix.Client + if accessToken != "" && homeserverURL != "" && userID != "" { + var cliErr error + if strings.HasPrefix(homeserverURL, "https://") { + cli, cliErr = matrix.GetClient(homeserverURL, userID, accessToken) + } else if strings.HasPrefix(homeserverURL, "http://") { + cli, cliErr = matrix.GetClient(homeserverURL, userID, accessToken) + } else { + cli, cliErr = matrix.GetClient("https://"+homeserverURL, userID, accessToken) + } + if cliErr != nil { + localLog.Println(cliErr) + } + cli.SetCredentials(userID, accessToken) + } else { + var err error + cli, err = matrix.LoginUser(username, password) + if err != nil { + localLog.Println(err) + } + } + + results <- cli +} //NewLoginUI initializes the login Screen func NewLoginUI(windowWidth, windowHeight int) *widgets.QWidget { @@ -78,22 +108,13 @@ func NewLoginUI(windowWidth, windowHeight int) *widgets.QWidget { loginButton.ConnectClicked(func(checked bool) { //TODO register enter and show loader or so + if username != "" && password != "" { localLog.Println("Starting Login Sequenze in background") - var wg sync.WaitGroup results := make(chan *matrix.Client) wg.Add(1) - go func(username, password string, localLog *log.Logger, results chan<- *matrix.Client) { - defer wg.Done() - cli, err := matrix.LoginUser(username, password) - if err != nil { - localLog.Println(err) - } - - results <- cli - - }(username, password, localLog, results) + go DoLogin(username, password, "", "", "", localLog, results) go func() { wg.Wait() // wait for each execTask to return @@ -103,7 +124,7 @@ func NewLoginUI(windowWidth, windowHeight int) *widgets.QWidget { //Show MainUI for result := range results { //TODO Don't switch screen on wrong login data. - MainUI := newMainUI(windowWidth, windowHeight, result) + MainUI := NewMainUI(windowWidth, windowHeight, result) window.SetCentralWidget(MainUI) } } else { @@ -117,7 +138,7 @@ func NewLoginUI(windowWidth, windowHeight int) *widgets.QWidget { } //NewMainUI initializes the login Screen -func newMainUI(windowWidth, windowHeight int, cli *matrix.Client) *widgets.QWidget { +func NewMainUI(windowWidth, windowHeight int, cli *matrix.Client) *widgets.QWidget { widget := widgets.NewQWidget(nil, 0) topLayout := widgets.NewQVBoxLayout2(widget) @@ -208,6 +229,15 @@ func newMainUI(windowWidth, windowHeight int, cli *matrix.Client) *widgets.QWidg results <- false } else { cli.ClearCredentials() + //Flush complete DB + db.View(func(tx *buntdb.Tx) error { + var QueryErr error + QueryErr = tx.DeleteAll() + if QueryErr != nil { + return QueryErr + } + return nil + }) results <- true } }(cli, localLog, results)