stravaganza

package module
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2020 License: GPL-3.0 Imports: 11 Imported by: 43

README

stravaganza

Build Status Go Report Card Coverage Godoc Releases LICENSE

Installation
go get -u github.com/jackal-xmpp/stravaganza
Example
package main

import (
	"fmt"
	"os"

	"github.com/jackal-xmpp/stravaganza"
)

func main() {
	iq, err := stravaganza.NewBuilder("iq").
		WithAttribute("id", "zid615d9").
		WithAttribute("from", "ortuman@jackal.im/yard").
		WithAttribute("to", "noelia@jackal.im/balcony").
		WithAttribute("type", "get").
		WithChild(
			stravaganza.NewBuilder("ping").
				WithAttribute("xmlns", "urn:xmpp:ping").
				Build(),
		).
		BuildIQ(true)
	if err != nil {
		_, _ = fmt.Fprint(os.Stderr, err.Error())
		return
	}
	_ = iq.ToXML(os.Stdout, true)
}

Expected output:

<iq id="zid615d9" from="ortuman@jackal.im/yard" to="noelia@jackal.im/balcony" type="get"><ping xmlns="urn:xmpp:ping"/></iq>
Contributing
  • Fork it
  • Create your feature branch (git checkout -b my-new-feature)
  • Commit your changes (git commit -am 'Add some feature')
  • Push to the branch (git push origin my-new-feature)
  • Create new Pull Request
License

GPLv3

Documentation

Index

Constants

View Source
const (
	// ID represents 'id' node attribute.
	ID = "id"

	// Language represents 'xml:lang' node attribute.
	Language = "xml:lang"

	// From represents 'from' node attribute.
	From = "from"

	// To represents 'to' node attribute.
	To = "to"

	// Type represents 'type' node attribute.
	Type = "type"

	// Version represents 'version' node attribute.
	Version = "version"

	// Namespace represents 'xmlns' node attribute.
	Namespace = "xmlns"
)
View Source
const (
	// GetType represents a 'get' IQ type.
	GetType = "get"

	// SetType represents a 'set' IQ type.
	SetType = "set"

	// ResultType represents a 'result' IQ type.
	ResultType = "result"
)
View Source
const (
	// NormalType represents a 'normal' message type.
	NormalType = "normal"

	// HeadlineType represents a 'headline' message type.
	HeadlineType = "headline"

	// ChatType represents a 'chat' message type.
	ChatType = "chat"

	// GroupChatType represents a 'groupchat' message type.
	GroupChatType = "groupchat"
)
View Source
const (
	// AvailableType represents an 'available' Presence type.
	AvailableType = ""

	// UnavailableType represents a 'unavailable' Presence type.
	UnavailableType = "unavailable"

	// SubscribeType represents a 'subscribe' Presence type.
	SubscribeType = "subscribe"

	// UnsubscribeType represents a 'unsubscribe' Presence type.
	UnsubscribeType = "unsubscribe"

	// SubscribedType represents a 'subscribed' Presence type.
	SubscribedType = "subscribed"

	// UnsubscribedType represents a 'unsubscribed' Presence type.
	UnsubscribedType = "unsubscribed"

	// ProbeType represents a 'probe' Presence type.
	ProbeType = "probe"
)
View Source
const (
	// ErrorType represents a generic 'error' type stanza.
	ErrorType = "error"
)

Variables

View Source
var (
	// ErrBadRequest is returned by the stream when the  sender
	// has sent XML that is malformed or that cannot be processed.
	ErrBadRequest = newStanzaError(400, modifyErrorType, badRequestErrorReason)

	// ErrConflict is returned by the stream when access cannot be
	// granted because an existing resource or session exists with
	// the same name or address.
	ErrConflict = newStanzaError(409, cancelErrorType, conflictErrorReason)

	// ErrFeatureNotImplemented is returned by the stream when the feature
	// requested is not implemented by the server and therefore cannot be processed.
	ErrFeatureNotImplemented = newStanzaError(501, cancelErrorType, featureNotImplementedErrorReason)

	// ErrForbidden is returned by the stream when the requesting
	// entity does not possess the required permissions to perform the action.
	ErrForbidden = newStanzaError(403, authErrorType, forbiddenErrorReason)

	// ErrGone is returned by the stream when the recipient or server
	// can no longer be contacted at this address.
	ErrGone = newStanzaError(302, modifyErrorType, goneErrorReason)

	// ErrInternalServerError is returned by the stream when the server
	// could not process the stanza because of a misconfiguration
	// or an otherwise-undefined internal server error.
	ErrInternalServerError = newStanzaError(500, waitErrorType, internalServerErrorErrorReason)

	// ErrItemNotFound is returned by the stream when the addressed
	// JID or item requested cannot be found.
	ErrItemNotFound = newStanzaError(404, cancelErrorType, itemNotFoundErrorReason)

	// ErrJidMalformed is returned by the stream when the sending entity
	// has provided or communicated an XMPP address or aspect thereof that
	// does not adhere to the syntax defined in https://xmpp.org/rfcs/rfc3920.html#addressing.
	ErrJidMalformed = newStanzaError(400, modifyErrorType, jidMalformedErrorReason)

	// ErrNotAcceptable is returned by the stream when the server
	// understands the request but is refusing to process it because
	// it does not meet the defined criteria.
	ErrNotAcceptable = newStanzaError(406, modifyErrorType, notAcceptableErrorReason)

	// ErrNotAllowed is returned by the stream when the recipient
	// or server does not allow any entity to perform the action.
	ErrNotAllowed = newStanzaError(405, cancelErrorType, notAllowedErrorReason)

	// ErrNotAuthorized is returned by the stream when the sender
	// must provide proper credentials before being allowed to perform the action,
	// or has provided improper credentials.
	ErrNotAuthorized = newStanzaError(405, authErrorType, notAuthroizedErrorReason)

	// ErrPaymentRequired is returned by the stream when the requesting entity
	// is not authorized to access the requested service because payment is required.
	ErrPaymentRequired = newStanzaError(402, authErrorType, paymentRequiredErrorReason)

	// ErrRecipientUnavailable is returned by the stream when the intended
	// recipient is temporarily unavailable.
	ErrRecipientUnavailable = newStanzaError(404, waitErrorType, recipientUnavailableErrorReason)

	// ErrRedirect is returned by the stream when the recipient or server
	// is redirecting requests for this information to another entity, usually temporarily.
	ErrRedirect = newStanzaError(302, modifyErrorType, redirectErrorReason)

	// ErrRegistrationRequired is returned by the stream when the requesting entity
	// is not authorized to access the requested service because registration is required.
	ErrRegistrationRequired = newStanzaError(407, authErrorType, registrationRequiredErrorReason)

	// ErrRemoteServerNotFound is returned by the stream when a remote server
	// or service specified as part or all of the JID of the intended recipient does not exist.
	ErrRemoteServerNotFound = newStanzaError(404, cancelErrorType, remoteServerNotFoundErrorReason)

	// ErrRemoteServerTimeout is returned by the stream when a remote server
	// or service specified as part or all of the JID of the intended recipient
	// could not be contacted within a reasonable amount of time.
	ErrRemoteServerTimeout = newStanzaError(504, waitErrorType, remoteServerTimeoutErrorReason)

	// ErrResourceConstraint is returned by the stream when the server or recipient
	// lacks the system resources necessary to service the request.
	ErrResourceConstraint = newStanzaError(500, waitErrorType, resourceConstraintErrorReason)

	// ErrServiceUnavailable is returned by the stream when the server or recipient
	// does not currently provide the requested service.
	ErrServiceUnavailable = newStanzaError(503, cancelErrorType, serviceUnavailableErrorReason)

	// ErrSubscriptionRequired is returned by the stream when the requesting entity
	// is not authorized to access the requested service because a subscription is required.
	ErrSubscriptionRequired = newStanzaError(407, authErrorType, subscriptionRequiredErrorReason)

	// ErrUndefinedCondition is returned by the stream when the error condition
	// is not one of those defined by the other conditions in this list.
	ErrUndefinedCondition = newStanzaError(500, waitErrorType, undefinedConditionErrorReason)

	// ErrUnexpectedCondition is returned by the stream when the recipient or server
	// understood the request but was not expecting it at this time.
	ErrUnexpectedCondition = newStanzaError(400, waitErrorType, unexpectedConditionErrorReason)

	// ErrUnexpectedRequest is returned by the stream when the recipient or server
	// understood the request but was not expecting it at this time.
	ErrUnexpectedRequest = newStanzaError(400, cancelErrorType, unexpectedRequestErrorReason)
)

Functions

This section is empty.

Types

type Attribute

type Attribute struct {
	Label string
	Value string
}

Attribute represents an XML node attribute (label=value).

type AttributeReader

type AttributeReader interface {
	// AllChildren returns a list of all node attributes.
	AllAttributes() []Attribute

	// AttributeCount returns node total attribute count.
	AttributeCount() int

	// Attribute returns XML node attribute value.
	Attribute(label string) Attribute
}

AttributeReader defines an XML attributes read-only interface.

type Builder

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

Builder builds generic XML node elements.

func NewBuilder

func NewBuilder(name string) *Builder

NewBuilder returns a name initialized builder instance.

func NewBuilderFromBinary

func NewBuilderFromBinary(b []byte) (*Builder, error)

NewBuilderFromBinary returns an element builder derived from an element binary representation.

func NewBuilderFromElement

func NewBuilderFromElement(element Element) *Builder

NewBuilderFromElement returns an element builder derived from a copied element.

func NewIQBuilder added in v0.6.0

func NewIQBuilder() *Builder

NewPresenceBuilder returns an 'iq' stanza builder instance.

func NewMessageBuilder added in v0.6.0

func NewMessageBuilder() *Builder

NewMessageBuilder returns a 'message' stanza builder instance.

func NewPresenceBuilder added in v0.6.0

func NewPresenceBuilder() *Builder

NewPresenceBuilder returns a 'presence' stanza builder instance.

func (*Builder) Build

func (b *Builder) Build() Element

Build returns a new element instance.

func (*Builder) BuildIQ

func (b *Builder) BuildIQ(validateJIDs bool) (*IQ, error)

BuildIQ validates and returns a new IQ stanza.

func (*Builder) BuildMessage

func (b *Builder) BuildMessage(validateJIDs bool) (*Message, error)

BuildMessage validates and returns a new Message stanza.

func (*Builder) BuildPresence

func (b *Builder) BuildPresence(validateJIDs bool) (*Presence, error)

BuildPresence validates and returns a new Presence stanza.

func (*Builder) BuildStanza

func (b *Builder) BuildStanza(validateJIDs bool) (Stanza, error)

BuildStanza validates and returns a generic stanza instance.

func (*Builder) WithAttribute

func (b *Builder) WithAttribute(label, value string) *Builder

WithAttribute sets an XML node attribute (label=value).

func (*Builder) WithAttributes

func (b *Builder) WithAttributes(attributes ...Attribute) *Builder

WithAttributes sets all XML node attributes.

func (*Builder) WithChild

func (b *Builder) WithChild(child Element) *Builder

WithChild appends a new sub element.

func (*Builder) WithChildren

func (b *Builder) WithChildren(children ...Element) *Builder

WithChildren appends all new sub elements.

func (*Builder) WithName

func (b *Builder) WithName(name string) *Builder

WithName sets XML node name.

func (*Builder) WithText

func (b *Builder) WithText(text string) *Builder

WithText sets XML node text value.

func (*Builder) WithoutAttribute

func (b *Builder) WithoutAttribute(label string) *Builder

WithoutAttribute removes an XML node attribute.

func (*Builder) WithoutChildren

func (b *Builder) WithoutChildren(name string) *Builder

WithoutChildren removes all elements with a given name.

func (*Builder) WithoutChildrenNamespace

func (b *Builder) WithoutChildrenNamespace(name, ns string) *Builder

WithoutChildrenNamespace removes all elements with a given name and namespace.

type Capabilities

type Capabilities struct {
	Node string
	Hash string
	Ver  string
}

Capabilities represents presence entity capabilities

type Element

type Element interface {
	AttributeReader
	ElementReader
	XMLSerializer
	encoding.BinaryMarshaler
	fmt.Stringer
	fmt.GoStringer

	// Name returns XML node name.
	Name() string

	// Text returns XML node text value.
	Text() string
	// contains filtered or unexported methods
}

Element represents a generic XML node element.

type ElementReader

type ElementReader interface {
	// AllChildren returns a list of all child nodes.
	AllChildren() []Element

	// ChildrenCount returns child elements count.
	ChildrenCount() int

	// Child returns first element identified by name.
	Child(name string) Element

	// Children returns all elements identified by name.
	// Returns an empty array if no elements are found.
	Children(name string) []Element

	// ChildNamespace returns first element identified by name and namespace.
	// Returns nil if no element is found.
	ChildNamespace(name, ns string) Element

	// ChildrenNamespace returns all elements identified by name and namespace.
	ChildrenNamespace(name, ns string) []Element
}

ElementReader defines an XML sub elements read-only interface.

type IQ

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

IQ type represents an <iq> element.

func (*IQ) Error

func (s *IQ) Error() Element

func (*IQ) FromJID

func (s *IQ) FromJID() *jid.JID

func (*IQ) ID

func (s *IQ) ID() string

func (*IQ) IsError

func (s *IQ) IsError() bool

func (*IQ) IsGet

func (iq *IQ) IsGet() bool

IsGet returns true if this is a 'get' type IQ.

func (*IQ) IsResult

func (iq *IQ) IsResult() bool

IsResult returns true if this is a 'result' type IQ.

func (*IQ) IsSet

func (iq *IQ) IsSet() bool

IsSet returns true if this is a 'set' type IQ.

func (*IQ) Namespace

func (s *IQ) Namespace() string

func (*IQ) ResultIQ

func (iq *IQ) ResultIQ() *IQ

ResultIQ returns the instance associated result IQ.

func (*IQ) ToJID

func (s *IQ) ToJID() *jid.JID

func (*IQ) Type

func (s *IQ) Type() string

type Message

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

Message type represents a <message> element.

func (*Message) Error

func (s *Message) Error() Element

func (*Message) FromJID

func (s *Message) FromJID() *jid.JID

func (*Message) ID

func (s *Message) ID() string

func (*Message) IsChat

func (m *Message) IsChat() bool

IsChat returns true if this is a 'chat' type Message.

func (*Message) IsError

func (s *Message) IsError() bool

func (*Message) IsGroupChat

func (m *Message) IsGroupChat() bool

IsGroupChat returns true if this is a 'groupchat' type Message.

func (*Message) IsHeadline

func (m *Message) IsHeadline() bool

IsHeadline returns true if this is a 'headline' type Message.

func (*Message) IsMessageWithBody

func (m *Message) IsMessageWithBody() bool

IsMessageWithBody returns true if the message has a body sub element.

func (*Message) IsNormal

func (m *Message) IsNormal() bool

IsNormal returns true if this is a 'normal' type Message.

func (*Message) Namespace

func (s *Message) Namespace() string

func (*Message) ToJID

func (s *Message) ToJID() *jid.JID

func (*Message) Type

func (s *Message) Type() string

type Presence

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

Presence type represents a <presence> element.

func (*Presence) Capabilities

func (p *Presence) Capabilities() *Capabilities

Capabilities returns presence stanza capabilities element

func (*Presence) Error

func (s *Presence) Error() Element

func (*Presence) FromJID

func (s *Presence) FromJID() *jid.JID

func (*Presence) ID

func (s *Presence) ID() string

func (*Presence) IsAvailable

func (p *Presence) IsAvailable() bool

IsAvailable returns true if this is an 'available' type Presence.

func (*Presence) IsError

func (s *Presence) IsError() bool

func (*Presence) IsProbe

func (p *Presence) IsProbe() bool

IsProbe returns true if this is an 'probe' type Presence.

func (*Presence) IsSubscribe

func (p *Presence) IsSubscribe() bool

IsSubscribe returns true if this is a 'subscribe' type Presence.

func (*Presence) IsSubscribed

func (p *Presence) IsSubscribed() bool

IsSubscribed returns true if this is a 'subscribed' type Presence.

func (*Presence) IsUnavailable

func (p *Presence) IsUnavailable() bool

IsUnavailable returns true if this is an 'unavailable' type Presence.

func (*Presence) IsUnsubscribe

func (p *Presence) IsUnsubscribe() bool

IsUnsubscribe returns true if this is an 'unsubscribe' type Presence.

func (*Presence) IsUnsubscribed

func (p *Presence) IsUnsubscribed() bool

IsUnsubscribed returns true if this is an 'unsubscribed' type Presence.

func (*Presence) Namespace

func (s *Presence) Namespace() string

func (*Presence) Priority

func (p *Presence) Priority() int8

Priority returns presence stanza priority value.

func (*Presence) ShowState

func (p *Presence) ShowState() ShowState

ShowState returns presence stanza show state.

func (*Presence) Status

func (p *Presence) Status() string

Status returns presence stanza default status.

func (*Presence) ToJID

func (s *Presence) ToJID() *jid.JID

func (*Presence) Type

func (s *Presence) Type() string

type ShowState

type ShowState int

ShowState represents Presence show state.

const (
	// AvailableShowState represents 'available' Presence show state.
	AvailableShowState ShowState = iota

	// AwayShowState represents 'away' Presence show state.
	AwayShowState

	// ChatShowState represents 'chat' Presence show state.
	ChatShowState

	// DoNotDisturbShowState represents 'dnd' Presence show state.
	DoNotDisturbShowState

	// ExtendedAwaysShowState represents 'xa' Presence show state.
	ExtendedAwaysShowState
)

type Stanza

type Stanza interface {
	Element

	// ToJID returns stanza 'to' JID value.
	ToJID() *jid.JID

	// FromJID returns stanza 'from' JID value.
	FromJID() *jid.JID

	// ID returns 'id' node attribute.
	ID() string

	// Namespace returns 'xmlns' node attribute.
	Namespace() string

	// Type returns 'type' node attribute.
	Type() string

	// IsError returns true if stanza has a 'type' attribute of value 'error'.
	IsError() bool

	// Error returns stanza error sub element.
	Error() Element
}

Stanza represents an XMPP stanza element.

func BadRequestError

func BadRequestError(stanza Stanza) Stanza

BadRequestError returns an error copy of the element attaching 'bad-request' error sub element.

func ConflictError

func ConflictError(stanza Stanza) Stanza

ConflictError returns an error copy of the element attaching 'conflict' error sub element.

func FeatureNotImplementedError

func FeatureNotImplementedError(stanza Stanza) Stanza

FeatureNotImplementedError returns an error copy of the element attaching 'feature-not-implemented' error sub element.

func ForbiddenError

func ForbiddenError(stanza Stanza) Stanza

ForbiddenError returns an error copy of the element attaching 'forbidden' error sub element.

func GoneError

func GoneError(stanza Stanza) Stanza

GoneError returns an error copy of the element attaching 'gone' error sub element.

func InternalServerError

func InternalServerError(stanza Stanza) Stanza

InternalServerError returns an error copy of the element attaching 'internal-server-error' error sub element.

func ItemNotFoundError

func ItemNotFoundError(stanza Stanza) Stanza

ItemNotFoundError returns an error copy of the element attaching 'item-not-found' error sub element.

func JidMalformedError

func JidMalformedError(stanza Stanza) Stanza

JidMalformedError returns an error copy of the element attaching 'jid-malformed' error sub element.

func NotAcceptableError

func NotAcceptableError(stanza Stanza) Stanza

NotAcceptableError returns an error copy of the element attaching 'not-acceptable' error sub element.

func NotAllowedError

func NotAllowedError(stanza Stanza) Stanza

NotAllowedError returns an error copy of the element attaching 'not-allowed' error sub element.

func NotAuthorizedError

func NotAuthorizedError(stanza Stanza) Stanza

NotAuthorizedError returns an error copy of the element attaching 'not-authorized' error sub element.

func PaymentRequiredError

func PaymentRequiredError(stanza Stanza) Stanza

PaymentRequiredError returns an error copy of the element attaching 'payment-required' error sub element.

func RecipientUnavailableError

func RecipientUnavailableError(stanza Stanza) Stanza

RecipientUnavailableError returns an error copy of the element attaching 'recipient-unavailable' error sub element.

func RedirectError

func RedirectError(stanza Stanza) Stanza

RedirectError returns an error copy of the element attaching 'redirect' error sub element.

func RegistrationRequiredError

func RegistrationRequiredError(stanza Stanza) Stanza

RegistrationRequiredError returns an error copy of the element attaching 'registration-required' error sub element.

func RemoteServerNotFoundError

func RemoteServerNotFoundError(stanza Stanza) Stanza

RemoteServerNotFoundError returns an error copy of the element attaching 'remote-server-not-found' error sub element.

func RemoteServerTimeoutError

func RemoteServerTimeoutError(stanza Stanza) Stanza

RemoteServerTimeoutError returns an error copy of the element attaching 'remote-server-timeout' error sub element.

func ResourceConstraintError

func ResourceConstraintError(stanza Stanza) Stanza

ResourceConstraintError returns an error copy of the element attaching 'resource-constraint' error sub element.

func ServiceUnavailableError

func ServiceUnavailableError(stanza Stanza) Stanza

ServiceUnavailableError returns an error copy of the element attaching 'service-unavailable' error sub element.

func SubscriptionRequiredError

func SubscriptionRequiredError(stanza Stanza) Stanza

SubscriptionRequiredError returns an error copy of the element attaching 'subscription-required' error sub element.

func ToStanzaError

func ToStanzaError(stanza Stanza, stanzaError *StanzaError, errorElements []Element) Stanza

ToStanzaError returns the derived equivalent error stanza.

func UndefinedConditionError

func UndefinedConditionError(stanza Stanza) Stanza

UndefinedConditionError returns an error copy of the element attaching 'undefined-condition' error sub element.

func UnexpectedConditionError

func UnexpectedConditionError(stanza Stanza) Stanza

UnexpectedConditionError returns an error copy of the element attaching 'unexpected-condition' error sub element.

func UnexpectedRequestError

func UnexpectedRequestError(stanza Stanza) Stanza

UnexpectedRequestError returns an error copy of the element attaching 'unexpected-request' error sub element.

type StanzaError

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

StanzaError represents a stanza "error" element.

func (*StanzaError) Error

func (se *StanzaError) Error() string

Error satisfies error interface.

func (*StanzaError) ToElement

func (se *StanzaError) ToElement(errElements []Element) Element

ToElement returns StanzaError equivalent XML element.

type XMLSerializer

type XMLSerializer interface {
	// ToXML serializes element to a raw XML representation.
	// includeClosing determines if closing tag should be attached.
	ToXML(w io.Writer, includeClosing bool) error
}

XMLSerializer represents element common XML serializer interface.

Directories

Path Synopsis
internal
pb

Jump to

Keyboard shortcuts

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