1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
// Copyright Martin Dosch.
// Use of this source code is governed by the BSD-2-clause
// license that can be found in the LICENSE file.
// TODO: Add logging.
package main
import (
"context"
"encoding/xml"
"fmt"
"io"
"log"
"log/slog"
"runtime"
"strings"
"time"
"github.com/beevik/etree" // BSD-2-clause
"github.com/xmppo/go-xmpp" // BSD-3-Clause
)
func getDiscoInfo(client *xmpp.Client, drc chan xmpp.DiscoResult, target string) (dr xmpp.DiscoResult, err error) {
id := getID()
c := make(chan xmpp.DiscoResult, defaultBufferSize)
go getDr(target, c, drc)
slog.Info("stanzahandling: requesting disco info")
_, err = client.RawInformation(client.JID(), target, id, "get",
fmt.Sprintf("<query xmlns='%s'/>", nsDiscoInfo))
if err != nil {
return
}
select {
case dr = <-c:
slog.Info("stanzahandling: requesting disco info: received reply")
case <-time.After(60 * time.Second):
slog.Info("stanzahandling: requesting disco info: timeout")
return dr, fmt.Errorf("get disco info: %s didn't reply to disco info request", target)
}
return
}
func getDr(jid string, c chan xmpp.DiscoResult, drc chan xmpp.DiscoResult) {
for {
dr := <-drc
c <- dr
return
}
}
func getDiscoItems(client *xmpp.Client, disc chan xmpp.DiscoItems, target string) (dis xmpp.DiscoItems, err error) {
id := getID()
c := make(chan xmpp.DiscoItems, defaultBufferSize)
go getDis(target, c, disc)
slog.Info("stanzahandling: requesting disco items")
_, err = client.RawInformation(client.JID(), target, id, "get",
fmt.Sprintf("<query xmlns='%s'/>", nsDiscoItems))
if err != nil {
return
}
select {
case dis = <-c:
slog.Info("stanzahandling: requesting disco items: received reply")
case <-time.After(60 * time.Second):
slog.Info("stanzahandling: requesting disco items: timeout")
return dis, fmt.Errorf("get disco items: %s didn't reply to disco items request", target)
}
return
}
func getDis(jid string, c chan xmpp.DiscoItems, disc chan xmpp.DiscoItems) {
for {
dis := <-disc
if dis.Jid == jid {
c <- dis
return
}
}
}
func sendIQ(client *xmpp.Client, iqc chan xmpp.IQ, target string, iQtype string, content string) (xmpp.IQ, error) {
var iq xmpp.IQ
id := getID()
c := make(chan xmpp.IQ, defaultBufferSize)
go getIQ(id, c, iqc)
_, err := client.RawInformation(client.JID(), target, id,
iQtype, content)
if err != nil {
return iq, fmt.Errorf("send iq: failed to send iq: %w", err)
}
select {
case iq = <-c:
case <-time.After(60 * time.Second):
return iq, fmt.Errorf("send iq: server didn't reply to IQ: %s", content)
}
return iq, nil
}
func getIQ(id string, c chan xmpp.IQ, iqc chan xmpp.IQ) {
for {
iq := <-iqc
if iq.ID == id {
c <- iq
return
}
}
}
func getPresence(from string, c chan xmpp.Presence, prsc chan xmpp.Presence) {
for {
p := <-prsc
if strings.HasPrefix(p.From, from) {
c <- p
return
}
}
}
func rcvStanzas(client *xmpp.Client, ctx context.Context, iqc chan xmpp.IQ, msgc chan xmpp.Chat, prsc chan xmpp.Presence, disc chan xmpp.DiscoItems, drc chan xmpp.DiscoResult) {
type messageError struct {
XMLName xml.Name `xml:"message-error"`
Text string `xml:"text"`
}
var me messageError
var received interface{}
r := make(chan interface{}, defaultBufferSize)
e := make(chan error, defaultBufferSize)
go func() {
for {
select {
case <-ctx.Done():
return
default:
}
rcv, err := client.Recv()
if err != nil {
e <- err
} else {
r <- rcv
}
}
}()
for {
select {
case <-ctx.Done():
return
case err := <-e:
switch err {
case io.EOF, io.ErrUnexpectedEOF:
return
case nil:
continue
default:
closeAndExit(client, fmt.Errorf("receive stanzas: %v", err))
return
}
case received = <-r:
}
switch v := received.(type) {
case xmpp.Chat:
if v.Type == "error" {
for _, oe := range v.OtherElem {
err := xml.Unmarshal([]byte("<message-error>"+
oe.InnerXML+"</message-error>"), &me)
if err == nil {
fmt.Printf("%s: %s\n", v.Remote, me.Text)
}
}
}
msgc <- v
case xmpp.Presence:
prsc <- v
case xmpp.DiscoItems:
disc <- v
case xmpp.DiscoResult:
drc <- v
case xmpp.IQ:
switch v.Type {
case "get":
var xmlns *etree.Attr
iq := etree.NewDocument()
err := iq.ReadFromBytes(v.Query)
if err != nil {
log.Println("Couldn't parse IQ:",
string(v.Query), err)
}
query := iq.SelectElement("query")
if query != nil {
xmlns = query.SelectAttr("xmlns")
}
if xmlns == nil {
break
}
switch xmlns.Value {
case nsDiscoInfo:
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", 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)
xmlString, err := root.WriteToString()
if err == nil {
_, err = client.SendOrg(xmlString)
if err != nil {
log.Println(err)
}
}
default:
root := etree.NewDocument()
root.WriteSettings.AttrSingleQuote = true
reply := root.CreateElement("iq")
reply.CreateAttr("type", strError)
reply.CreateAttr("from", client.JID())
reply.CreateAttr("to", v.From)
reply.CreateAttr("id", v.ID)
errorReply := reply.CreateElement(strError)
errorReply.CreateAttr("type", "cancel")
su := errorReply.CreateElement("service-unavailable")
su.CreateAttr("xmlns", nsXMPPStanzas)
xmlString, err := root.WriteToString()
if err == nil {
_, err = client.SendOrg(xmlString)
if err != nil {
log.Println(err)
}
}
}
case "set":
root := etree.NewDocument()
root.WriteSettings.AttrSingleQuote = true
reply := root.CreateElement("iq")
reply.CreateAttr("type", strError)
reply.CreateAttr("from", client.JID())
reply.CreateAttr("to", v.From)
reply.CreateAttr("id", v.ID)
errorReply := reply.CreateElement(strError)
errorReply.CreateAttr("type", "cancel")
su := errorReply.CreateElement("service-unavailable")
su.CreateAttr("xmlns", nsXMPPStanzas)
xmlString, err := root.WriteToString()
if err == nil {
_, err = client.SendOrg(xmlString)
if err != nil {
log.Println(err)
}
}
case "reply", strError:
iqc <- v
default:
iqc <- v
}
}
}
}
|