xco

package module
v0.0.0-...-9f71bc5 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2019 License: MIT Imports: 11 Imported by: 4

README

go-xco

GoDoc

Library for building XMPP/Jabber (XEP-0114) components in golang.

XEPs

Additional XEPs have been added through forks and cherry-picked into a downstream branch, then merged into master. If you've got additional XEPs to add, create a pull request or issue to list it.

Usage:

import (
	"github.com/sheenobu/go-xco"
)

func main(){

	opts := xco.Options{
		Name:         Name,
		SharedSecret: SharedSecret,
		Address:      Address,
	}

	c, err := xco.NewComponent(opts)
	if err != nil {
		panic(err)
	}

	// Uppercase Echo Component
	c.MessageHandler = xco.BodyResponseHandler(func(msg *xco.Message) (string, error) {
		return strings.ToUpper(msg.Body), nil
	})
	
	c.Run()
}

Raw Usage

The various handlers take the arguments of Component and either Message, Iq, Presence, etc.

You can work with the messages directly without a helper function:

// Uppercase Echo Component
c.MessageHandler = func(c *xco.Component, msg *xco.Message) error {
	resp := xco.Message{
		Header: xco.Header{
			From: msg.To,
			To:   msg.From,
			ID:   msg.ID,
		},
		Subject: msg.Subject,
		Thread:  msg.Thread,
		Type:    msg.Type,
		Body:    strings.ToUpper(msg.Body),
		XMLName: msg.XMLName,
	}

	return c.Send(&resp)
}

Documentation

Index

Constants

View Source
const (

	// CHAT defines the chat message type
	CHAT = MessageType("chat")

	// ERROR defines the error message type
	ERROR = MessageType("error")

	// GROUPCHAT defines the group chat message type
	GROUPCHAT = MessageType("groupchat")

	// HEADLINE defines the headline message type
	HEADLINE = MessageType("headline")

	// NORMAL defines the normal message type
	NORMAL = MessageType("normal")
)
View Source
const (

	// SUBSCRIBE represents the subscribe Presence message type
	SUBSCRIBE = "subscribe"

	// SUBSCRIBED represents the subscribed Presence message type
	SUBSCRIBED = "subscribed"

	// UNSUBSCRIBE represents the unsubsribe Presence message type
	UNSUBSCRIBE = "unsubscribe"

	// UNSUBSCRIBED represents the unsubsribed Presence message type
	UNSUBSCRIBED = "unsubscribed"

	// UNAVAILABLE represents the unavailable Presence message type
	UNAVAILABLE = "unavailable"

	// PROBE represents the probe Presence message type
	PROBE = "probe"
)

Variables

This section is empty.

Functions

func AlwaysOnlinePresenceHandler

func AlwaysOnlinePresenceHandler(c *Component, p *Presence) error

AlwaysOnlinePresenceHandler always returns "subscribed" to any presence requests

Types

type Address

type Address struct {
	LocalPart    string
	DomainPart   string
	ResourcePart string
}

Address is an XMPP JID address

func ParseAddress

func ParseAddress(s string) (Address, error)

ParseAddress parses the address from the given string

func (*Address) Bare

func (a *Address) Bare() *Address

Bare returns a copy of this address with the resource part made blank.

func (*Address) Equals

func (a *Address) Equals(o *Address) bool

Equals compares the given address

func (*Address) MarshalXMLAttr

func (a *Address) MarshalXMLAttr(name xml.Name) (xml.Attr, error)

MarshalXMLAttr marks the Address struct as being able to be written as an XML attribute

func (*Address) String

func (a *Address) String() string

String formats the address as an XMPP JID

func (*Address) UnmarshalXMLAttr

func (a *Address) UnmarshalXMLAttr(attr xml.Attr) error

UnmarshalXMLAttr marks the Address struct as being able to be parsed as an XML attribute

type Component

type Component struct {
	MessageHandler   MessageHandler
	DiscoInfoHandler DiscoInfoHandler
	PresenceHandler  PresenceHandler
	IqHandler        IqHandler
	UnknownHandler   UnknownElementHandler
	// contains filtered or unexported fields
}

A Component is an instance of a Jabber Component (XEP-0114)

func NewComponent

func NewComponent(opts Options) (*Component, error)

NewComponent creates a new component from the given options

func (*Component) Close

func (c *Component) Close()

Close closes the Component

func (*Component) Run

func (c *Component) Run() (err error)

Run runs the component handlers loop and waits for it to finish

func (*Component) Send

func (c *Component) Send(i interface{}) error

Send sends the given pointer struct by serializing it to XML.

func (*Component) Write

func (c *Component) Write(b []byte) (int, error)

Write implements the io.Writer interface to allow direct writing to the XMPP connection

type DiscoFeature

type DiscoFeature struct {
	// Var is a mandatory protocol namespace offered by the entity.
	Var string `xml:"var,attr"`

	XMLName string `xml:"feature"`
}

DiscoFeature represents a feature element in a response to a service discovery info query.

See the registry of features at http://xmpp.org/registrar/disco-features.html

type DiscoIdentity

type DiscoIdentity struct {
	// Category is a mandatory description of the category of this
	// identity.  Category often contains values like "conference",
	// "directory", "gateway", "server", "client", etc.
	//
	// See the category registry at http://xmpp.org/registrar/disco-categories.html
	Category string `xml:"category,attr"`

	// Type is a mandatory description of the type of this identity.
	// The type goes together with the Category to help requesting
	// entities know which services are offered.
	//
	// For example, if Category is "gateway" then Type might be "msn"
	// or "aim".  See the type registry at http://xmpp.org/registrar/disco-categories.html
	Type string `xml:"type,attr"`

	// Name is an optional natural language name for the entity
	// described by this identity.
	Name string `xml:"name,attr,omitempty"`

	XMLName string `xml:"identity"`
}

DiscoIdentity represents an identity element in a response to a service discovery info query.

type DiscoInfoHandler

type DiscoInfoHandler func(c *Component, iq *Iq) ([]DiscoIdentity, []DiscoFeature, error)

DiscoInfoHandler handles an incoming service discovery request. The target entity is described by iq.To. This function's first return value is a slice of identities for the target entity. The second is a slice of features offered by the target entity.

You don't have to include a feature indicating support for service discovery, one is automatically added for you. If you return 0 identity elements, service discovery is disabled.

type DiscoInfoQuery

type DiscoInfoQuery struct {
	Identities []DiscoIdentity
	Features   []DiscoFeature

	XMLName xml.Name `xml:"http://jabber.org/protocol/disco#info query"`
}

DiscoInfoQuery represents a service discovery info query message. See section 3.1 in XEP-0030.

type Error

type Error struct {
	XMLName xml.Name

	Code string `xml:"code,omitempty,attr"`
	Type string `xml:"type,omitempty,attr"`
}

Error is the error sent over XMPP

func (*Error) String

func (e *Error) String() string
type Header struct {
	ID   string   `xml:"id,attr,omitempty"`
	From *Address `xml:"from,attr,omitempty"`
	To   *Address `xml:"to,attr,omitempty"`
}

Header contains the common fields for every XEP-0114 message

type Iq

type Iq struct {
	Header

	Type string `xml:"type,attr"`

	Content string `xml:",innerxml"`

	Vcard *Vcard `xml:"vcard-temp vCard,omitempty"`

	XMLName string `xml:"iq"`
}

Iq represents an info/query message

func (*Iq) IsDiscoInfo

func (iq *Iq) IsDiscoInfo() bool

IsDiscoInfo returns true if an iq stanza is a service discovery info query.

type IqHandler

type IqHandler func(c *Component, iq *Iq) error

IqHandler handles an incoming Iq (info/query) request

type Message

type Message struct {
	Header
	Type MessageType `xml:"type,attr,omitempty"`

	Subject string `xml:"subject,omitempty"`
	Body    string `xml:"body,omitempty"`
	Error   *Error `xml:"error"`
	Thread  string `xml:"thread,omitempty"`
	Content string `xml:",innerxml"` // allow arbitrary content

	// XEP-0184 message delivery receipts
	ReceiptRequest *xml.Name   `xml:"urn:xmpp:receipts request,omitempty"`
	ReceiptAck     *ReceiptAck `xml:"urn:xmpp:receipts received,omitempty"`

	// XEP-0172 User nicknames
	Nick string `xml:"http://jabber.org/protocol/nick nick,omitempty"`

	XMLName xml.Name `xml:"message"`
}

A Message is an incoming or outgoing Component message

func (*Message) Response

func (m *Message) Response() *Message

Response returns a new message representing a response to this message. The To and From attributes of the header are reversed to indicate the new origin.

type MessageHandler

type MessageHandler func(*Component, *Message) error

A MessageHandler handles an incoming message

func BodyResponseHandler

func BodyResponseHandler(fn func(*Message) (string, error)) MessageHandler

BodyResponseHandler builds a simple request-response style function which returns the body of the response message

type MessageType

type MessageType string

MessageType defines the constants for the types of messages within XEP-0114

type Options

type Options struct {

	// Name defines the component name
	Name string

	// SharedSecret is the secret shared between the server and component
	SharedSecret string

	// Address is the address of the XMPP server
	Address string

	// The (optional) parent context
	Context context.Context

	// Logger is an optional logger to which to send raw XML stanzas
	// sent and received.  It's primarily intended for debugging and
	// development.
	Logger *log.Logger
}

Options define the series of options required to build a component

type Presence

type Presence struct {
	Header

	Show     string `xml:"show,omitempty"`
	Status   string `xml:"status,omitempty"`
	Priority byte   `xml:"priority,omitempty"`

	Type string `xml:"type,attr,omitempty"`

	// XEP-0172 User nicknames
	Nick string `xml:"http://jabber.org/protocol/nick nick,omitempty"`

	XMLName string `xml:"presence"`
}

Presence represents a message identifying whether an entity is available and the subscription requests/responses for the entity

type PresenceHandler

type PresenceHandler func(c *Component, p *Presence) error

PresenceHandler handles incoming presence requests

func ToAddressPresenceHandler

func ToAddressPresenceHandler(fn func(subject Address) error) PresenceHandler

ToAddressPresenceHandler calls the function with the To address

type ReceiptAck

type ReceiptAck struct {
	ID string `xml:"id,attr"`
}

ReceiptAck represents an acknowledgement that the message with ID has been received.

type Sender

type Sender interface {
	Send(i interface{}) error
}

A Sender is an interface which allows sending of arbitrary objects as XML to an XMPP server.

type UnknownElementHandler

type UnknownElementHandler func(*Component, *xml.StartElement) error

UnknownElementHandler handles unknown XML entities sent through XMPP

type Vcard

type Vcard struct {
	FullName string `xml:"FN,omitempty"`
}

Vcard represents details about a XEP-0054 vCard.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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