fix query roster

process subscription="remove" roster
improve roster process
This commit is contained in:
Jesse Kuang 2019-01-09 17:35:00 +08:00
parent 2c5079ea28
commit c18873b880
4 changed files with 139 additions and 19 deletions

25
.gitignore vendored Normal file
View File

@ -0,0 +1,25 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so
# Folders
_obj
_test
# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
*.exe
*.test
*.swp
bin/

31
GNUmakefile Normal file
View File

@ -0,0 +1,31 @@
#
# Makefile for hookAPI
#
# switches:
# define the ones you want in the CFLAGS definition...
#
# TRACE - turn on tracing/debugging code
#
#
#
#
# Version for distribution
VER=1_0r1
MAKEFILE=GNUmakefile
# We Use Compact Memory Model
all: bin/example
@[ -d bin ] || exit
bin/example: _example/example.go xmpp.go
@[ -d bin ] || mkdir bin
go build -o $@ _example/example.go
@strip $@ || echo "example OK"
clean:
distclean: clean
@rm -rf bin

View File

@ -5,10 +5,11 @@ import (
"crypto/tls" "crypto/tls"
"flag" "flag"
"fmt" "fmt"
"github.com/mattn/go-xmpp" "github.com/kjx98/go-xmpp"
"log" "log"
"os" "os"
"strings" "strings"
"time"
) )
var server = flag.String("server", "", "server") var server = flag.String("server", "", "server")
@ -16,7 +17,7 @@ var username = flag.String("username", "", "username")
var password = flag.String("password", "", "password") var password = flag.String("password", "", "password")
var status = flag.String("status", "xa", "status") var status = flag.String("status", "xa", "status")
var statusMessage = flag.String("status-msg", "I for one welcome our new codebot overlords.", "status message") var statusMessage = flag.String("status-msg", "I for one welcome our new codebot overlords.", "status message")
var notls = flag.Bool("notls", false, "No TLS") var notls = flag.Bool("notls", true, "No TLS")
var debug = flag.Bool("debug", false, "debug output") var debug = flag.Bool("debug", false, "debug output")
var session = flag.Bool("session", false, "use server session") var session = flag.Bool("session", false, "use server session")
@ -55,6 +56,7 @@ func main() {
Debug: *debug, Debug: *debug,
Session: *session, Session: *session,
Status: *status, Status: *status,
Resource: "bot",
StatusMessage: *statusMessage, StatusMessage: *statusMessage,
} }
@ -72,18 +74,52 @@ func main() {
} }
switch v := chat.(type) { switch v := chat.(type) {
case xmpp.Chat: case xmpp.Chat:
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) fmt.Println(v.Remote, v.Text)
}
}
case xmpp.Presence: case xmpp.Presence:
fmt.Println(v.From, v.Show) fmt.Println("Presence:", v.From, v.Show, v.Type)
case xmpp.Roster, xmpp.Contact:
// TODO: update local roster
fmt.Println("Roster/Contact:", v)
case xmpp.IQ:
// ping ignore
if v.Type == "result" && v.ID == "c2s1" {
fmt.Printf("Got pong from %s to %s\n", v.From, v.To)
}
default:
fmt.Printf("def: %v\n", v)
} }
} }
}() }()
// get roster first
talk.Roster()
talk.SendOrg("<presence/>")
for { for {
in := bufio.NewReader(os.Stdin) in := bufio.NewReader(os.Stdin)
line, err := in.ReadString('\n') line, err := in.ReadString('\n')
if err != nil { if err != nil {
continue continue
} }
if len(line) >= 4 && line[:4] == "quit" {
break
}
line = strings.TrimRight(line, "\n") line = strings.TrimRight(line, "\n")
tokens := strings.SplitN(line, " ", 2) tokens := strings.SplitN(line, " ", 2)
@ -91,4 +127,6 @@ func main() {
talk.Send(xmpp.Chat{Remote: tokens[0], Type: "chat", Text: tokens[1]}) talk.Send(xmpp.Chat{Remote: tokens[0], Type: "chat", Text: tokens[1]})
} }
} }
talk.SendOrg("</stream:stream")
time.Sleep(time.Second * 2)
} }

42
xmpp.go
View File

@ -655,13 +655,38 @@ func (c *Client) Recv() (stanza interface{}, err error) {
return Presence{v.From, v.To, v.Type, v.Show, v.Status}, nil return Presence{v.From, v.To, v.Type, v.Show, v.Status}, nil
case *clientIQ: case *clientIQ:
// TODO check more strictly // TODO check more strictly
if bytes.Equal(bytes.TrimSpace(v.Query), []byte(`<ping xmlns='urn:xmpp:ping'/>`)) || bytes.Equal(bytes.TrimSpace(v.Query), []byte(`<ping xmlns="urn:xmpp:ping"/>`)) { if v.Query.XMLName.Space == "urn:xmpp:ping" {
fmt.Println("clientIQ ping")
err := c.SendResultPing(v.ID, v.From) err := c.SendResultPing(v.ID, v.From)
if err != nil { if err != nil {
return Chat{}, err return Chat{}, err
} }
} }
return IQ{ID: v.ID, From: v.From, To: v.To, Type: v.Type, Query: v.Query}, nil // <query xmlns='jabber:iq:roster' ver='5'>
// TODO: shall we check XMLName.Local is "query"?
if (v.Type == "result" || v.Type == "set") &&
v.Query.XMLName.Space == "jabber:iq:roster" {
var item rosterItem
var r Roster
vv := strings.Split(v.Query.InnerXML, "/>")
for _, ss := range vv {
if strings.TrimSpace(ss) == "" {
continue
}
ss += "/>"
if err := xml.Unmarshal([]byte(ss), &item); err != nil {
return nil, errors.New("unmarshal roster <query>: " + err.Error())
} else {
if item.Subscription == "remove" {
continue
}
r = append(r, Contact{item.Jid, item.Name, item.Group})
}
}
return Chat{Type: "roster", Roster: r}, nil
}
return IQ{ID: v.ID, From: v.From, To: v.To, Type: v.Type,
Query: []byte(v.Query.InnerXML)}, nil
} }
} }
} }
@ -862,7 +887,8 @@ type clientIQ struct {
ID string `xml:"id,attr"` ID string `xml:"id,attr"`
To string `xml:"to,attr"` To string `xml:"to,attr"`
Type string `xml:"type,attr"` // error, get, result, set Type string `xml:"type,attr"` // error, get, result, set
Query []byte `xml:",innerxml"` Query XMLElement `xml:",any"`
Error clientError Error clientError
Bind bindBind Bind bindBind
} }
@ -880,11 +906,11 @@ type clientQuery struct {
} }
type rosterItem struct { type rosterItem struct {
XMLName xml.Name `xml:"jabber:iq:roster item"` XMLName xml.Name `xml:"item"`
Jid string `xml:",attr"` Jid string `xml:"jid,attr"`
Name string `xml:",attr"` Name string `xml:"name,attr"`
Subscription string `xml:",attr"` Subscription string `xml:"subscription,attr"`
Group []string Group []string `"xml:"group"`
} }
// Scan XML token stream to find next StartElement. // Scan XML token stream to find next StartElement.