stdchat

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

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

Go to latest
Published: Jun 8, 2020 License: MIT Imports: 7 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidMsg = errors.New("not a valid message")
View Source
var JSON basicJSONer = json

JSON encoder/decoder. Marshal, MarshalIndent, Unmarshal.

Functions

func DecodeMsg

func DecodeMsg(rawMsg []byte, v interface{}) error

DecodeMsg decodes rawMsg bytes into v. Error returned will be of type DecodeMsgError, use Unwrap to get the error from the JSON unmarshaller.

func EncodeMsg

func EncodeMsg(msg interface{}) ([]byte, error)

EncodeMsg encodes msg into JSON bytes.

func IsType

func IsType(msgType, part string) bool

IsType returns true if msgType is of type part.

Types

type BaseMsg

type BaseMsg struct {
	TypeInfo             // msg/* info/* group-enter/* other/* ...
	ID       string      `json:"id"`
	Protocol string      `json:"proto,omitempty"` // empty if not from the protocol.
	Time     time.Time   `json:"time,omitempty"`  // can be zero if from client.
	Message  MessageInfo `json:"msg,omitempty"`
	Values   ValuesInfo  `json:"values,omitempty"`
}

BaseMsg is the basis for a msg.

func (*BaseMsg) GetBaseMsg

func (msg *BaseMsg) GetBaseMsg() *BaseMsg

func (BaseMsg) GetID

func (msg BaseMsg) GetID() string

func (BaseMsg) GetMessage

func (msg BaseMsg) GetMessage(msgType string) Message

func (BaseMsg) GetMessageString

func (msg BaseMsg) GetMessageString() string

func (*BaseMsg) GetProtocol

func (msg *BaseMsg) GetProtocol() string

func (*BaseMsg) Init

func (msg *BaseMsg) Init(id, typ, protocol string)

func (*BaseMsg) IsMsg

func (msg *BaseMsg) IsMsg() bool

type BaseMsger

type BaseMsger interface {
	Msger
	GetBaseMsg() *BaseMsg
	GetID() string
	GetMessage(msgType string) Message
	GetMessageString() string
}

BaseMsger is anything based on BaseMsg.

func ParseBaseMsg

func ParseBaseMsg(rawMsg []byte) (BaseMsger, error)

ParseBaseMsg parses rawMsg JSON into a specific base msg type.

type ChatMsg

type ChatMsg struct {
	NetMsg
	Destination EntityInfo  `json:"dest,omitempty"`
	From        EntityInfo  `json:"from,omitempty"`
	ReplyToID   string      `json:"replyTo,omitempty"`
	Attachments []MediaInfo `json:"attachments,omitempty"`
}

ChatMsg is a chat msg.

func (*ChatMsg) GetChatMsg

func (msg *ChatMsg) GetChatMsg() *ChatMsg

func (*ChatMsg) Init

func (msg *ChatMsg) Init(id, typ, protocol, networkID string)

func (*ChatMsg) IsMsg

func (msg *ChatMsg) IsMsg() bool

type ChatMsger

type ChatMsger interface {
	NetMsger
	GetChatMsg() *ChatMsg
}

ChatMsger is anything based on a ChatMsg.

type CmdMsg

type CmdMsg struct {
	BaseMsg
	Network EntityInfo `json:"net,omitempty"` // optional, if applicable.
	Command string     `json:"cmd"`
	Args    []string   `json:"args,omitempty"`
}

CmdMsg is a msg for a command. The Message should be empty, it is reserved for future use.

func NewCmd

func NewCmd(id, command string, args ...string) *CmdMsg

NewCmd is a helper to create a command.

func NewLogin

func NewLogin(id, remote, userID, auth string) *CmdMsg

NewLogin is a login request.

func NewLogout

func NewLogout(id, logoutID string) *CmdMsg

NewLogout is a logout request. logoutID can be the network ID or conn ID (if applicable)

func NewLogoutReason

func NewLogoutReason(id, logoutID, reason string) *CmdMsg

NewLogoutReason is NewLogout with a reason for logging out. The reason may (or may not) be announced to chat users as a leave message.

func NewRaw

func NewRaw(id, netID string, args ...string) *CmdMsg

NewRaw is a helper to create a raw command.

func (CmdMsg) GetNetwork

func (msg CmdMsg) GetNetwork() EntityInfo

GetNetwork gets the network, this is optional!

func (CmdMsg) IsMsg

func (msg CmdMsg) IsMsg() bool

type ConnMsg

type ConnMsg struct {
	NetMsg
	Connection EntityInfo `json:"conn"`
	State      ConnState  `json:"state"`
	Cause      string     `json:"cause,omitempty"`
}

ConnMsg is a msg for a connection. An implementation is not required to use all the ConnState values, but at least Connected and Disconnected are needed. Note: the value of Cause may change or may be removed completely!

func (*ConnMsg) Init

func (msg *ConnMsg) Init(id, typ, protocol, netID string, connID string, state ConnState)

type ConnState

type ConnState string

ConnState represents the connection state. Note that ConnectFailed during Connecting or Reconnecting does not indicate the end of the connection attempt, a Disconnected does. ConnectFailed goes to the conn topic, not error; it is a ConnMsg and is not an exceptional error.

const (
	Connecting    ConnState = "connecting"
	Connected     ConnState = "connected"
	Reconnecting  ConnState = "reconnecting"
	Disconnected  ConnState = "disconnected"
	ConnectFailed ConnState = "failed"
)

type DecodeMsgError

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

func (*DecodeMsgError) Error

func (err *DecodeMsgError) Error() string

func (*DecodeMsgError) Unwrap

func (err *DecodeMsgError) Unwrap() error

type EnterMsg

type EnterMsg struct {
	ChatMsg
	Member MemberInfo `json:"member"` // the member who entered.
}

EnterMsg is about a member entering a chat.

type EntityInfo

type EntityInfo struct {
	IDInfo
	TypeInfo // the type of entity, like: net, user, server, ...
}

EntityInfo represents an entity with an ID, optional name and a type.

func (*EntityInfo) Init

func (x *EntityInfo) Init(id, typ string)

type IDInfo

type IDInfo struct {
	ID          string `json:"id"`
	Name        string `json:"name,omitempty"`     // use GetName
	DisplayName string `json:"dispName,omitempty"` // use GetDisplayName
}

IDInfo has information on an object's ID and name. Name and DisplayName are both optional, they fallback to Name and ID if empty.

func (IDInfo) GetDisplayName

func (x IDInfo) GetDisplayName() string

GetName gets the display name (or name if no display name)

func (IDInfo) GetID

func (x IDInfo) GetID() string

GetID gets the unique identifier.

func (IDInfo) GetName

func (x IDInfo) GetName() string

GetName gets the name (or ID if no name)

func (*IDInfo) SetName

func (x *IDInfo) SetName(name, displayName string)

SetName sets the name and display name. Set the ID before calling this.

type KeyValueInfo

type KeyValueInfo [2]string

KeyValueInfo represents a key-value pair. [0] is the key name, [1] is the value.

func (KeyValueInfo) Key

func (kvi KeyValueInfo) Key() string

Key

func (KeyValueInfo) Value

func (kvi KeyValueInfo) Value() string

Value

type LeaveMsg

type LeaveMsg struct {
	ChatMsg
	User EntityInfo `json:"user"` // the member who left.

}

LeaveMsg is about a member leaving a chat.

type MediaInfo

type MediaInfo struct {
	TypeInfo            // MIME type, or hint such as "image/*"
	Name     string     `json:"name,omitempty"` // Default is file name from URL.
	URL      string     `json:"url"`
	ThumbURL string     `json:"thumb,omitempty"`   // optional thumbnail.
	Expires  time.Time  `json:"expires,omitempty"` // When the URL expires.
	Values   ValuesInfo `json:"values,omitempty"`
}

MediaInfo has info related to media, such as an image or video.

func (MediaInfo) GetDisplayName

func (x MediaInfo) GetDisplayName() string

func (MediaInfo) GetID

func (x MediaInfo) GetID() string

func (MediaInfo) GetName

func (x MediaInfo) GetName() string

func (*MediaInfo) Init

func (x *MediaInfo) Init(typ, url string)

func (*MediaInfo) MarshalJSON

func (x *MediaInfo) MarshalJSON() ([]byte, error)

type MemberChangedMsg

type MemberChangedMsg struct {
	ChatMsg
	User   EntityInfo `json:"user"`   // the user who changed.
	Member MemberInfo `json:"member"` // the updated member.

}

MemberChangedMsg is a msg about a member changing in a chat.

type MemberInfo

type MemberInfo struct {
	TypeInfo            // member
	Info     UserInfo   `json:"info"`
	Values   ValuesInfo `json:"values,omitempty"` // member values.
}

MemberInfo has information on a chat member.

type Message

type Message struct {
	TypeInfo        // MIME type
	Content  string `json:"content"`
}

Message is a message in a specific MIME type. Text types are assumed to be in UTF-8 unless otherwise specified.

func (*Message) SetText

func (msg *Message) SetText(content string)

func (Message) Valid

func (msg Message) Valid() bool

type MessageInfo

type MessageInfo []Message

MessageInfo represents a message in various types/formats.

func (MessageInfo) Get

func (msg MessageInfo) Get(msgType string) Message

Get message in the specified type, or an empty message.

func (*MessageInfo) Set

func (msg *MessageInfo) Set(msgType, content string)

func (*MessageInfo) SetText

func (msg *MessageInfo) SetText(content string)

SetText in text/plain type.

func (MessageInfo) String

func (msg MessageInfo) String() string

String returns the content for text/plain, or empty string.

type Msger

type Msger interface {
	IsMsg() bool
	GetProtocol() string
	GetType() string
}

Msger is anything with a common msg type.

type NetMsg

type NetMsg struct {
	BaseMsg
	Network EntityInfo `json:"net"`
}

NetMsg is a generic network message.

func (NetMsg) GetNetwork

func (msg NetMsg) GetNetwork() EntityInfo

func (*NetMsg) Init

func (msg *NetMsg) Init(id, typ, protocol, networkID string)

func (*NetMsg) IsMsg

func (msg *NetMsg) IsMsg() bool

type NetMsger

type NetMsger interface {
	BaseMsger
	GetNetwork() EntityInfo
}

NetMsger is any base msg which also has a network.

type NetworkStateInfo

type NetworkStateInfo struct {
	TypeInfo              // network-state
	Network    EntityInfo `json:"net"`
	Protocol   string     `json:"proto"`
	Connection EntityInfo `json:"conn,omitempty"` // Optional, if connections.
	Myself     EntityInfo `json:"myself"`
	Values     ValuesInfo `json:"values,omitempty"`
	Ready      bool       `json:"ready"`
}

NetworkStateInfo is network state information.

func (NetworkStateInfo) GetProtocol

func (x NetworkStateInfo) GetProtocol() string

func (NetworkStateInfo) String

func (x NetworkStateInfo) String() string

type ProtocolStateInfo

type ProtocolStateInfo struct {
	TypeInfo            // proto-state
	Protocol string     `json:"proto"`
	Values   ValuesInfo `json:"values,omitempty"`
}

ProtocolStateInfo is protocol state information.

func (ProtocolStateInfo) GetProtocol

func (x ProtocolStateInfo) GetProtocol() string

func (ProtocolStateInfo) String

func (x ProtocolStateInfo) String() string

type StateEntry

type StateEntry struct {
	Statuser
}

func (StateEntry) MarshalJSON

func (se StateEntry) MarshalJSON() ([]byte, error)

func (StateEntry) UnmarshalJSON

func (se StateEntry) UnmarshalJSON(data []byte) error

type StateMsg

type StateMsg struct {
	BaseMsg              // The protocol is empty.
	List    []StateEntry `json:"list"`
}

type Statuser

type Statuser interface {
	GetType() string
	GetProtocol() string
	String() string
}

Statuser has status info.

type SubscribeMsg

type SubscribeMsg struct {
	ChatMsg
	Subject    MessageInfo  `json:"subject,omitempty"`
	Photo      MediaInfo    `json:"photo,omitempty"`   // URL or no photo.
	Members    []MemberInfo `json:"members,omitempty"` // includes myself on subscribe.
	Myself     EntityInfo   `json:"myself"`            // myself as the new member.
	HistoryURL string       `json:"history,omitempty"` // see SubscriptionStateInfo
}

SubscribeMsg is a msg about yourself subscribing or unsubscribing to a chat. Myself is not a MemberInfo because Members includes myself, and SubscribeMsg is also reused for leaving, which has no need for MemberInfo. See the destination type for the type of chat.

type SubscriptionStateInfo

type SubscriptionStateInfo struct {
	TypeInfo                 // subscription-state
	Network     EntityInfo   `json:"net"`
	Protocol    string       `json:"proto"`
	Destination EntityInfo   `json:"dest"`
	Subject     MessageInfo  `json:"subject,omitempty"`
	Members     []MemberInfo `json:"members,omitempty"`
	Values      ValuesInfo   `json:"values,omitempty"`
	HistoryURL  string       `json:"history,omitempty"` // empty if not supported.
}

SubscriptionStateInfo is subscription state information. HistoryURL can be a URL with a known JSON REST API to fetch history, if supported. TODO: define history API.

func (SubscriptionStateInfo) GetProtocol

func (x SubscriptionStateInfo) GetProtocol() string

func (SubscriptionStateInfo) String

func (x SubscriptionStateInfo) String() string

type TypeInfo

type TypeInfo struct {
	Type string `json:"type"`
}

TypeInfo has information on the type of an object.

func (TypeInfo) GetType

func (x TypeInfo) GetType() string

GetType gets the type.

func (TypeInfo) IsType

func (x TypeInfo) IsType(part string) bool

IsType returns true if this object is of type part.

type TypingMsg

type TypingMsg struct {
	ChatMsg           // typing
	Typing  bool      `json:"typing"`
	Expires time.Time `json:"expires,omitempty"` // Optional; when typing state expires.
}

TypingMsg a msg for a user typing a message. Destination is where they are typing.

type UserChangedMsg

type UserChangedMsg struct {
	NetMsg
	User   EntityInfo `json:"user"` // the user who changed.
	Info   UserInfo   `json:"info"` // the changes.
	Myself bool       `json:"myself,omitempty"`
}

UserChangedMsg is a msg about a user changing.

type UserInfo

type UserInfo struct {
	User   EntityInfo `json:"user"`             // user
	Photo  MediaInfo  `json:"photo,omitempty"`  // URL or no photo.
	Values ValuesInfo `json:"values,omitempty"` // user values.
}

UserInfo has information on a user.

type ValuesInfo

type ValuesInfo []KeyValueInfo

ValuesInfo represents zero or more auxiliary key-value pairs. Protocol-specific keys are PROTO.* and adhere to that protocol. Implementation-specific keys are x-NAME.* or PROTO.x-NAME.* (depending on scope) Duplicate keys are valid.

func (*ValuesInfo) Add

func (vi *ValuesInfo) Add(key, value string) *ValuesInfo

Add the specified key and value, regardless if the key exists or not.

func (ValuesInfo) AppendAll

func (vi ValuesInfo) AppendAll(result []string, key string) []string

AppendAll appends all the values for the specified key and returns the updates.

func (ValuesInfo) Get

func (vi ValuesInfo) Get(key string) string

Get the value of the specified key, or empty string.

func (ValuesInfo) GetAll

func (vi ValuesInfo) GetAll(key string) []string

GetAll returns all the values for the specified key, or nil if none.

func (ValuesInfo) Lookup

func (vi ValuesInfo) Lookup(key string) (string, bool)

Lookup the value of the specified key, returns true if found.

func (*ValuesInfo) Set

func (vi *ValuesInfo) Set(key, value string) *ValuesInfo

Set will update the value for an existing key, or add the new key-value pair. If more than one matching key exists, updates only the first and ignores the rest.

Directories

Path Synopsis
cmd
Package provider is a helper package for writing a service provider.
Package provider is a helper package for writing a service provider.

Jump to

Keyboard shortcuts

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