mirror of
https://salsa.debian.org/mdosch/go-sendxmpp
synced 2024-11-12 13:10:25 +00:00
Move iq handling functions in separate file.
This commit is contained in:
parent
b4db5512fd
commit
9140360674
48
iqhandling.go
Normal file
48
iqhandling.go
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user