mautrix

package module
v0.0.0-...-03172ff Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2018 License: GPL-3.0 Imports: 9 Imported by: 0

README

mautrix

A Matrix client-server library intended for bots.

Use go get maunium.net/go/mautrix to get this package

This project is a fork of geir54/goMatrix, but most of the code has been rewritten.

N.B. sync.go has not been rewritten, so it is still licensed under MPL 2.0

Documentation

Index

Constants

View Source
const (
	EvtRoomAliases           = "m.room.aliases"
	EvtRoomCanonicalAlias    = "m.room.canonical_alias"
	EvtRoomCreate            = "m.room.create"
	EvtRoomJoinRules         = "m.room.join_rules"
	EvtRoomMember            = "m.room.member"
	EvtRoomPowerLevels       = "m.room.power_levels"
	EvtRoomRedaction         = "m.room.redaction"
	EvtRoomHistoryVisibility = "m.room.history_visibility"
	EvtRoomThirdPartyInvite  = "m.room.third_party_invite"
	EvtRoomGuestAccess       = "m.room.guest_access"
)

Room events

View Source
const (
	EvtRoomMessage         = "m.room.message"
	EvtRoomMessageFeedback = "m.room.message.feedback"
	EvtRoomName            = "m.room.name"
	EvtRoomTopic           = "m.room.topic"
	EvtRoomAvatar          = "m.room.avatar"
)

Instant messaging events

View Source
const (
	MsgText     = "m.text"
	MsgEmote    = "m.emote"
	MsgNotice   = "m.notice"
	MsgImage    = "m.image"
	MsgFile     = "m.file"
	MsgLocation = "m.location"
	MsgVideo    = "m.video"
	MsgAudio    = "m.audio"
)

Message types

View Source
const (
	LoginPassword   = "m.login.password"
	LoginReCAPTCHA  = "m.login.recaptcha"
	LoginOAuth2     = "m.login.oauth2"
	LoginEmailIdent = "m.login.email.identity"
	LoginToken      = "m.login.token"
	LoginDummy      = "m.login.dummy"
)

Login types

View Source
const (
	EvtCallInvite     = "m.call.invite"
	EvtCallCandidates = "m.call.candidates"
	EvtCallAnswer     = "m.call.answer"
	EvtCallHangup     = "m.call.hangup"
)

VoIP events

View Source
const (
	EvtTyping   = "m.typing"
	EvtReceipt  = "m.receipt"
	EvtRead     = "m.read"
	EvtPresence = "m.presence"
)

Presence events

View Source
const (
	EvtTag = "m.tag"
)

Room tagging

Variables

This section is empty.

Functions

This section is empty.

Types

type CompletedRequest

type CompletedRequest struct {
	Source   Request
	Response *http.Response
	Method   string
	Error    error
}

CompletedRequest is a Matrix API request that has been called.

func (CompletedRequest) CheckStatus

func (creq CompletedRequest) CheckStatus(expected int) bool

CheckStatus makes sure that the response HTTP status code is correct.

func (CompletedRequest) CheckStatusOK

func (creq CompletedRequest) CheckStatusOK() bool

CheckStatusOK makes sure that the response HTTP status code is OK 200.

func (CompletedRequest) Close

func (creq CompletedRequest) Close()

Close closes the response body stream.

func (CompletedRequest) JSON

func (creq CompletedRequest) JSON(decodeInto interface{}) error

JSON reads the body of the response and parses it from JSON into an interface.

func (CompletedRequest) OK

func (creq CompletedRequest) OK() bool

OK checks if the request completed successfully.

func (CompletedRequest) Text

func (creq CompletedRequest) Text() (string, error)

Text reads the body of the response and returns it as a string.

type Event

type Event struct {
	ID               string                 `json:"event_id"`
	Type             string                 `json:"type"`
	Sender           string                 `json:"sender"`
	StateKey         string                 `json:"state_key"`
	Content          map[string]interface{} `json:"content"`
	OriginServerTime int64                  `json:"origin_server_ts"`
	Age              int64                  `json:"age"`
	TransactionID    string                 `json:"txn_id"`
	Unsigned         Unsigned               `json:"unsigned"`

	Room *Room `json:"-"`
}

Event represents a single event

func (Event) CanMarkRead

func (evt Event) CanMarkRead() bool

CanMarkRead checks if the event can be marked as read

func (Event) MarkRead

func (evt Event) MarkRead() bool

MarkRead marks this event as read

type EventContainer

type EventContainer struct {
	Events []Event `json:"events"`
}

EventContainer contains an array of events

type EventContent

type EventContent struct {
	Name string `json:"name"`
	Body string `json:"body"`
}

EventContent contains the name and body of an event

type Invite

type Invite struct {
	Sender  string
	Name    string
	ID      string
	Members map[string]string
	Session *MatrixBot
}

Invite wraps an invite to a room

func (Invite) Accept

func (i Invite) Accept() error

Accept the invite

type InvitedRoom

type InvitedRoom struct {
	InviteState EventContainer `json:"invite_state"`
}

InvitedRoom is a room that the user has been invited to

type LeftRoom

type LeftRoom struct {
	// Member list and other persistent data
	State EventContainer `json:"state"`
	// Messages, state changes, etc..
	Timeline EventContainer `json:"timeline"`
}

LeftRoom is a room the user has left or been banned from

type MatrixBot

type MatrixBot struct {
	NextBatch   string
	Rooms       map[string]*Room
	Invites     map[string]*Invite
	Presence    map[string]string
	AccessToken string
	MatrixID    string
	HomeServer  string
	TxnID       int
	Timeline    chan Event
	InviteChan  chan string
	JoinChan    chan string
	// contains filtered or unexported fields
}

MatrixBot is a client-server Matrix session

func Create

func Create(homeserver string) *MatrixBot

Create a Session

func (*MatrixBot) DummyLogin

func (mx *MatrixBot) DummyLogin() error

DummyLogin tries to log in without authentication

func (*MatrixBot) GetRoom

func (mx *MatrixBot) GetRoom(roomID string) *Room

GetRoom gets the room pointer with the given ID

func (*MatrixBot) GetURL

func (mx *MatrixBot) GetURL(path string, args ...interface{}) string

GetURL gets the URL for the given path on this session

func (*MatrixBot) Join

func (mx *MatrixBot) Join(roomID string) error

Join a room

func (*MatrixBot) Listen

func (mx *MatrixBot) Listen()

Listen for updates from the homeserver

func (*MatrixBot) NewJSONRequest

func (mx *MatrixBot) NewJSONRequest(payload interface{}, url string, urlArgs ...interface{}) Request

NewJSONRequest creates a new Matrix API request and marshals the given object to JSON and uses it as the payload.

func (*MatrixBot) NewPlainRequest

func (mx *MatrixBot) NewPlainRequest(url string, urlArgs ...interface{}) Request

NewPlainRequest creates a new payloadless Matrix API request.

func (*MatrixBot) NewRequest

func (mx *MatrixBot) NewRequest(payload, contentType, url string, urlArgs ...interface{}) Request

NewRequest creates a new Matrix API request with a custom payload.

func (*MatrixBot) NextTransactionID

func (mx *MatrixBot) NextTransactionID() int

NextTransactionID returns the next message transaction ID

func (*MatrixBot) PasswordLogin

func (mx *MatrixBot) PasswordLogin(user, password string) error

PasswordLogin tries to log in with username and password

func (*MatrixBot) SetToken

func (mx *MatrixBot) SetToken(user, token string) error

SetToken sets the access token to use

func (*MatrixBot) Stop

func (mx *MatrixBot) Stop()

Stop the listener

func (*MatrixBot) Sync

func (mx *MatrixBot) Sync() error

Sync the current status with the homeserver

type Member

type Member struct {
	Membership  string
	Power       int
	DisplayName string
}

Member contains some information about a room member

type Request

type Request struct {
	URL         string
	Payload     string
	ContentType string
	Session     *MatrixBot
}

Request wraps a Matrix API request.

func (Request) GET

func (req Request) GET() CompletedRequest

GET executes this request with HTTP GET.

func (Request) HTTP

func (req Request) HTTP(method string) CompletedRequest

HTTP executes this request with the given HTTP method.

func (Request) POST

func (req Request) POST() CompletedRequest

POST executes this request with HTTP POST.

func (Request) PUT

func (req Request) PUT() CompletedRequest

PUT executes this request with HTTP PUT.

type Room

type Room struct {
	ID      string
	Name    string
	Members map[string]Member
	Aliases []string
	Session *MatrixBot
}

Room is a room

func (*Room) Emote

func (r *Room) Emote(message string) (SendResponse, error)

Send emote message to this room

func (*Room) Send

func (r *Room) Send(message string) (SendResponse, error)

Send sends a message to this room

func (*Room) SendHTML

func (r *Room) SendHTML(message string) (SendResponse, error)

SendHTML sends a HTML message to this room

func (*Room) Sendf

func (r *Room) Sendf(message string, args ...interface{}) (SendResponse, error)

Sendf formats and sends a message to this room

func (*Room) SendfHTML

func (r *Room) SendfHTML(message string, args ...interface{}) (SendResponse, error)

SendfHTML formats and sends a HTML message to this room

type SRoom

type SRoom struct {
	// Typing notifications, presence updates, etc..
	Ephemeral EventContainer `json:"ephemeral"`
	// Member list and other persistent data
	State EventContainer `json:"state"`
	// Messages, state changes, etc..
	Timeline Timeline `json:"timeline"`
	// Tags and custom configs
	AccountData EventContainer `json:"account_data"`
}

SRoom represents a single room

type SendResponse

type SendResponse struct {
	EventID   string `json:"event_id"`
	Error     string `json:"error"`
	ErrorCode string `json:"errcode"`
}

SendResponse wraps the response to a room send request

type SyncData

type SyncData struct {
	NextBatch string         `json:"next_batch"`
	Rooms     SyncRooms      `json:"rooms"`
	Presence  EventContainer `json:"presence"`
	Initial   bool           `json:"-"`
}

SyncData contains everything in a single synchronization

type SyncRooms

type SyncRooms struct {
	Join    map[string]SRoom       `json:"join"`
	Invited map[string]InvitedRoom `json:"invite"`
	Leave   map[string]LeftRoom    `json:"leave"`
}

SyncRooms contains all joined and invited rooms

type Timeline

type Timeline struct {
	EventContainer
	Limited   bool   `json:"limited"`
	PrevBatch string `json:"prev_batch"`
}

Timeline wraps things in a timeline

type Unsigned

type Unsigned struct {
	InviteRoomState []Event `json:"invite_room_state"`
}

Unsigned contains the unsigned event contents

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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