Added functionality to read message from an input file (, )

http_upload
Martin Dosch 6 years ago
parent ae19493f59
commit 74f97f6fec

@ -48,6 +48,8 @@ Usage: go-sendxmpp [-cdtx] [-f value] [--help] [-j value] [-p value] [-r value]
--help Show help.
-j, --jserver=value
XMPP server address.
-m, --message=value
Set file including the message.
-p, --password=value
Password for XMPP account.
-r, --resource=value

@ -130,6 +130,45 @@ func parseConfig(configPath string) (configuration, error) {
return output, err
}
func readMessage(messageFilePath string) (string, error) {
var (
output string
err error
)
// Check that message file is existing.
_, err = os.Stat(messageFilePath)
if os.IsNotExist(err) {
return output, err
}
// Open message file.
file, err := os.Open(messageFilePath)
if err != nil {
return output, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
if output == "" {
output = scanner.Text()
} else {
output = output + "\n" + scanner.Text()
}
}
if err := scanner.Err(); err != nil {
if err != io.EOF {
return "", err
}
}
return output, err
}
func main() {
var (
@ -144,12 +183,12 @@ func main() {
flagUser := getopt.StringLong("username", 'u', "", "Username for XMPP account.")
flagPassword := getopt.StringLong("password", 'p', "", "Password for XMPP account.")
flagChatroom := getopt.BoolLong("chatroom", 'c', "Send message to a chatroom.")
// flagMessage := getopt.StringLong("message", 'm', "", "The message you want to send.")
flagTLS := getopt.BoolLong("tls", 't', "Use TLS.")
flagStartTLS := getopt.BoolLong("start-tls", 'x', "Use StartTLS.")
flagResource := getopt.StringLong("resource", 'r', "go-sendxmpp", "Set resource. "+
"When sending to a chatroom this is used as 'alias'. (Default: go-sendxmpp)")
flagFile := getopt.StringLong("file", 'f', "", "Set configuration file. (Default: ~/.sendxmppr)")
flagFile := getopt.StringLong("file", 'f', "", "Set configuration file. (Default: ~/.sendxmpprc)")
flagMessageFile := getopt.StringLong("message", 'm', "", "Set file including the message.")
// Parse command line flags.
getopt.Parse()
@ -220,6 +259,14 @@ func main() {
Debug: *flagDebug,
}
// Read message from file.
if *flagMessageFile != "" {
message, err = readMessage(*flagMessageFile)
if err != nil {
log.Fatal(err)
}
}
// Connect to server.
client, err := options.NewClient()
if err != nil {

Loading…
Cancel
Save