go-xmpp/_example/example.go

95 lines
2.1 KiB
Go
Raw Normal View History

2011-02-27 18:44:24 -08:00
package main
import (
2011-11-04 06:40:10 -07:00
"bufio"
2014-09-16 20:35:47 -07:00
"crypto/tls"
2011-02-27 18:44:24 -08:00
"flag"
2013-10-20 20:07:59 -07:00
"fmt"
2011-05-10 06:53:04 -07:00
"github.com/mattn/go-xmpp"
2011-02-27 18:44:24 -08:00
"log"
"os"
"strings"
)
2019-01-08 21:52:43 -08:00
var server = flag.String("server", "", "server")
2011-02-27 18:44:24 -08:00
var username = flag.String("username", "", "username")
var password = flag.String("password", "", "password")
2014-10-04 09:29:19 -07:00
var status = flag.String("status", "xa", "status")
var statusMessage = flag.String("status-msg", "I for one welcome our new codebot overlords.", "status message")
2013-05-14 19:24:46 -07:00
var notls = flag.Bool("notls", false, "No TLS")
2013-10-20 20:07:59 -07:00
var debug = flag.Bool("debug", false, "debug output")
var session = flag.Bool("session", false, "use server session")
2011-07-12 06:03:21 -07:00
2014-09-16 20:35:47 -07:00
func serverName(host string) string {
return strings.Split(host, ":")[0]
}
2011-02-27 18:44:24 -08:00
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()
}
2011-02-27 18:44:24 -08:00
}
2014-09-16 20:35:47 -07:00
if !*notls {
2014-10-04 09:29:19 -07:00
xmpp.DefaultConfig = tls.Config{
ServerName: serverName(*server),
2014-09-16 20:35:47 -07:00
InsecureSkipVerify: false,
}
}
2013-05-14 19:24:46 -07:00
var talk *xmpp.Client
var err error
options := xmpp.Options{Host: *server,
2014-10-04 09:29:19 -07:00
User: *username,
Password: *password,
NoTLS: *notls,
Debug: *debug,
Session: *session,
Status: *status,
StatusMessage: *statusMessage,
2014-10-04 09:22:05 -07:00
}
talk, err = options.NewClient()
2011-02-27 18:44:24 -08:00
if err != nil {
log.Fatal(err)
}
go func() {
2011-06-27 00:36:36 -07:00
for {
chat, err := talk.Recv()
if err != nil {
log.Fatal(err)
}
2013-02-11 17:43:33 -08:00
switch v := chat.(type) {
case xmpp.Chat:
2013-05-14 19:24:46 -07:00
fmt.Println(v.Remote, v.Text)
2013-02-11 17:43:33 -08:00
case xmpp.Presence:
2013-05-14 19:24:46 -07:00
fmt.Println(v.From, v.Show)
2013-02-11 17:43:33 -08:00
}
2011-02-27 18:44:24 -08:00
}
}()
for {
2011-11-04 06:40:10 -07:00
in := bufio.NewReader(os.Stdin)
line, err := in.ReadString('\n')
2011-02-27 18:44:24 -08:00
if err != nil {
continue
}
2011-11-04 06:40:10 -07:00
line = strings.TrimRight(line, "\n")
2011-02-27 18:44:24 -08:00
2011-06-27 18:53:36 -07:00
tokens := strings.SplitN(line, " ", 2)
2011-02-27 18:44:24 -08:00
if len(tokens) == 2 {
2013-05-14 19:24:46 -07:00
talk.Send(xmpp.Chat{Remote: tokens[0], Type: "chat", Text: tokens[1]})
2011-02-27 18:44:24 -08:00
}
}
}