go-sendxmpp/stanzahandling.go

198 lines
5.0 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 (
"context"
2024-02-27 19:54:09 +00:00
"errors"
2023-06-07 20:28:01 +00:00
"fmt"
"io"
2022-07-26 20:28:04 +00:00
"log"
"runtime"
2024-02-27 19:54:09 +00:00
"time"
2022-07-26 20:28:04 +00:00
2023-11-25 18:52:55 +00:00
"github.com/beevik/etree" // BSD-2-clause
2024-01-10 15:21:39 +00:00
"github.com/xmppo/go-xmpp" // BSD-3-Clause
2022-07-26 20:28:04 +00:00
)
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()
2024-04-12 10:57:08 +00:00
c := make(chan xmpp.IQ, defaultBufferSize)
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,
2023-08-14 08:18:39 +00:00
iQtype, content)
if err != nil {
2023-06-07 20:28:01 +00:00
return iq, fmt.Errorf("sendIQ: failed to send iq: %w", err)
2022-07-26 20:28:04 +00:00
}
2024-02-27 19:54:09 +00:00
select {
case iq = <-c:
case <-time.After(60 * time.Second):
return iq, errors.New("sendIQ: server didn't reply to IQ: " + content)
}
2022-07-26 20:28:04 +00:00
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, ctx context.Context, iqc chan xmpp.IQ, msgc chan xmpp.Chat) {
var received interface{}
2024-04-12 10:57:08 +00:00
r := make(chan interface{}, defaultBufferSize)
e := make(chan error, defaultBufferSize)
go func() {
for {
2024-04-12 10:57:08 +00:00
select {
case <-ctx.Done():
return
default:
}
rcv, err := client.Recv()
if err != nil {
e <- err
} else {
r <- rcv
}
}
}()
for {
select {
case <-ctx.Done():
return
2024-04-12 10:57:08 +00:00
case err := <-e:
if err != nil {
if err != io.EOF {
closeAndExit(client, err)
return
}
return
}
case received = <-r:
2022-07-26 20:28:04 +00:00
}
switch v := received.(type) {
case xmpp.Chat:
2024-04-12 10:57:08 +00:00
2022-07-26 20:28:04 +00:00
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()
2024-04-12 10:57:08 +00:00
err := iq.ReadFromBytes(v.Query)
if 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")
}
2024-02-25 17:34:33 +00:00
if xmlns == nil {
break
}
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)
feat2 := replyQuery.CreateElement("feature")
feat2.CreateAttr("var", nsVersion)
xmlString, err := root.WriteToString()
if err == nil {
_, err = client.SendOrg(xmlString)
if err != nil {
log.Println(err)
}
}
case nsVersion:
root := etree.NewDocument()
root.WriteSettings.AttrSingleQuote = true
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", nsVersion)
rqName := replyQuery.CreateElement("name")
rqName.CreateText("go-sendxmpp")
rqVersion := replyQuery.CreateElement("version")
rqVersion.CreateText(version)
rqOS := replyQuery.CreateElement("os")
rqOS.CreateText(runtime.GOOS)
2022-07-26 20:28:04 +00:00
xmlString, err := root.WriteToString()
if err == nil {
2022-07-26 20:28:04 +00:00
_, err = client.SendOrg(xmlString)
if 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()
if err == nil {
2022-07-26 20:28:04 +00:00
_, err = client.SendOrg(xmlString)
if 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()
if err == nil {
2022-07-26 20:28:04 +00:00
_, err = client.SendOrg(xmlString)
if 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
}
}
}
}