From 7d9cf97445a2a743c73f1e82d18afce9627f2d2c Mon Sep 17 00:00:00 2001 From: Chteufleur Date: Sat, 23 Apr 2016 22:20:56 +0200 Subject: [PATCH] Permit remove registration. Working but can be improve --- database/database.go | 69 ++++++++++++++++++++++++++++++-------------- gateway/gateway.go | 6 ++++ gateway/steam.go | 14 ++++++++- main.go | 2 +- xmpp/commands.go | 23 +++++++++++++-- xmpp/xmpp.go | 17 +++++++++++ 6 files changed, 104 insertions(+), 27 deletions(-) diff --git a/database/database.go b/database/database.go index b75e50d..09c058d 100644 --- a/database/database.go +++ b/database/database.go @@ -49,6 +49,12 @@ func Close() { func (newLine *DatabaseLine) AddLine() bool { log.Printf("%sAdd new line %v", LogInfo, newLine) + + isUserRegistred := getLine(newLine.Jid) != nil + if isUserRegistred { + return newLine.UpdateLine() + } + stmt, err := db.Prepare(insertDatabaseStmt) if err != nil { log.Printf("%sError on insert jid %s", LogError, newLine.Jid, err) @@ -82,34 +88,51 @@ func (newLine *DatabaseLine) UpdateLine() bool { } func RemoveLine(jid string) bool { + line := new(DatabaseLine) + line.Jid = jid + return line.UpdateLine() + // FIXME not working - log.Printf("%sRemove line %s", LogInfo, jid) + /* + log.Printf("%sRemove line %s", LogInfo, jid) - stmt, err := db.Prepare(deleteDatabaseStmt) - if err != nil { - log.Printf("%sError on delete jid %s", LogError, jid, err) - return false - } - defer stmt.Close() - res, err := stmt.Exec(jid) - if err != nil { - log.Printf("%sError on delete SQL statement", LogError, err) - return false - } + stmt, err := db.Prepare(deleteDatabaseStmt) + if err != nil { + log.Printf("%sError on delete jid %s", LogError, jid, err) + return false + } + defer stmt.Close() + res, err := stmt.Exec(jid) + if err != nil { + log.Printf("%sError on delete SQL statement", LogError, err) + return false + } - affect, err := res.RowsAffected() - if err != nil { - log.Printf("%sError on delete SQL statement", LogError, err) - return false - } - if affect == 0 { - return false - } + affect, err := res.RowsAffected() + if err != nil { + log.Printf("%sError on delete SQL statement", LogError, err) + return false + } + if affect == 0 { + return false + } - return true + return true + */ } func GetLine(jid string) *DatabaseLine { + ret := getLine(jid) + + if ret.SteamLogin == "" { + log.Printf("%sLine empty", LogDebug) + return nil + } + + return ret +} + +func getLine(jid string) *DatabaseLine { log.Printf("%sGet line %s", LogInfo, jid) ret := new(DatabaseLine) @@ -141,7 +164,9 @@ func GetAllLines() []DatabaseLine { for rows.Next() { user := new(DatabaseLine) rows.Scan(&user.Jid, &user.SteamLogin, &user.SteamPwd) - ret = append(ret, *user) + if user.SteamLogin != "" { + ret = append(ret, *user) + } } return ret diff --git a/gateway/gateway.go b/gateway/gateway.go index 78bd2b4..5e878a1 100644 --- a/gateway/gateway.go +++ b/gateway/gateway.go @@ -18,6 +18,7 @@ type GatewayInfo struct { SentryFile string FriendSteamId map[string]*StatusSteamFriend SteamConnecting bool + Deleting bool // XMPP XMPP_JID_Client string @@ -44,3 +45,8 @@ func (g *GatewayInfo) Disconnect() { g.XMPP_Disconnect() go g.SteamDisconnect() } + +func (g *GatewayInfo) Delete() { + g.Deleting = true + g.Disconnect() +} diff --git a/gateway/steam.go b/gateway/steam.go index c17ccbd..efe5c86 100644 --- a/gateway/steam.go +++ b/gateway/steam.go @@ -30,6 +30,11 @@ const ( ) func (g *GatewayInfo) SteamRun() { + if g.Deleting { + log.Printf("%sDeleting gateway", LogSteamInfo) + return + } + log.Printf("%sRunning", LogSteamInfo) g.setLoginInfos() g.SteamClient = steam.NewClient() @@ -141,7 +146,14 @@ func (g *GatewayInfo) setLoginInfos() { } func (g *GatewayInfo) IsSteamConnected() bool { - return g.SteamClient.Connected() + ret := g != nil + if ret { + ret = g.SteamClient != nil + if ret { + ret = g.SteamClient.Connected() + } + } + return ret } func (g *GatewayInfo) SteamConnect() { diff --git a/main.go b/main.go index 283409b..97c2e06 100644 --- a/main.go +++ b/main.go @@ -15,7 +15,7 @@ import ( ) const ( - Version = "v0.3.7" + Version = "v0.3.8" configurationFilePath = "xmpp4steam.cfg" ) diff --git a/xmpp/commands.go b/xmpp/commands.go index e0da871..b66e7c8 100644 --- a/xmpp/commands.go +++ b/xmpp/commands.go @@ -9,9 +9,10 @@ import ( ) const ( - CommandAuthcode = "steamAuthCodeCommand" - CommandGetIdentifiants = "steamGetIdentifiants" - CommandDisconnectSteam = "disconnectSteam" + CommandAuthcode = "steamAuthCodeCommand" + CommandGetIdentifiants = "steamGetIdentifiants" + CommandDisconnectSteam = "disconnectSteam" + CommandRemoveRegistration = "removeRegistration" ) var ( @@ -30,6 +31,8 @@ func execDiscoCommand(iq *xmpp.Iq) { discoItem.Item = append(discoItem.Item, *discoI) discoI = &xmpp.DiscoItem{JID: jid.Domain, Node: CommandDisconnectSteam, Name: "Force Steam deconnexion"} discoItem.Item = append(discoItem.Item, *discoI) + discoI = &xmpp.DiscoItem{JID: jid.Domain, Node: CommandRemoveRegistration, Name: "Remove registration"} + discoItem.Item = append(discoItem.Item, *discoI) reply.PayloadEncode(discoItem) comp.Out <- reply @@ -79,6 +82,20 @@ func execCommandAdHoc(iq *xmpp.Iq) { } else { note.Value = "Your are not registred." } + cmd.Note = *note + } else if adHoc.Node == CommandRemoveRegistration { + cmd.Status = xmpp.StatusAdHocCompleted + cmdXForm := &xmpp.AdHocXForm{Type: xmpp.TypeAdHocResult, Title: "Remove registration"} + cmd.XForm = *cmdXForm + note := &xmpp.AdHocNote{Type: xmpp.TypeAdHocNoteInfo} + + jidBare := strings.SplitN(iq.From, "/", 2)[0] + if RemoveUser(jidBare) { + note.Value = "Remove registration success." + } else { + note.Value = "Failed to remove your registration." + } + cmd.Note = *note } reply.PayloadEncode(cmd) diff --git a/xmpp/xmpp.go b/xmpp/xmpp.go index 3bfa727..7c19ef6 100644 --- a/xmpp/xmpp.go +++ b/xmpp/xmpp.go @@ -2,6 +2,7 @@ package xmpp import ( "git.kingpenguin.tk/chteufleur/go-xmpp.git/src/xmpp" + "git.kingpenguin.tk/chteufleur/go-xmpp4steam.git/database" "git.kingpenguin.tk/chteufleur/go-xmpp4steam.git/gateway" "log" @@ -173,6 +174,7 @@ func AddNewUser(jid, steamLogin, steamPwd string) { g.XMPP_JID_Client = jid g.SentryFile = gateway.SentryDirectory + jid g.FriendSteamId = make(map[string]*gateway.StatusSteamFriend) + g.Deleting = false g.XMPP_Out = comp.Out g.XMPP_Connected_Client = make(map[string]bool) @@ -180,3 +182,18 @@ func AddNewUser(jid, steamLogin, steamPwd string) { MapGatewayInfo[jid] = g go g.Run() } + +func RemoveUser(jidBare string) bool { + ret := database.RemoveLine(jidBare) + + if ret { + g := MapGatewayInfo[jidBare] + ret = g != nil + if ret { + g.Delete() + MapGatewayInfo[jidBare] = nil + } + } + + return ret +}