From 66c008d79892996e0263086c3acf1f6f6b32147d Mon Sep 17 00:00:00 2001 From: Jesse Kuang <2829915232@qq.com> Date: Thu, 10 Jan 2019 22:53:01 +0800 Subject: [PATCH] add iq:version, iq:last --- xmpp.go | 21 ++++++++++++++++----- xmpp_version.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 xmpp_version.go diff --git a/xmpp.go b/xmpp.go index b7c89f3..bb0b454 100644 --- a/xmpp.go +++ b/xmpp.go @@ -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) { } // // 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" { diff --git a/xmpp_version.go b/xmpp_version.go new file mode 100644 index 0000000..b13f74e --- /dev/null +++ b/xmpp_version.go @@ -0,0 +1,32 @@ +package xmpp + +import ( + "fmt" + "runtime" + "time" +) + +func (c *Client) SendVersion(id, toServer, fromU string) error { + _, err := fmt.Fprintf(c.conn, "", xmlEscape(fromU), xmlEscape(toServer), xmlEscape(id)) + if err != nil { + return err + } + _, err = fmt.Fprintf(c.conn, ""+ + "go-xmpp0.1%s"+ + "\n", runtime.GOOS) + return err +} + +func (c *Client) SendIQLast(id, toServer, fromU string) error { + _, err := fmt.Fprintf(c.conn, "\n", xmlEscape(fromU), + xmlEscape(toServer), xmlEscape(id)) + if err != nil { + return err + } + tt := time.Now().Sub(c.loginTime) + _, err = fmt.Fprintf(c.conn, "Working\n", int(tt.Seconds())) + return err +}