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 ( var (
ChanAuthCode = make(chan string) ChanAuthCode = make(chan string)
identity = &xmpp.DiscoIdentity{Category: "gateway", Type: "steam", Name: "Steam Gateway"} 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) { func execDiscoCommand(iq *xmpp.Iq) {
log.Printf("%sDiscovery item iq received", LogInfo) log.Printf("%sAd-Hoc Command", LogInfo)
discoInfo := &xmpp.DiscoItems{} // Disco Ad-Hoc
iq.PayloadDecode(discoInfo) reply := iq.Response(xmpp.IQTypeResult)
if discoInfo.Node == "" { discoItem := &xmpp.DiscoItems{Node: xmpp.NodeAdHocCommand}
// 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}
jidBare := strings.SplitN(iq.From, "/", 2)[0] jidBareFrom := strings.SplitN(iq.From, "/", 2)[0]
dbUser := database.GetLine(jidBare) jidBareTo := strings.SplitN(iq.To, "/", 2)[0]
dbUser := database.GetLine(jidBareFrom)
if jidBareTo == jid.Domain {
// Ad-Hoc command only on gateway
// Add available commands // Add available commands
if dbUser == nil { if dbUser == nil {
discoI := &xmpp.DiscoItem{JID: jid.Domain, Node: CommandGetIdentifiants, Name: "Steam registration"} 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"} discoI = &xmpp.DiscoItem{JID: jid.Domain, Node: CommandToggleDebugMode, Name: "Toggle debug mode"}
discoItem.Item = append(discoItem.Item, *discoI) discoItem.Item = append(discoItem.Item, *discoI)
} }
reply.PayloadEncode(discoItem)
comp.Out <- reply
} }
reply.PayloadEncode(discoItem)
comp.Out <- reply
} }
func execDisco(iq *xmpp.Iq) { func execDisco(iq *xmpp.Iq) {
log.Printf("%sDisco Feature", LogInfo) log.Printf("%sDisco Feature", LogInfo)
reply := iq.Response(xmpp.IQTypeResult) jidBareTo := strings.SplitN(iq.To, "/", 2)[0]
discoInfo := &xmpp.DiscoInfo{} discoInfoReceived := &xmpp.DiscoItems{}
discoInfo.Identity = append(discoInfo.Identity, *identity) iq.PayloadDecode(discoInfoReceived)
discoInfo.Feature = append(discoInfo.Feature, xmpp.DiscoFeature{Var: xmpp.NSJabberClient})
discoInfo.Feature = append(discoInfo.Feature, xmpp.DiscoFeature{Var: xmpp.NodeAdHocCommand})
reply.PayloadEncode(discoInfo) switch iq.PayloadName().Space {
comp.Out <- reply 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) { func execCommandAdHoc(iq *xmpp.Iq) {

View File

@ -59,10 +59,10 @@ func mainXMPP() {
for x := range comp.In { for x := range comp.In {
switch v := x.(type) { switch v := x.(type) {
case *xmpp.Presence: case *xmpp.Presence:
jidBare := strings.SplitN(v.From, "/", 2)[0] jidBareFrom := strings.SplitN(v.From, "/", 2)[0]
g := MapGatewayInfo[jidBare] g := MapGatewayInfo[jidBareFrom]
if g != nil { if g != nil {
log.Printf("%sPresence transfered to %s", LogDebug, jidBare) log.Printf("%sPresence transfered to %s", LogDebug, jidBareFrom)
g.ReceivedXMPP_Presence(v) g.ReceivedXMPP_Presence(v)
} else { } else {
if v.Type != gateway.Type_error && v.Type != gateway.Type_probe { if v.Type != gateway.Type_error && v.Type != gateway.Type_probe {
@ -71,37 +71,27 @@ func mainXMPP() {
} }
case *xmpp.Message: case *xmpp.Message:
jidBare := strings.SplitN(v.From, "/", 2)[0] jidBareFrom := strings.SplitN(v.From, "/", 2)[0]
g := MapGatewayInfo[jidBare] g := MapGatewayInfo[jidBareFrom]
if g != nil { if g != nil {
log.Printf("%sMessage transfered to %s", LogDebug, jidBare) log.Printf("%sMessage transfered to %s", LogDebug, jidBareFrom)
g.ReceivedXMPP_Message(v) g.ReceivedXMPP_Message(v)
} else { } else {
SendMessage(v.From, "", "Your are not registred. If you want to register, please, send an Ad-Hoc command.") SendMessage(v.From, "", "Your are not registred. If you want to register, please, send an Ad-Hoc command.")
} }
case *xmpp.Iq: case *xmpp.Iq:
jidBare := strings.SplitN(v.To, "/", 2)[0] jidBareTo := strings.SplitN(v.To, "/", 2)[0]
switch v.PayloadName().Space { switch v.PayloadName().Space {
case xmpp.NSDiscoItems:
if jidBare == jid.Domain {
execDiscoCommand(v)
} else {
sendNotSupportedFeature(v)
}
case xmpp.NSDiscoInfo: case xmpp.NSDiscoInfo:
execDisco(v) execDisco(v)
case xmpp.NodeAdHocCommand: case xmpp.NSDiscoItems:
if jidBare == jid.Domain { execDisco(v)
execCommandAdHoc(v)
} else {
sendNotSupportedFeature(v)
}
case xmpp.NSVCardTemp: case xmpp.NSVCardTemp:
if jidBare == jid.Domain { if jidBareTo == jid.Domain {
reply := v.Response(xmpp.IQTypeResult) reply := v.Response(xmpp.IQTypeResult)
vcard := &xmpp.VCard{} vcard := &xmpp.VCard{}
reply.PayloadEncode(vcard) reply.PayloadEncode(vcard)
@ -111,7 +101,7 @@ func mainXMPP() {
} }
case xmpp.NSJabberClient: case xmpp.NSJabberClient:
if jidBare == jid.Domain { if jidBareTo == jid.Domain {
reply := v.Response(xmpp.IQTypeResult) reply := v.Response(xmpp.IQTypeResult)
reply.PayloadEncode(&xmpp.SoftwareVersion{Name: "go-xmpp4steam", Version: SoftVersion}) reply.PayloadEncode(&xmpp.SoftwareVersion{Name: "go-xmpp4steam", Version: SoftVersion})
comp.Out <- reply comp.Out <- reply