matrix

package
v0.4.7-1-testing-2025-... Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2025 License: GPL-2.0 Imports: 9 Imported by: 0

README

Matrix Bridge

This package implements Matrix protocol support for bridging NNTP newsgroups to Matrix rooms.

Features

  • Matrix Client: Full Matrix client implementation
  • Room Management: Automatic room creation for newsgroups
  • Rich Formatting: HTML and plain text message support
  • Real-time Bridging: Live article streaming to Matrix rooms
  • Error Handling: Robust error handling and logging

Components

client.go

Core Matrix client implementation:

  • MatrixClient - Main client instance with HTTP API
  • MatrixMessage - Rich message formatting (HTML + plain text)
  • RoomCreateRequest/Response - Room management
  • ArticleToMessage() - Converts NNTP articles to Matrix messages
bridge.go

Bridge management:

  • Bridge - Manages room registration and article bridging
  • Thread-safe operations with mutex protection
  • Enable/disable functionality
  • Room mapping (newsgroup → Matrix room ID)

Usage

// Create bridge
bridge := matrix.NewBridge("https://matrix.org", "access_token", "@user:matrix.org")
bridge.Enable()

// Register newsgroup (creates Matrix room)
newsgroup := &models.Newsgroup{Name: "comp.lang.go", Description: "Go programming"}
bridge.RegisterNewsgroup(newsgroup)

// Bridge article (sends to Matrix room)
article := &models.Article{MessageID: "123@example.com", Subject: "Hello", BodyText: "World"}
bridge.BridgeArticle(article, "comp.lang.go")

Matrix Room Features

Each newsgroup gets its own Matrix room with:

  • Room Name: Newsgroup name (e.g., "comp.lang.go")
  • Room Topic: Newsgroup description
  • Public Visibility: Discoverable in room directory
  • Room Alias: #newsgroup_comp.lang.go:homeserver.com

Message Format

Articles are formatted as rich Matrix messages:

HTML Format
<h3>Subject: Hello World</h3>
<p><strong>From:</strong> user@example.com</p>
<p><strong>Date:</strong> 2025-07-09T12:00:00Z</p>
<p><strong>Message-ID:</strong> 123@example.com</p>
<hr>
<pre>Article body content here...</pre>
Plain Text Format
Subject: Hello World
From: user@example.com
Date: 2025-07-09T12:00:00Z
Message-ID: 123@example.com

Article body content here...

Configuration

Prerequisites
  1. Matrix account on a homeserver (matrix.org, etc.)
  2. Access token (obtain via Element or API)
  3. User ID in format @username:homeserver.com
Enable via command line:
./web --enable-matrix \
      --matrix-homeserver=https://matrix.org \
      --matrix-accesstoken=your_access_token \
      --matrix-userid=@your_user:matrix.org
Getting Access Token
Via Element Web Client:
  1. Login to Element
  2. Settings → Help & About → Access Token
  3. Copy the token (starts with syt_ or mda_)
Via API:
curl -XPOST -d '{"type":"m.login.password", "user":"username", "password":"password"}' \
     "https://matrix.org/_matrix/client/r0/login"

Matrix API Endpoints Used

  • POST /_matrix/client/r0/createRoom - Create newsgroup rooms
  • POST /_matrix/client/r0/rooms/{roomId}/send/m.room.message/{txnId} - Send articles

Room Settings

Created rooms have these settings:

  • Preset: public_chat (anyone can join)
  • Visibility: public (listed in directory)
  • History Visibility: Inherited from homeserver
  • Join Rules: public

Error Handling

The bridge handles common Matrix errors:

  • Rate Limiting: HTTP 429 responses
  • Authentication: Invalid/expired tokens
  • Permission: Insufficient room permissions
  • Network: Connection timeouts and failures

Security Considerations

  • Access Token: Store securely, has full account access
  • Room Permissions: Bot user should have moderator rights
  • Rate Limits: Respect Matrix homeserver rate limits
  • Content Policy: Follow homeserver content policies

TODO

  • Support for Matrix → NNTP bridging (replies)
  • Rate limiting and backoff strategies
  • End-to-end encryption support
  • Moderation tools integration
  • Threading support for Matrix replies
  • Media attachment support
  • Webhook support for real-time events

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bridge

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

func NewBridge

func NewBridge(homeserver, accessToken, userID string) *Bridge

func (*Bridge) BridgeArticle

func (b *Bridge) BridgeArticle(article *models.Article, newsgroup string) error

func (*Bridge) Disable

func (b *Bridge) Disable()

func (*Bridge) Enable

func (b *Bridge) Enable()

func (*Bridge) IsEnabled

func (b *Bridge) IsEnabled() bool

func (*Bridge) RegisterNewsgroup

func (b *Bridge) RegisterNewsgroup(newsgroup *models.Newsgroup) error

type MatrixClient

type MatrixClient struct {
	Homeserver  string
	AccessToken string
	UserID      string
	Client      *http.Client
}

func NewMatrixClient

func NewMatrixClient(homeserver, accessToken, userID string) *MatrixClient

func (*MatrixClient) ArticleToMessage

func (mc *MatrixClient) ArticleToMessage(article *models.Article) *MatrixMessage

func (*MatrixClient) CreateRoom

func (mc *MatrixClient) CreateRoom(newsgroup *models.Newsgroup) (string, error)

func (*MatrixClient) SendArticle

func (mc *MatrixClient) SendArticle(roomID string, article *models.Article) error

func (*MatrixClient) SendMessage

func (mc *MatrixClient) SendMessage(roomID string, message *MatrixMessage) error

type MatrixEvent

type MatrixEvent struct {
	Type     string      `json:"type"`
	Content  interface{} `json:"content"`
	StateKey string      `json:"state_key,omitempty"`
}

type MatrixMessage

type MatrixMessage struct {
	MsgType       string `json:"msgtype"`
	Body          string `json:"body"`
	Format        string `json:"format,omitempty"`
	FormattedBody string `json:"formatted_body,omitempty"`
}

type RoomCreateRequest

type RoomCreateRequest struct {
	Name          string         `json:"name"`
	Topic         string         `json:"topic,omitempty"`
	Preset        string         `json:"preset,omitempty"`
	Visibility    string         `json:"visibility"`
	RoomAliasName string         `json:"room_alias_name,omitempty"`
	InitialState  []MatrixEvent  `json:"initial_state,omitempty"`
	PowerLevels   map[string]int `json:"power_level_content_override,omitempty"`
}

type RoomCreateResponse

type RoomCreateResponse struct {
	RoomID string `json:"room_id"`
}

Jump to

Keyboard shortcuts

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