From 950dfc43dc904289ff4cb53ff1341c76fd2cb99d Mon Sep 17 00:00:00 2001 From: Chteufleur Date: Sun, 5 Jun 2016 20:30:20 +0200 Subject: [PATCH] Make full implementation Iq stanza --- httpAuth.cfg | 2 +- xmpp/client.go | 21 ++++++++++++++++----- xmpp/xmpp.go | 51 ++++++++++++++++++++++++++++---------------------- 3 files changed, 46 insertions(+), 28 deletions(-) diff --git a/httpAuth.cfg b/httpAuth.cfg index fc21647..72ffb10 100644 --- a/httpAuth.cfg +++ b/httpAuth.cfg @@ -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 diff --git a/xmpp/client.go b/xmpp/client.go index 61bd1a8..9fa698f 100644 --- a/xmpp/client.go +++ b/xmpp/client.go @@ -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 } diff --git a/xmpp/xmpp.go b/xmpp/xmpp.go index d0f3c80..34cc08a 100644 --- a/xmpp/xmpp.go +++ b/xmpp/xmpp.go @@ -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)