Debian Codegraph @

stanzahandling.go

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