From c0493f3cdd42147cadf9dd616ff9737490687095 Mon Sep 17 00:00:00 2001 From: Martin Dosch Date: Thu, 14 Mar 2024 21:19:54 +0100 Subject: [PATCH] Remove unnecessary passing around of mutex MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …it's a global variable anyway… --- main.go | 48 +++++++++++++++++++++++------------------------ stanzahandling.go | 5 ++--- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/main.go b/main.go index 578e6b9..d35fad7 100644 --- a/main.go +++ b/main.go @@ -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) } diff --git a/stanzahandling.go b/stanzahandling.go index bd2b6bc..16cda64 100644 --- a/stanzahandling.go +++ b/stanzahandling.go @@ -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) {