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-08-14 07:34:39 +00:00
|
|
|
"context"
|
2024-02-27 19:54:09 +00:00
|
|
|
"errors"
|
2023-06-07 20:28:01 +00:00
|
|
|
"fmt"
|
2022-07-26 20:28:04 +00:00
|
|
|
"log"
|
2023-10-29 09:20:44 +00:00
|
|
|
"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()
|
|
|
|
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,
|
2023-08-14 08:18:39 +00:00
|
|
|
iQtype, content)
|
2023-08-16 06:49:43 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-20 17:21:51 +00:00
|
|
|
func rcvStanzas(client *xmpp.Client, iqc chan xmpp.IQ, msgc chan xmpp.Chat, ctx context.Context, cancel context.CancelFunc) {
|
2022-07-26 20:28:04 +00:00
|
|
|
for {
|
|
|
|
received, err := client.Recv()
|
2023-08-14 07:34:39 +00:00
|
|
|
// Don't print errors if the program is getting shut down,
|
|
|
|
// as the errors might be triggered from trying to read from
|
|
|
|
// a closed connection.
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
2023-08-16 06:49:43 +00:00
|
|
|
if err != nil {
|
2024-02-20 17:21:51 +00:00
|
|
|
closeAndExit(client, cancel, err)
|
2023-08-14 07:34:39 +00:00
|
|
|
}
|
2022-07-26 20:28:04 +00:00
|
|
|
}
|
|
|
|
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-08-16 06:49:43 +00:00
|
|
|
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()
|
2023-05-09 19:23:29 +00:00
|
|
|
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)
|
2023-10-29 09:20:44 +00:00
|
|
|
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()
|
2023-08-16 06:49:43 +00:00
|
|
|
if err == nil {
|
2022-07-26 20:28:04 +00:00
|
|
|
_, err = client.SendOrg(xmlString)
|
2023-08-16 06:49:43 +00:00
|
|
|
if err != nil {
|
2022-07-26 20:28:04 +00:00
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
root := etree.NewDocument()
|
2023-05-09 19:23:29 +00:00
|
|
|
root.WriteSettings.AttrSingleQuote = true
|
2022-07-26 20:28:04 +00:00
|
|
|
reply := root.CreateElement("iq")
|
2023-06-04 13:55:31 +00:00
|
|
|
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)
|
2023-06-04 13:55:31 +00:00
|
|
|
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-08-16 06:49:43 +00:00
|
|
|
if err == nil {
|
2022-07-26 20:28:04 +00:00
|
|
|
_, err = client.SendOrg(xmlString)
|
2023-08-16 06:49:43 +00:00
|
|
|
if err != nil {
|
2022-07-26 20:28:04 +00:00
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "set":
|
|
|
|
root := etree.NewDocument()
|
2023-05-09 19:23:29 +00:00
|
|
|
root.WriteSettings.AttrSingleQuote = true
|
2022-07-26 20:28:04 +00:00
|
|
|
reply := root.CreateElement("iq")
|
2023-06-04 13:55:31 +00:00
|
|
|
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)
|
2023-06-04 13:55:31 +00:00
|
|
|
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-08-16 06:49:43 +00:00
|
|
|
if err == nil {
|
2022-07-26 20:28:04 +00:00
|
|
|
_, err = client.SendOrg(xmlString)
|
2023-08-16 06:49:43 +00:00
|
|
|
if err != nil {
|
2022-07-26 20:28:04 +00:00
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
2023-06-04 13:55:31 +00:00
|
|
|
case "reply", strError:
|
2022-07-26 20:28:04 +00:00
|
|
|
iqc <- v
|
|
|
|
default:
|
|
|
|
iqc <- v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|