Last active
November 24, 2017 11:36
-
-
Save trevordixon/79ad5a688c70551a8f2c134273a00f7f to your computer and use it in GitHub Desktop.
Simple command line XMPP instant messaging program that opens a conversation with a single contact
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Adapted from https://github.com/mattn/go-xmpp/blob/master/_example/example.go. | |
package main | |
import ( | |
"crypto/tls" | |
"flag" | |
"fmt" | |
"log" | |
"os" | |
"strings" | |
"golang.org/x/crypto/ssh/terminal" | |
"github.com/mattn/go-xmpp" | |
) | |
var server = flag.String("server", "", "server") | |
var username = flag.String("username", "", "username") | |
var password = flag.String("password", "", "password") | |
var status = flag.String("status", "xa", "status") | |
var statusMessage = flag.String("status-msg", "Connected", "status message") | |
var notls = flag.Bool("notls", false, "No TLS") | |
var debug = flag.Bool("debug", false, "debug output") | |
var session = flag.Bool("session", false, "use server session") | |
var me = flag.String("me", "Me", "name to use for sender") | |
var recipient = flag.String("recipient", "", "username of contact to message") | |
var recipientName = flag.String("recipient-name", "", "name to use for recipient") | |
func serverName(host string) string { | |
return strings.Split(host, ":")[0] | |
} | |
func main() { | |
flag.Usage = func() { | |
fmt.Fprintf(os.Stderr, "usage: example [options]\n") | |
flag.PrintDefaults() | |
os.Exit(2) | |
} | |
flag.Parse() | |
if *username == "" || *password == "" { | |
if *debug && *username == "" && *password == "" { | |
fmt.Fprintf(os.Stderr, "no username or password were given; attempting ANONYMOUS auth\n") | |
} else if *username != "" || *password != "" { | |
flag.Usage() | |
} | |
} | |
if !*notls { | |
xmpp.DefaultConfig = tls.Config{ | |
ServerName: serverName(*server), | |
InsecureSkipVerify: false, | |
} | |
} | |
var talk *xmpp.Client | |
var err error | |
options := xmpp.Options{Host: *server, | |
User: *username, | |
Password: *password, | |
NoTLS: *notls, | |
Debug: *debug, | |
Session: *session, | |
Status: *status, | |
StatusMessage: *statusMessage, | |
} | |
talk, err = options.NewClient() | |
if err != nil { | |
log.Fatal(err) | |
} | |
oldState, err := terminal.MakeRaw(0) | |
if err != nil { | |
panic(err) | |
} | |
defer func() { | |
terminal.Restore(0, oldState) | |
fmt.Println() | |
}() | |
t := terminal.NewTerminal(os.Stdin, "") | |
t.SetPrompt(fmt.Sprintf("%s%s%s: ", t.Escape.Red, *me, t.Escape.Reset)) | |
go func() { | |
for { | |
chat, err := talk.Recv() | |
if err != nil { | |
log.Fatal(err) | |
} | |
switch v := chat.(type) { | |
case xmpp.Chat: | |
if strings.HasPrefix(v.Remote, *recipient) { | |
t.Write([]byte(fmt.Sprintf("%s%s%s: %s\n", t.Escape.Cyan, *recipientName, t.Escape.Reset, v.Text))) | |
} | |
case xmpp.Presence: | |
} | |
} | |
}() | |
for { | |
line, err := t.ReadLine() | |
if err != nil { | |
break | |
} | |
line = strings.TrimRight(line, "\n") | |
if line == "" { | |
continue | |
} | |
talk.Send(xmpp.Chat{Remote: *recipient, Type: "chat", Text: line}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment