Handle disco

This commit is contained in:
Chteufleur 2016-09-13 18:44:42 +02:00
parent d32b143bb3
commit db6d7326e6
2 changed files with 58 additions and 46 deletions

View File

@ -17,27 +17,24 @@ const (
)
var (
ChanAuthCode = make(chan string)
identity = &xmpp.DiscoIdentity{Category: "gateway", Type: "steam", Name: "Steam Gateway"}
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)
log.Printf("%sAd-Hoc Command", LogInfo)
discoInfo := &xmpp.DiscoItems{}
iq.PayloadDecode(discoInfo)
if discoInfo.Node == "" {
// Disco feature
execDisco(iq)
return
} else if discoInfo.Node == xmpp.NodeAdHocCommand {
// Disco Ad-Hoc
reply := iq.Response(xmpp.IQTypeResult)
discoItem := &xmpp.DiscoItems{Node: xmpp.NodeAdHocCommand}
// 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)
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"}
@ -53,23 +50,48 @@ func execDiscoCommand(iq *xmpp.Iq) {
discoI = &xmpp.DiscoItem{JID: jid.Domain, Node: CommandToggleDebugMode, Name: "Toggle debug mode"}
discoItem.Item = append(discoItem.Item, *discoI)
}
reply.PayloadEncode(discoItem)
comp.Out <- reply
}
reply.PayloadEncode(discoItem)
comp.Out <- reply
}
func execDisco(iq *xmpp.Iq) {
log.Printf("%sDisco Feature", LogInfo)
reply := iq.Response(xmpp.IQTypeResult)
jidBareTo := strings.SplitN(iq.To, "/", 2)[0]
discoInfo := &xmpp.DiscoInfo{}
discoInfo.Identity = append(discoInfo.Identity, *identity)
discoInfo.Feature = append(discoInfo.Feature, xmpp.DiscoFeature{Var: xmpp.NSJabberClient})
discoInfo.Feature = append(discoInfo.Feature, xmpp.DiscoFeature{Var: xmpp.NodeAdHocCommand})
discoInfoReceived := &xmpp.DiscoItems{}
iq.PayloadDecode(discoInfoReceived)
reply.PayloadEncode(discoInfo)
comp.Out <- reply
switch iq.PayloadName().Space {
case xmpp.NSDiscoInfo:
reply := iq.Response(xmpp.IQTypeResult)
discoInfo := &xmpp.DiscoInfo{}
if jidBareTo == jid.Domain {
discoInfo.Identity = append(discoInfo.Identity, *identityGateway)
discoInfo.Feature = append(discoInfo.Feature, xmpp.DiscoFeature{Var: xmpp.NodeAdHocCommand})
} else {
discoInfo.Identity = append(discoInfo.Identity, *identityClients)
discoInfo.Feature = append(discoInfo.Feature, xmpp.DiscoFeature{Var: xmpp.NSChatStatesNotification})
}
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) {

View File

@ -59,10 +59,10 @@ 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]
g := MapGatewayInfo[jidBareFrom]
if g != nil {
log.Printf("%sPresence transfered to %s", LogDebug, jidBare)
log.Printf("%sPresence transfered to %s", LogDebug, jidBareFrom)
g.ReceivedXMPP_Presence(v)
} else {
if v.Type != gateway.Type_error && v.Type != gateway.Type_probe {
@ -71,37 +71,27 @@ 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)
log.Printf("%sMessage transfered to %s", LogDebug, jidBareFrom)
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]
jidBareTo := strings.SplitN(v.To, "/", 2)[0]
switch v.PayloadName().Space {
case xmpp.NSDiscoItems:
if jidBare == jid.Domain {
execDiscoCommand(v)
} else {
sendNotSupportedFeature(v)
}
case xmpp.NSDiscoInfo:
execDisco(v)
case xmpp.NodeAdHocCommand:
if jidBare == jid.Domain {
execCommandAdHoc(v)
} else {
sendNotSupportedFeature(v)
}
case xmpp.NSDiscoItems:
execDisco(v)
case xmpp.NSVCardTemp:
if jidBare == jid.Domain {
if jidBareTo == jid.Domain {
reply := v.Response(xmpp.IQTypeResult)
vcard := &xmpp.VCard{}
reply.PayloadEncode(vcard)
@ -111,7 +101,7 @@ func mainXMPP() {
}
case xmpp.NSJabberClient:
if jidBare == jid.Domain {
if jidBareTo == jid.Domain {
reply := v.Response(xmpp.IQTypeResult)
reply.PayloadEncode(&xmpp.SoftwareVersion{Name: "go-xmpp4steam", Version: SoftVersion})
comp.Out <- reply