go-xmpp/example/example.go
2013-10-21 03:07:59 +00:00

69 lines
1.3 KiB
Go

package main
import (
"bufio"
"flag"
"fmt"
"github.com/mattn/go-xmpp"
"log"
"os"
"strings"
)
var server = flag.String("server", "talk.google.com:443", "server")
var username = flag.String("username", "", "username")
var password = flag.String("password", "", "password")
var notls = flag.Bool("notls", false, "No TLS")
var debug = flag.Bool("debug", false, "debug output")
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: example [options]\n")
flag.PrintDefaults()
os.Exit(2)
}
flag.Parse()
if *username == "" || *password == "" {
flag.Usage()
}
var talk *xmpp.Client
var err error
if *notls {
talk, err = xmpp.NewClientNoTLS(*server, *username, *password, *debug)
} else {
talk, err = xmpp.NewClient(*server, *username, *password, *debug)
}
if err != nil {
log.Fatal(err)
}
go func() {
for {
chat, err := talk.Recv()
if err != nil {
log.Fatal(err)
}
switch v := chat.(type) {
case xmpp.Chat:
fmt.Println(v.Remote, v.Text)
case xmpp.Presence:
fmt.Println(v.From, v.Show)
}
}
}()
for {
in := bufio.NewReader(os.Stdin)
line, err := in.ReadString('\n')
if err != nil {
continue
}
line = strings.TrimRight(line, "\n")
tokens := strings.SplitN(line, " ", 2)
if len(tokens) == 2 {
talk.Send(xmpp.Chat{Remote: tokens[0], Type: "chat", Text: tokens[1]})
}
}
}