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"
|
2019-01-10 19:20:54 -08:00
|
|
|
"encoding/xml"
|
2011-02-27 18:44:24 -08:00
|
|
|
"flag"
|
2013-10-20 20:07:59 -07:00
|
|
|
"fmt"
|
2019-01-09 01:35:00 -08:00
|
|
|
"github.com/kjx98/go-xmpp"
|
2011-02-27 18:44:24 -08:00
|
|
|
"log"
|
|
|
|
"os"
|
2019-01-10 19:20:54 -08:00
|
|
|
"runtime"
|
2011-02-27 18:44:24 -08:00
|
|
|
"strings"
|
2019-01-09 01:35:00 -08:00
|
|
|
"time"
|
2011-02-27 18:44:24 -08:00
|
|
|
)
|
|
|
|
|
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")
|
2019-01-09 01:35:00 -08:00
|
|
|
var notls = flag.Bool("notls", true, "No TLS")
|
2013-10-20 20:07:59 -07:00
|
|
|
var debug = flag.Bool("debug", false, "debug output")
|
2014-04-22 09:05:35 -07:00
|
|
|
var session = flag.Bool("session", false, "use server session")
|
2011-07-12 06:03:21 -07:00
|
|
|
|
2019-01-10 19:20:54 -08:00
|
|
|
type rosterItem struct {
|
|
|
|
XMLName xml.Name `xml:"item"`
|
|
|
|
Jid string `xml:"jid,attr"`
|
|
|
|
Name string `xml:"name,attr"`
|
|
|
|
Subscription string `xml:"subscription,attr"`
|
|
|
|
Group []string `"xml:"group"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type contactType struct {
|
|
|
|
Jid string
|
|
|
|
Name string
|
|
|
|
Subscription string
|
|
|
|
Online bool
|
|
|
|
}
|
|
|
|
|
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 == "" {
|
2015-04-12 22:18:06 -07:00
|
|
|
if *debug && *username == "" && *password == "" {
|
2015-04-12 22:12:16 -07:00
|
|
|
fmt.Fprintf(os.Stderr, "no username or password were given; attempting ANONYMOUS auth\n")
|
2015-04-12 22:18:06 -07:00
|
|
|
} else if *username != "" || *password != "" {
|
|
|
|
flag.Usage()
|
2015-04-12 22:12:16 -07:00
|
|
|
}
|
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,
|
|
|
|
}
|
2019-01-10 04:43:44 -08:00
|
|
|
} else {
|
|
|
|
xmpp.DefaultConfig = tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
}
|
2014-09-16 20:35:47 -07:00
|
|
|
}
|
|
|
|
|
2013-05-14 19:24:46 -07:00
|
|
|
var talk *xmpp.Client
|
|
|
|
var err error
|
2014-04-22 09:05:35 -07:00
|
|
|
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,
|
2019-01-09 01:35:00 -08:00
|
|
|
Resource: "bot",
|
2014-10-04 09:29:19 -07:00
|
|
|
StatusMessage: *statusMessage,
|
2014-10-04 09:22:05 -07:00
|
|
|
}
|
2014-04-22 09:05:35 -07:00
|
|
|
|
|
|
|
talk, err = options.NewClient()
|
|
|
|
|
2011-02-27 18:44:24 -08:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-01-10 19:20:54 -08:00
|
|
|
loginTime := time.Now()
|
2011-02-27 18:44:24 -08:00
|
|
|
|
|
|
|
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:
|
2019-01-09 01:35:00 -08:00
|
|
|
if v.Type == "roster" {
|
|
|
|
fmt.Println("roster", v.Roster)
|
|
|
|
} else {
|
|
|
|
for _, element := range v.OtherElem {
|
|
|
|
if element.XMLName.Space == "jabber:x:conference" {
|
|
|
|
// if not join
|
|
|
|
talk.JoinMUCNoHistory(v.Remote, "bot")
|
|
|
|
}
|
|
|
|
// composing, paused, active
|
|
|
|
if element.XMLName.Space ==
|
|
|
|
"http://jabber.org/protocol/chatstates" &&
|
|
|
|
element.XMLName.Local == "composing" {
|
|
|
|
fmt.Println(v.Remote, "is composing")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if strings.TrimSpace(v.Text) != "" {
|
|
|
|
fmt.Println(v.Remote, v.Text)
|
|
|
|
}
|
|
|
|
}
|
2013-02-11 17:43:33 -08:00
|
|
|
case xmpp.Presence:
|
2019-01-10 19:20:54 -08:00
|
|
|
switch v.Type {
|
|
|
|
case "subscribe":
|
|
|
|
// Approve all subscription
|
|
|
|
fmt.Printf("Presence: %s Approve %s subscription\n",
|
|
|
|
v.To, v.From)
|
|
|
|
talk.ApproveSubscription(v.From)
|
|
|
|
talk.RequestSubscription(v.From)
|
|
|
|
case "unsubscribe":
|
|
|
|
fmt.Printf("Presence: %s Revoke %s subscription\n",
|
|
|
|
v.To, v.From)
|
|
|
|
talk.RevokeSubscription(v.From)
|
|
|
|
default:
|
|
|
|
fmt.Printf("Presence: %s %s Type(%s)\n", v.From, v.Show, v.Type)
|
|
|
|
}
|
2019-01-09 01:35:00 -08:00
|
|
|
case xmpp.Roster, xmpp.Contact:
|
|
|
|
// TODO: update local roster
|
|
|
|
fmt.Println("Roster/Contact:", v)
|
|
|
|
case xmpp.IQ:
|
|
|
|
// ping ignore
|
2019-01-10 19:20:54 -08:00
|
|
|
switch v.QueryName.Space {
|
|
|
|
case "jabber:iq:version":
|
|
|
|
if err := talk.RawVersion(v.To, v.From, v.ID,
|
|
|
|
"0.1", runtime.GOOS); err != nil {
|
|
|
|
fmt.Println("RawVersion:", err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
case "jabber:iq:last":
|
|
|
|
tt := time.Now().Sub(loginTime)
|
|
|
|
last := int(tt.Seconds())
|
|
|
|
if err := talk.RawLast(v.To, v.From, v.ID, last); err != nil {
|
|
|
|
fmt.Println("RawLast:", err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
case "urn:xmpp:time":
|
|
|
|
if err := talk.RawIQtime(v.To, v.From, v.ID); err != nil {
|
|
|
|
fmt.Println("RawIQtime:", err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
case "jabber:iq:roster":
|
|
|
|
var item rosterItem
|
|
|
|
if v.Type != "result" && v.Type != "set" {
|
|
|
|
// only result and set processed
|
|
|
|
fmt.Println("jabber:iq:roster, type:", v.Type)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
vv := strings.Split(v.Query, "/>")
|
|
|
|
for _, ss := range vv {
|
|
|
|
if strings.TrimSpace(ss) == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ss += "/>"
|
|
|
|
if err := xml.Unmarshal([]byte(ss), &item); err != nil {
|
|
|
|
fmt.Println("unmarshal roster <query>: ", err)
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
if item.Subscription == "remove" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
//may loop whiel presence is unavailable
|
|
|
|
if item.Subscription == "from" {
|
|
|
|
fmt.Printf("%s Approve %s subscription\n",
|
|
|
|
v.To, item.Jid)
|
|
|
|
talk.RequestSubscription(item.Jid)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
fmt.Printf("roster item %s subscription(%s), %v\n",
|
|
|
|
item.Jid, item.Subscription, item.Group)
|
|
|
|
if v.Type == "set" && item.Subscription == "both" {
|
|
|
|
// shall we check presence unavailable
|
|
|
|
pr := xmpp.Presence{From: v.To, To: item.Jid,
|
|
|
|
Show: "xa"}
|
|
|
|
talk.SendPresence(pr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2019-01-09 01:35:00 -08:00
|
|
|
if v.Type == "result" && v.ID == "c2s1" {
|
|
|
|
fmt.Printf("Got pong from %s to %s\n", v.From, v.To)
|
2019-01-09 22:46:50 -08:00
|
|
|
} else {
|
|
|
|
fmt.Printf("Got from %s to %s IQ, tag: (%v), query(%s)\n",
|
|
|
|
v.From, v.To, v.QueryName, v.Query)
|
2019-01-09 01:35:00 -08:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
fmt.Printf("def: %v\n", v)
|
2013-02-11 17:43:33 -08:00
|
|
|
}
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|
|
|
|
}()
|
2019-01-09 01:35:00 -08:00
|
|
|
// get roster first
|
|
|
|
talk.Roster()
|
2019-01-10 19:20:54 -08:00
|
|
|
//talk.RevokeSubscription("wkpb@hot-chilli.net")
|
|
|
|
//talk.SendOrg("<presence from='wkpb@hot-chilli.net' to='kjx@hot-chilli.net' type='subscribe'/>")
|
2019-01-09 22:46:50 -08:00
|
|
|
// test conf
|
2019-01-10 19:20:54 -08:00
|
|
|
talk.JoinMUCNoHistory("test@conference.jabb3r.org", "bot")
|
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
|
|
|
|
}
|
2019-01-09 01:35:00 -08:00
|
|
|
if len(line) >= 4 && line[:4] == "quit" {
|
|
|
|
break
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
2019-01-09 01:35:00 -08:00
|
|
|
talk.SendOrg("</stream:stream")
|
|
|
|
time.Sleep(time.Second * 2)
|
2011-02-27 18:44:24 -08:00
|
|
|
}
|