From d92b2c7d043c83d4c833eb56f57e029cb42127f7 Mon Sep 17 00:00:00 2001 From: Martin Dosch Date: Thu, 21 Apr 2022 21:26:14 +0200 Subject: [PATCH] Code clean up. The switch/case conditions should be easier to read then a lot of nested if/else clauses. --- main.go | 200 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 100 insertions(+), 100 deletions(-) diff --git a/main.go b/main.go index 6c83f2e..8e39cb9 100644 --- a/main.go +++ b/main.go @@ -117,22 +117,21 @@ func main() { // Parse command line flags. getopt.Parse() - // If requested, show help and quit. - if *flagHelp { + switch { + case *flagHelp: + // If requested, show help and quit. getopt.Usage() os.Exit(0) - } - // If requested, show version and quit. - if *flagVersion { + case *flagVersion: + // If requested, show version and quit. fmt.Println("go-sendxmpp", VERSION) fmt.Println("License: BSD-2-clause") os.Exit(0) - } - // Quit if Ox (OpenPGP for XMPP) is requested for unsupported operations like - // groupchat, http-upload or listening. - if *flagOx && (*flagHttpUpload != "" || *flagChatroom || *flagListen) { + case *flagOx: + // Quit if Ox (OpenPGP for XMPP) is requested for unsupported operations like + // groupchat, http-upload or listening. switch { case *flagHttpUpload != "": log.Fatal("No Ox support for http-upload available.") @@ -280,7 +279,8 @@ func main() { recipients[i].Jid = validatedJid } - if *flagOxGenPrivKey { + switch { + case *flagOxGenPrivKey: validatedOwnJid, err := MarshalJID(user) if err != nil { log.Fatal(err) @@ -290,9 +290,8 @@ func main() { log.Fatal(err) } os.Exit(0) - } - if *flagOxImportPrivKey != "" { + case *flagOxImportPrivKey != "": validatedOwnJid, err := MarshalJID(user) if err != nil { log.Fatal(err) @@ -351,8 +350,9 @@ func main() { reg := regexp.MustCompile(`[\x{0000}-\x{0008}\x{000B}\x{000C}\x{000E}-\x{001F}]`) message = reg.ReplaceAllString(message, "") + switch { // Send raw XML to chatroom - if *flagChatroom && *flagRaw { + case *flagChatroom && *flagRaw: var err error // Join the MUCs. for _, recipient := range recipients { @@ -389,9 +389,8 @@ func main() { // if the connection is closed immediately after sending a message. time.Sleep(100 * time.Millisecond) return - } - if *flagListen { + case *flagListen: for { received, err := client.Recv() if err != nil { @@ -419,10 +418,9 @@ func main() { continue } } - } - // Send message to chatroom(s) if the flag is set. - if *flagChatroom { + case *flagChatroom: + // Send message to chatroom(s) if the flag is set. for _, recipient := range recipients { @@ -442,8 +440,9 @@ func main() { } } - // Send in endless loop (for usage with e.g. "tail -f"). - if *flagInteractive { + switch { + case *flagInteractive: + // Send in endless loop (for usage with e.g. "tail -f"). for { scanner := bufio.NewScanner(os.Stdin) scanner.Scan() @@ -459,7 +458,7 @@ func main() { } } } - } else { + default: // Send the message. for _, recipient := range recipients { if *flagHttpUpload != "" { @@ -476,90 +475,91 @@ func main() { log.Fatal(err) } } - } - for _, recipient := range recipients { - // After sending the message, leave the Muc - _, err = client.LeaveMUC(recipient.Jid) - if err != nil { - log.Println(err) - } - } - } else { - // Send raw XML - if *flagRaw { - _, err = client.SendOrg(message) - if err != nil { - // Try to nicely close connection, - // even if there was an error sending. - _ = client.Close() - log.Fatal(err) - } - // Wait for a short time as some messages are not delievered by the server - // if the connection is closed immediately after sending a message. - time.Sleep(100 * time.Millisecond) - return - } - - // Send in endless loop (for usage with e.g. "tail -f"). - if *flagInteractive { - for { - scanner := bufio.NewScanner(os.Stdin) - scanner.Scan() - message = scanner.Text() - for _, recipient := range recipients { - if *flagOx { - if recipient.OxKeyRing == nil { - continue - } - oxMessage, err := oxEncrypt(client, oxPrivKey, - recipient.Jid, recipient.OxKeyRing, message) - if err != nil { - fmt.Println("Ox: couldn't encrypt to", - recipient.Jid) - continue - } - _, err = client.SendOrg(oxMessage) - if err != nil { - log.Fatal(err) - } - } else { - _, err = client.Send(xmpp.Chat{Remote: recipient.Jid, - Type: "chat", Text: message}) - if err != nil { - // Try to nicely close connection, - // even if there was an error sending. - _ = client.Close() - log.Fatal(err) - } - } - } - } - } else { for _, recipient := range recipients { - if *flagHttpUpload != "" { + // After sending the message, leave the Muc + _, err = client.LeaveMUC(recipient.Jid) + if err != nil { + log.Println(err) + } + } + } + case *flagRaw: + // Send raw XML + _, err = client.SendOrg(message) + if err != nil { + // Try to nicely close connection, + // even if there was an error sending. + _ = client.Close() + log.Fatal(err) + } + // Wait for a short time as some messages are not delievered by the server + // if the connection is closed immediately after sending a message. + time.Sleep(100 * time.Millisecond) + return + + case *flagInteractive: + // Send in endless loop (for usage with e.g. "tail -f"). + for { + scanner := bufio.NewScanner(os.Stdin) + scanner.Scan() + message = scanner.Text() + for _, recipient := range recipients { + switch { + case *flagOx: + if recipient.OxKeyRing == nil { + continue + } + oxMessage, err := oxEncrypt(client, oxPrivKey, + recipient.Jid, recipient.OxKeyRing, message) + if err != nil { + fmt.Println("Ox: couldn't encrypt to", + recipient.Jid) + continue + } + _, err = client.SendOrg(oxMessage) + if err != nil { + log.Fatal(err) + } + default: _, err = client.Send(xmpp.Chat{Remote: recipient.Jid, - Type: "chat", Ooburl: message, Text: message}) - } else { - if *flagOx { - if recipient.OxKeyRing == nil { - continue - } - oxMessage, err := oxEncrypt(client, oxPrivKey, - recipient.Jid, recipient.OxKeyRing, message) - if err != nil { - fmt.Println("Ox: couldn't encrypt to", recipient.Jid) - continue - } - _, err = client.SendOrg(oxMessage) - if err != nil { - log.Fatal(err) - } - } else { - _, err = client.Send(xmpp.Chat{Remote: recipient.Jid, - Type: "chat", Text: message}) + Type: "chat", Text: message}) + if err != nil { + // Try to nicely close connection, + // even if there was an error sending. + _ = client.Close() + log.Fatal(err) } } + } + } + default: + for _, recipient := range recipients { + switch { + case *flagHttpUpload != "": + _, err = client.Send(xmpp.Chat{Remote: recipient.Jid, + Type: "chat", Ooburl: message, Text: message}) + if err != nil { + fmt.Println("Couldn't send message to", + recipient.Jid) + } + case *flagOx: + if recipient.OxKeyRing == nil { + continue + } + oxMessage, err := oxEncrypt(client, oxPrivKey, + recipient.Jid, recipient.OxKeyRing, message) + if err != nil { + fmt.Println("Ox: couldn't encrypt to", recipient.Jid) + continue + } + _, err = client.SendOrg(oxMessage) + if err != nil { + log.Fatal(err) + } + default: + _, err = client.Send(xmpp.Chat{Remote: recipient.Jid, + Type: "chat", Text: message}) if err != nil { // Try to nicely close connection, // even if there was an error sending.