stravaganza

package module
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2023 License: Apache-2.0 Imports: 13 Imported by: 43

README

stravaganza

CI 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").
		WithValidateJIDs(true).		
		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()
	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

Apache License 2.0

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"

	// StreamNamespace represents 'xmlns:stream' node attribute.
	StreamNamespace = "xmlns:stream"
)
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"
)
View Source
const IQName = "iq"

IQName represents 'iq' stanza type name.

View Source
const MessageName = "message"

MessageName represents 'message' stanza type name.

View Source
const PresenceName = "presence"

PresenceName represents 'presence' stanza type name.

Variables

View Source
var File_stravaganza_proto protoreflect.FileDescriptor

Functions

func IsStanza added in v0.10.0

func IsStanza(el Element) bool

IsStanza tells whether or not el element is of type stanza.

Types

type Attribute

type Attribute struct {
	Label string
	Value string
}

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

type AttributeReader

type AttributeReader interface {
	// AllAttributes 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) string
}

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 NewBuilderFromProto added in v0.9.0

func NewBuilderFromProto(protoFrom *PBElement) *Builder

NewBuilderFromProto returns an element builder derived from proto type.

func NewIQBuilder added in v0.6.0

func NewIQBuilder() *Builder

NewIQBuilder 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() (*IQ, error)

BuildIQ validates and returns a new IQ stanza.

func (*Builder) BuildMessage

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

BuildMessage validates and returns a new Message stanza.

func (*Builder) BuildPresence

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

BuildPresence validates and returns a new Presence stanza.

func (*Builder) BuildStanza

func (b *Builder) BuildStanza() (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) WithValidateJIDs added in v1.1.1

func (b *Builder) WithValidateJIDs(validateJIDs bool) *Builder

WithValidateJIDs sets validate JIDs 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
	encoding.BinaryUnmarshaler
	fmt.Stringer
	fmt.GoStringer

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

	// Text returns XML node text value.
	Text() string

	// Proto returns element protobuf message.
	Proto() *PBElement
}

Element represents a generic XML node element.

func EmptyElement added in v1.1.1

func EmptyElement() Element

EmptyElement returns an empty element instace. Useful in case you want to deserialize an element using encoding.UnmarshalBinary interface.

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) ResultBuilder added in v0.18.1

func (iq *IQ) ResultBuilder() *Builder

ResultBuilder returns a builder instance associated to iq result stanza.

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 PBAttribute added in v0.7.0

type PBAttribute struct {
	Label string `protobuf:"bytes,1,opt,name=label,proto3" json:"label,omitempty"`
	Value string `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"`
	// contains filtered or unexported fields
}

func (*PBAttribute) Descriptor deprecated added in v0.7.0

func (*PBAttribute) Descriptor() ([]byte, []int)

Deprecated: Use PBAttribute.ProtoReflect.Descriptor instead.

func (*PBAttribute) GetLabel added in v0.7.0

func (x *PBAttribute) GetLabel() string

func (*PBAttribute) GetValue added in v0.7.0

func (x *PBAttribute) GetValue() string

func (*PBAttribute) ProtoMessage added in v0.7.0

func (*PBAttribute) ProtoMessage()

func (*PBAttribute) ProtoReflect added in v0.7.0

func (x *PBAttribute) ProtoReflect() protoreflect.Message

func (*PBAttribute) Reset added in v0.7.0

func (x *PBAttribute) Reset()

func (*PBAttribute) String added in v0.7.0

func (x *PBAttribute) String() string

type PBElement added in v0.7.0

type PBElement struct {
	Name       string         `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
	Attributes []*PBAttribute `protobuf:"bytes,2,rep,name=attributes,proto3" json:"attributes,omitempty"`
	Elements   []*PBElement   `protobuf:"bytes,3,rep,name=elements,proto3" json:"elements,omitempty"`
	Text       string         `protobuf:"bytes,4,opt,name=text,proto3" json:"text,omitempty"`
	// contains filtered or unexported fields
}

func (*PBElement) Descriptor deprecated added in v0.7.0

func (*PBElement) Descriptor() ([]byte, []int)

Deprecated: Use PBElement.ProtoReflect.Descriptor instead.

func (*PBElement) GetAttributes added in v0.7.0

func (x *PBElement) GetAttributes() []*PBAttribute

func (*PBElement) GetElements added in v0.7.0

func (x *PBElement) GetElements() []*PBElement

func (*PBElement) GetName added in v0.7.0

func (x *PBElement) GetName() string

func (*PBElement) GetText added in v0.7.0

func (x *PBElement) GetText() string

func (*PBElement) ProtoMessage added in v0.7.0

func (*PBElement) ProtoMessage()

func (*PBElement) ProtoReflect added in v0.7.0

func (x *PBElement) ProtoReflect() protoreflect.Message

func (*PBElement) Reset added in v0.7.0

func (x *PBElement) Reset()

func (*PBElement) String added in v0.7.0

func (x *PBElement) String() 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.

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
errors

Jump to

Keyboard shortcuts

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