Make full implementation Iq stanza

This commit is contained in:
Chteufleur 2016-06-05 20:30:20 +02:00
parent 0a737a4f01
commit 950dfc43dc
3 changed files with 46 additions and 28 deletions

View File

@ -3,4 +3,4 @@ xmpp_server_address=192.168.1.2
xmpp_server_port=5347
xmpp_hostname=xmppsteam.kingpenguin.tk
xmpp_secret=xmpp4steam_password
xmpp_debug=false
xmpp_debug=true

View File

@ -4,6 +4,7 @@ import (
"git.kingpenguin.tk/chteufleur/go-xmpp.git/src/xmpp"
"log"
"strconv"
)
type Client struct {
@ -18,11 +19,22 @@ type Client struct {
func (client *Client) QueryClient() {
log.Printf("%sQuery JID %s", LogInfo, client.JID)
client.askViaMessage()
clientJID, _ := xmpp.ParseJID(client.JID)
if clientJID.Resource == "" {
client.askViaMessage()
} else {
client.askViaIQ()
}
}
func (client *Client) askViaIQ() {
stanzaID++
stanzaIDstr := strconv.Itoa(stanzaID)
m := xmpp.Iq{Type: xmpp.IQTypeGet, To: client.JID, From: jid.Domain, Id: stanzaIDstr}
confirm := &xmpp.Confirm{Id: client.Transaction, Method: client.Method, URL: client.Domain}
m.PayloadEncode(confirm)
WaitMessageAnswers[stanzaIDstr] = client
comp.Out <- m
}
func (client *Client) askViaMessage() {
@ -30,10 +42,9 @@ func (client *Client) askViaMessage() {
m.Thread = xmpp.SessionID()
m.Body = "Auth request for "+client.Domain+".\nTransaction identifier is: "+client.Transaction+"\nReply to this message to confirm the request."
m.Confir = &xmpp.Confirm{ID: client.Transaction, Method: client.Method, URL: client.Domain}
m.Confir = &xmpp.Confirm{Id: client.Transaction, Method: client.Method, URL: client.Domain}
log.Printf("%sSenp message %v", LogInfo, m)
comp.Out <- m
WaitMessageAnswers[client.Transaction] = client
comp.Out <- m
}

View File

@ -19,6 +19,8 @@ var (
SoftVersion = ""
stanzaID = 0
jid xmpp.JID
stream = new(xmpp.Stream)
comp = new(xmpp.XMPP)
@ -26,7 +28,6 @@ var (
ChanAction = make(chan string)
WaitMessageAnswers = make(map[string]*Client)
waitIQAnswers = make(map[string]*Client)
Debug = true
)
@ -51,14 +52,11 @@ func mainXMPP() {
case *xmpp.Presence:
case *xmpp.Message:
client := WaitMessageAnswers[v.Confir.ID]
if client != nil {
if v.Error != nil {
client.ChanReply <- false
} else {
client.ChanReply <- true
}
delete(WaitMessageAnswers, v.Confir.ID)
confirm := v.Confir
if confirm != nil {
client := WaitMessageAnswers[confirm.Id]
delete(WaitMessageAnswers, confirm.Id)
processConfirm(v, client)
}
case *xmpp.Iq:
@ -77,6 +75,13 @@ func mainXMPP() {
reply.PayloadEncode(&xmpp.SoftwareVersion{Name: "HTTP authentification component", Version: SoftVersion})
comp.Out <- reply
case xmpp.NSHTTPAuth:
confirm := &xmpp.Confirm{}
v.PayloadDecode(confirm)
client := WaitMessageAnswers[v.Id]
delete(WaitMessageAnswers, v.Id)
processConfirm(v, client)
default:
reply := v.Response(xmpp.IQTypeError)
reply.PayloadEncode(xmpp.NewError("cancel", xmpp.FeatureNotImplemented, ""))
@ -89,6 +94,21 @@ func mainXMPP() {
}
}
func processConfirm(x interface{}, client *Client) {
mes, mesOK := x.(*xmpp.Message)
iq, iqOK := x.(*xmpp.Iq)
if client != nil {
if mesOK && mes.Error != nil {
client.ChanReply <- false
} else if iqOK && iq.Error != nil {
client.ChanReply <- false
} else {
client.ChanReply <- true
}
}
}
func must(v interface{}, err error) interface{} {
if err != nil {
log.Fatal(LogError, err)
@ -96,19 +116,6 @@ func must(v interface{}, err error) interface{} {
return v
}
/*
func SendMessage(to, subject, message string) {
m := xmpp.Message{From: jid.Domain, To: to, Body: message, Type: "chat"}
if subject != "" {
m.Subject = subject
}
log.Printf("%sSenp message %v", LogInfo, m)
comp.Out <- m
}
*/
func execDiscoCommand(iq *xmpp.Iq) {
log.Printf("%sDiscovery item iq received", LogInfo)
reply := iq.Response(xmpp.IQTypeResult)