go-sendxmpp/stanzahandling.go

134 lines
3.6 KiB
Go
Raw Normal View History

2023-05-11 18:05:31 +00:00
// Copyright Martin Dosch.
2022-07-26 20:28:04 +00:00
// Use of this source code is governed by the BSD-2-clause
// license that can be found in the LICENSE file.
package main
import (
2023-06-07 20:28:01 +00:00
"errors"
"fmt"
2022-07-26 20:28:04 +00:00
"log"
"github.com/beevik/etree" // BSD-2-clause
"github.com/mattn/go-xmpp" // BSD-3-Clause
)
2023-06-07 20:33:43 +00:00
func sendIQ(client *xmpp.Client, iqc chan xmpp.IQ, target string, iQtype string, content string) (xmpp.IQ, error) {
2022-07-26 20:28:04 +00:00
var iq xmpp.IQ
id := getID()
c := make(chan xmpp.IQ)
2023-06-04 14:18:02 +00:00
go getIQ(id, c, iqc)
2022-07-26 20:28:04 +00:00
_, err := client.RawInformation(client.JID(), target, id,
iQtype, content+"\n")
2023-06-07 20:28:01 +00:00
if errors.Unwrap(err) != nil {
return iq, fmt.Errorf("sendIQ: failed to send iq: %w", err)
2022-07-26 20:28:04 +00:00
}
iq = <-c
return iq, nil
}
2023-06-07 20:33:43 +00:00
func getIQ(id string, c chan xmpp.IQ, iqc chan xmpp.IQ) {
2022-07-26 20:28:04 +00:00
for {
iq := <-iqc
if iq.ID == id {
c <- iq
return
}
}
}
func rcvStanzas(client *xmpp.Client, iqc chan xmpp.IQ, msgc chan xmpp.Chat) {
for {
received, err := client.Recv()
2023-06-07 20:28:01 +00:00
if errors.Unwrap(err) != nil {
2022-07-26 20:28:04 +00:00
log.Println(err)
}
switch v := received.(type) {
case xmpp.Chat:
msgc <- v
case xmpp.IQ:
switch v.Type {
case "get":
2022-09-01 20:13:13 +00:00
var xmlns *etree.Attr
2022-07-26 20:28:04 +00:00
iq := etree.NewDocument()
err = iq.ReadFromBytes(v.Query)
2023-06-07 20:28:01 +00:00
if errors.Unwrap(err) != nil {
2022-07-26 20:28:04 +00:00
log.Println("Couldn't parse IQ:",
string(v.Query), err)
}
query := iq.SelectElement("query")
2022-09-01 20:13:13 +00:00
if query != nil {
xmlns = query.SelectAttr("xmlns")
}
2022-07-26 20:28:04 +00:00
switch xmlns.Value {
case nsDiscoInfo:
root := etree.NewDocument()
root.WriteSettings.AttrSingleQuote = true
2022-07-26 20:28:04 +00:00
reply := root.CreateElement("iq")
reply.CreateAttr("type", "result")
reply.CreateAttr("from", client.JID())
reply.CreateAttr("to", v.From)
reply.CreateAttr("id", v.ID)
replyQuery := reply.CreateElement("query")
replyQuery.CreateAttr("xmlns", nsDiscoInfo)
identity := replyQuery.CreateElement("identity")
identity.CreateAttr("category", "client")
identity.CreateAttr("type", "bot")
identity.CreateAttr("name", "go-sendxmpp")
feat := replyQuery.CreateElement("feature")
feat.CreateAttr("var", nsDiscoInfo)
xmlString, err := root.WriteToString()
2023-06-07 20:28:01 +00:00
if errors.Unwrap(err) == nil {
2022-07-26 20:28:04 +00:00
_, err = client.SendOrg(xmlString)
2023-06-07 20:28:01 +00:00
if errors.Unwrap(err) != nil {
2022-07-26 20:28:04 +00:00
log.Println(err)
}
}
default:
root := etree.NewDocument()
root.WriteSettings.AttrSingleQuote = true
2022-07-26 20:28:04 +00:00
reply := root.CreateElement("iq")
reply.CreateAttr("type", strError)
2022-07-26 20:28:04 +00:00
reply.CreateAttr("from", client.JID())
reply.CreateAttr("to", v.From)
reply.CreateAttr("id", v.ID)
errorReply := reply.CreateElement(strError)
2022-07-26 20:28:04 +00:00
errorReply.CreateAttr("type", "cancel")
su := errorReply.CreateElement("service-unavailable")
su.CreateAttr("xmlns", nsXMPPStanzas)
xmlString, err := root.WriteToString()
2023-06-07 20:28:01 +00:00
if errors.Unwrap(err) == nil {
2022-07-26 20:28:04 +00:00
_, err = client.SendOrg(xmlString)
2023-06-07 20:28:01 +00:00
if errors.Unwrap(err) != nil {
2022-07-26 20:28:04 +00:00
log.Println(err)
}
}
}
case "set":
root := etree.NewDocument()
root.WriteSettings.AttrSingleQuote = true
2022-07-26 20:28:04 +00:00
reply := root.CreateElement("iq")
reply.CreateAttr("type", strError)
2022-07-26 20:28:04 +00:00
reply.CreateAttr("from", client.JID())
reply.CreateAttr("to", v.From)
reply.CreateAttr("id", v.ID)
errorReply := reply.CreateElement(strError)
2022-07-26 20:28:04 +00:00
errorReply.CreateAttr("type", "cancel")
su := errorReply.CreateElement("service-unavailable")
su.CreateAttr("xmlns", nsXMPPStanzas)
xmlString, err := root.WriteToString()
2023-06-07 20:28:01 +00:00
if errors.Unwrap(err) == nil {
2022-07-26 20:28:04 +00:00
_, err = client.SendOrg(xmlString)
2023-06-07 20:28:01 +00:00
if errors.Unwrap(err) != nil {
2022-07-26 20:28:04 +00:00
log.Println(err)
}
}
case "reply", strError:
2022-07-26 20:28:04 +00:00
iqc <- v
default:
iqc <- v
}
}
}
}