mirror of
https://salsa.debian.org/mdosch/go-sendxmpp
synced 2024-11-18 21:25:31 +00:00
Remove mutex and context.
Those should hopefully no longer be necessary as the races have been fixed in the library.
This commit is contained in:
parent
f1e9bfcdc8
commit
11a31d6ad0
60
main.go
60
main.go
@ -6,7 +6,6 @@ package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"fmt"
|
||||
@ -33,8 +32,7 @@ type configuration struct {
|
||||
alias string
|
||||
}
|
||||
|
||||
func closeAndExit(client *xmpp.Client, cancel context.CancelFunc, err error) {
|
||||
cancel()
|
||||
func closeAndExit(client *xmpp.Client, err error) {
|
||||
client.Close()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@ -353,8 +351,7 @@ func main() {
|
||||
|
||||
iqc := make(chan xmpp.IQ, defaultBufferSize)
|
||||
msgc := make(chan xmpp.Chat, defaultBufferSize)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
go rcvStanzas(client, iqc, msgc, ctx, cancel)
|
||||
go rcvStanzas(client, iqc, msgc)
|
||||
for _, r := range getopt.Args() {
|
||||
var re recipientsType
|
||||
re.Jid = r
|
||||
@ -372,7 +369,7 @@ func main() {
|
||||
for i, recipient := range recipients {
|
||||
validatedJid, err := MarshalJID(recipient.Jid)
|
||||
if err != nil {
|
||||
closeAndExit(client, cancel, err)
|
||||
closeAndExit(client, err)
|
||||
}
|
||||
recipients[i].Jid = validatedJid
|
||||
}
|
||||
@ -381,52 +378,52 @@ func main() {
|
||||
case *flagOxGenPrivKeyX25519:
|
||||
validatedOwnJid, err := MarshalJID(user)
|
||||
if err != nil {
|
||||
closeAndExit(client, cancel, err)
|
||||
closeAndExit(client, err)
|
||||
}
|
||||
err = oxGenPrivKey(validatedOwnJid, client, iqc, *flagOxPassphrase, "x25519")
|
||||
if err != nil {
|
||||
closeAndExit(client, cancel, err)
|
||||
closeAndExit(client, err)
|
||||
}
|
||||
os.Exit(0)
|
||||
case *flagOxGenPrivKeyRSA:
|
||||
validatedOwnJid, err := MarshalJID(user)
|
||||
if err != nil {
|
||||
closeAndExit(client, cancel, err)
|
||||
closeAndExit(client, err)
|
||||
}
|
||||
err = oxGenPrivKey(validatedOwnJid, client, iqc, *flagOxPassphrase, "rsa")
|
||||
if err != nil {
|
||||
closeAndExit(client, cancel, err)
|
||||
closeAndExit(client, err)
|
||||
}
|
||||
os.Exit(0)
|
||||
case *flagOxImportPrivKey != "":
|
||||
validatedOwnJid, err := MarshalJID(user)
|
||||
if err != nil {
|
||||
closeAndExit(client, cancel, err)
|
||||
closeAndExit(client, err)
|
||||
}
|
||||
err = oxImportPrivKey(validatedOwnJid, *flagOxImportPrivKey,
|
||||
client, iqc)
|
||||
if err != nil {
|
||||
closeAndExit(client, cancel, err)
|
||||
closeAndExit(client, err)
|
||||
}
|
||||
os.Exit(0)
|
||||
case *flagOxDeleteNodes:
|
||||
validatedOwnJid, err := MarshalJID(user)
|
||||
if err != nil {
|
||||
closeAndExit(client, cancel, err)
|
||||
closeAndExit(client, err)
|
||||
}
|
||||
err = oxDeleteNodes(validatedOwnJid, client, iqc)
|
||||
if err != nil {
|
||||
closeAndExit(client, cancel, err)
|
||||
closeAndExit(client, err)
|
||||
}
|
||||
os.Exit(0)
|
||||
case *flagOx:
|
||||
validatedOwnJid, err := MarshalJID(user)
|
||||
if err != nil {
|
||||
closeAndExit(client, cancel, err)
|
||||
closeAndExit(client, err)
|
||||
}
|
||||
oxPrivKey, err = oxGetPrivKey(validatedOwnJid, *flagOxPassphrase)
|
||||
if err != nil {
|
||||
closeAndExit(client, cancel, err)
|
||||
closeAndExit(client, err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -434,7 +431,7 @@ func main() {
|
||||
message, err = httpUpload(client, iqc, tlsConfig.ServerName,
|
||||
*flagHTTPUpload, timeout)
|
||||
if err != nil {
|
||||
closeAndExit(client, cancel, err)
|
||||
closeAndExit(client, err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -444,7 +441,7 @@ func main() {
|
||||
// Check if the URI is valid.
|
||||
uri, err := validURI(message)
|
||||
if err != nil {
|
||||
closeAndExit(client, cancel, err)
|
||||
closeAndExit(client, err)
|
||||
}
|
||||
message = uri.String()
|
||||
}
|
||||
@ -467,7 +464,7 @@ func main() {
|
||||
_, err = client.JoinMUCNoHistory(recipient.Jid, alias)
|
||||
}
|
||||
if err != nil {
|
||||
closeAndExit(client, cancel, err)
|
||||
closeAndExit(client, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -479,7 +476,7 @@ func main() {
|
||||
// Send raw XML
|
||||
_, err = client.SendOrg(message)
|
||||
if err != nil {
|
||||
closeAndExit(client, cancel, err)
|
||||
closeAndExit(client, err)
|
||||
}
|
||||
case *flagInteractive:
|
||||
// Send in endless loop (for usage with e.g. "tail -f").
|
||||
@ -489,18 +486,13 @@ func main() {
|
||||
signal.Notify(c, os.Interrupt)
|
||||
go func() {
|
||||
for range c {
|
||||
closeAndExit(client, cancel, nil)
|
||||
closeAndExit(client, nil)
|
||||
}
|
||||
}()
|
||||
for {
|
||||
message, err = reader.ReadString('\n')
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
default:
|
||||
if err != nil {
|
||||
closeAndExit(client, cancel, errors.New("failed to read from stdin"))
|
||||
}
|
||||
if err != nil {
|
||||
closeAndExit(client, errors.New("failed to read from stdin"))
|
||||
}
|
||||
message = strings.TrimSuffix(message, "\n")
|
||||
|
||||
@ -524,7 +516,7 @@ func main() {
|
||||
}
|
||||
_, err = client.SendOrg(oxMessage)
|
||||
if err != nil {
|
||||
closeAndExit(client, cancel, err)
|
||||
closeAndExit(client, err)
|
||||
}
|
||||
default:
|
||||
_, err = client.Send(xmpp.Chat{
|
||||
@ -532,7 +524,7 @@ func main() {
|
||||
Type: msgType, Text: message,
|
||||
})
|
||||
if err != nil {
|
||||
closeAndExit(client, cancel, err)
|
||||
closeAndExit(client, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -544,7 +536,7 @@ func main() {
|
||||
signal.Notify(c, os.Interrupt)
|
||||
go func() {
|
||||
for range c {
|
||||
closeAndExit(client, cancel, nil)
|
||||
closeAndExit(client, nil)
|
||||
}
|
||||
}()
|
||||
for {
|
||||
@ -651,7 +643,7 @@ func main() {
|
||||
}
|
||||
_, err = client.SendOrg(oxMessage)
|
||||
if err != nil {
|
||||
closeAndExit(client, cancel, err)
|
||||
closeAndExit(client, err)
|
||||
}
|
||||
default:
|
||||
_, err = client.Send(xmpp.Chat{
|
||||
@ -659,10 +651,10 @@ func main() {
|
||||
Type: msgType, Text: message,
|
||||
})
|
||||
if err != nil {
|
||||
closeAndExit(client, cancel, err)
|
||||
closeAndExit(client, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
closeAndExit(client, cancel, nil)
|
||||
closeAndExit(client, nil)
|
||||
}
|
||||
|
@ -5,7 +5,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
@ -44,35 +43,11 @@ func getIQ(id string, c chan xmpp.IQ, iqc chan xmpp.IQ) {
|
||||
}
|
||||
}
|
||||
|
||||
func rcvStanzas(client *xmpp.Client, iqc chan xmpp.IQ, msgc chan xmpp.Chat, ctx context.Context, cancel context.CancelFunc) {
|
||||
var err error
|
||||
var received interface{}
|
||||
r := make(chan interface{})
|
||||
func rcvStanzas(client *xmpp.Client, iqc chan xmpp.IQ, msgc chan xmpp.Chat) {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
default:
|
||||
go func() {
|
||||
received, err = client.Recv()
|
||||
r <- received
|
||||
}()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-r:
|
||||
}
|
||||
}
|
||||
// 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:
|
||||
if err != nil {
|
||||
closeAndExit(client, cancel, err)
|
||||
}
|
||||
received, err := client.Recv()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
switch v := received.(type) {
|
||||
case xmpp.Chat:
|
||||
|
Loading…
Reference in New Issue
Block a user