2018-12-26 09:50:01 -08:00
|
|
|
package xmpp // import "gosrc.io/xmpp"
|
2016-01-06 07:51:12 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/xml"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strings"
|
2016-02-17 04:45:39 -08:00
|
|
|
"time"
|
2016-01-06 07:51:12 -08:00
|
|
|
)
|
|
|
|
|
2019-06-06 02:58:50 -07:00
|
|
|
//=============================================================================
|
2019-06-08 09:09:22 -07:00
|
|
|
// EventManager
|
2018-09-26 08:26:14 -07:00
|
|
|
|
2019-06-06 02:58:50 -07:00
|
|
|
// ConnState represents the current connection state.
|
|
|
|
type ConnState = uint8
|
2018-09-26 08:26:14 -07:00
|
|
|
|
2019-06-06 02:58:50 -07:00
|
|
|
// This is a the list of events happening on the connection that the
|
|
|
|
// client can be notified about.
|
|
|
|
const (
|
|
|
|
StateDisconnected ConnState = iota
|
|
|
|
StateConnected
|
|
|
|
StateSessionEstablished
|
2019-06-08 02:15:51 -07:00
|
|
|
StateStreamError
|
2019-06-06 02:58:50 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// Event is a structure use to convey event changes related to client state. This
|
|
|
|
// is for example used to notify the client when the client get disconnected.
|
|
|
|
type Event struct {
|
|
|
|
State ConnState
|
|
|
|
Description string
|
2019-06-08 02:15:51 -07:00
|
|
|
StreamError string
|
2018-09-26 08:26:14 -07:00
|
|
|
}
|
|
|
|
|
2019-06-06 02:58:50 -07:00
|
|
|
// EventHandler is use to pass events about state of the connection to
|
|
|
|
// client implementation.
|
|
|
|
type EventHandler func(Event)
|
|
|
|
|
|
|
|
type EventManager struct {
|
|
|
|
// Store current state
|
|
|
|
CurrentState ConnState
|
|
|
|
|
|
|
|
// Callback used to propagate connection state changes
|
|
|
|
Handler EventHandler
|
2018-09-26 08:26:14 -07:00
|
|
|
}
|
|
|
|
|
2019-06-06 02:58:50 -07:00
|
|
|
func (em EventManager) updateState(state ConnState) {
|
|
|
|
em.CurrentState = state
|
|
|
|
if em.Handler != nil {
|
|
|
|
em.Handler(Event{State: em.CurrentState})
|
|
|
|
}
|
2018-09-26 08:26:14 -07:00
|
|
|
}
|
|
|
|
|
2019-06-08 02:15:51 -07:00
|
|
|
func (em EventManager) streamError(error, desc string) {
|
|
|
|
em.CurrentState = StateStreamError
|
|
|
|
if em.Handler != nil {
|
|
|
|
em.Handler(Event{State: em.CurrentState, StreamError: error, Description: desc})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 08:26:14 -07:00
|
|
|
// Client
|
|
|
|
// ============================================================================
|
|
|
|
|
2018-02-13 13:07:15 -08:00
|
|
|
// Client is the main structure used to connect as a client on an XMPP
|
2016-02-15 02:05:44 -08:00
|
|
|
// server.
|
2016-01-06 07:51:12 -08:00
|
|
|
type Client struct {
|
2019-06-06 02:58:50 -07:00
|
|
|
// Store user defined options and states
|
2018-09-26 07:25:04 -07:00
|
|
|
config Config
|
2016-01-06 07:51:12 -08:00
|
|
|
// Session gather data that can be accessed by users of this library
|
|
|
|
Session *Session
|
2017-10-21 04:58:58 -07:00
|
|
|
// TCP level connection / can be replaced by a TLS session after starttls
|
2016-01-06 07:51:12 -08:00
|
|
|
conn net.Conn
|
2019-06-06 02:58:50 -07:00
|
|
|
// Packet channel
|
2019-06-10 01:58:41 -07:00
|
|
|
RecvChannel chan Packet
|
2019-06-06 02:58:50 -07:00
|
|
|
// Track and broadcast connection state
|
|
|
|
EventManager
|
2016-01-06 07:51:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Setting up the client / Checking the parameters
|
|
|
|
*/
|
|
|
|
|
2018-09-26 07:25:04 -07:00
|
|
|
// NewClient generates a new XMPP client, based on Config passed as parameters.
|
2018-09-23 09:43:46 -07:00
|
|
|
// If host is not specified, the DNS SRV should be used to find the host from the domainpart of the JID.
|
2016-02-13 08:01:06 -08:00
|
|
|
// Default the port to 5222.
|
2018-09-26 07:25:04 -07:00
|
|
|
// TODO: better config checks
|
|
|
|
func NewClient(config Config) (c *Client, err error) {
|
2016-01-06 07:51:12 -08:00
|
|
|
// TODO: If option address is nil, use the Jid domain to compose the address
|
2018-09-26 07:25:04 -07:00
|
|
|
if config.Address, err = checkAddress(config.Address); err != nil {
|
2019-06-07 06:23:23 -07:00
|
|
|
return nil, NewConnError(err, true)
|
2016-01-06 07:51:12 -08:00
|
|
|
}
|
|
|
|
|
2018-09-26 07:25:04 -07:00
|
|
|
if config.Password == "" {
|
2016-01-06 07:51:12 -08:00
|
|
|
err = errors.New("missing password")
|
2019-06-07 06:23:23 -07:00
|
|
|
return nil, NewConnError(err, true)
|
2016-01-06 07:51:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse JID
|
2019-06-07 06:23:23 -07:00
|
|
|
if config.parsedJid, err = NewJid(config.Jid); err != nil {
|
|
|
|
err = errors.New("missing jid")
|
|
|
|
return nil, NewConnError(err, true)
|
2016-01-06 07:51:12 -08:00
|
|
|
}
|
|
|
|
|
2019-06-07 03:16:58 -07:00
|
|
|
c = new(Client)
|
|
|
|
c.config = config
|
|
|
|
|
2018-09-26 07:25:04 -07:00
|
|
|
if c.config.ConnectTimeout == 0 {
|
|
|
|
c.config.ConnectTimeout = 15 // 15 second as default
|
2016-02-17 04:45:39 -08:00
|
|
|
}
|
2019-06-06 02:58:50 -07:00
|
|
|
|
2019-06-09 03:48:22 -07:00
|
|
|
// Create a default channel that developers can override
|
2019-06-10 01:58:41 -07:00
|
|
|
c.RecvChannel = make(chan Packet)
|
2019-06-06 02:58:50 -07:00
|
|
|
|
2016-01-06 07:51:12 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-23 09:43:46 -07:00
|
|
|
// TODO Pass JID to be able to add default address based on JID, if addr is empty
|
2016-01-06 07:51:12 -08:00
|
|
|
func checkAddress(addr string) (string, error) {
|
|
|
|
var err error
|
|
|
|
hostport := strings.Split(addr, ":")
|
|
|
|
if len(hostport) > 2 {
|
|
|
|
err = errors.New("too many colons in xmpp server address")
|
|
|
|
return addr, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Address is composed of two parts, we are good
|
|
|
|
if len(hostport) == 2 && hostport[1] != "" {
|
|
|
|
return addr, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Port was not passed, we append XMPP default port:
|
|
|
|
return strings.Join([]string{hostport[0], "5222"}, ":"), err
|
|
|
|
}
|
|
|
|
|
2016-02-13 08:01:06 -08:00
|
|
|
// Connect triggers actual TCP connection, based on previously defined parameters.
|
2019-06-08 09:09:22 -07:00
|
|
|
func (c *Client) Connect() error {
|
2016-01-06 07:51:12 -08:00
|
|
|
var err error
|
2016-02-17 04:45:39 -08:00
|
|
|
|
2019-06-06 02:58:50 -07:00
|
|
|
c.conn, err = net.DialTimeout("tcp", c.config.Address, time.Duration(c.config.ConnectTimeout)*time.Second)
|
2016-02-17 04:45:39 -08:00
|
|
|
if err != nil {
|
2019-06-08 09:09:22 -07:00
|
|
|
return err
|
2016-01-06 07:51:12 -08:00
|
|
|
}
|
2019-06-06 02:58:50 -07:00
|
|
|
c.updateState(StateConnected)
|
2016-01-06 07:51:12 -08:00
|
|
|
|
2019-06-08 09:52:19 -07:00
|
|
|
// Client is ok, we now open XMPP session
|
2018-09-26 07:25:04 -07:00
|
|
|
if c.conn, c.Session, err = NewSession(c.conn, c.config); err != nil {
|
2019-06-08 09:09:22 -07:00
|
|
|
return err
|
2016-01-06 07:51:12 -08:00
|
|
|
}
|
2019-06-06 02:58:50 -07:00
|
|
|
c.updateState(StateSessionEstablished)
|
2016-01-06 07:51:12 -08:00
|
|
|
|
|
|
|
// We're connected and can now receive and send messages.
|
|
|
|
//fmt.Fprintf(client.conn, "<presence xml:lang='en'><show>%s</show><status>%s</status></presence>", "chat", "Online")
|
2016-01-06 08:08:51 -08:00
|
|
|
// TODO: Do we always want to send initial presence automatically ?
|
|
|
|
// Do we need an option to avoid that or do we rely on client to send the presence itself ?
|
2016-01-06 07:51:12 -08:00
|
|
|
fmt.Fprintf(c.Session.socketProxy, "<presence/>")
|
|
|
|
|
2019-06-06 02:58:50 -07:00
|
|
|
// Start the receiver go routine
|
|
|
|
go c.recv()
|
|
|
|
|
2019-06-08 09:09:22 -07:00
|
|
|
return err
|
2016-01-06 07:51:12 -08:00
|
|
|
}
|
|
|
|
|
2019-06-06 02:58:50 -07:00
|
|
|
func (c *Client) Disconnect() {
|
|
|
|
_ = c.SendRaw("</stream:stream>")
|
|
|
|
// TODO: Add a way to wait for stream close acknowledgement from the server for clean disconnect
|
|
|
|
_ = c.conn.Close()
|
|
|
|
}
|
|
|
|
|
2019-06-08 09:09:22 -07:00
|
|
|
func (c *Client) SetHandler(handler EventHandler) {
|
|
|
|
c.Handler = handler
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recv abstracts receiving preparsed XMPP packets from a channel.
|
|
|
|
// Channel allow client to receive / dispatch packets in for range loop.
|
|
|
|
// TODO: Deprecate this function in favor of reading directly from the RecvChannel ?
|
2019-06-10 01:58:41 -07:00
|
|
|
func (c *Client) Recv() <-chan Packet {
|
2019-06-08 09:09:22 -07:00
|
|
|
return c.RecvChannel
|
|
|
|
}
|
|
|
|
|
2019-06-06 02:58:50 -07:00
|
|
|
func (c *Client) recv() (err error) {
|
2016-01-06 07:51:12 -08:00
|
|
|
for {
|
2018-01-13 09:50:17 -08:00
|
|
|
val, err := next(c.Session.decoder)
|
2016-01-06 07:51:12 -08:00
|
|
|
if err != nil {
|
2019-06-06 02:58:50 -07:00
|
|
|
c.updateState(StateDisconnected)
|
2016-01-06 07:51:12 -08:00
|
|
|
return err
|
|
|
|
}
|
2019-06-08 02:15:51 -07:00
|
|
|
|
|
|
|
// Handle stream errors
|
|
|
|
switch packet := val.(type) {
|
|
|
|
case StreamError:
|
|
|
|
c.RecvChannel <- val
|
|
|
|
close(c.RecvChannel)
|
|
|
|
c.streamError(packet.Error.Local, packet.Text)
|
|
|
|
return errors.New("stream error: " + packet.Error.Local)
|
|
|
|
}
|
|
|
|
|
2019-06-06 02:58:50 -07:00
|
|
|
c.RecvChannel <- val
|
2016-01-06 07:51:12 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-26 00:55:39 -08:00
|
|
|
// Send marshalls XMPP stanza and sends it to the server.
|
|
|
|
func (c *Client) Send(packet Packet) error {
|
|
|
|
data, err := xml.Marshal(packet)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("cannot marshal packet " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := fmt.Fprintf(c.conn, string(data)); err != nil {
|
|
|
|
return errors.New("cannot send packet " + err.Error())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendRaw sends an XMPP stanza as a string to the server.
|
|
|
|
// It can be invalid XML or XMPP content. In that case, the server will
|
|
|
|
// disconnect the client. It is up to the user of this method to
|
|
|
|
// carefully craft the XML content to produce valid XMPP.
|
|
|
|
func (c *Client) SendRaw(packet string) error {
|
2019-06-06 02:58:50 -07:00
|
|
|
var err error
|
|
|
|
_, err = fmt.Fprintf(c.Session.socketProxy, packet)
|
|
|
|
return err
|
2016-01-06 07:51:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func xmlEscape(s string) string {
|
|
|
|
var b bytes.Buffer
|
|
|
|
xml.Escape(&b, []byte(s))
|
|
|
|
return b.String()
|
|
|
|
}
|