shared

package
v1.3.8 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultMaxMessageBytes is the chat content cap when neither message env var is set.
	DefaultMaxMessageBytes int64 = 32 << 10
	// DefaultMaxFileBytes is the file payload cap when neither file env var is set.
	DefaultMaxFileBytes int64 = 1024 * 1024

	EnvMaxMessageBytes = "MARCHAT_MAX_MESSAGE_BYTES"
	EnvMaxMessageMB    = "MARCHAT_MAX_MESSAGE_MB"
	EnvMaxFileBytes    = "MARCHAT_MAX_FILE_BYTES"
	EnvMaxFileMB       = "MARCHAT_MAX_FILE_MB"
)

Variables

View Source
var (
	// ClientVersion is the version of the MarChat client
	ClientVersion = "dev"

	// ServerVersion is the version of the MarChat server
	ServerVersion = "dev"

	// BuildTime is the time when the binary was built
	BuildTime = "unknown"

	// GitCommit is the git commit hash
	GitCommit = "unknown"
)

Version variables that can be set at build time using ldflags

Functions

func CompareVersions

func CompareVersions(a, b string) int

CompareVersions compares two semver-style version strings (e.g. "1.2.3"). Returns -1 if a < b, 0 if a == b, 1 if a > b. Non-numeric or "dev" versions are treated as 0.0.0 so they satisfy any constraint.

func ContentExceedsLimit added in v1.3.8

func ContentExceedsLimit(content string, maxBytes int64) bool

ContentExceedsLimit reports whether content's UTF-8 byte length is above the effective message cap. A length equal to the cap is allowed.

func DecryptMessage

func DecryptMessage(sessionKey *SessionKey, encrypted *EncryptedMessage) ([]byte, error)

DecryptMessage decrypts a message using ChaCha20-Poly1305.

func EffectiveMaxMessageBytes added in v1.3.8

func EffectiveMaxMessageBytes(n int64) int64

EffectiveMaxMessageBytes returns n, or DefaultMaxMessageBytes when n is not positive.

func FormatMessageLimit added in v1.3.8

func FormatMessageLimit(n int64) string

FormatMessageLimit renders a message cap for admin UI. Non-positive n uses the default. Whole megabytes use the same "%.1f MB" form as file size. Other whole kibibytes use KiB.

func GetServerVersionInfo

func GetServerVersionInfo() string

GetServerVersionInfo returns a formatted server version string

func GetVersionInfo

func GetVersionInfo() string

GetVersionInfo returns a formatted version string

func MaxBytesFromEnv added in v1.3.8

func MaxBytesFromEnv(bytesKey, mbKey string, defaultBytes int64) int64

MaxBytesFromEnv is the client path: an invalid or non-positive setting falls back to defaultBytes instead of failing startup.

func ParseMaxBytesEnv added in v1.3.8

func ParseMaxBytesEnv(bytesKey, mbKey string, defaultBytes int64) (int64, error)

ParseMaxBytesEnv reads bytesKey, then mbKey (megabytes). An empty pair returns defaultBytes. A present value that does not parse or is not positive is an error. bytesKey wins when both are set.

Types

type EncryptedMessage

type EncryptedMessage struct {
	Type        MessageType `json:"type"`
	Sender      string      `json:"sender"`
	CreatedAt   time.Time   `json:"created_at"`
	Content     string      `json:"content,omitempty"`      // Plaintext for system messages
	Encrypted   []byte      `json:"encrypted,omitempty"`    // Encrypted payload
	Nonce       []byte      `json:"nonce,omitempty"`        // For encrypted messages
	Recipient   string      `json:"recipient,omitempty"`    // For direct messages
	IsEncrypted bool        `json:"is_encrypted,omitempty"` // Flag for encrypted messages
	File        *FileMeta   `json:"file,omitempty"`         // For file messages
}

EncryptedMessage represents an E2E encrypted message (AEAD payload and metadata).

func EncryptMessage

func EncryptMessage(sessionKey *SessionKey, plaintext []byte) (*EncryptedMessage, error)

EncryptMessage encrypts a message using ChaCha20-Poly1305.

func EncryptTextMessage

func EncryptTextMessage(sessionKey *SessionKey, sender, content string) (*EncryptedMessage, error)

EncryptTextMessage encrypts a text message.

type FileMeta

type FileMeta struct {
	Filename string `json:"filename"`
	Size     int64  `json:"size"`
	Data     []byte `json:"data"` // raw bytes (base64-encoded in JSON)
}

type Handshake

type Handshake struct {
	Username string `json:"username"`
	Admin    bool   `json:"admin"`
	AdminKey string `json:"admin_key,omitempty"`
}

Handshake is sent by the client on WebSocket connect for authentication

type Message

type Message struct {
	Sender    string      `json:"sender"`
	Content   string      `json:"content"`
	CreatedAt time.Time   `json:"created_at"`
	Type      MessageType `json:"type,omitempty"`
	Encrypted bool        `json:"encrypted,omitempty"`

	// MessageID uniquely identifies a message for edits, deletes, reactions, pins
	MessageID int64 `json:"message_id,omitempty"`

	// Recipient for direct messages (empty = broadcast to all)
	Recipient string `json:"recipient,omitempty"`

	// Edited indicates the message has been modified
	Edited bool `json:"edited,omitempty"`

	Channel string `json:"channel,omitempty"`

	// Reaction metadata
	Reaction *ReactionMeta `json:"reaction,omitempty"`

	// For file messages, Content is empty and File is set
	File *FileMeta `json:"file,omitempty"`
}

func DecryptTextMessage

func DecryptTextMessage(sessionKey *SessionKey, encrypted *EncryptedMessage) (*Message, error)

DecryptTextMessage decrypts a text message and returns the original Message.

type MessageType

type MessageType string

MessageType distinguishes between text and file messages

const (
	TextMessage      MessageType = "text"
	FileMessageType  MessageType = "file"
	AdminCommandType MessageType = "admin_command"
	EditMessageType  MessageType = "edit"
	DeleteMessage    MessageType = "delete"
	TypingMessage    MessageType = "typing"
	ReactionMessage  MessageType = "reaction"
	DirectMessage    MessageType = "dm"
	SearchMessage    MessageType = "search"
	PinMessage       MessageType = "pin"
	ReadReceiptType  MessageType = "read_receipt"
	JoinChannelType  MessageType = "join_channel"
	LeaveChannelType MessageType = "leave_channel"
	ListChannelsType MessageType = "list_channels"
)

type ReactionMeta

type ReactionMeta struct {
	Emoji     string `json:"emoji"`
	TargetID  int64  `json:"target_id"`
	IsRemoval bool   `json:"is_removal,omitempty"`
}

ReactionMeta contains reaction data for a message

type SessionKey

type SessionKey struct {
	Key       []byte    `json:"key"`
	CreatedAt time.Time `json:"created_at"`
	KeyID     string    `json:"key_id"`
}

SessionKey holds 32-byte ChaCha20-Poly1305 key material for the global E2E model. KeyID is a base64(SHA256(key)) fingerprint for logs and display, not a wire identifier.

Jump to

Keyboard shortcuts

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