Code clean up.

The switch/case conditions should be easier to read then
a lot of nested if/else clauses.
This commit is contained in:
Martin Dosch 2022-04-21 21:26:14 +02:00
parent 33066fea8a
commit d92b2c7d04

200
main.go
View File

@ -117,22 +117,21 @@ func main() {
// Parse command line flags. // Parse command line flags.
getopt.Parse() getopt.Parse()
// If requested, show help and quit. switch {
if *flagHelp { case *flagHelp:
// If requested, show help and quit.
getopt.Usage() getopt.Usage()
os.Exit(0) os.Exit(0)
}
// If requested, show version and quit. case *flagVersion:
if *flagVersion { // If requested, show version and quit.
fmt.Println("go-sendxmpp", VERSION) fmt.Println("go-sendxmpp", VERSION)
fmt.Println("License: BSD-2-clause") fmt.Println("License: BSD-2-clause")
os.Exit(0) os.Exit(0)
}
// Quit if Ox (OpenPGP for XMPP) is requested for unsupported operations like case *flagOx:
// groupchat, http-upload or listening. // Quit if Ox (OpenPGP for XMPP) is requested for unsupported operations like
if *flagOx && (*flagHttpUpload != "" || *flagChatroom || *flagListen) { // groupchat, http-upload or listening.
switch { switch {
case *flagHttpUpload != "": case *flagHttpUpload != "":
log.Fatal("No Ox support for http-upload available.") log.Fatal("No Ox support for http-upload available.")
@ -280,7 +279,8 @@ func main() {
recipients[i].Jid = validatedJid recipients[i].Jid = validatedJid
} }
if *flagOxGenPrivKey { switch {
case *flagOxGenPrivKey:
validatedOwnJid, err := MarshalJID(user) validatedOwnJid, err := MarshalJID(user)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -290,9 +290,8 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
os.Exit(0) os.Exit(0)
}
if *flagOxImportPrivKey != "" { case *flagOxImportPrivKey != "":
validatedOwnJid, err := MarshalJID(user) validatedOwnJid, err := MarshalJID(user)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -351,8 +350,9 @@ func main() {
reg := regexp.MustCompile(`[\x{0000}-\x{0008}\x{000B}\x{000C}\x{000E}-\x{001F}]`) reg := regexp.MustCompile(`[\x{0000}-\x{0008}\x{000B}\x{000C}\x{000E}-\x{001F}]`)
message = reg.ReplaceAllString(message, "") message = reg.ReplaceAllString(message, "")
switch {
// Send raw XML to chatroom // Send raw XML to chatroom
if *flagChatroom && *flagRaw { case *flagChatroom && *flagRaw:
var err error var err error
// Join the MUCs. // Join the MUCs.
for _, recipient := range recipients { for _, recipient := range recipients {
@ -389,9 +389,8 @@ func main() {
// if the connection is closed immediately after sending a message. // if the connection is closed immediately after sending a message.
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
return return
}
if *flagListen { case *flagListen:
for { for {
received, err := client.Recv() received, err := client.Recv()
if err != nil { if err != nil {
@ -419,10 +418,9 @@ func main() {
continue continue
} }
} }
}
// Send message to chatroom(s) if the flag is set. case *flagChatroom:
if *flagChatroom { // Send message to chatroom(s) if the flag is set.
for _, recipient := range recipients { for _, recipient := range recipients {
@ -442,8 +440,9 @@ func main() {
} }
} }
// Send in endless loop (for usage with e.g. "tail -f"). switch {
if *flagInteractive { case *flagInteractive:
// Send in endless loop (for usage with e.g. "tail -f").
for { for {
scanner := bufio.NewScanner(os.Stdin) scanner := bufio.NewScanner(os.Stdin)
scanner.Scan() scanner.Scan()
@ -459,7 +458,7 @@ func main() {
} }
} }
} }
} else { default:
// Send the message. // Send the message.
for _, recipient := range recipients { for _, recipient := range recipients {
if *flagHttpUpload != "" { if *flagHttpUpload != "" {
@ -476,90 +475,91 @@ func main() {
log.Fatal(err) 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 { 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, _, err = client.Send(xmpp.Chat{Remote: recipient.Jid,
Type: "chat", Ooburl: message, Text: message}) Type: "chat", Text: message})
} else { if err != nil {
if *flagOx { // Try to nicely close connection,
if recipient.OxKeyRing == nil { // even if there was an error sending.
continue _ = client.Close()
} log.Fatal(err)
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})
} }
} }
}
}
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 { if err != nil {
// Try to nicely close connection, // Try to nicely close connection,
// even if there was an error sending. // even if there was an error sending.