xmpp

package module
v0.0.0-...-ee83f8d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 19, 2019 License: MIT Imports: 17 Imported by: 11

README

go-xmpp

This library lets you open a XMPP stream over a TCP connection (or any other io.ReadWriter) and exchange XMPP protocol messages.

This is a work in progress in its early stages, some things still don't work, some might change drastically. Not for use in production environments.

Quick start

tcp, _ := net.Dial("tcp", "server.com:5222")

stream := &xmpp.StreamHeader{
    Namespace: xmpp.NamespaceClient,
    To:        "server.com",
}

conn, err := xmpp.Open(tcp, stream) // Open a bidirectional XMPP stream over the TCP connection (as a client)

conn.Features() // returns *xmpp.Features with stream features
conn.Write(&xmpp.StartTLS{}) // Send a starttls message
conn.Read() // Read an XMPP protocol message from the stream
conn.Close() // Close the XMPP stream

Documentation

Index

Constants

View Source
const (
	TLSRequired = iota
	TLSPreferred
	TLSDisabled
)
View Source
const (
	Away = "away"
	Chat = "chat"
	DND  = "dnd"
	XA   = "xa"
)
View Source
const (
	NamespaceClient = "jabber:client"
	NamespaceServer = "jabber:server"
	DefaultVersion  = "1.0"
)

Stream namespaces

Variables

View Source
var DefinedConditions = []string{
	"bad-format",
	"bad-namespace-prefix",
	"conflict",
	"connection-timeout",
	"host-gone",
	"host-unknown",
	"improper-addressing",
	"internal-server-error",
	"invalid-from",
	"invalid-namespace",
	"invalid-xml",
	"not-authorized",
	"not-well-formed",
	"policy-violation",
	"remote-connection-failed",
	"reset",
	"resource-constraint",
	"restricted-xml",
	"see-other-host",
	"system-shutdown",
	"undefined-condition",
	"unsupported-encoding",
	"unsupported-feature",
	"unsupported-stanza-type",
	"unsupported-version",
}

DefinedConditions is a list of conditions defined by RFC6120

View Source
var ErrEndOfElement = errors.New("end of element")
View Source
var ErrEndOfStream = errors.New("end of stream")

ErrEndOfStream indicates the XMPP stream has ended

View Source
var ErrNoPrototype = errors.New("prototype not found")
View Source
var ErrStreamError = errors.New("stream error")

ErrStreamError indicates invalid stream data

Functions

func AddElement

func AddElement(e interface{})

func GetElement

func GetElement(name xml.Name) interface{}

func HandleStanza

func HandleStanza(handler StanzaHandler, stanza Stanza) bool

HandleStanza routes a stanza to a typed stanza handler

func Identify

func Identify(s interface{}) (id xml.Name)

Identify returns an XMPP message name and namespace (can be empty)

func Open

func Open(handler Handler, cfg *Config) error

Open opens a new session using provided config and routes session events to the provided handler

Types

type Abort

type Abort struct {
	XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl abort"`
}

type Auth

type Auth struct {
	XMLName   xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl auth"`
	Mechanism string   `xml:"mechanism,attr"`
	Data      string   `xml:",chardata"`
}

type Authenticator

type Authenticator interface {
	Name() string
	Data() string
	Challenge(string) string
}

Authenticator defines an interface for SASL authentication

func NewPlainAuthenticator

func NewPlainAuthenticator(creds Credentials) Authenticator

NewPlainAuthenticator instantiates a PLAIN authenticator using provided credentials

func NewScramSHA1Authenticator

func NewScramSHA1Authenticator(creds Credentials) Authenticator

type Bind

type Bind struct {
	XMLName  xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-bind bind"`
	JID      JID      `xml:"jid,omitempty"`
	Resource string   `xml:"resource,omitempty"`
}

type Broadcast

type Broadcast struct {
	// contains filtered or unexported fields
}

Broadcast routes XMPP events to mutiple handlers

func (*Broadcast) Add

func (m *Broadcast) Add(h Handler)

Add adds a handler to the router

func (*Broadcast) HandleStanza

func (m *Broadcast) HandleStanza(stanza Stanza)

HandleStanza routes a stanza

func (*Broadcast) Offline

func (m *Broadcast) Offline(e error)

Offline routes an Offline event

func (*Broadcast) Online

func (m *Broadcast) Online(s Session)

Online routes an Online event

type Callbacks

type Callbacks struct {
	Session
	// contains filtered or unexported fields
}

func (*Callbacks) Handle

func (c *Callbacks) Handle(s Stanza) bool

func (*Callbacks) WriteIQ

func (c *Callbacks) WriteIQ(iq *IQ, callback IQCallback) error

func (*Callbacks) WriteMessage

func (c *Callbacks) WriteMessage(msg *Message, callback MessageCallback) error

func (*Callbacks) WritePresence

func (c *Callbacks) WritePresence(p *Presence, callback PresenceCallback) error

type Challenge

type Challenge struct {
	XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl challenge"`
	Data    string   `xml:",chardata"`
}

type Closer

type Closer interface {
	Close() error
}

type Compression

type Compression struct {
	XMLName xml.Name `xml:"http://jabber.org/features/compress compression"`
	Methods []string `xml:"method"`
}

type Config

type Config struct {
	JID      JID
	Password string
	Host     string
	Logger   Logger
	TLSMode  int
}

Config represents XMPP client configuration

type Conn

type Conn struct {
	// contains filtered or unexported fields
}

Conn represents an XMPP connection

func Connect

func Connect(addr string, to JID, logger Logger) (*Conn, error)

Connect establishes an XMPP connection

func (*Conn) Authenticate

func (c *Conn) Authenticate(username, password string) error

Authenticate authenticates to the server and restarts the stream

func (*Conn) Bind

func (c *Conn) Bind(resourceName string) (JID, error)

Bind binds the XMPP stream to a resource name

func (*Conn) Close

func (c *Conn) Close() error

Close closes the XMPP connection

func (*Conn) Features

func (c *Conn) Features() *Features

Features returns the current stream features

func (*Conn) Read

func (c *Conn) Read() (interface{}, error)

Read reads the next XMPP message from the stream

func (*Conn) RestartStream

func (c *Conn) RestartStream(transport io.ReadWriteCloser) (err error)

RestartStream restarts the current stream. If transport is provided, it replaces the currently used transport.

func (*Conn) StartTLS

func (c *Conn) StartTLS(serverName string) error

StartTLS upgrades the connection to TLS if possible and replaces the stream

func (*Conn) Transport

func (c *Conn) Transport() io.ReadWriteCloser

Transport returns the transport used for the stream

func (*Conn) Write

func (c *Conn) Write(msg interface{}) error

Write writes an XMPP message to the stream

type Container

type Container struct {
	Children []interface{} `xml:"-"`
}

func (*Container) AddChild

func (c *Container) AddChild(child interface{})

func (*Container) Child

func (c *Container) Child(t interface{}) interface{}

Child returns the first child with a given name

func (*Container) ChildCount

func (c *Container) ChildCount(t interface{}) int

ChildCount returns the number of children with a given name

type Credentials

type Credentials struct {
	Username string
	Password string
}

Credentials holds authentication information

type Error

type Error struct {
	Condition string
	Text      string
	Extra     interface{}
}

func (*Error) MarshalXML

func (e *Error) MarshalXML(enc *xml.Encoder, start xml.StartElement) error

func (*Error) UnmarshalXML

func (e *Error) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error

type Features

type Features struct {
	XMLName xml.Name `xml:"http://etherx.jabber.org/streams features"`
	Container
}

func (*Features) MarshalXML

func (f *Features) MarshalXML(enc *xml.Encoder, start xml.StartElement) error

func (*Features) UnmarshalXML

func (f *Features) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error

type Filter

type Filter interface {
	ApplyFilter(Stanza) error
}

type Generic

type Generic struct {
	XMLName xml.Name
	Attrs   map[string]string `xml:"-"`
	Text    string            `xml:",chardata"`
	Container
}

A Generic represents an unknown XML element

func (*Generic) MarshalXML

func (g *Generic) MarshalXML(enc *xml.Encoder, start xml.StartElement) error

MarshalXML implements XML encoding

func (*Generic) UnmarshalXML

func (g *Generic) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error

UnmarshalXML implements XML decoding

type Handler

type Handler interface {
	Online(Session)
	HandleStanza(Stanza)
	Offline(error)
}

Handler defines an interface for XMPP event handlers

type IQ

type IQ struct {
	XMLName xml.Name `xml:"iq"`
	ID      string   `xml:"id,attr,omitempty"`
	To      JID      `xml:"to,attr,omitempty"`
	From    JID      `xml:"from,attr,omitempty"`
	Type    string   `xml:"type,attr,omitempty"`
	Lang    string   `xml:"lang,attr,omitempty"`
	Container
}

IQ represents an IQ stanza

func (*IQ) GetFrom

func (iq *IQ) GetFrom() JID

GetFrom returns the from field

func (*IQ) GetID

func (iq *IQ) GetID() string

GetID returns the id field

func (*IQ) GetLang

func (iq *IQ) GetLang() string

GetLang returns the lang field

func (*IQ) GetTo

func (iq *IQ) GetTo() JID

GetTo returns the to field

func (*IQ) GetType

func (iq *IQ) GetType() string

GetType returns the type field

func (*IQ) MarshalXML

func (iq *IQ) MarshalXML(enc *xml.Encoder, start xml.StartElement) error

MarshalXML marshals IQ to XML

func (*IQ) Response

func (iq *IQ) Response(items ...interface{}) *IQ

Response constructs an response IQ containing provided items

func (*IQ) Result

func (iq *IQ) Result() bool

Result returns true if the IQ type is result

func (*IQ) SetFrom

func (iq *IQ) SetFrom(s JID)

SetFrom sets the from field

func (*IQ) SetID

func (iq *IQ) SetID(s string)

SetID sets the id field

func (*IQ) SetLang

func (iq *IQ) SetLang(s string)

SetLang sets the lang field

func (*IQ) SetTo

func (iq *IQ) SetTo(s JID)

SetTo sets the to field

func (*IQ) SetType

func (iq *IQ) SetType(s string)

SetType sets the type field

func (*IQ) UnmarshalXML

func (iq *IQ) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error

UnmarshalXML unmarshals an IQ from XML

type IQCallback

type IQCallback func(*IQ)

type JID

type JID string

JID is the type used to store JIDs

func (JID) Bare

func (jid JID) Bare() JID

Bare returns the bare JID (ie. without the resource part)

func (JID) Domain

func (jid JID) Domain() JID

Domain returns the domain part of the JID

func (JID) Local

func (jid JID) Local() string

Local returns the local part of the JID

func (JID) Resource

func (jid JID) Resource() string

Resource returns the resource part of the JID

func (JID) String

func (jid JID) String() string

String satisfies Stringer interface

func (JID) Valid

func (jid JID) Valid() bool

Valid verifies the validity of the JID [STUB]

type Logger

type Logger interface {
	Received([]byte)
	Sent([]byte)
}

type Mechanisms

type Mechanisms struct {
	XMLName    xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl mechanisms"`
	Mechanisms []string `xml:"mechanism"`
}

func (*Mechanisms) Include

func (m *Mechanisms) Include(name string) bool

type Message

type Message struct {
	XMLName xml.Name `xml:"message"`
	ID      string   `xml:"id,attr,omitempty"`
	To      JID      `xml:"to,attr,omitempty"`
	From    JID      `xml:"from,attr,omitempty"`
	Type    string   `xml:"type,attr,omitempty"`
	Lang    string   `xml:"lang,attr,omitempty"`
	Subject string   `xml:"subject,omitempty"`
	Body    string   `xml:"body,omitempty"`
	Thread  string   `xml:"thread,omitempty"`
	Container
}

A Message represents a message stanza

func (*Message) Chat

func (m *Message) Chat() bool

Chat returns true if the message is of type chat

func (*Message) Error

func (m *Message) Error() bool

Chat returns true if the message is of type error

func (*Message) GetFrom

func (m *Message) GetFrom() JID

GetFrom returns the from field

func (*Message) GetID

func (m *Message) GetID() string

GetID returns the id field

func (*Message) GetLang

func (m *Message) GetLang() string

GetLang returns the lang field

func (*Message) GetTo

func (m *Message) GetTo() JID

GetTo returns the to field

func (*Message) GetType

func (m *Message) GetType() string

GetType returns the type field

func (*Message) Headline

func (m *Message) Headline() bool

Chat returns true if the message is of type headline

func (*Message) MarshalXML

func (m *Message) MarshalXML(enc *xml.Encoder, start xml.StartElement) error

func (*Message) Normal

func (m *Message) Normal() bool

Normal returns true if the message is of type normal

func (*Message) Reply

func (m *Message) Reply(format string, a ...interface{}) *Message

Reply builds a reply struct to the message

func (*Message) SetFrom

func (m *Message) SetFrom(s JID)

SetFrom sets the from field

func (*Message) SetID

func (m *Message) SetID(s string)

SetID sets the id field

func (*Message) SetLang

func (m *Message) SetLang(s string)

SetLang sets the lang field

func (*Message) SetTo

func (m *Message) SetTo(s JID)

SetTo sets the to field

func (*Message) SetType

func (m *Message) SetType(s string)

SetType sets the type field

func (*Message) UnmarshalXML

func (m *Message) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error

type MessageCallback

type MessageCallback func(*Message)

type PlainAuth

type PlainAuth struct {
	Username string
	Password string
}

PlainAuth implements a plain SASL authentication

func (*PlainAuth) Challenge

func (auth *PlainAuth) Challenge(string) string

Challenge satisfies Authenticator interface. It returns an empty string since PLAIN mechanism doesn't support challenges.

func (*PlainAuth) Data

func (auth *PlainAuth) Data() string

Data returns authentication data encoded for PLAIN mechanism

func (*PlainAuth) Name

func (auth *PlainAuth) Name() string

Name returns the name of the authenticator

type Presence

type Presence struct {
	XMLName  xml.Name `xml:"presence"`
	ID       string   `xml:"id,attr,omitempty"`
	To       JID      `xml:"to,attr,omitempty"`
	From     JID      `xml:"from,attr,omitempty"`
	Type     string   `xml:"type,attr,omitempty"`
	Lang     string   `xml:"lang,attr,omitempty"`
	Show     string   `xml:"show,omitempty"`
	Status   string   `xml:"status,omitempty"`
	Priority int      `xml:"priority,omitempty"`
	Container
}

func (*Presence) GetFrom

func (m *Presence) GetFrom() JID

GetFrom returns the from field

func (*Presence) GetID

func (m *Presence) GetID() string

GetID returns the id field

func (*Presence) GetLang

func (m *Presence) GetLang() string

GetLang returns the lang field

func (*Presence) GetTo

func (m *Presence) GetTo() JID

GetTo returns the to field

func (*Presence) GetType

func (m *Presence) GetType() string

GetType returns the type field

func (*Presence) MarshalXML

func (p *Presence) MarshalXML(enc *xml.Encoder, start xml.StartElement) error

func (*Presence) SetFrom

func (m *Presence) SetFrom(s JID)

SetFrom sets the from field

func (*Presence) SetID

func (m *Presence) SetID(s string)

SetID sets the id field

func (*Presence) SetLang

func (m *Presence) SetLang(s string)

SetLang sets the lang field

func (*Presence) SetTo

func (m *Presence) SetTo(s JID)

SetTo sets the to field

func (*Presence) SetType

func (m *Presence) SetType(s string)

SetType sets the type field

func (*Presence) UnmarshalXML

func (p *Presence) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error

type PresenceCallback

type PresenceCallback func(*Presence)

type Proceed

type Proceed struct {
	XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-tls proceed"`
}

type ReadWriteCloser

type ReadWriteCloser interface {
	Reader
	Writer
	Closer
}

type ReadWriter

type ReadWriter interface {
	Reader
	Writer
}

type Reader

type Reader interface {
	Read() (interface{}, error)
}

type Register

type Register struct {
	XMLName xml.Name `xml:"http://jabber.org/features/iq-register register"`
}

type Response

type Response struct {
	XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl response"`
	Data    string   `xml:",chardata"`
}

type RosterItem

type RosterItem struct {
	XMLName      xml.Name `xml:"item"`
	JID          JID      `xml:"jid,attr"`
	Name         string   `xml:"name,attr,omitempty"`
	Subscription string   `xml:"subscription,attr,omitempty"`
	Group        []string `xml:"group,omitempty"`
}

RosterItem represents a roster item

type RosterQuery

type RosterQuery struct {
	XMLName xml.Name     `xml:"jabber:iq:roster query"`
	Items   []RosterItem `xml:"item"`
}

RosterQuery represents a roster query element

type SASLFailure

type SASLFailure struct {
	XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl failure"`
}

type ScramAuth

type ScramAuth struct {
	Username string
	Password string
	// contains filtered or unexported fields
}

func (*ScramAuth) Challenge

func (auth *ScramAuth) Challenge(challenge string) string

func (*ScramAuth) Data

func (auth *ScramAuth) Data() string

func (*ScramAuth) Name

func (auth *ScramAuth) Name() string

type Session

type Session interface {
	JID() JID
	Write(Stanza) error
	Close(error)
	AddFilter(Filter)
}

Session defines an interface for session

type Stanza

type Stanza interface {
	GetID() string
	GetFrom() JID
	GetTo() JID
	GetType() string
	GetLang() string

	SetID(string)
	SetFrom(JID)
	SetTo(JID)
	SetType(string)
	SetLang(string)
}

type StanzaHandler

type StanzaHandler interface{}

StanzaHandler defines an interface for specific stanza handler

type StartTLS

type StartTLS struct {
	XMLName  xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-tls starttls"`
	Required *string  `xml:"required,omitempty"`
}

type Stream

type Stream struct {
	// contains filtered or unexported fields
}

Stream manages a bidirectional XMPP stream using an underlying transport

func NewStream

func NewStream(transport io.ReadWriter) *Stream

NewStream instantiates a new stream using the provided transport

func (*Stream) Close

func (s *Stream) Close() error

Close closes the XMPP stream. No writes can be performed afterwards.

func (*Stream) Read

func (s *Stream) Read() (interface{}, error)

Read reads and returns a message from the stream

func (*Stream) ReadHeader

func (s *Stream) ReadHeader() (*StreamHeader, error)

ReadHeader reads and returns a stream header. It ignores the first ProcInst read from the stream.

func (*Stream) Transport

func (s *Stream) Transport() io.ReadWriter

Transport returns the underlying transport object

func (*Stream) Write

func (s *Stream) Write(msg interface{}) error

Write writes an XMPP message to the stream

func (*Stream) WriteHeader

func (s *Stream) WriteHeader(stream *StreamHeader) error

WriteHeader writes a stream header to the stream.

type StreamHeader

type StreamHeader struct {
	Version   string
	Namespace string
	From      JID
	To        JID
	ID        string
}

StreamHeader represents stream info exchanged in the initial <stream/> element

func NewHeader

func NewHeader(namesapce string, from JID, to JID) *StreamHeader

NewHeader returns a stream header in client protocol namespace

func ParseStreamHeader

func ParseStreamHeader(start *xml.StartElement) *StreamHeader

ParseStreamHeader decodes the opening of the XML stream without touching the content of the stream

func (*StreamHeader) Reply

func (header *StreamHeader) Reply(id string) *StreamHeader

Reply returns a server stream header

func (*StreamHeader) XMLStartElement

func (header *StreamHeader) XMLStartElement() (start xml.StartElement)

XMLStartElement returns an XML start element containing all stream info

type Success

type Success struct {
	XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl success"`
	Data    string   `xml:",chardata"`
}

type TLSFailure

type TLSFailure struct {
	XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-tls failure"`
}

type UniqueID

type UniqueID struct {
	// contains filtered or unexported fields
}

UniqueID holds the state of a unique id generator

func (*UniqueID) Next

func (s *UniqueID) Next() string

Next returns the next unique ID

type Writer

type Writer interface {
	Write(interface{}) error
}

Directories

Path Synopsis
examples
ext

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL