Remove unnecessary passing around of mutex

…it's a global variable anyway…
This commit is contained in:
Martin Dosch 2024-03-14 21:19:54 +01:00
parent 4a54f8419b
commit c0493f3cdd
No known key found for this signature in database
GPG Key ID: 52A57CFCE13D657D
2 changed files with 26 additions and 27 deletions

48
main.go
View File

@ -35,7 +35,7 @@ type configuration struct {
var mutex sync.Mutex
func closeAndExit(client *xmpp.Client, cancel context.CancelFunc, err error, mutex *sync.Mutex) {
func closeAndExit(client *xmpp.Client, cancel context.CancelFunc, err error) {
cancel()
mutex.Lock()
defer mutex.Unlock()
@ -340,7 +340,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, &mutex)
go rcvStanzas(client, iqc, msgc, ctx, cancel)
for _, r := range getopt.Args() {
var re recipientsType
re.Jid = r
@ -358,7 +358,7 @@ func main() {
for i, recipient := range recipients {
validatedJid, err := MarshalJID(recipient.Jid)
if err != nil {
closeAndExit(client, cancel, err, &mutex)
closeAndExit(client, cancel, err)
}
recipients[i].Jid = validatedJid
}
@ -367,52 +367,52 @@ func main() {
case *flagOxGenPrivKeyX25519:
validatedOwnJid, err := MarshalJID(user)
if err != nil {
closeAndExit(client, cancel, err, &mutex)
closeAndExit(client, cancel, err)
}
err = oxGenPrivKey(validatedOwnJid, client, iqc, *flagOxPassphrase, "x25519")
if err != nil {
closeAndExit(client, cancel, err, &mutex)
closeAndExit(client, cancel, err)
}
os.Exit(0)
case *flagOxGenPrivKeyRSA:
validatedOwnJid, err := MarshalJID(user)
if err != nil {
closeAndExit(client, cancel, err, &mutex)
closeAndExit(client, cancel, err)
}
err = oxGenPrivKey(validatedOwnJid, client, iqc, *flagOxPassphrase, "rsa")
if err != nil {
closeAndExit(client, cancel, err, &mutex)
closeAndExit(client, cancel, err)
}
os.Exit(0)
case *flagOxImportPrivKey != "":
validatedOwnJid, err := MarshalJID(user)
if err != nil {
closeAndExit(client, cancel, err, &mutex)
closeAndExit(client, cancel, err)
}
err = oxImportPrivKey(validatedOwnJid, *flagOxImportPrivKey,
client, iqc)
if err != nil {
closeAndExit(client, cancel, err, &mutex)
closeAndExit(client, cancel, err)
}
os.Exit(0)
case *flagOxDeleteNodes:
validatedOwnJid, err := MarshalJID(user)
if err != nil {
closeAndExit(client, cancel, err, &mutex)
closeAndExit(client, cancel, err)
}
err = oxDeleteNodes(validatedOwnJid, client, iqc)
if err != nil {
closeAndExit(client, cancel, err, &mutex)
closeAndExit(client, cancel, err)
}
os.Exit(0)
case *flagOx:
validatedOwnJid, err := MarshalJID(user)
if err != nil {
closeAndExit(client, cancel, err, &mutex)
closeAndExit(client, cancel, err)
}
oxPrivKey, err = oxGetPrivKey(validatedOwnJid, *flagOxPassphrase)
if err != nil {
closeAndExit(client, cancel, err, &mutex)
closeAndExit(client, cancel, err)
}
}
@ -420,7 +420,7 @@ func main() {
message, err = httpUpload(client, iqc, tlsConfig.ServerName,
*flagHTTPUpload, timeout)
if err != nil {
closeAndExit(client, cancel, err, &mutex)
closeAndExit(client, cancel, err)
}
}
@ -430,7 +430,7 @@ func main() {
// Check if the URI is valid.
uri, err := validURI(message)
if err != nil {
closeAndExit(client, cancel, err, &mutex)
closeAndExit(client, cancel, err)
}
message = uri.String()
}
@ -453,7 +453,7 @@ func main() {
_, err = client.JoinMUCNoHistory(recipient.Jid, alias)
}
if err != nil {
closeAndExit(client, cancel, err, &mutex)
closeAndExit(client, cancel, err)
}
}
}
@ -465,7 +465,7 @@ func main() {
// Send raw XML
_, err = client.SendOrg(message)
if err != nil {
closeAndExit(client, cancel, err, &mutex)
closeAndExit(client, cancel, err)
}
case *flagInteractive:
// Send in endless loop (for usage with e.g. "tail -f").
@ -475,7 +475,7 @@ func main() {
signal.Notify(c, os.Interrupt)
go func() {
for range c {
closeAndExit(client, cancel, nil, &mutex)
closeAndExit(client, cancel, nil)
}
}()
for {
@ -485,7 +485,7 @@ func main() {
return
default:
if err != nil {
closeAndExit(client, cancel, errors.New("failed to read from stdin"), &mutex)
closeAndExit(client, cancel, errors.New("failed to read from stdin"))
}
}
message = strings.TrimSuffix(message, "\n")
@ -510,7 +510,7 @@ func main() {
}
_, err = client.SendOrg(oxMessage)
if err != nil {
closeAndExit(client, cancel, err, &mutex)
closeAndExit(client, cancel, err)
}
default:
_, err = client.Send(xmpp.Chat{
@ -518,7 +518,7 @@ func main() {
Type: msgType, Text: message,
})
if err != nil {
closeAndExit(client, cancel, err, &mutex)
closeAndExit(client, cancel, err)
}
}
}
@ -629,7 +629,7 @@ func main() {
}
_, err = client.SendOrg(oxMessage)
if err != nil {
closeAndExit(client, cancel, err, &mutex)
closeAndExit(client, cancel, err)
}
default:
_, err = client.Send(xmpp.Chat{
@ -637,10 +637,10 @@ func main() {
Type: msgType, Text: message,
})
if err != nil {
closeAndExit(client, cancel, err, &mutex)
closeAndExit(client, cancel, err)
}
}
}
}
closeAndExit(client, cancel, nil, &mutex)
closeAndExit(client, cancel, nil)
}

View File

@ -10,7 +10,6 @@ import (
"fmt"
"log"
"runtime"
"sync"
"time"
"github.com/beevik/etree" // BSD-2-clause
@ -45,7 +44,7 @@ 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, mutex *sync.Mutex) {
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{}
for {
@ -66,7 +65,7 @@ func rcvStanzas(client *xmpp.Client, iqc chan xmpp.IQ, msgc chan xmpp.Chat, ctx
default:
if err != nil {
mutex.Unlock()
closeAndExit(client, cancel, err, mutex)
closeAndExit(client, cancel, err)
}
}
switch v := received.(type) {