Move iq handling functions in separate file.

This commit is contained in:
Martin Dosch 2022-04-09 10:52:14 +08:00
parent b4db5512fd
commit 9140360674

48
iqhandling.go Normal file
View File

@ -0,0 +1,48 @@
// Copyright 2020 - 2021 Martin Dosch.
// Use of this source code is governed by the BSD-2-clause
// license that can be found in the LICENSE file.
package main
import (
"errors"
"log"
"github.com/mattn/go-xmpp" // BSD-3-Clause
)
func sendIQ(client *xmpp.Client, target string, iQtype string,
content string) (xmpp.IQ, error) {
var iq xmpp.IQ
id := getID()
c := make(chan xmpp.IQ)
go getIQ(client, id, c)
_, err := client.RawInformation(client.JID(), target, id,
iQtype, content)
if err != nil {
return iq, err
}
iq = <-c
close(c)
if iq.Type != "result" {
return iq, errors.New("No result for " + content)
}
return iq, nil
}
func getIQ(client *xmpp.Client, id string, c chan xmpp.IQ) {
for {
msg, err := client.Recv()
if err != nil {
log.Fatal(err)
}
switch v := msg.(type) {
case xmpp.IQ:
if v.ID == id {
c <- v
return
}
}
}
}