forked from chteufleur/go-xmpp4steam
Compare commits
31 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
f72f1653d0 | |
|
|
8a52613171 | |
|
|
7ad36a0321 | |
|
|
458b96bffb | |
|
|
4961ad8c9e | |
|
|
4a3fdd3da1 | |
|
|
4f860c3f59 | |
|
|
82c8ed7cfa | |
|
|
231a8acb5f | |
|
|
5268350afb | |
|
|
cb49e1fbe9 | |
|
|
17e46aba5e | |
|
|
1d5fce9b06 | |
|
|
e871a899f0 | |
|
|
1743a46eea | |
|
|
1d0a907b29 | |
|
|
195063bc75 | |
|
|
a4518d3f6b | |
|
|
795d443878 | |
|
|
b8576883ad | |
|
|
de65b55f7e | |
|
|
fdeb74e887 | |
|
|
db6d7326e6 | |
|
|
d32b143bb3 | |
|
|
1e63cc1759 | |
|
|
7e2db9f58b | |
|
|
ba48518357 | |
|
|
679dc1a04c | |
|
|
d64fd5cb16 | |
|
|
f9d8cea66f | |
|
|
e0c08b5760 |
13
README.md
13
README.md
|
|
@ -13,14 +13,21 @@ go-xmpp4steam is a XMPP/Steam gateway.
|
|||
* [cfg](https://github.com/jimlawless/cfg) for the configuration file.
|
||||
|
||||
|
||||
Download the CA at [https://kingpenguin.tk/ressources/cacert.pem](https://kingpenguin.tk/ressources/cacert.pem), then install it on your operating system.
|
||||
Once installed, go into your $GOPATH directory and go get the source code.
|
||||
### Build and run
|
||||
You must first [install go environment](https://golang.org/doc/install) on your system.
|
||||
Then, go into your $GOPATH directory and go get the source code (This will download the source code and the dependencies).
|
||||
```sh
|
||||
go get git.kingpenguin.tk/chteufleur/go-xmpp4steam.git
|
||||
```
|
||||
|
||||
First, you need to go into directory ``$GOPATH/src/chteufleur/go-xmpp4steam.git``.
|
||||
Then, you can run the project directly by using command ``go run main.go``.
|
||||
Or, in order to build the project you can run the command ``go build main.go``.
|
||||
It will generate a binary that you can run as any binary file.
|
||||
|
||||
### Configure
|
||||
Configure the gateway by editing the ``xmpp4steam.cfg`` file in order to give all XMPP component information.
|
||||
Configure the gateway by editing the ``xmpp4steam.conf`` file in order to give all XMPP component information. This configuration file has to be placed following the [XDG specification](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html) (example ``/etc/xdg/http-auth/xmpp4steam.conf``).
|
||||
An example of the config file can be found in [the repos](https://git.kingpenguin.tk/chteufleur/HTTPAuthentificationOverXMPP/src/master/xmpp4steam.conf).
|
||||
|
||||
### Utilization
|
||||
To register, you have to send an Ad-Hoc command to the gateway in order to give your Steam login information.
|
||||
|
|
|
|||
|
|
@ -0,0 +1,98 @@
|
|||
package configuration
|
||||
|
||||
import (
|
||||
"git.kingpenguin.tk/chteufleur/go-xmpp4steam.git/database"
|
||||
"git.kingpenguin.tk/chteufleur/go-xmpp4steam.git/gateway"
|
||||
"git.kingpenguin.tk/chteufleur/go-xmpp4steam.git/logger"
|
||||
|
||||
"github.com/jimlawless/cfg"
|
||||
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
XdgDirectoryName = "xmpp4steam"
|
||||
configurationFilePath = "xmpp4steam/xmpp4steam.conf"
|
||||
|
||||
PathConfEnvVariable = "XDG_CONFIG_DIRS"
|
||||
DefaultXdgConfigDirs = "/etc/xdg"
|
||||
|
||||
PathDataEnvVariable = "XDG_DATA_DIRS"
|
||||
DefaultXdgDataDirs = "/usr/local/share/:/usr/share/"
|
||||
PreferedPathDataDir = "/usr/local/share"
|
||||
)
|
||||
|
||||
var (
|
||||
MapConfig = make(map[string]string)
|
||||
)
|
||||
|
||||
func Init() {
|
||||
loadConfigFile()
|
||||
|
||||
dataPathDir := locateDataDirPath()
|
||||
database.DatabaseFile = dataPathDir + "/" + database.DatabaseFileName
|
||||
database.Init()
|
||||
|
||||
gateway.ServerAddrs = dataPathDir + "/" + gateway.ServerAddrs
|
||||
gateway.SentryDirectory = dataPathDir + "/" + gateway.SentryDirectory
|
||||
os.MkdirAll(gateway.SentryDirectory, 0700)
|
||||
}
|
||||
|
||||
func loadConfigFile() bool {
|
||||
ret := false
|
||||
envVariable := os.Getenv(PathConfEnvVariable)
|
||||
if envVariable == "" {
|
||||
envVariable = DefaultXdgConfigDirs
|
||||
}
|
||||
for _, path := range strings.Split(envVariable, ":") {
|
||||
logger.Debug.Println("Try to find configuration file into " + path)
|
||||
configFile := path + "/" + configurationFilePath
|
||||
if _, err := os.Stat(configFile); err == nil {
|
||||
// The config file exist
|
||||
if cfg.Load(configFile, MapConfig) == nil {
|
||||
// And has been loaded successfully
|
||||
logger.Info.Println("Find configuration file at " + configFile)
|
||||
ret = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func locateDataDirPath() string {
|
||||
ret := ""
|
||||
isDirFound := false
|
||||
envVariable := os.Getenv(PathDataEnvVariable)
|
||||
if envVariable == "" {
|
||||
envVariable = DefaultXdgDataDirs
|
||||
}
|
||||
for _, path := range strings.Split(envVariable, ":") {
|
||||
logger.Debug.Printf("Try to find data base directory into " + path)
|
||||
dbDir := path + "/" + XdgDirectoryName
|
||||
if fi, err := os.Stat(dbDir); err == nil && fi.IsDir() {
|
||||
// The database file exist
|
||||
logger.Info.Printf("Find data base directory at " + dbDir)
|
||||
isDirFound = true
|
||||
ret = dbDir
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !isDirFound {
|
||||
if strings.Contains(envVariable, PreferedPathDataDir) {
|
||||
ret = PreferedPathDataDir + "/" + XdgDirectoryName
|
||||
} else {
|
||||
ret = strings.Split(envVariable, ":")[0] + "/" + XdgDirectoryName
|
||||
}
|
||||
|
||||
if os.MkdirAll(ret, 0700) == nil {
|
||||
logger.Info.Printf("Creating new data base directory at " + ret)
|
||||
} else {
|
||||
logger.Error.Printf("Fail to create data base directory at " + ret)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
|
@ -2,22 +2,19 @@ package database
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"git.kingpenguin.tk/chteufleur/go-xmpp4steam.git/logger"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"log"
|
||||
)
|
||||
|
||||
const (
|
||||
databaseFile = "go_xmpp4steam.db"
|
||||
DatabaseFileName = "go_xmpp4steam.db"
|
||||
|
||||
createDatabaseStmt = "create table if not exists users (jid text not null primary key, steamLogin text, steamPwd text, debug int);"
|
||||
insertDatabaseStmt = "insert into users (jid, steamLogin, steamPwd, debug) values(?, ?, ?, ?)"
|
||||
deleteDatabaseStmt = "delete from users where jid=?"
|
||||
selectDatabaseStmt = "select jid, steamLogin, steamPwd, debug from users where jid=?"
|
||||
updateDatabaseStmt = "update users set steamLogin=?, steamPwd=?, debug=? where jid=?"
|
||||
|
||||
LogInfo = "\t[SQLITE INFO]\t"
|
||||
LogError = "\t[SQLITE ERROR]\t"
|
||||
LogDebug = "\t[SQLITE DEBUG]\t"
|
||||
createDatabaseStmt = "create table if not exists users (jid text not null primary key, steamLogin text, steamPwd text, debug int);"
|
||||
insertDatabaseStmt = "insert into users (jid, steamLogin, steamPwd, debug) values(?, ?, ?, ?)"
|
||||
deleteDatabaseStmt = "delete from users where jid=?"
|
||||
selectDatabaseStmt = "select jid, steamLogin, steamPwd, debug from users where jid=?"
|
||||
selectAllDatabaseStmt = "select jid, steamLogin, steamPwd, debug from users"
|
||||
updateDatabaseStmt = "update users set steamLogin=?, steamPwd=?, debug=? where jid=?"
|
||||
)
|
||||
|
||||
type DatabaseLine struct {
|
||||
|
|
@ -28,19 +25,24 @@ type DatabaseLine struct {
|
|||
}
|
||||
|
||||
var (
|
||||
db = new(sql.DB)
|
||||
db = new(sql.DB)
|
||||
DatabaseFile = ""
|
||||
)
|
||||
|
||||
func init() {
|
||||
d, err := sql.Open("sqlite3", databaseFile)
|
||||
}
|
||||
|
||||
func Init() {
|
||||
logger.Info.Printf("Init database (file %s)", DatabaseFile)
|
||||
d, err := sql.Open("sqlite3", DatabaseFile)
|
||||
if err != nil {
|
||||
log.Printf("%sError on openning database", LogError, err)
|
||||
logger.Error.Printf("Error on openning database : %v", err)
|
||||
}
|
||||
db = d
|
||||
|
||||
_, err = db.Exec(createDatabaseStmt)
|
||||
if err != nil {
|
||||
log.Printf("%sFailed to create table", LogError, err)
|
||||
logger.Error.Printf("Failed to create table : %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -49,7 +51,7 @@ func Close() {
|
|||
}
|
||||
|
||||
func (newLine *DatabaseLine) AddLine() bool {
|
||||
log.Printf("%sAdd new line %v", LogInfo, newLine)
|
||||
logger.Info.Printf("Add new line %v", newLine)
|
||||
|
||||
isUserRegistred := getLine(newLine.Jid) != nil
|
||||
if isUserRegistred {
|
||||
|
|
@ -58,7 +60,7 @@ func (newLine *DatabaseLine) AddLine() bool {
|
|||
|
||||
stmt, err := db.Prepare(insertDatabaseStmt)
|
||||
if err != nil {
|
||||
log.Printf("%sError on insert jid %s", LogError, newLine.Jid, err)
|
||||
logger.Error.Printf("Error on insert jid %s : %v", newLine.Jid, err)
|
||||
return false
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
|
@ -68,7 +70,7 @@ func (newLine *DatabaseLine) AddLine() bool {
|
|||
}
|
||||
_, err = stmt.Exec(newLine.Jid, newLine.SteamLogin, newLine.SteamPwd, debug)
|
||||
if err != nil {
|
||||
log.Printf("%sError on creating SQL statement", LogError, err)
|
||||
logger.Error.Printf("Error on creating SQL statement : %v", err)
|
||||
return false
|
||||
}
|
||||
|
||||
|
|
@ -76,10 +78,10 @@ func (newLine *DatabaseLine) AddLine() bool {
|
|||
}
|
||||
|
||||
func (newLine *DatabaseLine) UpdateLine() bool {
|
||||
log.Printf("%sUpdate line %s", LogInfo, newLine.Jid)
|
||||
logger.Info.Printf("Update line %s", newLine.Jid)
|
||||
stmt, err := db.Prepare(updateDatabaseStmt)
|
||||
if err != nil {
|
||||
log.Printf("%sError on update ", LogError, err)
|
||||
logger.Error.Printf("Error on update : %v", err)
|
||||
return false
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
|
@ -87,15 +89,32 @@ func (newLine *DatabaseLine) UpdateLine() bool {
|
|||
if newLine.Debug {
|
||||
debug = 1
|
||||
}
|
||||
if newLine.SteamPwd == "" {
|
||||
oldLine := GetLine(newLine.Jid)
|
||||
if oldLine != nil {
|
||||
newLine.SteamPwd = oldLine.SteamPwd
|
||||
}
|
||||
}
|
||||
_, err = stmt.Exec(newLine.SteamLogin, newLine.SteamPwd, debug, newLine.Jid)
|
||||
if err != nil {
|
||||
log.Printf("%sError on updating SQL statement", LogError, err)
|
||||
logger.Error.Printf("Error on updating SQL statement : %v", err)
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (dbUser *DatabaseLine) UpdateUser() bool {
|
||||
isUserRegistred := GetLine(dbUser.Jid) != nil
|
||||
var isSqlSuccess bool
|
||||
if isUserRegistred {
|
||||
isSqlSuccess = dbUser.UpdateLine()
|
||||
} else {
|
||||
isSqlSuccess = dbUser.AddLine()
|
||||
}
|
||||
return isSqlSuccess
|
||||
}
|
||||
|
||||
func RemoveLine(jid string) bool {
|
||||
// Update Steam login and password to blank before deleting,
|
||||
// because it is not really deleted in the SQLite file.
|
||||
|
|
@ -103,26 +122,26 @@ func RemoveLine(jid string) bool {
|
|||
line.Jid = jid
|
||||
line.UpdateLine()
|
||||
|
||||
log.Printf("%sRemove line %s", LogInfo, jid)
|
||||
logger.Info.Printf("Remove line %s", jid)
|
||||
stmt, err := db.Prepare(deleteDatabaseStmt)
|
||||
if err != nil {
|
||||
log.Printf("%sError on delete jid %s", LogError, jid, err)
|
||||
logger.Error.Printf("Error on delete jid %s : %v", jid, err)
|
||||
return false
|
||||
}
|
||||
defer stmt.Close()
|
||||
res, err := stmt.Exec(jid)
|
||||
if err != nil {
|
||||
log.Printf("%sError on delete SQL statement", LogError, err)
|
||||
logger.Error.Printf("Error on delete SQL statement : %v", err)
|
||||
return false
|
||||
}
|
||||
|
||||
affect, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
log.Printf("%sError on delete SQL statement", LogError, err)
|
||||
logger.Error.Printf("Error on delete SQL statement : %v", err)
|
||||
return false
|
||||
}
|
||||
if affect == 0 {
|
||||
log.Printf("%sNo line affected", LogDebug)
|
||||
logger.Debug.Printf("No line affected")
|
||||
return false
|
||||
}
|
||||
|
||||
|
|
@ -133,7 +152,7 @@ func GetLine(jid string) *DatabaseLine {
|
|||
ret := getLine(jid)
|
||||
|
||||
if ret == nil || ret.SteamLogin == "" {
|
||||
log.Printf("%sLine empty", LogDebug)
|
||||
logger.Debug.Printf("Line empty")
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -141,19 +160,19 @@ func GetLine(jid string) *DatabaseLine {
|
|||
}
|
||||
|
||||
func getLine(jid string) *DatabaseLine {
|
||||
log.Printf("%sGet line %s", LogInfo, jid)
|
||||
logger.Info.Printf("Get line %s", jid)
|
||||
ret := new(DatabaseLine)
|
||||
|
||||
stmt, err := db.Prepare(selectDatabaseStmt)
|
||||
if err != nil {
|
||||
log.Printf("%sError on select line", LogError, err)
|
||||
logger.Error.Printf("Error on select line : %v", err)
|
||||
return nil
|
||||
}
|
||||
defer stmt.Close()
|
||||
debug := 0
|
||||
err = stmt.QueryRow(jid).Scan(&ret.Jid, &ret.SteamLogin, &ret.SteamPwd, &debug)
|
||||
if err != nil {
|
||||
log.Printf("%sError on select scan", LogError, err)
|
||||
logger.Error.Printf("Error on select scan : %v", err)
|
||||
return nil
|
||||
}
|
||||
if debug == 1 {
|
||||
|
|
@ -166,18 +185,24 @@ func getLine(jid string) *DatabaseLine {
|
|||
}
|
||||
|
||||
func GetAllLines() []DatabaseLine {
|
||||
log.Printf("%sGet all lines", LogInfo)
|
||||
logger.Info.Printf("Get all lines")
|
||||
var ret []DatabaseLine
|
||||
|
||||
rows, err := db.Query("select jid, steamLogin, steamPwd from users")
|
||||
rows, err := db.Query(selectAllDatabaseStmt)
|
||||
if err != nil {
|
||||
log.Printf("%sError on select query", LogError, err)
|
||||
logger.Error.Printf("Error on select query : %v", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
user := new(DatabaseLine)
|
||||
rows.Scan(&user.Jid, &user.SteamLogin, &user.SteamPwd)
|
||||
debug := 0
|
||||
rows.Scan(&user.Jid, &user.SteamLogin, &user.SteamPwd, &debug)
|
||||
if user.SteamLogin != "" {
|
||||
if debug == 1 {
|
||||
user.Debug = true
|
||||
} else {
|
||||
user.Debug = false
|
||||
}
|
||||
ret = append(ret, *user)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,21 @@
|
|||
package gateway
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/Philipp15b/go-steam"
|
||||
)
|
||||
|
||||
const (
|
||||
resource = "go-xmpp4steam"
|
||||
)
|
||||
|
||||
var (
|
||||
SentryDirectory = "sentries/"
|
||||
resource = "go-xmpp4steam"
|
||||
XmppGroupUser = "Steam"
|
||||
|
||||
RemoteRosterRequestPermission = "remote-roster-request-permission"
|
||||
RemoteRosterRequestRoster = "remote-roster-request-roster"
|
||||
)
|
||||
|
||||
type GatewayInfo struct {
|
||||
|
|
@ -16,15 +25,33 @@ type GatewayInfo struct {
|
|||
SteamLoginInfo *steam.LogOnDetails
|
||||
SteamClient *steam.Client
|
||||
SentryFile string
|
||||
FriendSteamId map[string]*StatusSteamFriend
|
||||
friendSteamId *FriendSteam
|
||||
SteamConnecting bool
|
||||
Deleting bool
|
||||
|
||||
// XMPP
|
||||
XMPP_JID_Client string
|
||||
XMPP_Out chan interface{}
|
||||
XMPP_Connected_Client map[string]bool
|
||||
DebugMessage bool
|
||||
XMPP_JID_Client string
|
||||
XMPP_Out chan interface{}
|
||||
xmpp_Connected_Client *XmppConnectedClient
|
||||
DebugMessage bool
|
||||
xmpp_IQ_RemoteRoster_Request *XmppRemoteRosterRequest
|
||||
AllowEditRoster bool
|
||||
ChatstateNotificationData chan string
|
||||
}
|
||||
|
||||
type FriendSteam struct {
|
||||
steamId map[string]*StatusSteamFriend
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
type XmppConnectedClient struct {
|
||||
client map[string]bool
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
type XmppRemoteRosterRequest struct {
|
||||
request map[string]string
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
type StatusSteamFriend struct {
|
||||
|
|
@ -36,6 +63,7 @@ type StatusSteamFriend struct {
|
|||
|
||||
func (g *GatewayInfo) Run() {
|
||||
go g.SteamRun()
|
||||
go g.chatstatesNotification()
|
||||
}
|
||||
|
||||
func (g *GatewayInfo) SetSteamAuthCode(authCode string) {
|
||||
|
|
@ -43,11 +71,96 @@ func (g *GatewayInfo) SetSteamAuthCode(authCode string) {
|
|||
}
|
||||
|
||||
func (g *GatewayInfo) Disconnect() {
|
||||
g.XMPP_Disconnect()
|
||||
go g.XMPP_Disconnect()
|
||||
go g.SteamDisconnect()
|
||||
g.SteamConnecting = false
|
||||
}
|
||||
|
||||
func (g *GatewayInfo) Delete() {
|
||||
g.Deleting = true
|
||||
|
||||
if g.AllowEditRoster {
|
||||
g.removeAllUserFromRoster()
|
||||
}
|
||||
|
||||
g.Disconnect()
|
||||
}
|
||||
|
||||
func (s *GatewayInfo) CreateSteamIds() {
|
||||
s.friendSteamId = &FriendSteam{steamId: make(map[string]*StatusSteamFriend)}
|
||||
}
|
||||
|
||||
func (s *GatewayInfo) GetFriendSteamId(steamId string) *StatusSteamFriend {
|
||||
s.friendSteamId.RLock()
|
||||
defer s.friendSteamId.RUnlock()
|
||||
return s.friendSteamId.steamId[steamId]
|
||||
}
|
||||
|
||||
func (s *GatewayInfo) GetAllFriendSteamId() []string {
|
||||
s.friendSteamId.RLock()
|
||||
defer s.friendSteamId.RUnlock()
|
||||
allSteamIds := make([]string, len(s.friendSteamId.steamId))
|
||||
|
||||
i := 0
|
||||
for steamId := range s.friendSteamId.steamId {
|
||||
allSteamIds[i] = steamId
|
||||
i++
|
||||
}
|
||||
return allSteamIds
|
||||
}
|
||||
|
||||
func (s *GatewayInfo) SetFriendSteamId(steamId string, status *StatusSteamFriend) {
|
||||
s.friendSteamId.Lock()
|
||||
s.friendSteamId.steamId[steamId] = status
|
||||
s.friendSteamId.Unlock()
|
||||
}
|
||||
|
||||
func (s *GatewayInfo) RemoveFriendSteamId(steamId string) {
|
||||
s.friendSteamId.Lock()
|
||||
delete(s.friendSteamId.steamId, steamId)
|
||||
s.friendSteamId.Unlock()
|
||||
}
|
||||
|
||||
func (s *GatewayInfo) CreateXmppConnectedClient() {
|
||||
s.xmpp_Connected_Client = &XmppConnectedClient{client: make(map[string]bool)}
|
||||
}
|
||||
|
||||
func (s *GatewayInfo) SetXmppConnectedClient(jid string) {
|
||||
s.xmpp_Connected_Client.Lock()
|
||||
s.xmpp_Connected_Client.client[jid] = true
|
||||
s.xmpp_Connected_Client.Unlock()
|
||||
}
|
||||
|
||||
func (s *GatewayInfo) RemoveXmppConnectedClient(jid string) {
|
||||
s.xmpp_Connected_Client.Lock()
|
||||
delete(s.xmpp_Connected_Client.client, jid)
|
||||
s.xmpp_Connected_Client.Unlock()
|
||||
}
|
||||
|
||||
func (s *GatewayInfo) GetLenXmppConnectedClient() int {
|
||||
s.xmpp_Connected_Client.RLock()
|
||||
defer s.xmpp_Connected_Client.RUnlock()
|
||||
return len(s.xmpp_Connected_Client.client)
|
||||
}
|
||||
|
||||
func (s *GatewayInfo) CreateXmppRemoteRosterRequest() {
|
||||
s.xmpp_IQ_RemoteRoster_Request = &XmppRemoteRosterRequest{request: make(map[string]string)}
|
||||
}
|
||||
|
||||
func (s *GatewayInfo) SetXmppRemoteRosterRequest(iqId, value string) {
|
||||
s.xmpp_IQ_RemoteRoster_Request.Lock()
|
||||
s.xmpp_IQ_RemoteRoster_Request.request[iqId] = value
|
||||
s.xmpp_IQ_RemoteRoster_Request.Unlock()
|
||||
}
|
||||
|
||||
func (s *GatewayInfo) RemoveXmppRemoteRosterRequest(iqId string) {
|
||||
s.xmpp_IQ_RemoteRoster_Request.Lock()
|
||||
delete(s.xmpp_IQ_RemoteRoster_Request.request, iqId)
|
||||
s.xmpp_IQ_RemoteRoster_Request.Unlock()
|
||||
}
|
||||
|
||||
func (s *GatewayInfo) GetXmppRemoteRosterRequest(iqId string) string {
|
||||
s.xmpp_IQ_RemoteRoster_Request.RLock()
|
||||
defer s.xmpp_IQ_RemoteRoster_Request.RUnlock()
|
||||
return s.xmpp_IQ_RemoteRoster_Request.request[iqId]
|
||||
}
|
||||
|
|
|
|||
160
gateway/steam.go
160
gateway/steam.go
|
|
@ -1,20 +1,18 @@
|
|||
package gateway
|
||||
|
||||
import (
|
||||
"git.kingpenguin.tk/chteufleur/go-xmpp4steam.git/logger"
|
||||
"github.com/Philipp15b/go-steam"
|
||||
"github.com/Philipp15b/go-steam/protocol/steamlang"
|
||||
"github.com/Philipp15b/go-steam/steamid"
|
||||
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
serverAddrs = "servers.addr"
|
||||
|
||||
State_Offline = steamlang.EPersonaState_Offline
|
||||
State_Online = steamlang.EPersonaState_Online
|
||||
State_Busy = steamlang.EPersonaState_Busy
|
||||
|
|
@ -23,28 +21,30 @@ const (
|
|||
State_LookingToTrade = steamlang.EPersonaState_LookingToTrade
|
||||
State_LookingToPlay = steamlang.EPersonaState_LookingToPlay
|
||||
State_Max = steamlang.EPersonaState_Max
|
||||
)
|
||||
|
||||
LogSteamInfo = "\t[STEAM INFO]\t"
|
||||
LogSteamError = "\t[STEAM ERROR]\t"
|
||||
LogSteamDebug = "\t[STEAM DEBUG]\t"
|
||||
var (
|
||||
ServerAddrs = "servers.addr"
|
||||
)
|
||||
|
||||
func (g *GatewayInfo) SteamRun() {
|
||||
if g.Deleting {
|
||||
log.Printf("%sDeleting gateway", LogSteamInfo)
|
||||
logger.Info.Printf("[%s] Deleting gateway", g.XMPP_JID_Client)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("%sRunning", LogSteamInfo)
|
||||
logger.Info.Printf("[%s] Running", g.XMPP_JID_Client)
|
||||
steam.InitializeSteamDirectory()
|
||||
g.setLoginInfos()
|
||||
g.SteamClient = steam.NewClient()
|
||||
if g.SteamClient == nil {
|
||||
g.SteamClient = steam.NewClient()
|
||||
}
|
||||
g.SteamConnecting = false
|
||||
g.SteamClient.ConnectionTimeout = 10 * time.Second
|
||||
|
||||
g.mainSteam()
|
||||
|
||||
log.Printf("%sReach main method's end", LogSteamInfo)
|
||||
go g.SteamRun()
|
||||
logger.Info.Printf("[%s] Reach main method's end", g.XMPP_JID_Client)
|
||||
}
|
||||
|
||||
func (g *GatewayInfo) mainSteam() {
|
||||
|
|
@ -53,7 +53,7 @@ func (g *GatewayInfo) mainSteam() {
|
|||
case *steam.ConnectedEvent:
|
||||
// Connected on server
|
||||
g.SteamConnecting = false
|
||||
log.Printf("%sConnected on Steam serveur", LogSteamDebug)
|
||||
logger.Debug.Printf("[%s] Connected on Steam serveur", g.XMPP_JID_Client)
|
||||
g.SteamClient.Auth.LogOn(g.SteamLoginInfo)
|
||||
|
||||
case *steam.MachineAuthUpdateEvent:
|
||||
|
|
@ -65,27 +65,45 @@ func (g *GatewayInfo) mainSteam() {
|
|||
g.SendSteamPresence(steamlang.EPersonaState_Online)
|
||||
g.SendXmppMessage(XmppJidComponent, "", "Connected on Steam network")
|
||||
|
||||
case *steam.LoggedOffEvent:
|
||||
logger.Error.Printf("[%s] LoggedOffEvent: %v", g.XMPP_JID_Client, e)
|
||||
g.SendXmppMessage(XmppJidComponent, "", fmt.Sprintf("Disconnected of Steam network (%v)", e))
|
||||
g.SteamConnecting = false
|
||||
|
||||
case steam.FatalErrorEvent:
|
||||
log.Printf("%sFatalError: ", LogSteamError, e)
|
||||
g.SendXmppMessage(XmppJidComponent, "", "Steam Fatal Error : "+e.Error())
|
||||
logger.Error.Printf("[%s] FatalError: %v", g.XMPP_JID_Client, e)
|
||||
g.SendXmppMessage(XmppJidComponent, "", fmt.Sprintf("Steam Fatal Error : %v", e))
|
||||
g.DisconnectAllSteamFriend()
|
||||
return
|
||||
g.SteamConnecting = false
|
||||
|
||||
case *steam.DisconnectedEvent:
|
||||
logger.Info.Printf("[%s] Disconnected event", g.XMPP_JID_Client)
|
||||
g.SendXmppMessage(XmppJidComponent, "", fmt.Sprintf("Steam Error : %v", e))
|
||||
g.DisconnectAllSteamFriend()
|
||||
g.SteamConnecting = false
|
||||
|
||||
case error:
|
||||
log.Printf("%s", LogSteamError, e)
|
||||
logger.Error.Printf("[%s] error: %v", g.XMPP_JID_Client, e)
|
||||
g.SendXmppMessage(XmppJidComponent, "", "Steam Error : "+e.Error())
|
||||
|
||||
case *steam.LogOnFailedEvent:
|
||||
logger.Error.Printf("[%s] Login failed: %v", g.XMPP_JID_Client, e)
|
||||
g.SendXmppMessage(XmppJidComponent, "", fmt.Sprintf("Login failed : %v", e.Result))
|
||||
g.SteamConnecting = false
|
||||
|
||||
case *steam.ClientCMListEvent:
|
||||
// Save servers addresses
|
||||
b, err := json.Marshal(*e)
|
||||
if err != nil {
|
||||
log.Printf("%sFailed to json.Marshal() servers list", LogSteamError)
|
||||
} else {
|
||||
ioutil.WriteFile(serverAddrs, b, 0666)
|
||||
}
|
||||
// Doing nothing with server list
|
||||
|
||||
case *steam.PersonaStateEvent:
|
||||
logger.Debug.Printf("[%s] Received PersonaStateEvent: %v", g.XMPP_JID_Client, e)
|
||||
// Presenc received
|
||||
if _, ok := g.SteamClient.Social.Friends.GetCopy()[e.FriendId]; !ok {
|
||||
// Is not in friend list
|
||||
// Exepte for myself
|
||||
if g.SteamClient.SteamId() != e.FriendId {
|
||||
continue
|
||||
}
|
||||
}
|
||||
steamId := e.FriendId.ToString()
|
||||
name := e.Name
|
||||
gameName := e.GameName
|
||||
|
|
@ -109,24 +127,42 @@ func (g *GatewayInfo) mainSteam() {
|
|||
status = Status_extended_away
|
||||
tpye = Type_available
|
||||
}
|
||||
if _, ok := g.FriendSteamId[steamId]; !ok {
|
||||
steamFriendId := g.GetFriendSteamId(steamId)
|
||||
if steamFriendId == nil {
|
||||
// Send subscribsion
|
||||
g.SendXmppPresence(status, Type_subscribe, "", steamId+"@"+XmppJidComponent, gameName, name)
|
||||
g.FriendSteamId[steamId] = &StatusSteamFriend{XMPP_Status: status, XMPP_Type: tpye}
|
||||
g.SetFriendSteamId(steamId, &StatusSteamFriend{XMPP_Status: status, XMPP_Type: tpye})
|
||||
} else {
|
||||
g.FriendSteamId[steamId].XMPP_Status = status
|
||||
g.FriendSteamId[steamId].XMPP_Type = tpye
|
||||
g.FriendSteamId[steamId].SteamGameName = gameName
|
||||
g.FriendSteamId[steamId].SteamName = name
|
||||
steamFriendId.XMPP_Status = status
|
||||
steamFriendId.XMPP_Type = tpye
|
||||
steamFriendId.SteamGameName = gameName
|
||||
steamFriendId.SteamName = name
|
||||
}
|
||||
g.SendXmppPresence(status, tpye, "", steamId+"@"+XmppJidComponent, gameName, name)
|
||||
|
||||
case *steam.ChatMsgEvent:
|
||||
logger.Debug.Printf("[%s] Received ChatMsgEvent: %v", g.XMPP_JID_Client, e)
|
||||
// Message received
|
||||
g.SendXmppMessage(e.ChatterId.ToString()+"@"+XmppJidComponent, "", e.Message)
|
||||
from := e.ChatterId.ToString() + "@" + XmppJidComponent
|
||||
if e.EntryType == steamlang.EChatEntryType_Typing {
|
||||
g.SendXmppMessageComposing(from)
|
||||
} else if e.EntryType == steamlang.EChatEntryType_LeftConversation {
|
||||
g.SendXmppMessageLeaveConversation(from)
|
||||
} else {
|
||||
g.SendXmppMessage(from, "", e.Message)
|
||||
}
|
||||
|
||||
case *steam.ChatInviteEvent:
|
||||
logger.Debug.Printf("[%s] Received ChatInviteEvent: %v", g.XMPP_JID_Client, e)
|
||||
// Invitation to play
|
||||
if fromFriend, ok := g.SteamClient.Social.Friends.GetCopy()[e.FriendChatId]; ok {
|
||||
messageToSend := fmt.Sprintf("Currently playing to « %s », would you like to join ?", fromFriend.GameName)
|
||||
g.SendXmppMessage(e.FriendChatId.ToString()+"@"+XmppJidComponent, "", messageToSend)
|
||||
}
|
||||
|
||||
default:
|
||||
log.Printf("%s", LogSteamDebug, e)
|
||||
logger.Debug.Printf("[%s] Steam unmatch event (Type: %T): %v", g.XMPP_JID_Client, e, e)
|
||||
g.SendXmppMessage(XmppJidComponent, "", fmt.Sprintf("Steam unmatch event (Type: %T): %v", e, e))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -142,7 +178,7 @@ func (g *GatewayInfo) setLoginInfos() {
|
|||
if err == nil {
|
||||
g.SteamLoginInfo.SentryFileHash = sentryHash
|
||||
}
|
||||
log.Printf("%sAuthentification of (%s, %s)", LogSteamDebug, g.XMPP_JID_Client, g.SteamLoginInfo.Username)
|
||||
logger.Debug.Printf("Authentification of (%s, %s)", g.XMPP_JID_Client, g.SteamLoginInfo.Username)
|
||||
}
|
||||
|
||||
func (g *GatewayInfo) IsSteamConnected() bool {
|
||||
|
|
@ -153,43 +189,36 @@ func (g *GatewayInfo) IsSteamConnected() bool {
|
|||
ret = g.SteamClient.Connected()
|
||||
}
|
||||
}
|
||||
logger.Debug.Printf("[%s] Is Steam connected (Connected: %v)", g.XMPP_JID_Client, ret)
|
||||
return ret
|
||||
}
|
||||
|
||||
func (g *GatewayInfo) SteamConnect() {
|
||||
if g.IsSteamConnected() {
|
||||
log.Printf("%sTry to connect, but already connected", LogSteamDebug)
|
||||
logger.Debug.Printf("[%s] Try to connect, but already connected", g.XMPP_JID_Client)
|
||||
return
|
||||
}
|
||||
if g.SteamConnecting {
|
||||
log.Printf("%sTry to connect, but currently connecting…", LogSteamDebug)
|
||||
logger.Debug.Printf("[%s] Try to connect, but currently connecting…", g.XMPP_JID_Client)
|
||||
return
|
||||
}
|
||||
|
||||
g.SteamConnecting = true
|
||||
b, err := ioutil.ReadFile(serverAddrs)
|
||||
if err == nil {
|
||||
var toList steam.ClientCMListEvent
|
||||
err := json.Unmarshal(b, &toList)
|
||||
if err != nil {
|
||||
log.Printf("%sFailed to json.Unmarshal() servers list", LogSteamError)
|
||||
} else {
|
||||
log.Printf("%sConnecting...", LogSteamInfo, toList.Addresses[0])
|
||||
g.SteamClient.ConnectTo(toList.Addresses[0])
|
||||
}
|
||||
} else {
|
||||
log.Printf("%sFailed to read servers list file", LogSteamError)
|
||||
log.Printf("%sConnecting...", LogSteamInfo)
|
||||
g.SteamClient.Connect()
|
||||
}
|
||||
go func() {
|
||||
logger.Info.Printf("[%s] Connecting...", g.XMPP_JID_Client)
|
||||
g.SendXmppMessage(XmppJidComponent, "", "Connecting...")
|
||||
addr := g.SteamClient.Connect()
|
||||
logger.Info.Printf("[%s] Connected on %v", g.XMPP_JID_Client, addr)
|
||||
g.SendXmppMessage(XmppJidComponent, "", fmt.Sprintf("Connected on %v", addr))
|
||||
}()
|
||||
}
|
||||
|
||||
func (g *GatewayInfo) SteamDisconnect() {
|
||||
if !g.IsSteamConnected() {
|
||||
log.Printf("%sTry to disconnect, but already disconnected", LogSteamDebug)
|
||||
logger.Debug.Printf("[%s] Try to disconnect, but already disconnected", g.XMPP_JID_Client)
|
||||
return
|
||||
}
|
||||
log.Printf("%sSteam disconnect", LogSteamInfo)
|
||||
logger.Info.Printf("[%s] Steam disconnect", g.XMPP_JID_Client)
|
||||
|
||||
g.XMPP_Disconnect()
|
||||
g.DisconnectAllSteamFriend()
|
||||
|
|
@ -197,30 +226,45 @@ func (g *GatewayInfo) SteamDisconnect() {
|
|||
}
|
||||
|
||||
func (g *GatewayInfo) DisconnectAllSteamFriend() {
|
||||
for sid, _ := range g.FriendSteamId {
|
||||
logger.Debug.Printf("[%s] Disconnect all Steam friend", g.XMPP_JID_Client)
|
||||
for _, sid := range g.GetAllFriendSteamId() {
|
||||
g.SendXmppPresence(Status_offline, Type_unavailable, "", sid+"@"+XmppJidComponent, "", "")
|
||||
delete(g.FriendSteamId, sid)
|
||||
g.RemoveFriendSteamId(sid)
|
||||
}
|
||||
}
|
||||
|
||||
func (g *GatewayInfo) SendSteamMessage(steamId, message string) {
|
||||
g.sendSteamMessage(steamId, message, steamlang.EChatEntryType_ChatMsg)
|
||||
}
|
||||
|
||||
func (g *GatewayInfo) SendSteamMessageComposing(steamId string) {
|
||||
g.sendSteamMessage(steamId, "", steamlang.EChatEntryType_Typing)
|
||||
}
|
||||
|
||||
func (g *GatewayInfo) SendSteamMessageLeaveConversation(steamId string) {
|
||||
g.sendSteamMessage(steamId, "", steamlang.EChatEntryType_LeftConversation)
|
||||
}
|
||||
|
||||
func (g *GatewayInfo) sendSteamMessage(steamId, message string, chatEntryType steamlang.EChatEntryType) {
|
||||
if !g.IsSteamConnected() {
|
||||
log.Printf("%sTry to send message, but disconnected", LogSteamDebug)
|
||||
logger.Debug.Printf("[%s] Try to send message, but disconnected", g.XMPP_JID_Client)
|
||||
return
|
||||
}
|
||||
|
||||
steamIdUint64, err := strconv.ParseUint(steamId, 10, 64)
|
||||
if err == nil {
|
||||
g.SteamClient.Social.SendMessage(steamid.SteamId(steamIdUint64), steamlang.EChatEntryType_ChatMsg, message)
|
||||
logger.Debug.Printf("[%s] Send message to %v", g.XMPP_JID_Client, steamIdUint64)
|
||||
g.SteamClient.Social.SendMessage(steamid.SteamId(steamIdUint64), chatEntryType, message)
|
||||
} else {
|
||||
log.Printf("%sFailed to get SteamId from %s", LogSteamError, steamId)
|
||||
logger.Error.Printf("[%s] Failed to get SteamId from %s", g.XMPP_JID_Client, steamId)
|
||||
}
|
||||
}
|
||||
|
||||
func (g *GatewayInfo) SendSteamPresence(status steamlang.EPersonaState) {
|
||||
if !g.IsSteamConnected() {
|
||||
log.Printf("%sTry to send presence, but disconnected", LogSteamDebug)
|
||||
logger.Debug.Printf("[%s] Try to send presence, but disconnected", g.XMPP_JID_Client)
|
||||
return
|
||||
}
|
||||
logger.Debug.Printf("[%s] Send presence (Status: %v)", g.XMPP_JID_Client, status)
|
||||
g.SteamClient.Social.SetPersonaState(status)
|
||||
}
|
||||
|
|
|
|||
251
gateway/xmpp.go
251
gateway/xmpp.go
|
|
@ -2,10 +2,12 @@ package gateway
|
|||
|
||||
import (
|
||||
"git.kingpenguin.tk/chteufleur/go-xmpp.git/src/xmpp"
|
||||
"git.kingpenguin.tk/chteufleur/go-xmpp4steam.git/logger"
|
||||
"github.com/Philipp15b/go-steam/protocol/steamlang"
|
||||
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
|
|
@ -28,16 +30,20 @@ const (
|
|||
ActionConnexion = "action_xmpp_connexion"
|
||||
ActionDeconnexion = "action_xmpp_deconnexion"
|
||||
ActionMainMethodEnded = "action_xmpp_main_method_ended"
|
||||
|
||||
LogXmppInfo = "\t[XMPP INFO]\t"
|
||||
LogXmppError = "\t[XMPP ERROR]\t"
|
||||
LogXmppDebug = "\t[XMPP DEBUG]\t"
|
||||
)
|
||||
|
||||
var (
|
||||
XmppJidComponent = ""
|
||||
|
||||
iqId = uint64(0)
|
||||
)
|
||||
|
||||
func NextIqId() string {
|
||||
iqId += 1
|
||||
ret := strconv.FormatUint(iqId, 10)
|
||||
return ret
|
||||
}
|
||||
|
||||
func (g *GatewayInfo) ReceivedXMPP_Presence(presence *xmpp.Presence) {
|
||||
if presence.Type == Type_error {
|
||||
return
|
||||
|
|
@ -50,14 +56,14 @@ func (g *GatewayInfo) ReceivedXMPP_Presence(presence *xmpp.Presence) {
|
|||
if len(jid) == 2 {
|
||||
// Resource exist —> client speaking
|
||||
if presence.Type == Type_available {
|
||||
g.XMPP_Connected_Client[presence.From] = true
|
||||
g.SetXmppConnectedClient(presence.From)
|
||||
} else if presence.Type == Type_unavailable {
|
||||
delete(g.XMPP_Connected_Client, presence.From)
|
||||
g.RemoveXmppConnectedClient(presence.From)
|
||||
}
|
||||
}
|
||||
|
||||
if presence.Type == Type_probe {
|
||||
steamFriendStatus := g.FriendSteamId[steamJid[0]]
|
||||
steamFriendStatus := g.GetFriendSteamId(steamJid[0])
|
||||
if steamFriendStatus != nil {
|
||||
g.SendXmppPresence(steamFriendStatus.XMPP_Status, steamFriendStatus.XMPP_Type, "", steamJid[0]+"@"+XmppJidComponent, steamFriendStatus.SteamGameName, steamFriendStatus.SteamName)
|
||||
}
|
||||
|
|
@ -73,22 +79,13 @@ func (g *GatewayInfo) ReceivedXMPP_Presence(presence *xmpp.Presence) {
|
|||
// Destination is gateway itself
|
||||
if presence.Type == Type_unavailable {
|
||||
// Disconnect
|
||||
if len(g.XMPP_Connected_Client) <= 0 {
|
||||
if g.GetLenXmppConnectedClient() <= 0 {
|
||||
g.Disconnect()
|
||||
}
|
||||
} else if presence.Type == Type_available {
|
||||
go g.SteamConnect()
|
||||
transfertPresence = true
|
||||
}
|
||||
|
||||
} else {
|
||||
// Destination is Steam user
|
||||
if presence.Type == Type_unavailable {
|
||||
// Disconnect
|
||||
if len(g.XMPP_Connected_Client) <= 0 {
|
||||
g.Disconnect()
|
||||
} else {
|
||||
if presence.Type == Type_available {
|
||||
g.addUserIntoRoster(presence.To, "xmpp4steam gateway")
|
||||
}
|
||||
} else if presence.Type == Type_available {
|
||||
go g.SteamConnect()
|
||||
transfertPresence = true
|
||||
}
|
||||
|
|
@ -117,14 +114,73 @@ func (g *GatewayInfo) ReceivedXMPP_Presence(presence *xmpp.Presence) {
|
|||
|
||||
if g.IsSteamConnected() {
|
||||
g.SendSteamPresence(steamStatus)
|
||||
g.SendXmppPresence(presence.Show, presence.Type, presence.From, "", presence.Status, "")
|
||||
g.SendXmppPresence(presence.Show, presence.Type, "", "", presence.Status, "")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (g *GatewayInfo) ReceivedXMPP_Message(message *xmpp.Message) {
|
||||
steamID := strings.SplitN(message.To, "@", 2)[0]
|
||||
g.SendSteamMessage(steamID, message.Body)
|
||||
if message.Composing != nil {
|
||||
g.SendSteamMessageComposing(steamID)
|
||||
} else if message.Paused != nil {
|
||||
return
|
||||
} else if message.Inactive != nil {
|
||||
return
|
||||
} else if message.Gone != nil {
|
||||
g.SendSteamMessageLeaveConversation(steamID)
|
||||
} else {
|
||||
if message.Body != nil && len(message.Body) != 0 {
|
||||
g.SendSteamMessage(steamID, message.Body[0].Value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (g *GatewayInfo) ReceivedXMPP_IQ(iq *xmpp.IQ) bool {
|
||||
ret := false
|
||||
remoteRosterRequestValue := g.GetXmppRemoteRosterRequest(iq.ID)
|
||||
if remoteRosterRequestValue == RemoteRosterRequestPermission {
|
||||
g.RemoveXmppRemoteRosterRequest(iq.ID)
|
||||
|
||||
if iq.Type == xmpp.IQTypeError && iq.Error.Condition() == xmpp.ErrorForbidden {
|
||||
g.AllowEditRoster = false
|
||||
logger.Info.Printf("Set allow roster edition to %v", g.AllowEditRoster)
|
||||
} else if iq.Type == xmpp.IQTypeSet && iq.PayloadName().Space == xmpp.NSRemoteRosterManager {
|
||||
remoteRosterQuery := &xmpp.RemoteRosterManagerQuery{}
|
||||
iq.PayloadDecode(remoteRosterQuery)
|
||||
if remoteRosterQuery.Type == xmpp.RemoteRosterManagerTypeAllowed {
|
||||
g.AllowEditRoster = true
|
||||
} else if remoteRosterQuery.Type == xmpp.RemoteRosterManagerTypeRejected {
|
||||
g.AllowEditRoster = false
|
||||
} else {
|
||||
g.AllowEditRoster = false
|
||||
}
|
||||
logger.Info.Printf("Set allow roster edition to %v", g.AllowEditRoster)
|
||||
g.SendXmppMessage(XmppJidComponent, "", "Set allow roster edition to "+strconv.FormatBool(g.AllowEditRoster))
|
||||
} else {
|
||||
logger.Info.Printf("Check roster edition authorisation by querying roster's user")
|
||||
// Remote roster namespace may not be supported (like prosody), so we send a roster query
|
||||
iqId := NextIqId()
|
||||
g.SetXmppRemoteRosterRequest(iqId, RemoteRosterRequestRoster)
|
||||
iqSend := &xmpp.IQ{ID: iqId, Type: xmpp.IQTypeGet, From: iq.To, To: iq.From}
|
||||
iqSend.PayloadEncode(&xmpp.RosterQuery{})
|
||||
g.XMPP_Out <- iqSend
|
||||
}
|
||||
ret = true
|
||||
|
||||
} else if remoteRosterRequestValue == RemoteRosterRequestRoster {
|
||||
g.RemoveXmppRemoteRosterRequest(iq.ID)
|
||||
if iq.Type == xmpp.IQTypeResult && iq.PayloadName().Space == xmpp.NSRoster {
|
||||
g.AllowEditRoster = true
|
||||
} else {
|
||||
g.AllowEditRoster = false
|
||||
}
|
||||
logger.Info.Printf("Set allow roster edition to %v", g.AllowEditRoster)
|
||||
g.SendXmppMessage(XmppJidComponent, "", "Set allow roster edition to "+strconv.FormatBool(g.AllowEditRoster))
|
||||
ret = true
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func (g *GatewayInfo) XMPP_Disconnect() {
|
||||
|
|
@ -152,25 +208,164 @@ func (g *GatewayInfo) SendXmppPresence(status, tpye, to, from, message, nick str
|
|||
p.To = to
|
||||
}
|
||||
if from == "" {
|
||||
// TODO add an option to allow message comming directly from the gateway
|
||||
p.From = XmppJidComponent + "/" + resource
|
||||
} else {
|
||||
p.From = from + "/" + resource
|
||||
}
|
||||
|
||||
log.Printf("%sSend presence %v", LogXmppInfo, p)
|
||||
g.XMPP_Out <- p
|
||||
if tpye == Type_subscribe && g.AllowEditRoster {
|
||||
g.addUserIntoRoster(from, nick)
|
||||
} else {
|
||||
logger.Info.Printf("Send presence %v", p)
|
||||
g.XMPP_Out <- p
|
||||
}
|
||||
}
|
||||
|
||||
func (g *GatewayInfo) addUserIntoRoster(jid, nick string) {
|
||||
iq := xmpp.IQ{To: g.XMPP_JID_Client, Type: xmpp.IQTypeSet, ID: NextIqId()}
|
||||
query := &xmpp.RosterQuery{}
|
||||
queryItem := &xmpp.RosterItem{JID: jid, Name: nick, Subscription: xmpp.RosterSubscriptionBoth}
|
||||
queryItem.Groupes = append(queryItem.Groupes, XmppGroupUser)
|
||||
query.Items = append(query.Items, *queryItem)
|
||||
iq.PayloadEncode(query)
|
||||
logger.Info.Printf("Add user into roster %v", iq)
|
||||
g.XMPP_Out <- iq
|
||||
}
|
||||
|
||||
func (g *GatewayInfo) removeAllUserFromRoster() {
|
||||
// Friends
|
||||
for steamId := range g.SteamClient.Social.Friends.GetCopy() {
|
||||
iq := xmpp.IQ{To: g.XMPP_JID_Client, Type: xmpp.IQTypeSet, ID: NextIqId()}
|
||||
query := &xmpp.RosterQuery{}
|
||||
query.Items = append(query.Items, *&xmpp.RosterItem{JID: steamId.ToString() + "@" + XmppJidComponent, Subscription: xmpp.RosterSubscriptionRemove})
|
||||
iq.PayloadEncode(query)
|
||||
logger.Info.Printf("Remove steam user roster")
|
||||
g.XMPP_Out <- iq
|
||||
}
|
||||
// Myself
|
||||
iq := xmpp.IQ{To: g.XMPP_JID_Client, Type: xmpp.IQTypeSet, ID: NextIqId()}
|
||||
query := &xmpp.RosterQuery{}
|
||||
query.Items = append(query.Items, *&xmpp.RosterItem{JID: g.SteamClient.SteamId().ToString() + "@" + XmppJidComponent, Subscription: xmpp.RosterSubscriptionRemove})
|
||||
iq.PayloadEncode(query)
|
||||
logger.Info.Printf("Remove steam user roster")
|
||||
g.XMPP_Out <- iq
|
||||
}
|
||||
|
||||
func (g *GatewayInfo) SendXmppMessage(from, subject, message string) {
|
||||
g.sendXmppMessage(from, subject, message, &xmpp.Active{})
|
||||
g.ChatstateNotificationData <- from
|
||||
g.ChatstateNotificationData <- "stop"
|
||||
g.ChatstateNotificationData <- from
|
||||
g.ChatstateNotificationData <- "inactive"
|
||||
}
|
||||
|
||||
func (g *GatewayInfo) SendXmppMessageLeaveConversation(from string) {
|
||||
g.sendXmppMessage(from, "", "", &xmpp.Gone{})
|
||||
g.ChatstateNotificationData <- from
|
||||
g.ChatstateNotificationData <- "stop"
|
||||
}
|
||||
|
||||
func (g *GatewayInfo) SendXmppMessageComposing(from string) {
|
||||
g.sendXmppMessage(from, "", "", &xmpp.Composing{})
|
||||
g.ChatstateNotificationData <- from
|
||||
g.ChatstateNotificationData <- "paused"
|
||||
g.ChatstateNotificationData <- from
|
||||
g.ChatstateNotificationData <- "inactive"
|
||||
}
|
||||
|
||||
func (g *GatewayInfo) chatstatesNotification() {
|
||||
inactiveTimers := make(map[string]*time.Timer)
|
||||
pausedTimers := make(map[string]*time.Timer)
|
||||
g.ChatstateNotificationData = make(chan string)
|
||||
|
||||
for {
|
||||
jid := <-g.ChatstateNotificationData
|
||||
chatstate := <-g.ChatstateNotificationData
|
||||
|
||||
timerInactive, okInactive := inactiveTimers[jid]
|
||||
timerPaused, okPaused := pausedTimers[jid]
|
||||
|
||||
switch chatstate {
|
||||
case "stop":
|
||||
if okInactive {
|
||||
if timerInactive != nil {
|
||||
if !timerInactive.Stop() {
|
||||
<-timerInactive.C
|
||||
}
|
||||
delete(inactiveTimers, jid)
|
||||
}
|
||||
}
|
||||
if okPaused {
|
||||
if timerPaused != nil {
|
||||
if !timerPaused.Stop() {
|
||||
<-timerPaused.C
|
||||
}
|
||||
delete(pausedTimers, jid)
|
||||
}
|
||||
}
|
||||
|
||||
case "paused":
|
||||
if okInactive {
|
||||
if timerPaused != nil {
|
||||
if !timerPaused.Stop() {
|
||||
<-timerPaused.C
|
||||
}
|
||||
timerPaused.Reset(20 * time.Second)
|
||||
}
|
||||
} else {
|
||||
timerPaused = time.AfterFunc(20*time.Second, func() {
|
||||
g.sendXmppMessage(jid, "", "", &xmpp.Paused{})
|
||||
delete(pausedTimers, jid)
|
||||
})
|
||||
pausedTimers[jid] = timerPaused
|
||||
}
|
||||
|
||||
case "inactive":
|
||||
if okInactive {
|
||||
if timerInactive != nil {
|
||||
if !timerInactive.Stop() {
|
||||
<-timerInactive.C
|
||||
}
|
||||
timerInactive.Reset(120 * time.Second)
|
||||
}
|
||||
} else {
|
||||
timerInactive = time.AfterFunc(120*time.Second, func() {
|
||||
g.sendXmppMessage(jid, "", "", &xmpp.Inactive{})
|
||||
delete(inactiveTimers, jid)
|
||||
})
|
||||
inactiveTimers[jid] = timerInactive
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (g *GatewayInfo) sendXmppMessage(from, subject, message string, chatState interface{}) {
|
||||
if from != XmppJidComponent || from == XmppJidComponent && g.DebugMessage {
|
||||
m := xmpp.Message{To: g.XMPP_JID_Client, From: from, Body: message, Type: "chat"}
|
||||
m := xmpp.Message{To: g.XMPP_JID_Client, From: from, Type: "chat"}
|
||||
mBody := xmpp.MessageBody{Value: message}
|
||||
m.Body = append(m.Body, mBody)
|
||||
|
||||
if subject != "" {
|
||||
m.Subject = subject
|
||||
}
|
||||
|
||||
log.Printf("%sSend message %v", LogXmppInfo, m)
|
||||
switch v := chatState.(type) {
|
||||
case *xmpp.Active:
|
||||
m.Active = v
|
||||
case *xmpp.Composing:
|
||||
m.Composing = v
|
||||
case *xmpp.Paused:
|
||||
m.Paused = v
|
||||
case *xmpp.Inactive:
|
||||
m.Inactive = v
|
||||
case *xmpp.Gone:
|
||||
m.Gone = v
|
||||
default:
|
||||
m.Active = &xmpp.Active{}
|
||||
}
|
||||
|
||||
logger.Info.Printf("Send message %v", m)
|
||||
g.XMPP_Out <- m
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,18 @@
|
|||
package logger
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
)
|
||||
|
||||
var (
|
||||
Info *log.Logger
|
||||
Debug *log.Logger
|
||||
Error *log.Logger
|
||||
)
|
||||
|
||||
func Init(infoHandle io.Writer, warningHandle io.Writer, errorHandle io.Writer) {
|
||||
Info = log.New(infoHandle, "INFO : ", log.Ldate|log.Ltime|log.Lshortfile)
|
||||
Debug = log.New(warningHandle, "DEBUG : ", log.Ldate|log.Ltime|log.Lshortfile)
|
||||
Error = log.New(errorHandle, "ERROR : ", log.Ldate|log.Ltime|log.Lshortfile)
|
||||
}
|
||||
40
main.go
40
main.go
|
|
@ -1,49 +1,51 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"git.kingpenguin.tk/chteufleur/go-xmpp4steam.git/configuration"
|
||||
"git.kingpenguin.tk/chteufleur/go-xmpp4steam.git/database"
|
||||
"git.kingpenguin.tk/chteufleur/go-xmpp4steam.git/gateway"
|
||||
"git.kingpenguin.tk/chteufleur/go-xmpp4steam.git/logger"
|
||||
"git.kingpenguin.tk/chteufleur/go-xmpp4steam.git/xmpp"
|
||||
|
||||
"github.com/jimlawless/cfg"
|
||||
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
Version = "v1.0"
|
||||
configurationFilePath = "xmpp4steam.cfg"
|
||||
)
|
||||
|
||||
var (
|
||||
mapConfig = make(map[string]string)
|
||||
Version = "v1.1"
|
||||
)
|
||||
|
||||
func init() {
|
||||
logger.Init(os.Stdout, os.Stdout, os.Stderr)
|
||||
configuration.Init()
|
||||
logger.Info.Println("Running go-xmpp4steam " + Version)
|
||||
xmpp.SoftVersion = Version
|
||||
err := cfg.Load(configurationFilePath, mapConfig)
|
||||
if err != nil {
|
||||
log.Fatal("Failed to load configuration file.", err)
|
||||
}
|
||||
|
||||
// XMPP config
|
||||
xmpp.Addr = mapConfig["xmpp_server_address"] + ":" + mapConfig["xmpp_server_port"]
|
||||
xmpp.JidStr = mapConfig["xmpp_hostname"]
|
||||
xmpp.Secret = mapConfig["xmpp_secret"]
|
||||
xmpp.Debug = mapConfig["xmpp_debug"] == "true"
|
||||
xmpp.Addr = configuration.MapConfig["xmpp_server_address"] + ":" + configuration.MapConfig["xmpp_server_port"]
|
||||
xmpp.JidStr = configuration.MapConfig["xmpp_hostname"]
|
||||
xmpp.Secret = configuration.MapConfig["xmpp_secret"]
|
||||
xmpp.Debug = configuration.MapConfig["xmpp_debug"] == "true"
|
||||
if configuration.MapConfig["xmpp_group"] != "" {
|
||||
gateway.XmppGroupUser = configuration.MapConfig["xmpp_group"]
|
||||
}
|
||||
gateway.XmppJidComponent = xmpp.JidStr
|
||||
|
||||
for _, admin := range strings.Split(configuration.MapConfig["xmpp_admins"], ";") {
|
||||
xmpp.AdminUsers[admin] = true
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
go xmpp.Run()
|
||||
time.Sleep(1 * time.Second)
|
||||
allDbUsers := database.GetAllLines()
|
||||
for _, dbUser := range allDbUsers {
|
||||
xmpp.AddNewUser(dbUser.Jid, dbUser.SteamLogin, dbUser.SteamPwd, dbUser.Debug)
|
||||
}
|
||||
go xmpp.Run()
|
||||
|
||||
sigchan := make(chan os.Signal, 1)
|
||||
signal.Notify(sigchan, os.Interrupt)
|
||||
|
|
@ -53,6 +55,6 @@ func main() {
|
|||
|
||||
xmpp.Disconnect()
|
||||
|
||||
log.Println("Exit main()")
|
||||
logger.Info.Println("Exit main()")
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
{"Addresses":[{"IP":"146.66.152.11","Port":27020},{"IP":"162.254.197.42","Port":27020},{"IP":"146.66.152.10","Port":27020},{"IP":"146.66.152.10","Port":27017},{"IP":"162.254.197.40","Port":27018},{"IP":"146.66.152.10","Port":27018},{"IP":"162.254.197.40","Port":27019},{"IP":"162.254.197.41","Port":27017},{"IP":"162.254.197.41","Port":27020},{"IP":"162.254.197.40","Port":27020},{"IP":"146.66.152.10","Port":27019},{"IP":"162.254.197.42","Port":27019},{"IP":"162.254.197.41","Port":27021},{"IP":"162.254.197.42","Port":27018},{"IP":"162.254.197.42","Port":27017},{"IP":"162.254.197.42","Port":27021},{"IP":"162.254.197.41","Port":27019},{"IP":"162.254.197.41","Port":27018},{"IP":"162.254.196.40","Port":27020},{"IP":"146.66.152.11","Port":27018},{"IP":"162.254.197.40","Port":27021},{"IP":"162.254.196.43","Port":27019},{"IP":"162.254.196.43","Port":27018},{"IP":"162.254.196.42","Port":27021},{"IP":"146.66.152.11","Port":27017},{"IP":"162.254.196.41","Port":27018},{"IP":"162.254.196.41","Port":27019},{"IP":"162.254.196.42","Port":27017},{"IP":"162.254.196.43","Port":27021},{"IP":"162.254.196.40","Port":27018},{"IP":"162.254.196.40","Port":27017},{"IP":"162.254.196.42","Port":27020},{"IP":"162.254.196.41","Port":27020},{"IP":"162.254.196.41","Port":27021},{"IP":"162.254.196.41","Port":27017},{"IP":"162.254.196.42","Port":27018},{"IP":"162.254.197.40","Port":27017},{"IP":"162.254.196.40","Port":27019},{"IP":"146.66.152.11","Port":27019},{"IP":"162.254.196.43","Port":27017},{"IP":"162.254.196.43","Port":27020},{"IP":"162.254.196.40","Port":27021},{"IP":"162.254.196.42","Port":27019},{"IP":"146.66.155.8","Port":27020},{"IP":"155.133.242.9","Port":27019},{"IP":"146.66.155.8","Port":27019},{"IP":"155.133.242.8","Port":27020},{"IP":"185.25.180.15","Port":27020},{"IP":"155.133.242.8","Port":27018},{"IP":"146.66.155.8","Port":27017},{"IP":"185.25.182.10","Port":27020},{"IP":"185.25.182.10","Port":27017},{"IP":"185.25.182.10","Port":27018},{"IP":"185.25.182.10","Port":27019},{"IP":"155.133.242.8","Port":27019},{"IP":"155.133.242.9","Port":27020},{"IP":"185.25.180.14","Port":27019},{"IP":"146.66.155.8","Port":27018},{"IP":"185.25.180.14","Port":27020},{"IP":"185.25.180.15","Port":27018},{"IP":"185.25.180.14","Port":27017},{"IP":"155.133.242.9","Port":27018},{"IP":"185.25.180.15","Port":27019},{"IP":"185.25.180.14","Port":27018},{"IP":"155.133.242.9","Port":27017},{"IP":"155.133.242.8","Port":27017},{"IP":"185.25.180.15","Port":27017},{"IP":"208.78.164.11","Port":27018},{"IP":"208.78.164.14","Port":27018},{"IP":"208.78.164.13","Port":27019},{"IP":"208.78.164.11","Port":27019},{"IP":"208.78.164.9","Port":27019},{"IP":"208.78.164.13","Port":27018},{"IP":"208.78.164.12","Port":27019},{"IP":"208.78.164.13","Port":27017},{"IP":"208.78.164.9","Port":27017},{"IP":"208.78.164.10","Port":27018},{"IP":"208.78.164.14","Port":27017},{"IP":"208.78.164.12","Port":27018},{"IP":"208.78.164.10","Port":27019}]}
|
||||
{"Addresses":[{"IP":"162.254.197.41","Port":27018},{"IP":"162.254.197.40","Port":27018},{"IP":"146.66.152.10","Port":27017},{"IP":"146.66.152.10","Port":27020},{"IP":"146.66.152.11","Port":27017},{"IP":"162.254.197.42","Port":27017},{"IP":"162.254.197.41","Port":27021},{"IP":"162.254.197.40","Port":27020},{"IP":"146.66.152.11","Port":27020},{"IP":"162.254.197.42","Port":27019},{"IP":"146.66.152.10","Port":27019},{"IP":"162.254.197.41","Port":27020},{"IP":"146.66.152.11","Port":27019},{"IP":"162.254.197.41","Port":27019},{"IP":"146.66.152.11","Port":27018},{"IP":"162.254.197.41","Port":27017},{"IP":"146.66.152.10","Port":27018},{"IP":"162.254.197.42","Port":27021},{"IP":"162.254.197.40","Port":27021},{"IP":"162.254.197.42","Port":27018},{"IP":"162.254.197.42","Port":27020},{"IP":"162.254.197.40","Port":27019},{"IP":"162.254.196.41","Port":27017},{"IP":"162.254.196.41","Port":27021},{"IP":"162.254.196.43","Port":27017},{"IP":"162.254.196.41","Port":27019},{"IP":"162.254.196.41","Port":27020},{"IP":"162.254.196.43","Port":27019},{"IP":"162.254.196.42","Port":27019},{"IP":"162.254.196.41","Port":27018},{"IP":"162.254.196.43","Port":27018},{"IP":"162.254.196.42","Port":27020},{"IP":"162.254.196.42","Port":27018},{"IP":"162.254.196.40","Port":27019},{"IP":"162.254.196.40","Port":27017},{"IP":"162.254.197.40","Port":27017},{"IP":"162.254.196.42","Port":27017},{"IP":"162.254.196.40","Port":27020},{"IP":"162.254.196.40","Port":27021},{"IP":"162.254.196.40","Port":27018},{"IP":"162.254.196.42","Port":27021},{"IP":"162.254.196.43","Port":27020},{"IP":"162.254.196.43","Port":27021},{"IP":"146.66.155.8","Port":27017},{"IP":"185.25.182.10","Port":27020},{"IP":"146.66.155.8","Port":27018},{"IP":"146.66.155.8","Port":27019},{"IP":"185.25.182.10","Port":27018},{"IP":"185.25.182.10","Port":27019},{"IP":"146.66.155.8","Port":27020},{"IP":"185.25.182.10","Port":27017},{"IP":"185.25.180.14","Port":27020},{"IP":"185.25.180.14","Port":27019},{"IP":"185.25.180.14","Port":27018},{"IP":"155.133.242.8","Port":27017},{"IP":"155.133.242.9","Port":27020},{"IP":"155.133.242.8","Port":27018},{"IP":"155.133.242.9","Port":27017},{"IP":"185.25.180.14","Port":27017},{"IP":"155.133.242.9","Port":27019},{"IP":"185.25.180.15","Port":27020},{"IP":"155.133.242.8","Port":27020},{"IP":"155.133.242.8","Port":27019},{"IP":"185.25.180.15","Port":27019},{"IP":"155.133.242.9","Port":27018},{"IP":"185.25.180.15","Port":27017},{"IP":"185.25.180.15","Port":27018},{"IP":"208.78.164.13","Port":27019},{"IP":"208.78.164.12","Port":27019},{"IP":"208.78.164.12","Port":27018},{"IP":"208.78.164.10","Port":27017},{"IP":"208.78.164.14","Port":27017},{"IP":"208.78.164.10","Port":27018},{"IP":"208.78.164.13","Port":27017},{"IP":"208.78.164.13","Port":27018},{"IP":"208.78.164.10","Port":27019},{"IP":"208.78.164.12","Port":27017},{"IP":"208.78.164.14","Port":27018},{"IP":"208.78.164.14","Port":27019},{"IP":"162.254.195.46","Port":27020}]}
|
||||
257
xmpp/commands.go
257
xmpp/commands.go
|
|
@ -3,9 +3,11 @@ 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/logger"
|
||||
|
||||
"log"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
|
|
@ -14,47 +16,109 @@ const (
|
|||
CommandDisconnectSteam = "disconnectSteam"
|
||||
CommandRemoveRegistration = "removeRegistration"
|
||||
CommandToggleDebugMode = "toggleDebugMode"
|
||||
CommandUptimeMode = "uptime"
|
||||
CommandMessageBroadcast = "messageBroadcast"
|
||||
)
|
||||
|
||||
var (
|
||||
ChanAuthCode = make(chan string)
|
||||
ChanAuthCode = make(chan string)
|
||||
identityGateway = &xmpp.DiscoIdentity{Category: "gateway", Type: "steam", Name: "Steam Gateway"}
|
||||
identityClients = &xmpp.DiscoIdentity{Category: "client", Type: "pc", Name: "Steam client"}
|
||||
)
|
||||
|
||||
func execDiscoCommand(iq *xmpp.Iq) {
|
||||
log.Printf("%sDiscovery item iq received", LogInfo)
|
||||
func execDiscoCommand(iq *xmpp.IQ) {
|
||||
logger.Info.Printf("Ad-Hoc Command")
|
||||
|
||||
// Disco Ad-Hoc
|
||||
reply := iq.Response(xmpp.IQTypeResult)
|
||||
discoItem := &xmpp.DiscoItems{Node: xmpp.NodeAdHocCommand}
|
||||
|
||||
jidBare := strings.SplitN(iq.From, "/", 2)[0]
|
||||
dbUser := database.GetLine(jidBare)
|
||||
jidBareFrom := strings.SplitN(iq.From, "/", 2)[0]
|
||||
jidBareTo := strings.SplitN(iq.To, "/", 2)[0]
|
||||
dbUser := database.GetLine(jidBareFrom)
|
||||
|
||||
// Add available commands
|
||||
if dbUser == nil {
|
||||
discoI := &xmpp.DiscoItem{JID: jid.Domain, Node: CommandGetIdentifiants, Name: "Steam registration"}
|
||||
discoItem.Item = append(discoItem.Item, *discoI)
|
||||
} else {
|
||||
// Add only if user is registered
|
||||
discoI := &xmpp.DiscoItem{JID: jid.Domain, Node: CommandAuthcode, Name: "Add Steam Auth Code"}
|
||||
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)
|
||||
discoI = &xmpp.DiscoItem{JID: jid.Domain, Node: CommandToggleDebugMode, Name: "Toggle debug mode"}
|
||||
discoItem.Item = append(discoItem.Item, *discoI)
|
||||
if jidBareTo == jid.Domain {
|
||||
// Ad-Hoc command only on gateway
|
||||
// Add available commands
|
||||
if dbUser == nil {
|
||||
discoI := &xmpp.DiscoItem{JID: jid.Domain, Node: CommandGetIdentifiants, Name: "Steam registration"}
|
||||
discoItem.Item = append(discoItem.Item, *discoI)
|
||||
} else {
|
||||
// Add only if user is registered
|
||||
discoI := &xmpp.DiscoItem{JID: jid.Domain, Node: CommandAuthcode, Name: "Add Steam Auth Code"}
|
||||
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)
|
||||
discoI = &xmpp.DiscoItem{JID: jid.Domain, Node: CommandToggleDebugMode, Name: "Toggle debug mode"}
|
||||
discoItem.Item = append(discoItem.Item, *discoI)
|
||||
discoI = &xmpp.DiscoItem{JID: jid.Domain, Node: CommandUptimeMode, Name: "Uptime"}
|
||||
discoItem.Item = append(discoItem.Item, *discoI)
|
||||
}
|
||||
|
||||
if AdminUsers[jidBareFrom] {
|
||||
discoI := &xmpp.DiscoItem{JID: jid.Domain, Node: CommandMessageBroadcast, Name: "Broadcast a message"}
|
||||
discoItem.Item = append(discoItem.Item, *discoI)
|
||||
}
|
||||
}
|
||||
|
||||
reply.PayloadEncode(discoItem)
|
||||
comp.Out <- reply
|
||||
}
|
||||
|
||||
func execCommandAdHoc(iq *xmpp.Iq) {
|
||||
func execDisco(iq *xmpp.IQ) {
|
||||
logger.Info.Printf("Disco Feature")
|
||||
jidBareTo := strings.SplitN(iq.To, "/", 2)[0]
|
||||
|
||||
discoInfoReceived := &xmpp.DiscoItems{}
|
||||
iq.PayloadDecode(discoInfoReceived)
|
||||
|
||||
switch iq.PayloadName().Space {
|
||||
case xmpp.NSDiscoInfo:
|
||||
reply := iq.Response(xmpp.IQTypeResult)
|
||||
|
||||
discoInfo := &xmpp.DiscoInfo{}
|
||||
if jidBareTo == jid.Domain {
|
||||
// Only gateway
|
||||
discoInfo.Identity = append(discoInfo.Identity, *identityGateway)
|
||||
discoInfo.Feature = append(discoInfo.Feature, xmpp.DiscoFeature{Var: xmpp.NodeAdHocCommand})
|
||||
discoInfo.Feature = append(discoInfo.Feature, xmpp.DiscoFeature{Var: xmpp.NSJabberClient})
|
||||
discoInfo.Feature = append(discoInfo.Feature, xmpp.DiscoFeature{Var: xmpp.NSRegister})
|
||||
discoInfo.Feature = append(discoInfo.Feature, xmpp.DiscoFeature{Var: xmpp.NSPing})
|
||||
} else {
|
||||
// Only steam users
|
||||
discoInfo.Identity = append(discoInfo.Identity, *identityClients)
|
||||
discoInfo.Feature = append(discoInfo.Feature, xmpp.DiscoFeature{Var: xmpp.NSChatStatesNotification})
|
||||
}
|
||||
// Both
|
||||
discoInfo.Feature = append(discoInfo.Feature, xmpp.DiscoFeature{Var: xmpp.NSDiscoInfo})
|
||||
discoInfo.Feature = append(discoInfo.Feature, xmpp.DiscoFeature{Var: xmpp.NSDiscoItems})
|
||||
|
||||
reply.PayloadEncode(discoInfo)
|
||||
comp.Out <- reply
|
||||
|
||||
case xmpp.NSDiscoItems:
|
||||
if discoInfoReceived.Node == xmpp.NodeAdHocCommand {
|
||||
// Ad-Hoc command
|
||||
execDiscoCommand(iq)
|
||||
} else {
|
||||
reply := iq.Response(xmpp.IQTypeResult)
|
||||
discoItems := &xmpp.DiscoItems{}
|
||||
reply.PayloadEncode(discoItems)
|
||||
comp.Out <- reply
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func execCommandAdHoc(iq *xmpp.IQ) {
|
||||
adHoc := &xmpp.AdHocCommand{}
|
||||
iq.PayloadDecode(adHoc)
|
||||
jidBareFrom := strings.SplitN(iq.From, "/", 2)[0]
|
||||
|
||||
if adHoc.SessionID == "" && adHoc.Action == xmpp.ActionAdHocExecute {
|
||||
// First step in the command
|
||||
log.Printf("%sAd-Hoc command (Node : %s). First step.", LogInfo, adHoc.Node)
|
||||
logger.Info.Printf("Ad-Hoc command (Node : %s). First step.", adHoc.Node)
|
||||
|
||||
reply := iq.Response(xmpp.IQTypeResult)
|
||||
cmd := &xmpp.AdHocCommand{Node: adHoc.Node, Status: xmpp.StatusAdHocExecute, SessionID: xmpp.SessionID()}
|
||||
|
|
@ -68,24 +132,17 @@ func execCommandAdHoc(iq *xmpp.Iq) {
|
|||
|
||||
} else if adHoc.Node == CommandGetIdentifiants {
|
||||
// Command Auth Code
|
||||
cmdXForm := &xmpp.AdHocXForm{Type: xmpp.TypeAdHocForm, Title: "Steam Account Info", Instructions: "Please provide your Steam login and password."}
|
||||
note := &xmpp.AdHocNote{Type: xmpp.TypeAdHocNoteInfo, Value: "Please, be aware that the given Steam account information will be saved into an un-encrypted SQLite database."}
|
||||
|
||||
field := &xmpp.AdHocField{Var: "login", Label: "Steam Login", Type: xmpp.TypeAdHocFieldTextSingle}
|
||||
cmdXForm.Fields = append(cmdXForm.Fields, *field)
|
||||
field = &xmpp.AdHocField{Var: "password", Label: "Steam Password", Type: xmpp.TypeAdHocFieldTextPrivate}
|
||||
cmdXForm.Fields = append(cmdXForm.Fields, *field)
|
||||
|
||||
cmdXForm := getXFormRegistration("")
|
||||
cmd.XForm = *cmdXForm
|
||||
cmd.Note = *note
|
||||
|
||||
} else if adHoc.Node == CommandDisconnectSteam {
|
||||
// Command steam deconnection
|
||||
cmd.Status = xmpp.StatusAdHocCompleted
|
||||
cmdXForm := &xmpp.AdHocXForm{Type: xmpp.TypeAdHocResult, Title: "Force Steam deconnexion"}
|
||||
cmd.XForm = *cmdXForm
|
||||
note := &xmpp.AdHocNote{Type: xmpp.TypeAdHocNoteInfo}
|
||||
|
||||
jidBare := strings.SplitN(iq.From, "/", 2)[0]
|
||||
g := MapGatewayInfo[jidBare]
|
||||
g := MapGatewayInfo[jidBareFrom]
|
||||
if g != nil {
|
||||
g.Disconnect()
|
||||
note.Value = "Send deconnexion on Steam network"
|
||||
|
|
@ -94,13 +151,13 @@ func execCommandAdHoc(iq *xmpp.Iq) {
|
|||
}
|
||||
cmd.Note = *note
|
||||
} else if adHoc.Node == CommandRemoveRegistration {
|
||||
// Command remove registration
|
||||
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) {
|
||||
if RemoveUser(jidBareFrom) {
|
||||
note.Value = "Remove registration success."
|
||||
} else {
|
||||
note.Value = "Failed to remove your registration."
|
||||
|
|
@ -108,16 +165,16 @@ func execCommandAdHoc(iq *xmpp.Iq) {
|
|||
|
||||
cmd.Note = *note
|
||||
} else if adHoc.Node == CommandToggleDebugMode {
|
||||
// Command toggle debug mode
|
||||
cmd.Status = xmpp.StatusAdHocCompleted
|
||||
cmdXForm := &xmpp.AdHocXForm{Type: xmpp.TypeAdHocResult, Title: "Toggle debug mode"}
|
||||
cmd.XForm = *cmdXForm
|
||||
note := &xmpp.AdHocNote{Type: xmpp.TypeAdHocNoteInfo}
|
||||
|
||||
jidBare := strings.SplitN(iq.From, "/", 2)[0]
|
||||
dbUser := database.GetLine(jidBare)
|
||||
dbUser := database.GetLine(jidBareFrom)
|
||||
if dbUser != nil {
|
||||
dbUser.Debug = !dbUser.Debug
|
||||
g := MapGatewayInfo[jidBare]
|
||||
g := MapGatewayInfo[jidBareFrom]
|
||||
ok := dbUser.UpdateLine()
|
||||
if ok && g != nil {
|
||||
g.DebugMessage = dbUser.Debug
|
||||
|
|
@ -134,12 +191,29 @@ func execCommandAdHoc(iq *xmpp.Iq) {
|
|||
}
|
||||
|
||||
cmd.Note = *note
|
||||
} else if adHoc.Node == CommandUptimeMode {
|
||||
// Command get uptime
|
||||
cmd.Status = xmpp.StatusAdHocCompleted
|
||||
cmdXForm := &xmpp.AdHocXForm{Type: xmpp.TypeAdHocResult, Title: "Uptime"}
|
||||
cmd.XForm = *cmdXForm
|
||||
deltaT := time.Since(startTime)
|
||||
val := fmt.Sprintf("%dj %dh %dm %ds", int64(deltaT.Hours()/24), int64(deltaT.Hours())%24, int64(deltaT.Minutes())%60, int64(deltaT.Seconds())%60)
|
||||
note := &xmpp.AdHocNote{Type: xmpp.TypeAdHocNoteInfo, Value: val}
|
||||
|
||||
cmd.Note = *note
|
||||
} else if adHoc.Node == CommandMessageBroadcast && AdminUsers[jidBareFrom] {
|
||||
// Command send broadcast message
|
||||
cmdXForm := &xmpp.AdHocXForm{Type: xmpp.TypeAdHocForm, Title: "Broadcast a message", Instructions: "Message to broadcast to all user."}
|
||||
|
||||
field := &xmpp.AdHocField{Var: "message", Label: "Message", Type: xmpp.TypeAdHocFieldTextSingle}
|
||||
cmdXForm.Fields = append(cmdXForm.Fields, *field)
|
||||
cmd.XForm = *cmdXForm
|
||||
}
|
||||
reply.PayloadEncode(cmd)
|
||||
comp.Out <- reply
|
||||
} else if adHoc.Action == xmpp.ActionAdHocExecute || adHoc.Action == xmpp.ActionAdHocNext {
|
||||
// Last step in the command
|
||||
log.Printf("%sAd-Hoc command (Node : %s). Last step.", LogInfo, adHoc.Node)
|
||||
logger.Info.Printf("Ad-Hoc command (Node : %s). Last step.", adHoc.Node)
|
||||
reply := iq.Response(xmpp.IQTypeResult)
|
||||
cmd := &xmpp.AdHocCommand{Node: adHoc.Node, Status: xmpp.StatusAdHocCompleted, SessionID: adHoc.SessionID}
|
||||
|
||||
|
|
@ -158,12 +232,11 @@ func execCommandAdHoc(iq *xmpp.Iq) {
|
|||
}
|
||||
}
|
||||
if authCode != "" {
|
||||
// Succeded
|
||||
jidBare := strings.SplitN(iq.From, "/", 2)[0]
|
||||
g := MapGatewayInfo[jidBare]
|
||||
// Succeeded
|
||||
g := MapGatewayInfo[jidBareFrom]
|
||||
if g != nil {
|
||||
g.SetSteamAuthCode(authCode)
|
||||
note.Value = "Command succeded !"
|
||||
note.Value = "Command succeeded !"
|
||||
} else {
|
||||
note.Value = "Your are not registred. Please, register before sending Steam auth code."
|
||||
}
|
||||
|
|
@ -179,35 +252,11 @@ func execCommandAdHoc(iq *xmpp.Iq) {
|
|||
note := &xmpp.AdHocNote{Type: xmpp.TypeAdHocNoteInfo}
|
||||
|
||||
// Command Auth Code
|
||||
steamLogin := ""
|
||||
steamPwd := ""
|
||||
fields := adHoc.XForm.Fields
|
||||
for _, field := range fields {
|
||||
if field.Var == "login" {
|
||||
steamLogin = field.Value
|
||||
} else if field.Var == "password" {
|
||||
steamPwd = field.Value
|
||||
}
|
||||
}
|
||||
if steamLogin != "" && steamPwd != "" {
|
||||
// Succeded
|
||||
jidBare := strings.SplitN(iq.From, "/", 2)[0]
|
||||
dbUser := new(database.DatabaseLine)
|
||||
dbUser.Jid = jidBare
|
||||
dbUser.SteamLogin = steamLogin
|
||||
dbUser.SteamPwd = steamPwd
|
||||
dbUser.Debug = false
|
||||
|
||||
isUserRegistred := database.GetLine(dbUser.Jid) != nil
|
||||
var isSqlSuccess bool
|
||||
if isUserRegistred {
|
||||
isSqlSuccess = dbUser.UpdateLine()
|
||||
} else {
|
||||
isSqlSuccess = dbUser.AddLine()
|
||||
}
|
||||
if isSqlSuccess {
|
||||
dbUser := getUser(adHoc.XForm.Fields, iq)
|
||||
if dbUser != nil {
|
||||
if dbUser.UpdateUser() {
|
||||
AddNewUser(dbUser.Jid, dbUser.SteamLogin, dbUser.SteamPwd, dbUser.Debug)
|
||||
note.Value = "Command succeded !"
|
||||
note.Value = "Command succeeded !"
|
||||
} else {
|
||||
note.Value = "Error append while executing command"
|
||||
}
|
||||
|
|
@ -216,16 +265,80 @@ func execCommandAdHoc(iq *xmpp.Iq) {
|
|||
note.Value = "Failed because Steam login or Steam password is empty."
|
||||
}
|
||||
cmd.Note = *note
|
||||
|
||||
} else if adHoc.Node == CommandMessageBroadcast && AdminUsers[jidBareFrom] {
|
||||
cmdXForm := &xmpp.AdHocXForm{Type: xmpp.TypeAdHocResult, Title: "Broadcast a message"}
|
||||
cmd.XForm = *cmdXForm
|
||||
note := &xmpp.AdHocNote{Type: xmpp.TypeAdHocNoteInfo}
|
||||
|
||||
// Command Auth Code
|
||||
message := ""
|
||||
fields := adHoc.XForm.Fields
|
||||
for _, field := range fields {
|
||||
if field.Var == "message" {
|
||||
message = field.Value
|
||||
break
|
||||
}
|
||||
}
|
||||
if message != "" {
|
||||
// Succeeded
|
||||
for userJID := range MapGatewayInfo {
|
||||
SendMessage(userJID, "", message)
|
||||
}
|
||||
note.Value = "Message sended to all registered users"
|
||||
} else {
|
||||
// Failed
|
||||
note.Value = "There is no message to send"
|
||||
}
|
||||
cmd.Note = *note
|
||||
}
|
||||
|
||||
reply.PayloadEncode(cmd)
|
||||
comp.Out <- reply
|
||||
} else if adHoc.Action == xmpp.ActionAdHocCancel {
|
||||
// command canceled
|
||||
log.Printf("%sAd-Hoc command (Node : %s). Command canceled.", LogInfo, adHoc.Node)
|
||||
logger.Info.Printf("Ad-Hoc command (Node : %s). Command canceled.", adHoc.Node)
|
||||
reply := iq.Response(xmpp.IQTypeResult)
|
||||
cmd := &xmpp.AdHocCommand{Node: adHoc.Node, Status: xmpp.StatusAdHocCanceled, SessionID: adHoc.SessionID}
|
||||
reply.PayloadEncode(cmd)
|
||||
comp.Out <- reply
|
||||
}
|
||||
}
|
||||
|
||||
func getXFormRegistration(steamLogin string) *xmpp.AdHocXForm {
|
||||
cmdXForm := &xmpp.AdHocXForm{Type: xmpp.TypeAdHocForm, Title: "Steam Account Info", Instructions: "Please provide your Steam login and password (Please, be aware that the given Steam account information will be saved into an un-encrypted SQLite database)."}
|
||||
|
||||
field := &xmpp.AdHocField{Var: "login", Label: "Steam Login", Type: xmpp.TypeAdHocFieldTextSingle}
|
||||
field.Value = steamLogin
|
||||
cmdXForm.Fields = append(cmdXForm.Fields, *field)
|
||||
field = &xmpp.AdHocField{Var: "password", Label: "Steam Password", Type: xmpp.TypeAdHocFieldTextPrivate}
|
||||
cmdXForm.Fields = append(cmdXForm.Fields, *field)
|
||||
|
||||
return cmdXForm
|
||||
}
|
||||
|
||||
func getUser(fields []xmpp.AdHocField, iq *xmpp.IQ) *database.DatabaseLine {
|
||||
// Command Auth Code
|
||||
steamLogin := ""
|
||||
steamPwd := ""
|
||||
for _, field := range fields {
|
||||
if field.Var == "login" {
|
||||
steamLogin = field.Value
|
||||
} else if field.Var == "password" {
|
||||
steamPwd = field.Value
|
||||
}
|
||||
}
|
||||
if steamLogin != "" {
|
||||
// Succeeded
|
||||
jidBareFrom := strings.SplitN(iq.From, "/", 2)[0]
|
||||
dbUser := new(database.DatabaseLine)
|
||||
dbUser.Jid = jidBareFrom
|
||||
dbUser.SteamLogin = steamLogin
|
||||
dbUser.SteamPwd = steamPwd
|
||||
dbUser.Debug = false
|
||||
|
||||
return dbUser
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
|
|
|||
231
xmpp/xmpp.go
231
xmpp/xmpp.go
|
|
@ -4,19 +4,17 @@ 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"
|
||||
"git.kingpenguin.tk/chteufleur/go-xmpp4steam.git/logger"
|
||||
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
ActionConnexion = "action_xmpp_connexion"
|
||||
ActionDeconnexion = "action_xmpp_deconnexion"
|
||||
ActionMainMethodEnded = "action_xmpp_main_method_ended"
|
||||
|
||||
LogInfo = "\t[XMPP COMPONENT INFO]\t"
|
||||
LogError = "\t[XMPP COMPONENT ERROR]\t"
|
||||
LogDebug = "\t[XMPP COMPONENT DEBUG]\t"
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
@ -35,21 +33,25 @@ var (
|
|||
Debug = true
|
||||
|
||||
MapGatewayInfo = make(map[string]*gateway.GatewayInfo)
|
||||
AdminUsers = make(map[string]bool)
|
||||
startTime = time.Now()
|
||||
)
|
||||
|
||||
func Run() {
|
||||
log.Printf("%sRunning", LogInfo)
|
||||
logger.Info.Printf("Running")
|
||||
// Create stream and configure it as a component connection.
|
||||
jid = must(xmpp.ParseJID(JidStr)).(xmpp.JID)
|
||||
stream = must(xmpp.NewStream(Addr, &xmpp.StreamConfig{LogStanzas: Debug})).(*xmpp.Stream)
|
||||
comp = must(xmpp.NewComponentXMPP(stream, jid, Secret)).(*xmpp.XMPP)
|
||||
|
||||
mainXMPP()
|
||||
log.Printf("%sReach main method's end", LogInfo)
|
||||
time.Sleep(1 * time.Second)
|
||||
logger.Info.Printf("Reach XMPP Run method's end")
|
||||
go Run()
|
||||
}
|
||||
|
||||
func mainXMPP() {
|
||||
defer logger.Info.Printf("Reach main method's end")
|
||||
// Define xmpp out for all users
|
||||
for _, u := range MapGatewayInfo {
|
||||
u.XMPP_Out = comp.Out
|
||||
|
|
@ -58,11 +60,15 @@ func mainXMPP() {
|
|||
for x := range comp.In {
|
||||
switch v := x.(type) {
|
||||
case *xmpp.Presence:
|
||||
jidBare := strings.SplitN(v.From, "/", 2)[0]
|
||||
g := MapGatewayInfo[jidBare]
|
||||
jidBareFrom := strings.SplitN(v.From, "/", 2)[0]
|
||||
jidBareTo := strings.SplitN(v.To, "/", 2)[0]
|
||||
g := MapGatewayInfo[jidBareFrom]
|
||||
if g != nil {
|
||||
log.Printf("%sPresence transfered to %s", LogDebug, jidBare)
|
||||
g.ReceivedXMPP_Presence(v)
|
||||
if jidBareTo == jid.Domain || v.Type == gateway.Type_probe {
|
||||
// Forward only if presence is for component or is type probe, in order not to spam set presence on Steam
|
||||
logger.Debug.Printf("Presence transferred to %s", jidBareFrom)
|
||||
go g.ReceivedXMPP_Presence(v)
|
||||
}
|
||||
} else {
|
||||
if v.Type != gateway.Type_error && v.Type != gateway.Type_probe {
|
||||
SendPresence(gateway.Status_offline, gateway.Type_unavailable, jid.Domain, v.From, "Your are not registred", "")
|
||||
|
|
@ -70,83 +76,157 @@ func mainXMPP() {
|
|||
}
|
||||
|
||||
case *xmpp.Message:
|
||||
jidBare := strings.SplitN(v.From, "/", 2)[0]
|
||||
g := MapGatewayInfo[jidBare]
|
||||
jidBareFrom := strings.SplitN(v.From, "/", 2)[0]
|
||||
g := MapGatewayInfo[jidBareFrom]
|
||||
if g != nil {
|
||||
log.Printf("%sMessage transfered to %s", LogDebug, jidBare)
|
||||
g.ReceivedXMPP_Message(v)
|
||||
logger.Debug.Printf("Message transferred to %s", jidBareFrom)
|
||||
go g.ReceivedXMPP_Message(v)
|
||||
} else {
|
||||
SendMessage(v.From, "", "Your are not registred. If you want to register, please, send an Ad-Hoc command.")
|
||||
}
|
||||
|
||||
case *xmpp.Iq:
|
||||
jidBare := strings.SplitN(v.To, "/", 2)[0]
|
||||
case *xmpp.IQ:
|
||||
jidBareFrom := strings.SplitN(v.From, "/", 2)[0]
|
||||
jidBareTo := strings.SplitN(v.To, "/", 2)[0]
|
||||
|
||||
switch v.PayloadName().Space {
|
||||
case xmpp.NSDiscoItems:
|
||||
if jidBare == jid.Domain {
|
||||
execDiscoCommand(v)
|
||||
} else {
|
||||
g := MapGatewayInfo[jidBareFrom]
|
||||
iqTreated := false
|
||||
if g != nil {
|
||||
logger.Debug.Printf("Iq transferred to %s", jidBareFrom)
|
||||
iqTreated = g.ReceivedXMPP_IQ(v)
|
||||
}
|
||||
|
||||
if !iqTreated {
|
||||
switch v.PayloadName().Space {
|
||||
case xmpp.NSDiscoInfo:
|
||||
execDisco(v)
|
||||
|
||||
case xmpp.NSDiscoItems:
|
||||
execDisco(v)
|
||||
|
||||
case xmpp.NodeAdHocCommand:
|
||||
if jidBareTo == jid.Domain {
|
||||
execCommandAdHoc(v)
|
||||
} else {
|
||||
sendNotSupportedFeature(v)
|
||||
}
|
||||
|
||||
case xmpp.NSVCardTemp:
|
||||
if jidBareTo == jid.Domain {
|
||||
reply := v.Response(xmpp.IQTypeResult)
|
||||
vcard := &xmpp.VCard{}
|
||||
reply.PayloadEncode(vcard)
|
||||
comp.Out <- reply
|
||||
} else {
|
||||
sendNotSupportedFeature(v)
|
||||
}
|
||||
|
||||
case xmpp.NSJabberClient:
|
||||
if jidBareTo == jid.Domain {
|
||||
reply := v.Response(xmpp.IQTypeResult)
|
||||
reply.PayloadEncode(&xmpp.SoftwareVersion{Name: "go-xmpp4steam", Version: SoftVersion})
|
||||
comp.Out <- reply
|
||||
} else {
|
||||
sendNotSupportedFeature(v)
|
||||
}
|
||||
|
||||
case xmpp.NSRegister:
|
||||
if jidBareTo == jid.Domain {
|
||||
treatmentNSRegister(v)
|
||||
} else {
|
||||
sendNotSupportedFeature(v)
|
||||
}
|
||||
|
||||
case xmpp.NSRoster:
|
||||
// Do nothing
|
||||
|
||||
case xmpp.NSPing:
|
||||
if jidBareTo == jid.Domain {
|
||||
treatmentNSPing(v)
|
||||
} else {
|
||||
sendNotSupportedFeature(v)
|
||||
}
|
||||
|
||||
default:
|
||||
sendNotSupportedFeature(v)
|
||||
}
|
||||
|
||||
case xmpp.NodeAdHocCommand:
|
||||
if jidBare == jid.Domain {
|
||||
execCommandAdHoc(v)
|
||||
} else {
|
||||
sendNotSupportedFeature(v)
|
||||
}
|
||||
|
||||
case xmpp.NSVCardTemp:
|
||||
if jidBare == jid.Domain {
|
||||
reply := v.Response(xmpp.IQTypeResult)
|
||||
vcard := &xmpp.VCard{}
|
||||
reply.PayloadEncode(vcard)
|
||||
comp.Out <- reply
|
||||
} else {
|
||||
sendNotSupportedFeature(v)
|
||||
}
|
||||
|
||||
case xmpp.NSJabberClient:
|
||||
if jidBare == jid.Domain {
|
||||
reply := v.Response(xmpp.IQTypeResult)
|
||||
reply.PayloadEncode(&xmpp.SoftwareVersion{Name: "go-xmpp4steam", Version: SoftVersion})
|
||||
comp.Out <- reply
|
||||
} else {
|
||||
sendNotSupportedFeature(v)
|
||||
}
|
||||
|
||||
default:
|
||||
sendNotSupportedFeature(v)
|
||||
}
|
||||
|
||||
default:
|
||||
log.Printf("%srecv: %v", LogDebug, x)
|
||||
logger.Debug.Printf("recv: %v", x)
|
||||
}
|
||||
}
|
||||
|
||||
// Send deconnexion
|
||||
SendPresence(gateway.Status_offline, gateway.Type_unavailable, "", "", "", "")
|
||||
}
|
||||
|
||||
func must(v interface{}, err error) interface{} {
|
||||
if err != nil {
|
||||
log.Fatal(LogError, err)
|
||||
logger.Debug.Printf("%v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func sendNotSupportedFeature(iq *xmpp.Iq) {
|
||||
reply := iq.Response(xmpp.IQTypeError)
|
||||
reply.PayloadEncode(xmpp.NewError("cancel", xmpp.FeatureNotImplemented, ""))
|
||||
func treatmentNSRegister(iq *xmpp.IQ) {
|
||||
reply := iq.Response(xmpp.IQTypeResult)
|
||||
jidBareFrom := strings.SplitN(iq.From, "/", 2)[0]
|
||||
registerQuery := &xmpp.RegisterQuery{}
|
||||
|
||||
if iq.Type == xmpp.IQTypeGet {
|
||||
registerQuery.Instructions = "Please provide your Steam login and password (Please, be aware that the given Steam account information will be saved into an un-encrypted SQLite database)."
|
||||
|
||||
dbUser := database.GetLine(jidBareFrom)
|
||||
if dbUser != nil {
|
||||
// User already registered
|
||||
registerQuery.Registered = &xmpp.RegisterRegistered{}
|
||||
registerQuery.Username = dbUser.SteamLogin
|
||||
registerQuery.XForm = *getXFormRegistration(dbUser.SteamLogin)
|
||||
} else {
|
||||
registerQuery.XForm = *getXFormRegistration("")
|
||||
}
|
||||
reply.PayloadEncode(registerQuery)
|
||||
|
||||
} else if iq.Type == xmpp.IQTypeSet {
|
||||
iq.PayloadDecode(registerQuery)
|
||||
|
||||
if registerQuery.Remove != nil {
|
||||
RemoveUser(jidBareFrom)
|
||||
} else {
|
||||
dbUser := getUser(registerQuery.XForm.Fields, iq)
|
||||
if dbUser != nil {
|
||||
if dbUser.UpdateUser() {
|
||||
AddNewUser(dbUser.Jid, dbUser.SteamLogin, dbUser.SteamPwd, dbUser.Debug)
|
||||
} else {
|
||||
reply.Type = xmpp.IQTypeError
|
||||
reply.Error = xmpp.NewErrorWithCode("406", "modify", xmpp.ErrorNotAcceptable, "")
|
||||
}
|
||||
} else {
|
||||
reply.Type = xmpp.IQTypeError
|
||||
reply.Error = xmpp.NewErrorWithCode("409", "cancel", xmpp.ErrorConflict, "")
|
||||
}
|
||||
}
|
||||
}
|
||||
comp.Out <- reply
|
||||
}
|
||||
|
||||
func treatmentNSPing(iq *xmpp.IQ) {
|
||||
reply := iq.Response(xmpp.IQTypeResult)
|
||||
comp.Out <- reply
|
||||
}
|
||||
|
||||
func sendNotSupportedFeature(iq *xmpp.IQ) {
|
||||
if iq.Type != xmpp.IQTypeError && iq.Type != xmpp.IQTypeResult {
|
||||
reply := iq.Response(xmpp.IQTypeError)
|
||||
reply.PayloadEncode(xmpp.NewError("cancel", xmpp.ErrorFeatureNotImplemented, ""))
|
||||
comp.Out <- reply
|
||||
}
|
||||
}
|
||||
|
||||
func Disconnect() {
|
||||
log.Printf("%sXMPP disconnect", LogInfo)
|
||||
logger.Info.Printf("XMPP disconnect")
|
||||
for _, u := range MapGatewayInfo {
|
||||
u.SteamDisconnect()
|
||||
}
|
||||
comp.Close()
|
||||
}
|
||||
|
||||
func SendPresence(status, tpye, from, to, message, nick string) {
|
||||
|
|
@ -177,33 +257,46 @@ func SendPresence(status, tpye, from, to, message, nick string) {
|
|||
}
|
||||
|
||||
func SendMessage(to, subject, message string) {
|
||||
m := xmpp.Message{From: jid.Domain, To: to, Body: message, Type: "chat"}
|
||||
m := xmpp.Message{From: jid.Domain, To: to, Type: "chat"}
|
||||
mBody := xmpp.MessageBody{Value: message}
|
||||
m.Body = append(m.Body, mBody)
|
||||
|
||||
if subject != "" {
|
||||
m.Subject = subject
|
||||
}
|
||||
|
||||
log.Printf("%sSenp message %v", LogInfo, m)
|
||||
logger.Info.Printf("Senp message %v", m)
|
||||
comp.Out <- m
|
||||
}
|
||||
|
||||
func AddNewUser(jid, steamLogin, steamPwd string, debugMessage bool) {
|
||||
log.Printf("%sAdd user %s to the map", LogInfo, jid)
|
||||
func AddNewUser(jidUser, steamLogin, steamPwd string, debugMessage bool) {
|
||||
logger.Info.Printf("Add user %s to the map (debug mode set to %v)", jidUser, debugMessage)
|
||||
|
||||
g := new(gateway.GatewayInfo)
|
||||
g.SteamLogin = steamLogin
|
||||
g.SteamPassword = steamPwd
|
||||
g.XMPP_JID_Client = jid
|
||||
g.SentryFile = gateway.SentryDirectory + jid
|
||||
g.FriendSteamId = make(map[string]*gateway.StatusSteamFriend)
|
||||
g.XMPP_JID_Client = jidUser
|
||||
g.SentryFile = gateway.SentryDirectory + jidUser
|
||||
g.CreateSteamIds()
|
||||
g.Deleting = false
|
||||
|
||||
g.XMPP_Out = comp.Out
|
||||
g.XMPP_Connected_Client = make(map[string]bool)
|
||||
g.CreateXmppConnectedClient()
|
||||
g.DebugMessage = debugMessage
|
||||
g.CreateXmppRemoteRosterRequest()
|
||||
g.AllowEditRoster = false
|
||||
|
||||
MapGatewayInfo[jid] = g
|
||||
MapGatewayInfo[jidUser] = g
|
||||
go g.Run()
|
||||
|
||||
logger.Info.Printf("Check roster edition by asking with remote roster manager namespace")
|
||||
// Ask if remote roster is allow
|
||||
iqId := gateway.NextIqId()
|
||||
g.SetXmppRemoteRosterRequest(iqId, gateway.RemoteRosterRequestPermission)
|
||||
iq := xmpp.IQ{To: jidUser, From: jid.Domain, Type: xmpp.IQTypeGet, ID: iqId}
|
||||
// iq.PayloadEncode(&xmpp.RosterQuery{})
|
||||
iq.PayloadEncode(&xmpp.RemoteRosterManagerQuery{Reason: "Manage contacts in the Steam contact list", Type: xmpp.RemoteRosterManagerTypeRequest})
|
||||
comp.Out <- iq
|
||||
}
|
||||
|
||||
func RemoveUser(jidBare string) bool {
|
||||
|
|
|
|||
Loading…
Reference in New Issue