v0.18.1 Latest Latest

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

Go to latest
Published: Apr 16, 2024 License: MPL-2.0 Imports: 11 Imported by: 212




View Source
const UserIDMaxLength = 255


View Source
var (
	InvalidContentURI  = errors.New("invalid Matrix content URI")
	InputNotJSONString = errors.New("input doesn't look like a JSON string")
View Source
var (
	ErrInvalidScheme       = errors.New("matrix URI scheme must be exactly 'matrix'")
	ErrInvalidPartCount    = errors.New("matrix URIs must have exactly 2 or 4 segments")
	ErrInvalidFirstSegment = errors.New("invalid identifier in first segment of matrix URI")
	ErrEmptySecondSegment  = errors.New("the second segment of the matrix URI must not be empty")
	ErrInvalidThirdSegment = errors.New("invalid identifier in third segment of matrix URI")
	ErrEmptyFourthSegment  = errors.New("the fourth segment of the matrix URI must not be empty when the third segment is present")

Errors that can happen when parsing matrix: URIs

View Source
var (
	ErrNotMatrixTo                        = errors.New("that URL is not a URL")
	ErrInvalidMatrixToPartCount           = errors.New(" URLs must have exactly 1 or 2 segments")
	ErrEmptyMatrixToPrimaryIdentifier     = errors.New("the primary identifier in the URL is empty")
	ErrInvalidMatrixToPrimaryIdentifier   = errors.New("the primary identifier in the URL has an invalid sigil")
	ErrInvalidMatrixToSecondaryIdentifier = errors.New("the secondary identifier in the URL has an invalid sigil")

Errors that can happen when parsing URLs

View Source
var (
	ErrInvalidUserID         = errors.New("is not a valid user ID")
	ErrNoncompliantLocalpart = errors.New("contains characters that are not allowed")
	ErrUserIDTooLong         = errors.New("the given user ID is longer than 255 characters")
	ErrEmptyLocalpart        = errors.New("empty localparts are not allowed")
View Source
var ErrNotMatrixToOrMatrixURI = errors.New("that URL is not a URL nor matrix: URI")
View Source
var SigilToPathSegment = map[rune]string{
	'$': "e",
	'#': "r",
	'!': "roomid",
	'@': "u",

SigilToPathSegment contains a mapping from Matrix identifier sigils to matrix: URI path segments.

View Source
var ValidLocalpartRegex = regexp.MustCompile("^[0-9a-z-.=_/+]+$")


func DecodeUserLocalpart

func DecodeUserLocalpart(str string) (string, error)

DecodeUserLocalpart decodes the given string back into the original input string. Returns an error if the given string is not a valid user ID localpart encoding. See

This decodes quoted-printable bytes back into UTF8, and unescapes casing. For example:

_alph=40_bet=5f50up  =>  Alph@Bet_50up

Returns an error if the input string contains characters outside the range "a-z0-9._=-", has an invalid quote-printable byte (e.g. not hex), or has an invalid _ escaped byte (e.g. "_5").

func EncodeUserLocalpart

func EncodeUserLocalpart(str string) string

EncodeUserLocalpart encodes the given string into Matrix-compliant user ID localpart form. See

This returns a string with only the characters "a-z0-9._=-". The uppercase range A-Z are encoded using leading underscores ("_"). Characters outside the aforementioned ranges (including literal underscores ("_") and equals ("=")) are encoded as UTF8 code points (NOT NCRs) and converted to lower-case hex with a leading "=". For example:

Alph@Bet_50up  => _alph=40_bet=5f50up

func ParseCommonIdentifier added in v0.18.0

func ParseCommonIdentifier[Stringish ~string](identifier Stringish) (sigil byte, localpart, homeserver string)

ParseCommonIdentifier parses a common identifier according to

func ValidateUserLocalpart added in v0.8.4

func ValidateUserLocalpart(localpart string) error

ValidateUserLocalpart validates a Matrix user ID localpart using the grammar in


type Algorithm

type Algorithm string

Algorithm is a Matrix message encryption algorithm.

const (
	AlgorithmOlmV1    Algorithm = "m.olm.v1.curve25519-aes-sha2"
	AlgorithmMegolmV1 Algorithm = "m.megolm.v1.aes-sha2"

type BatchID added in v0.9.30

type BatchID string

A BatchID is a string identifying a batch of events being backfilled to a room.

func (BatchID) String added in v0.9.30

func (batchID BatchID) String() string

type ContentURI

type ContentURI struct {
	Homeserver string
	FileID     string

ContentURI represents a Matrix content URI.

func MustParseContentURI

func MustParseContentURI(uri string) ContentURI

func ParseContentURI

func ParseContentURI(uri string) (parsed ContentURI, err error)

ParseContentURI parses a Matrix content URI.

func ParseContentURIBytes

func ParseContentURIBytes(uri []byte) (parsed ContentURI, err error)

func (ContentURI) CUString

func (uri ContentURI) CUString() ContentURIString

func (ContentURI) IsEmpty

func (uri ContentURI) IsEmpty() bool

func (*ContentURI) MarshalJSON

func (uri *ContentURI) MarshalJSON() ([]byte, error)

func (ContentURI) MarshalText added in v0.3.6

func (uri ContentURI) MarshalText() ([]byte, error)

func (*ContentURI) Scan added in v0.12.0

func (uri *ContentURI) Scan(i interface{}) error

func (ContentURI) String

func (uri ContentURI) String() string

func (*ContentURI) UnmarshalJSON

func (uri *ContentURI) UnmarshalJSON(raw []byte) (err error)

func (*ContentURI) UnmarshalText added in v0.3.6

func (uri *ContentURI) UnmarshalText(raw []byte) (err error)

func (*ContentURI) Value added in v0.12.0

func (uri *ContentURI) Value() (driver.Value, error)

type ContentURIString

type ContentURIString string

ContentURIString is a string that's expected to be a Matrix content URI. It's useful for delaying the parsing of the content URI to move errors from the event content JSON parsing step to a later step where more appropriate errors can be produced.

func (ContentURIString) Parse

func (uriString ContentURIString) Parse() (ContentURI, error)

func (ContentURIString) ParseOrIgnore

func (uriString ContentURIString) ParseOrIgnore() ContentURI

type CrossSigningKey added in v0.12.0

type CrossSigningKey struct {
	Key   Ed25519
	First Ed25519

type CrossSigningUsage added in v0.8.0

type CrossSigningUsage string
const (
	XSUsageMaster      CrossSigningUsage = "master"
	XSUsageSelfSigning CrossSigningUsage = "self_signing"
	XSUsageUserSigning CrossSigningUsage = "user_signing"

type Curve25519

type Curve25519 string

Curve25519 is the base64 representation of an Curve25519 public key

func (Curve25519) Bytes added in v0.18.0

func (curve25519 Curve25519) Bytes() []byte

func (Curve25519) String

func (curve25519 Curve25519) String() string

type Device added in v0.12.0

type Device struct {
	UserID      UserID
	DeviceID    DeviceID
	IdentityKey Curve25519
	SigningKey  Ed25519

	Trust   TrustState
	Deleted bool
	Name    string

Device contains the identity details of a device and some additional info.

func (*Device) Fingerprint added in v0.12.0

func (device *Device) Fingerprint() string

type DeviceID

type DeviceID string

A DeviceID is an arbitrary string that references a specific device.

func (DeviceID) String

func (deviceID DeviceID) String() string

type DeviceKeyID

type DeviceKeyID string

A DeviceKeyID is a string formatted as <algorithm>:<device_id> that is used as the key in deviceid-key mappings.

func NewDeviceKeyID

func NewDeviceKeyID(algorithm KeyAlgorithm, deviceID DeviceID) DeviceKeyID

func (DeviceKeyID) Parse

func (deviceKeyID DeviceKeyID) Parse() (Algorithm, DeviceID)

func (DeviceKeyID) String

func (deviceKeyID DeviceKeyID) String() string

type Ed25519

type Ed25519 string

Ed25519 is the base64 representation of an Ed25519 public key

func (Ed25519) Bytes added in v0.18.0

func (ed25519 Ed25519) Bytes() []byte

func (Ed25519) Fingerprint added in v0.12.0

func (ed25519 Ed25519) Fingerprint() string

func (Ed25519) String

func (ed25519 Ed25519) String() string

type EventID

type EventID string

An EventID is a string starting with $ that references a specific event.

func (EventID) String

func (eventID EventID) String() string

type IdentityKey added in v0.5.5

type IdentityKey = Curve25519

type KeyAlgorithm

type KeyAlgorithm string
const (
	KeyAlgorithmCurve25519       KeyAlgorithm = "curve25519"
	KeyAlgorithmEd25519          KeyAlgorithm = "ed25519"
	KeyAlgorithmSignedCurve25519 KeyAlgorithm = "signed_curve25519"

type KeyBackupAlgorithm added in v0.18.0

type KeyBackupAlgorithm string
const (
	KeyBackupAlgorithmMegolmBackupV1 KeyBackupAlgorithm = "m.megolm_backup.v1.curve25519-aes-sha2"

type KeyBackupVersion added in v0.18.0

type KeyBackupVersion string

BackupVersion is an arbitrary string that identifies a server side key backup.

func (KeyBackupVersion) String added in v0.18.0

func (version KeyBackupVersion) String() string

type KeyID

type KeyID string

A KeyID a string formatted as <keyalgorithm>:<key_id> that is used as the key in one-time-key mappings.

func NewKeyID

func NewKeyID(algorithm KeyAlgorithm, keyID string) KeyID

func (KeyID) Parse

func (keyID KeyID) Parse() (KeyAlgorithm, string)

func (KeyID) String

func (keyID KeyID) String() string

type MatrixURI added in v0.9.13

type MatrixURI struct {
	Sigil1 rune
	Sigil2 rune
	MXID1  string
	MXID2  string
	Via    []string
	Action string

MatrixURI contains the result of parsing a matrix: URI using ParseMatrixURI

func ParseMatrixToURL added in v0.9.13

func ParseMatrixToURL(uri string) (*MatrixURI, error)

ParseMatrixToURL parses a URL into the same container as ParseMatrixURI parses matrix: URIs.

func ParseMatrixURI added in v0.9.13

func ParseMatrixURI(uri string) (*MatrixURI, error)

ParseMatrixURI implements the matrix: URI parsing algorithm.

Currently specified in

func ParseMatrixURIOrMatrixToURL added in v0.9.13

func ParseMatrixURIOrMatrixToURL(uri string) (*MatrixURI, error)

ParseMatrixURIOrMatrixToURL parses the given URL or matrix: URI into a unified representation.

func ProcessMatrixToURL added in v0.9.13

func ProcessMatrixToURL(uri *url.URL) (*MatrixURI, error)

ProcessMatrixToURL is the equivalent of ProcessMatrixURI for URLs.

func ProcessMatrixURI added in v0.9.13

func ProcessMatrixURI(uri *url.URL) (*MatrixURI, error)

ProcessMatrixURI implements steps 2-7 of the matrix: URI parsing algorithm (i.e. everything except parsing the URI itself, which is done with url.Parse or ParseMatrixURI)

func (*MatrixURI) EventID added in v0.9.13

func (uri *MatrixURI) EventID() EventID

EventID returns the event ID from the URI if the primary identifier is a room ID or alias and the secondary identifier is an event ID.

func (*MatrixURI) MatrixToURL added in v0.9.13

func (uri *MatrixURI) MatrixToURL() string

MatrixToURL converts to parsed matrix: URI into a URL

func (*MatrixURI) PrimaryIdentifier added in v0.9.13

func (uri *MatrixURI) PrimaryIdentifier() string

PrimaryIdentifier returns the first Matrix identifier in the URI. Currently room IDs, room aliases and user IDs can be in the primary identifier slot.

func (*MatrixURI) RoomAlias added in v0.9.13

func (uri *MatrixURI) RoomAlias() RoomAlias

RoomAlias returns the room alias from the URI if the primary identifier is a room alias.

func (*MatrixURI) RoomID added in v0.9.13

func (uri *MatrixURI) RoomID() RoomID

RoomID returns the room ID from the URI if the primary identifier is a room ID.

func (*MatrixURI) SecondaryIdentifier added in v0.9.13

func (uri *MatrixURI) SecondaryIdentifier() string

SecondaryIdentifier returns the second Matrix identifier in the URI. Currently only event IDs can be in the secondary identifier slot.

func (*MatrixURI) String added in v0.9.13

func (uri *MatrixURI) String() string

String converts the parsed matrix: URI back into the string representation.

func (*MatrixURI) UserID added in v0.9.13

func (uri *MatrixURI) UserID() UserID

UserID returns the user ID from the URI if the primary identifier is a user ID.

type OlmMsgType

type OlmMsgType int

OlmMsgType is an Olm message type

const (
	OlmMsgTypePreKey OlmMsgType = 0
	OlmMsgTypeMsg    OlmMsgType = 1

type RoomAlias

type RoomAlias string

A RoomAlias is a string starting with # that can be resolved into.

func NewRoomAlias

func NewRoomAlias(localpart, server string) RoomAlias

func (RoomAlias) EventURI added in v0.10.8

func (roomAlias RoomAlias) EventURI(eventID EventID) *MatrixURI

func (RoomAlias) String

func (roomAlias RoomAlias) String() string

func (RoomAlias) URI added in v0.10.8

func (roomAlias RoomAlias) URI() *MatrixURI

type RoomID

type RoomID string

A RoomID is a string starting with ! that references a specific room.

func (RoomID) EventURI added in v0.10.8

func (roomID RoomID) EventURI(eventID EventID, via ...string) *MatrixURI

func (RoomID) String

func (roomID RoomID) String() string

func (RoomID) URI added in v0.10.8

func (roomID RoomID) URI(via ...string) *MatrixURI

type Secret added in v0.18.0

type Secret string

Secret storage keys

const (
	SecretXSMaster       Secret = "m.cross_signing.master"
	SecretXSSelfSigning  Secret = "m.cross_signing.self_signing"
	SecretXSUserSigning  Secret = "m.cross_signing.user_signing"
	SecretMegolmBackupV1 Secret = "m.megolm_backup.v1"

func (Secret) String added in v0.18.0

func (s Secret) String() string

type SenderKey

type SenderKey = Curve25519

type SessionID

type SessionID string

A SessionID is an arbitrary string that identifies an Olm or Megolm session.

func (SessionID) String

func (sessionID SessionID) String() string

type SigningKey added in v0.5.5

type SigningKey = Ed25519

type TrustState added in v0.12.0

type TrustState int

TrustState determines how trusted a device is.

const (
	TrustStateBlacklisted          TrustState = -100
	TrustStateUnset                TrustState = 0
	TrustStateUnknownDevice        TrustState = 10
	TrustStateForwarded            TrustState = 20
	TrustStateCrossSignedUntrusted TrustState = 50
	TrustStateCrossSignedTOFU      TrustState = 100
	TrustStateCrossSignedVerified  TrustState = 200
	TrustStateVerified             TrustState = 300
	TrustStateInvalid              TrustState = (1 << 31) - 1

func ParseTrustState added in v0.12.0

func ParseTrustState(val string) TrustState

func (*TrustState) MarshalText added in v0.12.0

func (ts *TrustState) MarshalText() ([]byte, error)

func (TrustState) String added in v0.12.0

func (ts TrustState) String() string

func (*TrustState) UnmarshalText added in v0.12.0

func (ts *TrustState) UnmarshalText(data []byte) error

type UserID

type UserID string

UserID represents a Matrix user ID.

func NewEncodedUserID

func NewEncodedUserID(localpart, homeserver string) UserID

func NewUserID

func NewUserID(localpart, homeserver string) UserID

func (UserID) Homeserver added in v0.12.0

func (userID UserID) Homeserver() string

func (UserID) Localpart added in v0.12.0

func (userID UserID) Localpart() string

func (UserID) Parse

func (userID UserID) Parse() (localpart, homeserver string, err error)

Parse parses the user ID into the localpart and server name.

Note that this only enforces very basic user ID formatting requirements: user IDs start with a @, and contain a : after the @. If you want to enforce localpart validity, see the ParseAndValidate and ValidateUserLocalpart functions.

func (UserID) ParseAndDecode

func (userID UserID) ParseAndDecode() (localpart, homeserver string, err error)

func (UserID) ParseAndValidate added in v0.8.4

func (userID UserID) ParseAndValidate() (localpart, homeserver string, err error)

ParseAndValidate parses the user ID into the localpart and server name like Parse, and also validates that the localpart is allowed according to the user identifiers spec.

func (UserID) String

func (userID UserID) String() string

func (UserID) URI added in v0.10.8

func (userID UserID) URI() *MatrixURI

URI returns the user ID as a MatrixURI struct, which can then be stringified into a matrix: URI or a URL.

This does not parse or validate the user ID. Use the ParseAndValidate method if you want to ensure the user ID is valid first.

type VerificationTransactionID added in v0.18.0

type VerificationTransactionID string

VerificationTransactionID is a unique identifier for a verification transaction.

func NewVerificationTransactionID added in v0.18.0

func NewVerificationTransactionID() VerificationTransactionID

func (VerificationTransactionID) String added in v0.18.0

func (t VerificationTransactionID) String() string

Jump to

Keyboard shortcuts

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