add iq:version, iq:last

This commit is contained in:
Jesse Kuang 2019-01-10 22:53:01 +08:00
parent 224305b3ef
commit 66c008d798
2 changed files with 48 additions and 5 deletions

21
xmpp.go
View File

@ -62,10 +62,11 @@ func getCookie() Cookie {
// Client holds XMPP connection opitons
type Client struct {
conn net.Conn // connection to server
jid string // Jabber ID for our connection
domain string
p *xml.Decoder
conn net.Conn // connection to server
jid string // Jabber ID for our connection
domain string
loginTime time.Time
p *xml.Decoder
}
func (c *Client) JID() string {
@ -257,6 +258,7 @@ func (o Options) NewClient() (*Client, error) {
client.Close()
return nil, err
}
client.loginTime = time.Now()
return client, nil
}
@ -666,7 +668,16 @@ func (c *Client) Recv() (stanza interface{}, err error) {
}
// <query xmlns='jabber:iq:roster' ver='5'>
// TODO: shall we check XMLName.Local is "query"?
if v.Query.XMLName.Space == "jabber:iq:roster" {
switch v.Query.XMLName.Space {
case "jabber:iq:version":
if err := c.SendVersion(v.ID, v.From, v.To); err != nil {
return Chat{}, err
}
case "jabber:iq:last":
if err := c.SendIQLast(v.ID, v.From, v.To); err != nil {
return Chat{}, err
}
case "jabber:iq:roster":
var item rosterItem
var r Roster
if v.Type != "result" && v.Type != "set" {

32
xmpp_version.go Normal file
View File

@ -0,0 +1,32 @@
package xmpp
import (
"fmt"
"runtime"
"time"
)
func (c *Client) SendVersion(id, toServer, fromU string) error {
_, err := fmt.Fprintf(c.conn, "<iq type='result' from='%s' to='%s'"+
" id='%s'>", xmlEscape(fromU), xmlEscape(toServer), xmlEscape(id))
if err != nil {
return err
}
_, err = fmt.Fprintf(c.conn, "<query xmlns='jabber:iq:version'>"+
"<name>go-xmpp</name><version>0.1</version><os>%s</os>"+
"</query>\n</iq>", runtime.GOOS)
return err
}
func (c *Client) SendIQLast(id, toServer, fromU string) error {
_, err := fmt.Fprintf(c.conn, "<iq type='result' from='%s' to='%s'"+
"id='%s' type='result'>\n", xmlEscape(fromU),
xmlEscape(toServer), xmlEscape(id))
if err != nil {
return err
}
tt := time.Now().Sub(c.loginTime)
_, err = fmt.Fprintf(c.conn, "<query xmlns='jabber:iq:last' "+
"seconds='%d'>Working</query>\n</iq>", int(tt.Seconds()))
return err
}