2011-02-27 18:44:24 -08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"flag"
|
|
|
|
"github.com/kless/go-readin/readin"
|
2011-05-10 06:53:04 -07:00
|
|
|
"github.com/mattn/go-xmpp"
|
2011-09-28 12:26:19 -07:00
|
|
|
"github.com/mattn/go-iconv"
|
2011-02-27 18:44:24 -08:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2011-05-10 07:57:36 -07:00
|
|
|
var server = flag.String("server", "talk.google.com:443", "server")
|
2011-02-27 18:44:24 -08:00
|
|
|
var username = flag.String("username", "", "username")
|
|
|
|
var password = flag.String("password", "", "password")
|
|
|
|
|
2011-07-12 06:03:21 -07:00
|
|
|
func fromUTF8(s string) string {
|
|
|
|
ic, err := iconv.Open("char", "UTF-8")
|
|
|
|
if err != nil {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
defer ic.Close()
|
|
|
|
ret, _ := ic.Conv(s)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func toUTF8(s string) string {
|
|
|
|
ic, err := iconv.Open("UTF-8", "char")
|
|
|
|
if err != nil {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
defer ic.Close()
|
|
|
|
ret, _ := ic.Conv(s)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
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 == "" {
|
|
|
|
flag.Usage()
|
|
|
|
}
|
|
|
|
|
2011-05-10 07:57:36 -07:00
|
|
|
talk, err := xmpp.NewClient(*server, *username, *password)
|
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)
|
|
|
|
}
|
2011-07-12 06:03:21 -07:00
|
|
|
fmt.Println(chat.Remote, fromUTF8(chat.Text))
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
for {
|
|
|
|
line, err := readin.RepeatPrompt("")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err.String())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
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 {
|
2011-07-12 06:03:21 -07:00
|
|
|
talk.Send(xmpp.Chat{Remote: tokens[0], Type: "chat", Text: toUTF8(tokens[1])})
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|