From a66eac42c4997f3777fa62b8629c630e93b9cd20 Mon Sep 17 00:00:00 2001 From: horsley Date: Sat, 7 Mar 2026 17:44:24 +0000 Subject: [PATCH 1/5] feat: add Matrix channel support --- README.md | 37 +- README.zh.md | 1 + cmd/picoclaw-launcher-tui/internal/ui/app.go | 2 +- .../internal/ui/channel.go | 28 + cmd/picoclaw-launcher/README.md | 2 +- cmd/picoclaw-launcher/internal/ui/index.html | 12 + cmd/picoclaw/internal/gateway/helpers.go | 1 + config/config.example.json | 17 + docs/channels/matrix/README.md | 59 ++ docs/channels/matrix/README.zh.md | 59 ++ go.mod | 3 +- go.sum | 2 + pkg/channels/manager.go | 8 + pkg/channels/matrix/init.go | 13 + pkg/channels/matrix/matrix.go | 896 ++++++++++++++++++ pkg/channels/matrix/matrix_test.go | 218 +++++ pkg/config/config.go | 14 + pkg/config/config_test.go | 3 + pkg/config/defaults.go | 16 + pkg/migrate/sources/openclaw/common.go | 1 + .../sources/openclaw/openclaw_config.go | 44 +- .../sources/openclaw/openclaw_config_test.go | 94 ++ 22 files changed, 1523 insertions(+), 7 deletions(-) create mode 100644 docs/channels/matrix/README.md create mode 100644 docs/channels/matrix/README.zh.md create mode 100644 pkg/channels/matrix/init.go create mode 100644 pkg/channels/matrix/matrix.go create mode 100644 pkg/channels/matrix/matrix_test.go diff --git a/README.md b/README.md index db127a85f..f2de68be7 100644 --- a/README.md +++ b/README.md @@ -308,7 +308,7 @@ That's it! You have a working AI assistant in 2 minutes. ## 💬 Chat Apps -Talk to your picoclaw through Telegram, Discord, WhatsApp, DingTalk, LINE, or WeCom +Talk to your picoclaw through Telegram, Discord, WhatsApp, Matrix, QQ, DingTalk, LINE, or WeCom > **Note**: All webhook-based channels (LINE, WeCom, etc.) are served on a single shared Gateway HTTP server (`gateway.host`:`gateway.port`, default `127.0.0.1:18790`). There are no per-channel ports to configure. Note: Feishu uses WebSocket/SDK mode and does not use the shared HTTP webhook server. @@ -317,6 +317,7 @@ Talk to your picoclaw through Telegram, Discord, WhatsApp, DingTalk, LINE, or We | **Telegram** | Easy (just a token) | | **Discord** | Easy (bot token + intents) | | **WhatsApp** | Easy (native: QR scan; or bridge URL) | +| **Matrix** | Medium (homeserver + bot access token) | | **QQ** | Easy (AppID + AppSecret) | | **DingTalk** | Medium (app credentials) | | **LINE** | Medium (credentials + webhook URL) | @@ -528,6 +529,40 @@ picoclaw gateway ``` +
+Matrix + +**1. Prepare bot account** + +* Use your preferred homeserver (e.g. `https://matrix.org` or self-hosted) +* Create a bot user and obtain its access token + +**2. Configure** + +```json +{ + "channels": { + "matrix": { + "enabled": true, + "homeserver": "https://matrix.org", + "user_id": "@your-bot:matrix.org", + "access_token": "YOUR_MATRIX_ACCESS_TOKEN", + "allow_from": [] + } + } +} +``` + +**3. Run** + +```bash +picoclaw gateway +``` + +For full options (`device_id`, `join_on_invite`, `group_trigger`, `placeholder`, `reasoning_channel_id`), see [Matrix Channel Configuration Guide](docs/channels/matrix/README.md). + +
+
LINE diff --git a/README.zh.md b/README.zh.md index d42b3cbb8..c744e0d20 100644 --- a/README.zh.md +++ b/README.zh.md @@ -299,6 +299,7 @@ PicoClaw 支持多种聊天平台,使您的 Agent 能够连接到任何地方 | **Telegram** | ⭐ 简单 | 推荐,支持语音转文字,长轮询无需公网 | [查看文档](docs/channels/telegram/README.zh.md) | | **Discord** | ⭐ 简单 | Socket Mode,支持群组/私信,Bot 生态成熟 | [查看文档](docs/channels/discord/README.zh.md) | | **Slack** | ⭐ 简单 | **Socket Mode** (无需公网 IP),企业级支持 | [查看文档](docs/channels/slack/README.zh.md) | +| **Matrix** | ⭐⭐ 中等 | 联邦协议,支持自建 homeserver 与公开服务器 | [查看文档](docs/channels/matrix/README.zh.md) | | **QQ** | ⭐⭐ 中等 | 官方机器人 API,适合国内社群 | [查看文档](docs/channels/qq/README.zh.md) | | **钉钉 (DingTalk)** | ⭐⭐ 中等 | Stream 模式无需公网,企业办公首选 | [查看文档](docs/channels/dingtalk/README.zh.md) | | **企业微信 (WeCom)** | ⭐⭐⭐ 较难 | 支持群机器人(Webhook)、自建应用(API)和智能机器人(AI Bot) | [Bot 文档](docs/channels/wecom/wecom_bot/README.zh.md) / [App 文档](docs/channels/wecom/wecom_app/README.zh.md) / [AI Bot 文档](docs/channels/wecom/wecom_aibot/README.zh.md) | diff --git a/cmd/picoclaw-launcher-tui/internal/ui/app.go b/cmd/picoclaw-launcher-tui/internal/ui/app.go index 4947d6aea..8628afab3 100644 --- a/cmd/picoclaw-launcher-tui/internal/ui/app.go +++ b/cmd/picoclaw-launcher-tui/internal/ui/app.go @@ -423,7 +423,7 @@ func (s *appState) hasEnabledChannel() bool { c := s.config.Channels return c.Telegram.Enabled || c.Discord.Enabled || c.QQ.Enabled || c.MaixCam.Enabled || c.WhatsApp.Enabled || c.Feishu.Enabled || c.DingTalk.Enabled || c.Slack.Enabled || - c.LINE.Enabled || c.OneBot.Enabled || c.WeCom.Enabled || c.WeComApp.Enabled + c.Matrix.Enabled || c.LINE.Enabled || c.OneBot.Enabled || c.WeCom.Enabled || c.WeComApp.Enabled } func (s *appState) confirmApplyOrDiscard(onApply func(), onDiscard func()) { diff --git a/cmd/picoclaw-launcher-tui/internal/ui/channel.go b/cmd/picoclaw-launcher-tui/internal/ui/channel.go index 49a6ccc5d..16b7d053b 100644 --- a/cmd/picoclaw-launcher-tui/internal/ui/channel.go +++ b/cmd/picoclaw-launcher-tui/internal/ui/channel.go @@ -61,6 +61,12 @@ func (s *appState) buildChannelMenuItems() []MenuItem { s.config.Channels.Slack.Enabled, func() { s.push("channel-slack", s.slackForm()) }, ), + channelItem( + "Matrix", + "Matrix bot settings", + s.config.Channels.Matrix.Enabled, + func() { s.push("channel-matrix", s.matrixForm()) }, + ), channelItem( "LINE", "LINE bot settings", @@ -233,6 +239,28 @@ func (s *appState) lineForm() tview.Primitive { return wrapWithBack(form, s) } +func (s *appState) matrixForm() tview.Primitive { + cfg := &s.config.Channels.Matrix + form := baseChannelForm("Matrix", cfg.Enabled, s.makeChannelOnEnabled(&cfg.Enabled)) + form.AddInputField("Homeserver", cfg.Homeserver, 128, nil, func(text string) { + cfg.Homeserver = strings.TrimSpace(text) + }) + form.AddInputField("User ID", cfg.UserID, 128, nil, func(text string) { + cfg.UserID = strings.TrimSpace(text) + }) + form.AddInputField("Access Token", cfg.AccessToken, 128, nil, func(text string) { + cfg.AccessToken = strings.TrimSpace(text) + }) + form.AddInputField("Device ID", cfg.DeviceID, 128, nil, func(text string) { + cfg.DeviceID = strings.TrimSpace(text) + }) + form.AddCheckbox("Join On Invite", cfg.JoinOnInvite, func(checked bool) { + cfg.JoinOnInvite = checked + }) + addAllowFromField(form, &cfg.AllowFrom) + return wrapWithBack(form, s) +} + func (s *appState) onebotForm() tview.Primitive { cfg := &s.config.Channels.OneBot form := baseChannelForm("OneBot", cfg.Enabled, s.makeChannelOnEnabled(&cfg.Enabled)) diff --git a/cmd/picoclaw-launcher/README.md b/cmd/picoclaw-launcher/README.md index 641279bb1..0872a5f65 100644 --- a/cmd/picoclaw-launcher/README.md +++ b/cmd/picoclaw-launcher/README.md @@ -9,7 +9,7 @@ A standalone launcher for PicoClaw, providing visual JSON editing and OAuth prov - 📝 **Config Editor** — Sidebar-based settings UI with model management, channel configuration forms, and a raw JSON editor - 🤖 **Model Management** — Model card grid with availability status (grayed out without API key), primary model selection, add/edit/delete with required/optional field separation -- 📡 **Channel Configuration** — Form-based settings for 12 channel types (Telegram, Discord, Slack, WeCom, DingTalk, Feishu, LINE, WhatsApp, QQ, OneBot, MaixCAM, etc.) with documentation links +- 📡 **Channel Configuration** — Form-based settings for 13 channel types (Telegram, Discord, Slack, Matrix, WeCom, DingTalk, Feishu, LINE, WhatsApp, QQ, OneBot, MaixCAM, etc.) with documentation links - 🔐 **Provider Auth** — Login to OpenAI (Device Code), Anthropic (API Token), Google Antigravity (Browser OAuth) - 🌐 **Embedded Frontend** — Compiles to a single binary with no external dependencies - 🌍 **i18n** — Chinese/English language switching with browser auto-detection diff --git a/cmd/picoclaw-launcher/internal/ui/index.html b/cmd/picoclaw-launcher/internal/ui/index.html index d84fd4e6e..e77ef4fea 100644 --- a/cmd/picoclaw-launcher/internal/ui/index.html +++ b/cmd/picoclaw-launcher/internal/ui/index.html @@ -538,6 +538,7 @@ + @@ -606,6 +607,7 @@
+
@@ -1011,6 +1013,16 @@ const channelSchemas = { { key: 'app_token', label: 'App Token', type: 'password', placeholder: 'xapp-...' }, ] }, + matrix: { + title: 'Matrix', configKey: 'matrix', docSlug: null, + fields: [ + { key: 'homeserver', label: 'Homeserver', type: 'text', placeholder: 'https://matrix.org' }, + { key: 'user_id', label: 'User ID', type: 'text', placeholder: '@bot:matrix.org' }, + { key: 'access_token', label: 'Access Token', type: 'password', placeholder: 'syt_...' }, + { key: 'device_id', label: 'Device ID', type: 'text', placeholder: 'Optional device ID' }, + { key: 'join_on_invite', label: 'Join On Invite', type: 'toggle' }, + ] + }, wecom: { title: 'WeCom (Bot)', configKey: 'wecom', docSlug: 'wecom-bot', fields: [ diff --git a/cmd/picoclaw/internal/gateway/helpers.go b/cmd/picoclaw/internal/gateway/helpers.go index 00b53e62c..4f93b858a 100644 --- a/cmd/picoclaw/internal/gateway/helpers.go +++ b/cmd/picoclaw/internal/gateway/helpers.go @@ -19,6 +19,7 @@ import ( _ "github.com/sipeed/picoclaw/pkg/channels/irc" _ "github.com/sipeed/picoclaw/pkg/channels/line" _ "github.com/sipeed/picoclaw/pkg/channels/maixcam" + _ "github.com/sipeed/picoclaw/pkg/channels/matrix" _ "github.com/sipeed/picoclaw/pkg/channels/onebot" _ "github.com/sipeed/picoclaw/pkg/channels/pico" _ "github.com/sipeed/picoclaw/pkg/channels/qq" diff --git a/config/config.example.json b/config/config.example.json index 8e58e3252..ccec434d7 100644 --- a/config/config.example.json +++ b/config/config.example.json @@ -114,6 +114,23 @@ "allow_from": [], "reasoning_channel_id": "" }, + "matrix": { + "enabled": false, + "homeserver": "https://matrix.org", + "user_id": "@your-bot:matrix.org", + "access_token": "YOUR_MATRIX_ACCESS_TOKEN", + "device_id": "", + "join_on_invite": true, + "allow_from": [], + "group_trigger": { + "mention_only": true + }, + "placeholder": { + "enabled": true, + "text": "Thinking... 💭" + }, + "reasoning_channel_id": "" + }, "line": { "enabled": false, "channel_secret": "YOUR_LINE_CHANNEL_SECRET", diff --git a/docs/channels/matrix/README.md b/docs/channels/matrix/README.md new file mode 100644 index 000000000..c213aa80b --- /dev/null +++ b/docs/channels/matrix/README.md @@ -0,0 +1,59 @@ +# Matrix Channel Configuration Guide + +## 1. Example Configuration + +Add this to `config.json`: + +```json +{ + "channels": { + "matrix": { + "enabled": true, + "homeserver": "https://matrix.org", + "user_id": "@your-bot:matrix.org", + "access_token": "YOUR_MATRIX_ACCESS_TOKEN", + "device_id": "", + "join_on_invite": true, + "allow_from": [], + "group_trigger": { + "mention_only": true + }, + "placeholder": { + "enabled": true, + "text": "Thinking..." + }, + "reasoning_channel_id": "" + } + } +} +``` + +## 2. Field Reference + +| Field | Type | Required | Description | +|----------------------|----------|----------|-------------| +| enabled | bool | Yes | Enable or disable the Matrix channel | +| homeserver | string | Yes | Matrix homeserver URL (for example `https://matrix.org`) | +| user_id | string | Yes | Bot Matrix user ID (for example `@bot:matrix.org`) | +| access_token | string | Yes | Bot access token | +| device_id | string | No | Optional Matrix device ID | +| join_on_invite | bool | No | Auto-join invited rooms | +| allow_from | []string | No | User whitelist (Matrix user IDs) | +| group_trigger | object | No | Group trigger strategy (`mention_only` / `prefixes`) | +| placeholder | object | No | Placeholder message config | +| reasoning_channel_id | string | No | Target channel for reasoning output | + +## 3. Currently Supported + +- Text message send/receive +- Incoming image/audio/video/file download (MediaStore first, local path fallback) +- Incoming audio normalization into existing transcription flow (`[audio: ...]`) +- Outgoing image/audio/video/file upload and send +- Group trigger rules (including mention-only mode) +- Typing state (`m.typing`) +- Placeholder message + final reply replacement +- Auto-join invited rooms (can be disabled) + +## 4. TODO + +- Rich media metadata improvements (for example image/video size and thumbnails) diff --git a/docs/channels/matrix/README.zh.md b/docs/channels/matrix/README.zh.md new file mode 100644 index 000000000..efbc13093 --- /dev/null +++ b/docs/channels/matrix/README.zh.md @@ -0,0 +1,59 @@ +# Matrix 通道配置指南 + +## 1. 配置示例 + +在 `config.json` 中添加: + +```json +{ + "channels": { + "matrix": { + "enabled": true, + "homeserver": "https://matrix.org", + "user_id": "@your-bot:matrix.org", + "access_token": "YOUR_MATRIX_ACCESS_TOKEN", + "device_id": "", + "join_on_invite": true, + "allow_from": [], + "group_trigger": { + "mention_only": true + }, + "placeholder": { + "enabled": true, + "text": "Thinking... 💭" + }, + "reasoning_channel_id": "" + } + } +} +``` + +## 2. 参数说明 + +| 字段 | 类型 | 必填 | 说明 | +|----------------------|----------|------|------| +| enabled | bool | 是 | 是否启用 Matrix 通道 | +| homeserver | string | 是 | Matrix 服务器地址(例如 `https://matrix.org`) | +| user_id | string | 是 | 机器人 Matrix 用户 ID(例如 `@bot:matrix.org`) | +| access_token | string | 是 | 机器人 access token | +| device_id | string | 否 | 设备 ID(可选) | +| join_on_invite | bool | 否 | 是否自动加入邀请房间 | +| allow_from | []string | 否 | 白名单用户(Matrix 用户 ID) | +| group_trigger | object | 否 | 群聊触发策略(支持 `mention_only` / `prefixes`) | +| placeholder | object | 否 | 占位消息配置 | +| reasoning_channel_id | string | 否 | 思维链输出目标通道 | + +## 3. 当前支持 + +- 文本消息收发 +- 图片/音频/视频/文件消息入站下载(写入 MediaStore / 本地路径回退) +- 音频消息按统一标记进入现有转写流程(`[audio: ...]`) +- 图片/音频/视频/文件消息出站发送(上传到 Matrix 媒体库后发送) +- 群聊触发规则(支持仅 @ 提及时响应) +- Typing 状态(`m.typing`) +- 占位消息(`Thinking... 💭`)+ 最终回复替换 +- 自动加入邀请房间(可关闭) + +## 4. TODO + +- 富媒体细节增强(如 image/video 的尺寸、缩略图等 metadata) diff --git a/go.mod b/go.mod index 2bd5ddef9..14da724f7 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/bwmarrin/discordgo v0.29.0 github.com/caarlos0/env/v11 v11.3.1 github.com/chzyer/readline v1.5.1 + github.com/ergochat/irc-go v0.5.0 github.com/gdamore/tcell/v2 v2.13.8 github.com/google/uuid v1.6.0 github.com/gorilla/websocket v1.5.3 @@ -27,6 +28,7 @@ require ( golang.org/x/oauth2 v0.35.0 golang.org/x/time v0.14.0 google.golang.org/protobuf v1.36.11 + maunium.net/go/mautrix v0.26.3 modernc.org/sqlite v1.46.1 ) @@ -37,7 +39,6 @@ require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/dustin/go-humanize v1.0.1 // indirect github.com/elliotchance/orderedmap/v3 v3.1.0 // indirect - github.com/ergochat/irc-go v0.5.0 // indirect github.com/gdamore/encoding v1.0.1 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/lucasb-eyer/go-colorful v1.3.0 // indirect diff --git a/go.sum b/go.sum index 81a1cdd1e..e762e45bc 100644 --- a/go.sum +++ b/go.sum @@ -361,6 +361,8 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +maunium.net/go/mautrix v0.26.3 h1:tWZih6Vjw0qGTWuPmg9JUrQPzViTNDPGQLVc5UXC4nk= +maunium.net/go/mautrix v0.26.3/go.mod h1:v5ZdDoCwUpNqEj5OrhEoUa3L1kEddKPaAya9TgGXN38= modernc.org/cc/v4 v4.27.1 h1:9W30zRlYrefrDV2JE2O8VDtJ1yPGownxciz5rrbQZis= modernc.org/cc/v4 v4.27.1/go.mod h1:uVtb5OGqUKpoLWhqwNQo/8LwvoiEBLvZXIQ/SmO6mL0= modernc.org/ccgo/v4 v4.30.1 h1:4r4U1J6Fhj98NKfSjnPUN7Ze2c6MnAdL0hWw6+LrJpc= diff --git a/pkg/channels/manager.go b/pkg/channels/manager.go index 2b1cf8e84..cdd49538f 100644 --- a/pkg/channels/manager.go +++ b/pkg/channels/manager.go @@ -61,6 +61,7 @@ var channelRateConfig = map[string]float64{ "telegram": 20, "discord": 1, "slack": 1, + "matrix": 2, "line": 10, "irc": 2, } @@ -244,6 +245,13 @@ func (m *Manager) initChannels() error { m.initChannel("slack", "Slack") } + if m.config.Channels.Matrix.Enabled && + m.config.Channels.Matrix.Homeserver != "" && + m.config.Channels.Matrix.UserID != "" && + m.config.Channels.Matrix.AccessToken != "" { + m.initChannel("matrix", "Matrix") + } + if m.config.Channels.LINE.Enabled && m.config.Channels.LINE.ChannelAccessToken != "" { m.initChannel("line", "LINE") } diff --git a/pkg/channels/matrix/init.go b/pkg/channels/matrix/init.go new file mode 100644 index 000000000..6677f855e --- /dev/null +++ b/pkg/channels/matrix/init.go @@ -0,0 +1,13 @@ +package matrix + +import ( + "github.com/sipeed/picoclaw/pkg/bus" + "github.com/sipeed/picoclaw/pkg/channels" + "github.com/sipeed/picoclaw/pkg/config" +) + +func init() { + channels.RegisterFactory("matrix", func(cfg *config.Config, b *bus.MessageBus) (channels.Channel, error) { + return NewMatrixChannel(cfg.Channels.Matrix, b) + }) +} diff --git a/pkg/channels/matrix/matrix.go b/pkg/channels/matrix/matrix.go new file mode 100644 index 000000000..0d4c62ac5 --- /dev/null +++ b/pkg/channels/matrix/matrix.go @@ -0,0 +1,896 @@ +package matrix + +import ( + "context" + "fmt" + "mime" + "os" + "path/filepath" + "regexp" + "strings" + "sync" + "time" + + "maunium.net/go/mautrix" + "maunium.net/go/mautrix/event" + "maunium.net/go/mautrix/id" + + "github.com/sipeed/picoclaw/pkg/bus" + "github.com/sipeed/picoclaw/pkg/channels" + "github.com/sipeed/picoclaw/pkg/config" + "github.com/sipeed/picoclaw/pkg/identity" + "github.com/sipeed/picoclaw/pkg/logger" + "github.com/sipeed/picoclaw/pkg/media" +) + +const ( + typingRefreshInterval = 20 * time.Second + typingServerTTL = 30 * time.Second + roomKindCacheTTL = 5 * time.Minute +) + +type roomKindCacheEntry struct { + isGroup bool + expiresAt time.Time +} + +type typingSession struct { + stopCh chan struct{} + once sync.Once +} + +func newTypingSession() *typingSession { + return &typingSession{ + stopCh: make(chan struct{}), + } +} + +func (s *typingSession) stop() { + s.once.Do(func() { + close(s.stopCh) + }) +} + +// MatrixChannel implements the Channel interface for Matrix. +type MatrixChannel struct { + *channels.BaseChannel + + client *mautrix.Client + config config.MatrixConfig + syncer *mautrix.DefaultSyncer + + ctx context.Context + cancel context.CancelFunc + startTime time.Time + + typingMu sync.Mutex + typingSessions map[string]*typingSession // roomID -> session + + roomKindCache sync.Map // roomID -> roomKindCacheEntry +} + +func NewMatrixChannel(cfg config.MatrixConfig, messageBus *bus.MessageBus) (*MatrixChannel, error) { + homeserver := strings.TrimSpace(cfg.Homeserver) + userID := strings.TrimSpace(cfg.UserID) + accessToken := strings.TrimSpace(cfg.AccessToken) + if homeserver == "" { + return nil, fmt.Errorf("matrix homeserver is required") + } + if userID == "" { + return nil, fmt.Errorf("matrix user_id is required") + } + if accessToken == "" { + return nil, fmt.Errorf("matrix access_token is required") + } + + client, err := mautrix.NewClient(homeserver, id.UserID(userID), accessToken) + if err != nil { + return nil, fmt.Errorf("create matrix client: %w", err) + } + if cfg.DeviceID != "" { + client.DeviceID = id.DeviceID(cfg.DeviceID) + } + + syncer, ok := client.Syncer.(*mautrix.DefaultSyncer) + if !ok { + return nil, fmt.Errorf("matrix syncer is not *mautrix.DefaultSyncer") + } + + base := channels.NewBaseChannel( + "matrix", + cfg, + messageBus, + cfg.AllowFrom, + channels.WithMaxMessageLength(65536), + channels.WithGroupTrigger(cfg.GroupTrigger), + channels.WithReasoningChannelID(cfg.ReasoningChannelID), + ) + + return &MatrixChannel{ + BaseChannel: base, + client: client, + config: cfg, + syncer: syncer, + typingSessions: make(map[string]*typingSession), + startTime: time.Now(), + roomKindCache: sync.Map{}, + typingMu: sync.Mutex{}, + }, nil +} + +func (c *MatrixChannel) Start(ctx context.Context) error { + logger.InfoC("matrix", "Starting Matrix channel") + + c.ctx, c.cancel = context.WithCancel(ctx) + c.startTime = time.Now() + + c.syncer.OnEventType(event.EventMessage, c.handleMessageEvent) + c.syncer.OnEventType(event.StateMember, c.handleMemberEvent) + + c.SetRunning(true) + + go func() { + if err := c.client.SyncWithContext(c.ctx); err != nil && c.ctx.Err() == nil { + logger.ErrorCF("matrix", "Matrix sync stopped unexpectedly", map[string]any{ + "error": err.Error(), + }) + } + }() + + logger.InfoC("matrix", "Matrix channel started") + return nil +} + +func (c *MatrixChannel) Stop(ctx context.Context) error { + logger.InfoC("matrix", "Stopping Matrix channel") + c.SetRunning(false) + + if c.cancel != nil { + c.cancel() + } + c.stopTypingSessions(ctx) + + logger.InfoC("matrix", "Matrix channel stopped") + return nil +} + +func (c *MatrixChannel) Send(ctx context.Context, msg bus.OutboundMessage) error { + if !c.IsRunning() { + return channels.ErrNotRunning + } + + roomID := id.RoomID(strings.TrimSpace(msg.ChatID)) + if roomID == "" { + return fmt.Errorf("matrix room ID is empty: %w", channels.ErrSendFailed) + } + + content := strings.TrimSpace(msg.Content) + if content == "" { + return nil + } + + _, err := c.client.SendMessageEvent(ctx, roomID, event.EventMessage, &event.MessageEventContent{ + MsgType: event.MsgText, + Body: content, + }) + if err != nil { + return fmt.Errorf("matrix send: %w", channels.ErrTemporary) + } + return nil +} + +// SendMedia implements channels.MediaSender. +func (c *MatrixChannel) SendMedia(ctx context.Context, msg bus.OutboundMediaMessage) error { + if !c.IsRunning() { + return channels.ErrNotRunning + } + sendCtx := ctx + if sendCtx == nil { + sendCtx = context.Background() + } + + roomID := id.RoomID(strings.TrimSpace(msg.ChatID)) + if roomID == "" { + return fmt.Errorf("matrix room ID is empty: %w", channels.ErrSendFailed) + } + + store := c.GetMediaStore() + if store == nil { + return fmt.Errorf("no media store available: %w", channels.ErrSendFailed) + } + + for _, part := range msg.Parts { + if err := sendCtx.Err(); err != nil { + return err + } + + localPath, meta, err := store.ResolveWithMeta(part.Ref) + if err != nil { + logger.ErrorCF("matrix", "Failed to resolve media ref", map[string]any{ + "ref": part.Ref, + "error": err.Error(), + }) + continue + } + + fileInfo, err := os.Stat(localPath) + if err != nil { + logger.ErrorCF("matrix", "Failed to stat media file", map[string]any{ + "path": localPath, + "error": err.Error(), + }) + continue + } + + file, err := os.Open(localPath) + if err != nil { + logger.ErrorCF("matrix", "Failed to open media file", map[string]any{ + "path": localPath, + "error": err.Error(), + }) + continue + } + + filename := strings.TrimSpace(part.Filename) + if filename == "" { + filename = strings.TrimSpace(meta.Filename) + } + if filename == "" { + filename = filepath.Base(localPath) + } + if filename == "" { + filename = "file" + } + + contentType := strings.TrimSpace(part.ContentType) + if contentType == "" { + contentType = strings.TrimSpace(meta.ContentType) + } + if contentType == "" { + contentType = mime.TypeByExtension(strings.ToLower(filepath.Ext(filename))) + } + if contentType == "" { + contentType = "application/octet-stream" + } + + uploadResp, err := c.client.UploadMedia(sendCtx, mautrix.ReqUploadMedia{ + Content: file, + ContentLength: fileInfo.Size(), + ContentType: contentType, + FileName: filename, + }) + file.Close() + if err != nil { + logger.ErrorCF("matrix", "Failed to upload media", map[string]any{ + "path": localPath, + "type": part.Type, + "error": err.Error(), + }) + return fmt.Errorf("matrix upload media: %w", channels.ErrTemporary) + } + + msgType := matrixOutboundMsgType(part.Type, filename, contentType) + content := matrixOutboundContent( + part.Caption, + filename, + msgType, + contentType, + fileInfo.Size(), + uploadResp.ContentURI.CUString(), + ) + + if _, err := c.client.SendMessageEvent(sendCtx, roomID, event.EventMessage, content); err != nil { + logger.ErrorCF("matrix", "Failed to send media message", map[string]any{ + "room_id": roomID.String(), + "type": msgType, + "error": err.Error(), + }) + return fmt.Errorf("matrix send media: %w", channels.ErrTemporary) + } + } + + return nil +} + +// StartTyping implements channels.TypingCapable. +func (c *MatrixChannel) StartTyping(ctx context.Context, chatID string) (func(), error) { + if !c.IsRunning() { + return func() {}, nil + } + + roomID := id.RoomID(strings.TrimSpace(chatID)) + if roomID == "" { + return func() {}, fmt.Errorf("matrix room ID is empty") + } + + session := newTypingSession() + + c.typingMu.Lock() + if prev := c.typingSessions[chatID]; prev != nil { + prev.stop() + } + c.typingSessions[chatID] = session + c.typingMu.Unlock() + + parent := c.baseContext() + go c.typingLoop(parent, roomID, session) + + var once sync.Once + stop := func() { + once.Do(func() { + session.stop() + c.typingMu.Lock() + if current := c.typingSessions[chatID]; current == session { + delete(c.typingSessions, chatID) + } + c.typingMu.Unlock() + _, _ = c.client.UserTyping(context.Background(), roomID, false, 0) + }) + } + + return stop, nil +} + +// SendPlaceholder implements channels.PlaceholderCapable. +func (c *MatrixChannel) SendPlaceholder(ctx context.Context, chatID string) (string, error) { + if !c.config.Placeholder.Enabled { + return "", nil + } + + roomID := id.RoomID(strings.TrimSpace(chatID)) + if roomID == "" { + return "", fmt.Errorf("matrix room ID is empty") + } + + text := strings.TrimSpace(c.config.Placeholder.Text) + if text == "" { + text = "Thinking... 💭" + } + + resp, err := c.client.SendMessageEvent(ctx, roomID, event.EventMessage, &event.MessageEventContent{ + MsgType: event.MsgNotice, + Body: text, + }) + if err != nil { + return "", err + } + + return resp.EventID.String(), nil +} + +// EditMessage implements channels.MessageEditor. +func (c *MatrixChannel) EditMessage(ctx context.Context, chatID string, messageID string, content string) error { + roomID := id.RoomID(strings.TrimSpace(chatID)) + if roomID == "" { + return fmt.Errorf("matrix room ID is empty") + } + if strings.TrimSpace(messageID) == "" { + return fmt.Errorf("matrix message ID is empty") + } + + editContent := &event.MessageEventContent{ + MsgType: event.MsgText, + Body: content, + } + editContent.SetEdit(id.EventID(messageID)) + + _, err := c.client.SendMessageEvent(ctx, roomID, event.EventMessage, editContent) + return err +} + +func (c *MatrixChannel) handleMemberEvent(ctx context.Context, evt *event.Event) { + if !c.config.JoinOnInvite { + return + } + if evt == nil { + return + } + + member := evt.Content.AsMember() + if member.Membership != event.MembershipInvite { + return + } + if evt.GetStateKey() != c.client.UserID.String() { + return + } + + _, err := c.client.JoinRoomByID(c.baseContext(), evt.RoomID) + if err != nil { + logger.WarnCF("matrix", "Failed to auto-join invited room", map[string]any{ + "room_id": evt.RoomID.String(), + "error": err.Error(), + }) + return + } + + logger.InfoCF("matrix", "Joined room after invite", map[string]any{ + "room_id": evt.RoomID.String(), + }) +} + +func (c *MatrixChannel) handleMessageEvent(ctx context.Context, evt *event.Event) { + if evt == nil { + return + } + + // Ignore our own messages. + if evt.Sender == c.client.UserID { + return + } + + // Ignore historical events on first sync. + if time.UnixMilli(evt.Timestamp).Before(c.startTime) { + return + } + + msgEvt := evt.Content.AsMessage() + if msgEvt == nil { + return + } + + // Ignore edits. + if msgEvt.RelatesTo != nil && msgEvt.RelatesTo.GetReplaceID() != "" { + return + } + + roomID := evt.RoomID.String() + scope := channels.BuildMediaScope("matrix", roomID, evt.ID.String()) + + content, mediaPaths, ok := c.extractInboundContent(ctx, msgEvt, scope) + if !ok { + return + } + content = strings.TrimSpace(content) + if content == "" && len(mediaPaths) == 0 { + return + } + + senderID := evt.Sender.String() + sender := bus.SenderInfo{ + Platform: "matrix", + PlatformID: senderID, + CanonicalID: identity.BuildCanonicalID("matrix", senderID), + Username: senderID, + DisplayName: senderID, + } + + if !c.IsAllowedSender(sender) { + logger.DebugCF("matrix", "Message rejected by allowlist", map[string]any{ + "sender_id": senderID, + }) + return + } + + isGroup := c.isGroupRoom(ctx, evt.RoomID) + if isGroup { + isMentioned := c.isBotMentioned(msgEvt) + if isMentioned { + content = stripUserMention(content, c.client.UserID) + } + respond, cleaned := c.ShouldRespondInGroup(isMentioned, content) + if !respond { + return + } + content = cleaned + } else { + content = stripUserMention(content, c.client.UserID) + } + + content = strings.TrimSpace(content) + if content == "" { + return + } + + peerKind := "direct" + peerID := senderID + if isGroup { + peerKind = "group" + peerID = roomID + } + + metadata := map[string]string{ + "room_id": roomID, + "timestamp": fmt.Sprintf("%d", evt.Timestamp), + "is_group": fmt.Sprintf("%t", isGroup), + "sender_raw": senderID, + } + if replyTo := msgEvt.GetRelatesTo().GetReplyTo(); replyTo != "" { + metadata["reply_to_msg_id"] = replyTo.String() + } + + c.HandleMessage( + c.baseContext(), + bus.Peer{Kind: peerKind, ID: peerID}, + evt.ID.String(), + senderID, + roomID, + content, + mediaPaths, + metadata, + sender, + ) +} + +func (c *MatrixChannel) extractInboundContent( + ctx context.Context, + msgEvt *event.MessageEventContent, + scope string, +) (string, []string, bool) { + switch msgEvt.MsgType { + case event.MsgText, event.MsgNotice: + return msgEvt.Body, nil, true + case event.MsgImage, event.MsgAudio, event.MsgVideo, event.MsgFile: + return c.extractInboundMedia(ctx, msgEvt, scope) + default: + logger.DebugCF("matrix", "Ignoring unsupported matrix msgtype", map[string]any{ + "msgtype": msgEvt.MsgType, + }) + return "", nil, false + } +} + +func (c *MatrixChannel) extractInboundMedia( + ctx context.Context, + msgEvt *event.MessageEventContent, + scope string, +) (string, []string, bool) { + mediaKind := matrixMediaKind(msgEvt.MsgType) + label := matrixMediaLabel(msgEvt, mediaKind) + content := fmt.Sprintf("[%s: %s]", mediaKind, label) + if caption := strings.TrimSpace(msgEvt.GetCaption()); caption != "" { + content = caption + "\n" + content + } + + localPath, err := c.downloadMedia(ctx, msgEvt, mediaKind) + if err != nil { + logger.WarnCF("matrix", "Failed to download media; forwarding as text-only marker", map[string]any{ + "msgtype": msgEvt.MsgType, + "error": err.Error(), + }) + return content, nil, true + } + + filename := matrixMediaFilename(label, mediaKind, matrixContentType(msgEvt)) + ref := c.storeMedia(localPath, media.MediaMeta{ + Filename: filename, + ContentType: matrixContentType(msgEvt), + Source: "matrix", + }, scope) + return content, []string{ref}, true +} + +func (c *MatrixChannel) storeMedia(localPath string, meta media.MediaMeta, scope string) string { + if store := c.GetMediaStore(); store != nil { + ref, err := store.Store(localPath, meta, scope) + if err == nil { + return ref + } + logger.WarnCF("matrix", "Failed to store media in MediaStore, falling back to local path", map[string]any{ + "path": localPath, + "error": err.Error(), + }) + } + return localPath +} + +func (c *MatrixChannel) downloadMedia( + ctx context.Context, + msgEvt *event.MessageEventContent, + mediaKind string, +) (string, error) { + uri := matrixMediaURI(msgEvt) + if uri == "" { + return "", fmt.Errorf("empty matrix media URL") + } + parsed := uri.ParseOrIgnore() + if parsed.IsEmpty() { + return "", fmt.Errorf("invalid matrix media URL: %s", uri) + } + + dlCtx := c.baseContext() + if ctx != nil { + dlCtx = ctx + } + reqCtx, cancel := context.WithTimeout(dlCtx, 20*time.Second) + defer cancel() + + data, err := c.client.DownloadBytes(reqCtx, parsed) + if err != nil { + return "", err + } + + // Encrypted attachments put URL in msgEvt.File and require client-side decryption. + if msgEvt != nil && msgEvt.File != nil && msgEvt.URL == "" { + if err := msgEvt.File.DecryptInPlace(data); err != nil { + return "", fmt.Errorf("decrypt matrix media: %w", err) + } + } + + label := matrixMediaLabel(msgEvt, mediaKind) + ext := matrixMediaExt(label, matrixContentType(msgEvt), mediaKind) + tmp, err := os.CreateTemp("", "matrix-media-*"+ext) + if err != nil { + return "", err + } + defer tmp.Close() + + if _, err = tmp.Write(data); err != nil { + _ = os.Remove(tmp.Name()) + return "", err + } + + return tmp.Name(), nil +} + +func matrixContentType(msgEvt *event.MessageEventContent) string { + if msgEvt != nil && msgEvt.Info != nil { + return strings.TrimSpace(msgEvt.Info.MimeType) + } + return "" +} + +func matrixMediaURI(msgEvt *event.MessageEventContent) id.ContentURIString { + if msgEvt == nil { + return "" + } + if msgEvt.URL != "" { + return msgEvt.URL + } + if msgEvt.File != nil { + return msgEvt.File.URL + } + return "" +} + +func matrixMediaKind(msgType event.MessageType) string { + switch msgType { + case event.MsgAudio: + return "audio" + case event.MsgVideo: + return "video" + case event.MsgFile: + return "file" + default: + return "image" + } +} + +func matrixOutboundMsgType(partType, filename, contentType string) event.MessageType { + switch strings.ToLower(strings.TrimSpace(partType)) { + case "image": + return event.MsgImage + case "audio", "voice": + return event.MsgAudio + case "video": + return event.MsgVideo + case "file", "document": + return event.MsgFile + } + + ct := strings.ToLower(strings.TrimSpace(contentType)) + switch { + case strings.HasPrefix(ct, "image/"): + return event.MsgImage + case strings.HasPrefix(ct, "audio/"), ct == "application/ogg", ct == "application/x-ogg": + return event.MsgAudio + case strings.HasPrefix(ct, "video/"): + return event.MsgVideo + } + + switch strings.ToLower(strings.TrimSpace(filepath.Ext(filename))) { + case ".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp", ".svg": + return event.MsgImage + case ".mp3", ".wav", ".ogg", ".m4a", ".flac", ".aac", ".wma", ".opus": + return event.MsgAudio + case ".mp4", ".avi", ".mov", ".webm", ".mkv": + return event.MsgVideo + default: + return event.MsgFile + } +} + +func matrixOutboundContent( + caption, filename string, + msgType event.MessageType, + contentType string, + size int64, + uri id.ContentURIString, +) *event.MessageEventContent { + body := strings.TrimSpace(caption) + if body == "" { + body = filename + } + if body == "" { + body = matrixMediaKind(msgType) + } + + info := &event.FileInfo{MimeType: strings.TrimSpace(contentType)} + if size > 0 && size <= int64(int(^uint(0)>>1)) { + info.Size = int(size) + } + + content := &event.MessageEventContent{ + MsgType: msgType, + Body: body, + URL: uri, + FileName: filename, + Info: info, + } + return content +} + +func matrixMediaLabel(msgEvt *event.MessageEventContent, fallback string) string { + if msgEvt == nil { + return fallback + } + if v := strings.TrimSpace(msgEvt.FileName); v != "" { + return v + } + if v := strings.TrimSpace(msgEvt.Body); v != "" { + return v + } + return fallback +} + +func matrixMediaFilename(label, mediaKind, contentType string) string { + filename := strings.TrimSpace(label) + if filename == "" { + filename = mediaKind + } + if filepath.Ext(filename) == "" { + filename += matrixMediaExt("", contentType, mediaKind) + } + return filename +} + +func matrixMediaExt(filename, contentType, mediaKind string) string { + if ext := strings.TrimSpace(filepath.Ext(filename)); ext != "" { + return ext + } + if contentType != "" { + if exts, err := mime.ExtensionsByType(contentType); err == nil && len(exts) > 0 { + return exts[0] + } + } + switch mediaKind { + case "audio": + return ".ogg" + case "video": + return ".mp4" + case "file": + return ".bin" + default: + return ".jpg" + } +} + +func (c *MatrixChannel) isGroupRoom(ctx context.Context, roomID id.RoomID) bool { + now := time.Now() + if cached, ok := c.roomKindCache.Load(roomID.String()); ok { + entry := cached.(roomKindCacheEntry) + if now.Before(entry.expiresAt) { + return entry.isGroup + } + } + + qctx := c.baseContext() + if ctx != nil { + qctx = ctx + } + reqCtx, cancel := context.WithTimeout(qctx, 5*time.Second) + defer cancel() + + resp, err := c.client.JoinedMembers(reqCtx, roomID) + if err != nil { + logger.DebugCF("matrix", "Failed to query room members; assume direct", map[string]any{ + "room_id": roomID.String(), + "error": err.Error(), + }) + return false + } + + isGroup := len(resp.Joined) > 2 + c.roomKindCache.Store(roomID.String(), roomKindCacheEntry{ + isGroup: isGroup, + expiresAt: now.Add(roomKindCacheTTL), + }) + return isGroup +} + +func (c *MatrixChannel) isBotMentioned(msgEvt *event.MessageEventContent) bool { + if msgEvt == nil { + return false + } + + if msgEvt.Mentions != nil && msgEvt.Mentions.Has(c.client.UserID) { + return true + } + + userID := c.client.UserID.String() + if userID != "" && (strings.Contains(msgEvt.Body, userID) || strings.Contains(msgEvt.FormattedBody, userID)) { + return true + } + + localpart := matrixLocalpart(c.client.UserID) + if localpart == "" { + return false + } + + re := localpartMentionRegexp(localpart) + return re.MatchString(msgEvt.Body) || re.MatchString(msgEvt.FormattedBody) +} + +func (c *MatrixChannel) typingLoop(ctx context.Context, roomID id.RoomID, session *typingSession) { + sendTyping := func() { + _, err := c.client.UserTyping(ctx, roomID, true, typingServerTTL) + if err != nil { + logger.DebugCF("matrix", "Failed to send typing status", map[string]any{ + "room_id": roomID.String(), + "error": err.Error(), + }) + } + } + + sendTyping() + ticker := time.NewTicker(typingRefreshInterval) + defer ticker.Stop() + + for { + select { + case <-ctx.Done(): + return + case <-session.stopCh: + return + case <-ticker.C: + sendTyping() + } + } +} + +func (c *MatrixChannel) stopTypingSessions(ctx context.Context) { + c.typingMu.Lock() + sessions := c.typingSessions + c.typingSessions = make(map[string]*typingSession) + c.typingMu.Unlock() + + stopCtx := ctx + if stopCtx == nil { + stopCtx = context.Background() + } + for roomID, session := range sessions { + session.stop() + _, _ = c.client.UserTyping(stopCtx, id.RoomID(roomID), false, 0) + } +} + +func (c *MatrixChannel) baseContext() context.Context { + if c.ctx != nil { + return c.ctx + } + return context.Background() +} + +func matrixLocalpart(userID id.UserID) string { + s := strings.TrimPrefix(userID.String(), "@") + localpart, _, _ := strings.Cut(s, ":") + return strings.TrimSpace(localpart) +} + +func localpartMentionRegexp(localpart string) *regexp.Regexp { + pattern := `(?i)(^|[^[:alnum:]_])@` + regexp.QuoteMeta(localpart) + `(?::[A-Za-z0-9._:-]+)?([^[:alnum:]_]|$)` + return regexp.MustCompile(pattern) +} + +func stripUserMention(text string, userID id.UserID) string { + cleaned := strings.ReplaceAll(text, userID.String(), "") + + localpart := matrixLocalpart(userID) + if localpart != "" { + re := localpartMentionRegexp(localpart) + cleaned = re.ReplaceAllString(cleaned, "$1$2") + } + + cleaned = strings.TrimSpace(cleaned) + cleaned = strings.TrimLeft(cleaned, ",:; ") + return strings.TrimSpace(cleaned) +} diff --git a/pkg/channels/matrix/matrix_test.go b/pkg/channels/matrix/matrix_test.go new file mode 100644 index 000000000..68e4bcb70 --- /dev/null +++ b/pkg/channels/matrix/matrix_test.go @@ -0,0 +1,218 @@ +package matrix + +import ( + "context" + "testing" + + "maunium.net/go/mautrix" + "maunium.net/go/mautrix/event" + "maunium.net/go/mautrix/id" +) + +func TestMatrixLocalpartMentionRegexp(t *testing.T) { + re := localpartMentionRegexp("picoclaw") + + cases := []struct { + text string + want bool + }{ + {text: "@picoclaw hello", want: true}, + {text: "hi @picoclaw:matrix.org", want: true}, + {text: "欢迎一下picoclaw小龙虾", want: false}, // historical false-positive case in PR #356 + {text: "mail test@example.com", want: false}, + } + + for _, tc := range cases { + if got := re.MatchString(tc.text); got != tc.want { + t.Fatalf("text=%q match=%v want=%v", tc.text, got, tc.want) + } + } +} + +func TestStripUserMention(t *testing.T) { + userID := id.UserID("@picoclaw:matrix.org") + + cases := []struct { + in string + want string + }{ + {in: "@picoclaw:matrix.org hello", want: "hello"}, + {in: "@picoclaw, hello", want: "hello"}, + {in: "no mention here", want: "no mention here"}, + } + + for _, tc := range cases { + if got := stripUserMention(tc.in, userID); got != tc.want { + t.Fatalf("stripUserMention(%q)=%q want=%q", tc.in, got, tc.want) + } + } +} + +func TestIsBotMentioned(t *testing.T) { + ch := &MatrixChannel{ + client: &mautrix.Client{ + UserID: id.UserID("@picoclaw:matrix.org"), + }, + } + + cases := []struct { + name string + msg event.MessageEventContent + want bool + }{ + { + name: "mentions field", + msg: event.MessageEventContent{ + Body: "hello", + Mentions: &event.Mentions{ + UserIDs: []id.UserID{id.UserID("@picoclaw:matrix.org")}, + }, + }, + want: true, + }, + { + name: "full user id in body", + msg: event.MessageEventContent{ + Body: "@picoclaw:matrix.org hello", + }, + want: true, + }, + { + name: "localpart with at sign", + msg: event.MessageEventContent{ + Body: "@picoclaw hello", + }, + want: true, + }, + { + name: "localpart without at sign should not match", + msg: event.MessageEventContent{ + Body: "欢迎一下picoclaw小龙虾", + }, + want: false, + }, + } + + for _, tc := range cases { + if got := ch.isBotMentioned(&tc.msg); got != tc.want { + t.Fatalf("%s: got=%v want=%v", tc.name, got, tc.want) + } + } +} + +func TestMatrixMediaExt(t *testing.T) { + if got := matrixMediaExt("photo.png", "", "image"); got != ".png" { + t.Fatalf("filename extension mismatch: got=%q", got) + } + if got := matrixMediaExt("", "image/webp", "image"); got != ".webp" { + t.Fatalf("content-type extension mismatch: got=%q", got) + } + if got := matrixMediaExt("", "", "image"); got != ".jpg" { + t.Fatalf("default image extension mismatch: got=%q", got) + } + if got := matrixMediaExt("", "", "audio"); got != ".ogg" { + t.Fatalf("default audio extension mismatch: got=%q", got) + } + if got := matrixMediaExt("", "", "video"); got != ".mp4" { + t.Fatalf("default video extension mismatch: got=%q", got) + } + if got := matrixMediaExt("", "", "file"); got != ".bin" { + t.Fatalf("default file extension mismatch: got=%q", got) + } +} + +func TestExtractInboundContent_ImageNoURLFallback(t *testing.T) { + ch := &MatrixChannel{} + msg := &event.MessageEventContent{ + MsgType: event.MsgImage, + Body: "test.png", + } + + content, mediaRefs, ok := ch.extractInboundContent(context.Background(), msg, "matrix:room:event") + if !ok { + t.Fatal("expected ok for image fallback") + } + if content != "[image: test.png]" { + t.Fatalf("unexpected content: %q", content) + } + if len(mediaRefs) != 0 { + t.Fatalf("expected no media refs, got %d", len(mediaRefs)) + } +} + +func TestExtractInboundContent_AudioNoURLFallback(t *testing.T) { + ch := &MatrixChannel{} + msg := &event.MessageEventContent{ + MsgType: event.MsgAudio, + FileName: "voice.ogg", + Body: "please transcribe", + } + + content, mediaRefs, ok := ch.extractInboundContent(context.Background(), msg, "matrix:room:event") + if !ok { + t.Fatal("expected ok for audio fallback") + } + if content != "please transcribe\n[audio: voice.ogg]" { + t.Fatalf("unexpected content: %q", content) + } + if len(mediaRefs) != 0 { + t.Fatalf("expected no media refs, got %d", len(mediaRefs)) + } +} + +func TestMatrixOutboundMsgType(t *testing.T) { + cases := []struct { + name string + partType string + filename string + contentType string + want event.MessageType + }{ + {name: "explicit image", partType: "image", want: event.MsgImage}, + {name: "explicit audio", partType: "audio", want: event.MsgAudio}, + {name: "mime fallback video", contentType: "video/mp4", want: event.MsgVideo}, + {name: "extension fallback audio", filename: "voice.ogg", want: event.MsgAudio}, + {name: "unknown defaults file", filename: "report.txt", want: event.MsgFile}, + } + + for _, tc := range cases { + if got := matrixOutboundMsgType(tc.partType, tc.filename, tc.contentType); got != tc.want { + t.Fatalf("%s: got=%q want=%q", tc.name, got, tc.want) + } + } +} + +func TestMatrixOutboundContent(t *testing.T) { + content := matrixOutboundContent( + "please review", + "voice.ogg", + event.MsgAudio, + "audio/ogg", + 1234, + id.ContentURIString("mxc://matrix.org/abc"), + ) + if content.Body != "please review" { + t.Fatalf("unexpected body: %q", content.Body) + } + if content.FileName != "voice.ogg" { + t.Fatalf("unexpected filename: %q", content.FileName) + } + if content.Info == nil || content.Info.MimeType != "audio/ogg" { + t.Fatalf("unexpected content type: %+v", content.Info) + } + if content.Info == nil || content.Info.Size != 1234 { + t.Fatalf("unexpected size: %+v", content.Info) + } + + noCaption := matrixOutboundContent( + "", + "image.png", + event.MsgImage, + "image/png", + 0, + id.ContentURIString("mxc://matrix.org/def"), + ) + if noCaption.Body != "image.png" { + t.Fatalf("unexpected fallback body: %q", noCaption.Body) + } +} diff --git a/pkg/config/config.go b/pkg/config/config.go index 5c53c08ad..a55ad5eb8 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -225,6 +225,7 @@ type ChannelsConfig struct { QQ QQConfig `json:"qq"` DingTalk DingTalkConfig `json:"dingtalk"` Slack SlackConfig `json:"slack"` + Matrix MatrixConfig `json:"matrix"` LINE LINEConfig `json:"line"` OneBot OneBotConfig `json:"onebot"` WeCom WeComConfig `json:"wecom"` @@ -333,6 +334,19 @@ type SlackConfig struct { ReasoningChannelID string `json:"reasoning_channel_id" env:"PICOCLAW_CHANNELS_SLACK_REASONING_CHANNEL_ID"` } +type MatrixConfig struct { + Enabled bool `json:"enabled" env:"PICOCLAW_CHANNELS_MATRIX_ENABLED"` + Homeserver string `json:"homeserver" env:"PICOCLAW_CHANNELS_MATRIX_HOMESERVER"` + UserID string `json:"user_id" env:"PICOCLAW_CHANNELS_MATRIX_USER_ID"` + AccessToken string `json:"access_token" env:"PICOCLAW_CHANNELS_MATRIX_ACCESS_TOKEN"` + DeviceID string `json:"device_id,omitempty" env:"PICOCLAW_CHANNELS_MATRIX_DEVICE_ID"` + JoinOnInvite bool `json:"join_on_invite" env:"PICOCLAW_CHANNELS_MATRIX_JOIN_ON_INVITE"` + AllowFrom FlexibleStringSlice `json:"allow_from" env:"PICOCLAW_CHANNELS_MATRIX_ALLOW_FROM"` + GroupTrigger GroupTriggerConfig `json:"group_trigger,omitempty"` + Placeholder PlaceholderConfig `json:"placeholder,omitempty"` + ReasoningChannelID string `json:"reasoning_channel_id" env:"PICOCLAW_CHANNELS_MATRIX_REASONING_CHANNEL_ID"` +} + type LINEConfig struct { Enabled bool `json:"enabled" env:"PICOCLAW_CHANNELS_LINE_ENABLED"` ChannelSecret string `json:"channel_secret" env:"PICOCLAW_CHANNELS_LINE_CHANNEL_SECRET"` diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 10ebc7c90..47f79c6f0 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -283,6 +283,9 @@ func TestDefaultConfig_Channels(t *testing.T) { if cfg.Channels.Slack.Enabled { t.Error("Slack should be disabled by default") } + if cfg.Channels.Matrix.Enabled { + t.Error("Matrix should be disabled by default") + } } // TestDefaultConfig_WebTools verifies web tools config diff --git a/pkg/config/defaults.go b/pkg/config/defaults.go index 1902480c5..da0cb9833 100644 --- a/pkg/config/defaults.go +++ b/pkg/config/defaults.go @@ -97,6 +97,22 @@ func DefaultConfig() *Config { AppToken: "", AllowFrom: FlexibleStringSlice{}, }, + Matrix: MatrixConfig{ + Enabled: false, + Homeserver: "https://matrix.org", + UserID: "", + AccessToken: "", + DeviceID: "", + JoinOnInvite: true, + AllowFrom: FlexibleStringSlice{}, + GroupTrigger: GroupTriggerConfig{ + MentionOnly: true, + }, + Placeholder: PlaceholderConfig{ + Enabled: true, + Text: "Thinking... 💭", + }, + }, LINE: LINEConfig{ Enabled: false, ChannelSecret: "", diff --git a/pkg/migrate/sources/openclaw/common.go b/pkg/migrate/sources/openclaw/common.go index dddd98089..d57dbe34f 100644 --- a/pkg/migrate/sources/openclaw/common.go +++ b/pkg/migrate/sources/openclaw/common.go @@ -22,6 +22,7 @@ var supportedChannels = map[string]bool{ "qq": true, "dingtalk": true, "slack": true, + "matrix": true, "line": true, "onebot": true, "wecom": true, diff --git a/pkg/migrate/sources/openclaw/openclaw_config.go b/pkg/migrate/sources/openclaw/openclaw_config.go index 39ad48fad..19d63bb77 100644 --- a/pkg/migrate/sources/openclaw/openclaw_config.go +++ b/pkg/migrate/sources/openclaw/openclaw_config.go @@ -371,6 +371,8 @@ func (c *OpenClawConfig) IsChannelEnabled(name string) bool { return c.Channels.Discord == nil || c.Channels.Discord.Enabled == nil || *c.Channels.Discord.Enabled case "slack": return c.Channels.Slack == nil || c.Channels.Slack.Enabled == nil || *c.Channels.Slack.Enabled + case "matrix": + return c.Channels.Matrix == nil || c.Channels.Matrix.Enabled == nil || *c.Channels.Matrix.Enabled case "whatsapp": return c.Channels.WhatsApp == nil || c.Channels.WhatsApp.Enabled == nil || *c.Channels.WhatsApp.Enabled case "feishu": @@ -397,6 +399,11 @@ func GetChannelAllowFrom(ch any) []string { return nil } return c.AllowFrom + case *OpenClawMatrixConfig: + if c == nil { + return nil + } + return c.AllowFrom case *OpenClawWhatsAppConfig: if c == nil { return nil @@ -627,6 +634,7 @@ type ChannelsConfig struct { QQ QQConfig `json:"qq"` DingTalk DingTalkConfig `json:"dingtalk"` Slack SlackConfig `json:"slack"` + Matrix MatrixConfig `json:"matrix"` LINE LINEConfig `json:"line"` } @@ -687,6 +695,14 @@ type SlackConfig struct { AllowFrom []string `json:"allow_from"` } +type MatrixConfig struct { + Enabled bool `json:"enabled"` + Homeserver string `json:"homeserver"` + UserID string `json:"user_id"` + AccessToken string `json:"access_token"` + AllowFrom []string `json:"allow_from"` +} + type LINEConfig struct { Enabled bool `json:"enabled"` ChannelSecret string `json:"channel_secret"` @@ -862,12 +878,26 @@ func (c *OpenClawConfig) convertChannels(warnings *[]string) ChannelsConfig { } } + if c.Channels.Matrix != nil && supportedChannels["matrix"] { + enabled := c.Channels.Matrix.Enabled == nil || *c.Channels.Matrix.Enabled + channels.Matrix = MatrixConfig{ + Enabled: enabled, + AllowFrom: c.Channels.Matrix.AllowFrom, + } + if c.Channels.Matrix.Homeserver != nil { + channels.Matrix.Homeserver = *c.Channels.Matrix.Homeserver + } + if c.Channels.Matrix.UserID != nil { + channels.Matrix.UserID = *c.Channels.Matrix.UserID + } + if c.Channels.Matrix.AccessToken != nil { + channels.Matrix.AccessToken = *c.Channels.Matrix.AccessToken + } + } + if c.Channels.Signal != nil { *warnings = append(*warnings, "Channel 'signal': No PicoClaw adapter available") } - if c.Channels.Matrix != nil { - *warnings = append(*warnings, "Channel 'matrix': No PicoClaw adapter available") - } if c.Channels.IRC != nil { *warnings = append(*warnings, "Channel 'irc': No PicoClaw adapter available") } @@ -1020,6 +1050,14 @@ func (c ChannelsConfig) ToStandardChannels() config.ChannelsConfig { BotToken: c.Slack.BotToken, AppToken: c.Slack.AppToken, }, + Matrix: config.MatrixConfig{ + Enabled: c.Matrix.Enabled, + Homeserver: c.Matrix.Homeserver, + UserID: c.Matrix.UserID, + AccessToken: c.Matrix.AccessToken, + AllowFrom: c.Matrix.AllowFrom, + JoinOnInvite: true, + }, LINE: config.LINEConfig{ Enabled: c.LINE.Enabled, ChannelSecret: c.LINE.ChannelSecret, diff --git a/pkg/migrate/sources/openclaw/openclaw_config_test.go b/pkg/migrate/sources/openclaw/openclaw_config_test.go index 7d884522c..3a7d0c686 100644 --- a/pkg/migrate/sources/openclaw/openclaw_config_test.go +++ b/pkg/migrate/sources/openclaw/openclaw_config_test.go @@ -4,6 +4,7 @@ import ( "encoding/json" "os" "path/filepath" + "strings" "testing" ) @@ -375,6 +376,96 @@ func TestConvertToPicoClawWithQQAndDingTalk(t *testing.T) { } } +func TestConvertToPicoClawWithMatrix(t *testing.T) { + tmpDir := t.TempDir() + configPath := filepath.Join(tmpDir, "openclaw.json") + + testConfig := `{ + "channels": { + "matrix": { + "enabled": true, + "homeserver": "https://matrix.example.com", + "userId": "@bot:matrix.example.com", + "accessToken": "syt_test_token", + "allowFrom": ["@alice:matrix.example.com"] + } + } + }` + + err := os.WriteFile(configPath, []byte(testConfig), 0o644) + if err != nil { + t.Fatalf("failed to write test config: %v", err) + } + + cfg, err := LoadOpenClawConfig(configPath) + if err != nil { + t.Fatalf("failed to load config: %v", err) + } + + picoCfg, warnings, err := cfg.ConvertToPicoClaw("") + if err != nil { + t.Fatalf("failed to convert config: %v", err) + } + + if !picoCfg.Channels.Matrix.Enabled { + t.Error("matrix should be enabled") + } + if picoCfg.Channels.Matrix.Homeserver != "https://matrix.example.com" { + t.Errorf("expected matrix homeserver, got %q", picoCfg.Channels.Matrix.Homeserver) + } + if picoCfg.Channels.Matrix.UserID != "@bot:matrix.example.com" { + t.Errorf("expected matrix user_id, got %q", picoCfg.Channels.Matrix.UserID) + } + if picoCfg.Channels.Matrix.AccessToken != "syt_test_token" { + t.Errorf("expected matrix access_token, got %q", picoCfg.Channels.Matrix.AccessToken) + } + if len(picoCfg.Channels.Matrix.AllowFrom) != 1 || + picoCfg.Channels.Matrix.AllowFrom[0] != "@alice:matrix.example.com" { + t.Errorf("unexpected matrix allow_from: %#v", picoCfg.Channels.Matrix.AllowFrom) + } + + for _, w := range warnings { + if strings.Contains(w, "Channel 'matrix'") { + t.Fatalf("matrix should no longer be reported as unsupported, warning=%q", w) + } + } +} + +func TestConvertToPicoClawWithMatrixDisabled(t *testing.T) { + tmpDir := t.TempDir() + configPath := filepath.Join(tmpDir, "openclaw.json") + + testConfig := `{ + "channels": { + "matrix": { + "enabled": false, + "homeserver": "https://matrix.example.com", + "userId": "@bot:matrix.example.com", + "accessToken": "syt_test_token" + } + } + }` + + err := os.WriteFile(configPath, []byte(testConfig), 0o644) + if err != nil { + t.Fatalf("failed to write test config: %v", err) + } + + cfg, err := LoadOpenClawConfig(configPath) + if err != nil { + t.Fatalf("failed to load config: %v", err) + } + + picoCfg, _, err := cfg.ConvertToPicoClaw("") + if err != nil { + t.Fatalf("failed to convert config: %v", err) + } + + if picoCfg.Channels.Matrix.Enabled { + t.Error("matrix should respect enabled=false from source config") + } +} + func TestOpenClawAgentModel(t *testing.T) { model := &OpenClawAgentModel{ Primary: strPtr("anthropic/claude-3-opus"), @@ -425,6 +516,9 @@ func TestChannelEnabled(t *testing.T) { if !cfg.IsChannelEnabled("slack") { t.Error("slack should be enabled (explicitly set)") } + if !cfg.IsChannelEnabled("matrix") { + t.Error("matrix should be enabled (nil config defaults to enabled)") + } if cfg.IsChannelEnabled("line") { t.Error("line should return false (not in switch cases)") } From 64b99b34bb212195c13acd7280533b93068a2c25 Mon Sep 17 00:00:00 2001 From: horsley Date: Sat, 7 Mar 2026 18:05:09 +0000 Subject: [PATCH 2/5] fix(matrix): improve group mention detection --- pkg/channels/matrix/matrix.go | 72 +++++++++++++++++++++++++++++- pkg/channels/matrix/matrix_test.go | 16 +++++++ 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/pkg/channels/matrix/matrix.go b/pkg/channels/matrix/matrix.go index 0d4c62ac5..87dd37aa8 100644 --- a/pkg/channels/matrix/matrix.go +++ b/pkg/channels/matrix/matrix.go @@ -3,7 +3,9 @@ package matrix import ( "context" "fmt" + "html" "mime" + "net/url" "os" "path/filepath" "regexp" @@ -29,6 +31,8 @@ const ( roomKindCacheTTL = 5 * time.Minute ) +var matrixMentionHrefRegexp = regexp.MustCompile(`(?i)]+href=["']([^"']+)["']`) + type roomKindCacheEntry struct { isGroup bool expiresAt time.Time @@ -469,6 +473,12 @@ func (c *MatrixChannel) handleMessageEvent(ctx context.Context, evt *event.Event } respond, cleaned := c.ShouldRespondInGroup(isMentioned, content) if !respond { + logger.DebugCF("matrix", "Ignoring group message by trigger rules", map[string]any{ + "room_id": roomID, + "is_mentioned": isMentioned, + "mention_only": c.config.GroupTrigger.MentionOnly, + "prefixes": c.config.GroupTrigger.Prefixes, + }) return } content = cleaned @@ -807,7 +817,10 @@ func (c *MatrixChannel) isBotMentioned(msgEvt *event.MessageEventContent) bool { } userID := c.client.UserID.String() - if userID != "" && (strings.Contains(msgEvt.Body, userID) || strings.Contains(msgEvt.FormattedBody, userID)) { + if userID != "" && strings.Contains(msgEvt.Body, userID) { + return true + } + if mentionsUserInFormattedBody(msgEvt.FormattedBody, c.client.UserID) { return true } @@ -820,6 +833,63 @@ func (c *MatrixChannel) isBotMentioned(msgEvt *event.MessageEventContent) bool { return re.MatchString(msgEvt.Body) || re.MatchString(msgEvt.FormattedBody) } +func mentionsUserInFormattedBody(formattedBody string, userID id.UserID) bool { + target := strings.ToLower(strings.TrimSpace(userID.String())) + if target == "" { + return false + } + + formattedBody = strings.TrimSpace(formattedBody) + if formattedBody == "" { + return false + } + + if strings.Contains(strings.ToLower(formattedBody), target) { + return true + } + + matches := matrixMentionHrefRegexp.FindAllStringSubmatch(formattedBody, -1) + for _, match := range matches { + if len(match) < 2 { + continue + } + decoded := decodeMatrixMentionHref(match[1]) + if strings.Contains(strings.ToLower(decoded), target) { + return true + } + + u, err := url.Parse(decoded) + if err != nil { + continue + } + + if strings.Contains(strings.ToLower(u.Path), target) || strings.Contains(strings.ToLower(u.Fragment), target) { + return true + } + if strings.Contains(strings.ToLower(decodeMatrixMentionHref(u.Fragment)), target) { + return true + } + } + + return false +} + +func decodeMatrixMentionHref(v string) string { + decoded := html.UnescapeString(strings.TrimSpace(v)) + if decoded == "" { + return "" + } + + for i := 0; i < 2; i++ { + next, err := url.QueryUnescape(decoded) + if err != nil || next == decoded { + break + } + decoded = next + } + return decoded +} + func (c *MatrixChannel) typingLoop(ctx context.Context, roomID id.RoomID, session *typingSession) { sendTyping := func() { _, err := c.client.UserTyping(ctx, roomID, true, typingServerTTL) diff --git a/pkg/channels/matrix/matrix_test.go b/pkg/channels/matrix/matrix_test.go index 68e4bcb70..af31c671d 100644 --- a/pkg/channels/matrix/matrix_test.go +++ b/pkg/channels/matrix/matrix_test.go @@ -91,6 +91,22 @@ func TestIsBotMentioned(t *testing.T) { }, want: false, }, + { + name: "formatted mention href matrix.to plain", + msg: event.MessageEventContent{ + Body: "hello bot", + FormattedBody: `PicoClaw hello`, + }, + want: true, + }, + { + name: "formatted mention href matrix.to encoded", + msg: event.MessageEventContent{ + Body: "hello bot", + FormattedBody: `PicoClaw hello`, + }, + want: true, + }, } for _, tc := range cases { From cd955d730bbf0c9d16ab29de98ec469d1b9648f3 Mon Sep 17 00:00:00 2001 From: horsley Date: Sun, 8 Mar 2026 08:06:28 +0000 Subject: [PATCH 3/5] fix(ci): resolve linter and security check failures --- go.mod | 2 +- go.sum | 2 ++ pkg/channels/matrix/matrix.go | 3 ++- pkg/channels/matrix/matrix_test.go | 4 ++-- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 14da724f7..f60be046f 100644 --- a/go.mod +++ b/go.mod @@ -90,7 +90,7 @@ require ( github.com/yosida95/uritemplate/v3 v3.0.2 // indirect golang.org/x/arch v0.24.0 // indirect golang.org/x/crypto v0.48.0 // indirect - golang.org/x/net v0.50.0 // indirect + golang.org/x/net v0.51.0 // indirect golang.org/x/sync v0.19.0 // indirect golang.org/x/sys v0.41.0 // indirect ) diff --git a/go.sum b/go.sum index e762e45bc..4060997f8 100644 --- a/go.sum +++ b/go.sum @@ -271,6 +271,8 @@ golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= golang.org/x/net v0.50.0 h1:ucWh9eiCGyDR3vtzso0WMQinm2Dnt8cFMuQa9K33J60= golang.org/x/net v0.50.0/go.mod h1:UgoSli3F/pBgdJBHCTc+tp3gmrU4XswgGRgtnwWTfyM= +golang.org/x/net v0.51.0 h1:94R/GTO7mt3/4wIKpcR5gkGmRLOuE/2hNGeWq/GBIFo= +golang.org/x/net v0.51.0/go.mod h1:aamm+2QF5ogm02fjy5Bb7CQ0WMt1/WVM7FtyaTLlA9Y= golang.org/x/oauth2 v0.23.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI= golang.org/x/oauth2 v0.35.0 h1:Mv2mzuHuZuY2+bkyWXIHMfhNdJAdwW3FuWeCPYN5GVQ= golang.org/x/oauth2 v0.35.0/go.mod h1:lzm5WQJQwKZ3nwavOZ3IS5Aulzxi68dUSgRHujetwEA= diff --git a/pkg/channels/matrix/matrix.go b/pkg/channels/matrix/matrix.go index 87dd37aa8..7d361bcf8 100644 --- a/pkg/channels/matrix/matrix.go +++ b/pkg/channels/matrix/matrix.go @@ -611,7 +611,8 @@ func (c *MatrixChannel) downloadMedia( // Encrypted attachments put URL in msgEvt.File and require client-side decryption. if msgEvt != nil && msgEvt.File != nil && msgEvt.URL == "" { - if err := msgEvt.File.DecryptInPlace(data); err != nil { + err = msgEvt.File.DecryptInPlace(data) + if err != nil { return "", fmt.Errorf("decrypt matrix media: %w", err) } } diff --git a/pkg/channels/matrix/matrix_test.go b/pkg/channels/matrix/matrix_test.go index af31c671d..6a0ad03b8 100644 --- a/pkg/channels/matrix/matrix_test.go +++ b/pkg/channels/matrix/matrix_test.go @@ -18,7 +18,7 @@ func TestMatrixLocalpartMentionRegexp(t *testing.T) { }{ {text: "@picoclaw hello", want: true}, {text: "hi @picoclaw:matrix.org", want: true}, - {text: "欢迎一下picoclaw小龙虾", want: false}, // historical false-positive case in PR #356 + {text: "\u6b22\u8fce\u4e00\u4e0bpicoclaw\u5c0f\u9f99\u867e", want: false}, // historical false-positive case in PR #356 {text: "mail test@example.com", want: false}, } @@ -87,7 +87,7 @@ func TestIsBotMentioned(t *testing.T) { { name: "localpart without at sign should not match", msg: event.MessageEventContent{ - Body: "欢迎一下picoclaw小龙虾", + Body: "\u6b22\u8fce\u4e00\u4e0bpicoclaw\u5c0f\u9f99\u867e", }, want: false, }, From 6e16ac7f682a9f652e17912b01fd0be0440e86d8 Mon Sep 17 00:00:00 2001 From: horsley Date: Sun, 8 Mar 2026 09:23:02 +0000 Subject: [PATCH 4/5] fix(matrix): bound room cache and align temp media dir --- pkg/channels/matrix/matrix.go | 212 ++++++++++++++++++++++++----- pkg/channels/matrix/matrix_test.go | 54 ++++++++ 2 files changed, 234 insertions(+), 32 deletions(-) diff --git a/pkg/channels/matrix/matrix.go b/pkg/channels/matrix/matrix.go index 7d361bcf8..d51eee8fb 100644 --- a/pkg/channels/matrix/matrix.go +++ b/pkg/channels/matrix/matrix.go @@ -26,9 +26,13 @@ import ( ) const ( - typingRefreshInterval = 20 * time.Second - typingServerTTL = 30 * time.Second - roomKindCacheTTL = 5 * time.Minute + typingRefreshInterval = 20 * time.Second + typingServerTTL = 30 * time.Second + roomKindCacheTTL = 5 * time.Minute + roomKindCacheCleanupPeriod = 1 * time.Minute + roomKindCacheMaxEntries = 2048 + + matrixMediaTempDirName = "picoclaw_media" ) var matrixMentionHrefRegexp = regexp.MustCompile(`(?i)]+href=["']([^"']+)["']`) @@ -36,6 +40,109 @@ var matrixMentionHrefRegexp = regexp.MustCompile(`(?i)]+href=["']([^"']+)[" type roomKindCacheEntry struct { isGroup bool expiresAt time.Time + touchedAt time.Time +} + +type roomKindCache struct { + mu sync.Mutex + entries map[string]roomKindCacheEntry + maxEntries int + ttl time.Duration +} + +func newRoomKindCache(maxEntries int, ttl time.Duration) *roomKindCache { + if maxEntries <= 0 { + maxEntries = roomKindCacheMaxEntries + } + if ttl <= 0 { + ttl = roomKindCacheTTL + } + + return &roomKindCache{ + entries: make(map[string]roomKindCacheEntry), + maxEntries: maxEntries, + ttl: ttl, + } +} + +func (c *roomKindCache) get(roomID string, now time.Time) (bool, bool) { + c.mu.Lock() + defer c.mu.Unlock() + + entry, ok := c.entries[roomID] + if !ok { + return false, false + } + if !entry.expiresAt.After(now) { + delete(c.entries, roomID) + return false, false + } + + return entry.isGroup, true +} + +func (c *roomKindCache) set(roomID string, isGroup bool, now time.Time) { + c.mu.Lock() + defer c.mu.Unlock() + + if entry, ok := c.entries[roomID]; ok { + entry.isGroup = isGroup + entry.expiresAt = now.Add(c.ttl) + entry.touchedAt = now + c.entries[roomID] = entry + return + } + + c.cleanupExpiredLocked(now) + for len(c.entries) >= c.maxEntries { + if !c.evictOldestLocked() { + break + } + } + + c.entries[roomID] = roomKindCacheEntry{ + isGroup: isGroup, + expiresAt: now.Add(c.ttl), + touchedAt: now, + } +} + +func (c *roomKindCache) cleanupExpired(now time.Time) int { + c.mu.Lock() + defer c.mu.Unlock() + return c.cleanupExpiredLocked(now) +} + +func (c *roomKindCache) cleanupExpiredLocked(now time.Time) int { + removed := 0 + for roomID, entry := range c.entries { + if !entry.expiresAt.After(now) { + delete(c.entries, roomID) + removed++ + } + } + return removed +} + +func (c *roomKindCache) evictOldestLocked() bool { + if len(c.entries) == 0 { + return false + } + + var ( + oldestRoomID string + oldestAt time.Time + ) + + for roomID, entry := range c.entries { + if oldestRoomID == "" || entry.touchedAt.Before(oldestAt) { + oldestRoomID = roomID + oldestAt = entry.touchedAt + } + } + + delete(c.entries, oldestRoomID) + return true } type typingSession struct { @@ -70,7 +177,8 @@ type MatrixChannel struct { typingMu sync.Mutex typingSessions map[string]*typingSession // roomID -> session - roomKindCache sync.Map // roomID -> roomKindCacheEntry + roomKindCache *roomKindCache + localpartMentionR *regexp.Regexp } func NewMatrixChannel(cfg config.MatrixConfig, messageBus *bus.MessageBus) (*MatrixChannel, error) { @@ -111,14 +219,15 @@ func NewMatrixChannel(cfg config.MatrixConfig, messageBus *bus.MessageBus) (*Mat ) return &MatrixChannel{ - BaseChannel: base, - client: client, - config: cfg, - syncer: syncer, - typingSessions: make(map[string]*typingSession), - startTime: time.Now(), - roomKindCache: sync.Map{}, - typingMu: sync.Mutex{}, + BaseChannel: base, + client: client, + config: cfg, + syncer: syncer, + typingSessions: make(map[string]*typingSession), + startTime: time.Now(), + roomKindCache: newRoomKindCache(roomKindCacheMaxEntries, roomKindCacheTTL), + localpartMentionR: localpartMentionRegexp(matrixLocalpart(client.UserID)), + typingMu: sync.Mutex{}, }, nil } @@ -132,6 +241,7 @@ func (c *MatrixChannel) Start(ctx context.Context) error { c.syncer.OnEventType(event.StateMember, c.handleMemberEvent) c.SetRunning(true) + go c.runRoomKindCacheJanitor(c.ctx) go func() { if err := c.client.SyncWithContext(c.ctx); err != nil && c.ctx.Err() == nil { @@ -469,7 +579,7 @@ func (c *MatrixChannel) handleMessageEvent(ctx context.Context, evt *event.Event if isGroup { isMentioned := c.isBotMentioned(msgEvt) if isMentioned { - content = stripUserMention(content, c.client.UserID) + content = c.stripSelfMention(content) } respond, cleaned := c.ShouldRespondInGroup(isMentioned, content) if !respond { @@ -483,7 +593,7 @@ func (c *MatrixChannel) handleMessageEvent(ctx context.Context, evt *event.Event } content = cleaned } else { - content = stripUserMention(content, c.client.UserID) + content = c.stripSelfMention(content) } content = strings.TrimSpace(content) @@ -619,7 +729,11 @@ func (c *MatrixChannel) downloadMedia( label := matrixMediaLabel(msgEvt, mediaKind) ext := matrixMediaExt(label, matrixContentType(msgEvt), mediaKind) - tmp, err := os.CreateTemp("", "matrix-media-*"+ext) + mediaDir, err := matrixMediaTempDir() + if err != nil { + return "", fmt.Errorf("create matrix media directory: %w", err) + } + tmp, err := os.CreateTemp(mediaDir, "matrix-media-*"+ext) if err != nil { return "", err } @@ -777,11 +891,8 @@ func matrixMediaExt(filename, contentType, mediaKind string) string { func (c *MatrixChannel) isGroupRoom(ctx context.Context, roomID id.RoomID) bool { now := time.Now() - if cached, ok := c.roomKindCache.Load(roomID.String()); ok { - entry := cached.(roomKindCacheEntry) - if now.Before(entry.expiresAt) { - return entry.isGroup - } + if isGroup, ok := c.roomKindCache.get(roomID.String(), now); ok { + return isGroup } qctx := c.baseContext() @@ -801,10 +912,7 @@ func (c *MatrixChannel) isGroupRoom(ctx context.Context, roomID id.RoomID) bool } isGroup := len(resp.Joined) > 2 - c.roomKindCache.Store(roomID.String(), roomKindCacheEntry{ - isGroup: isGroup, - expiresAt: now.Add(roomKindCacheTTL), - }) + c.roomKindCache.set(roomID.String(), isGroup, now) return isGroup } @@ -825,13 +933,17 @@ func (c *MatrixChannel) isBotMentioned(msgEvt *event.MessageEventContent) bool { return true } - localpart := matrixLocalpart(c.client.UserID) - if localpart == "" { + mentionR := c.localpartMentionR + if mentionR == nil { + mentionR = localpartMentionRegexp(matrixLocalpart(c.client.UserID)) + } + if mentionR == nil { return false } - re := localpartMentionRegexp(localpart) - return re.MatchString(msgEvt.Body) || re.MatchString(msgEvt.FormattedBody) + // Matrix users are addressed as MXID "@localpart:server", but many clients + // emit plain-text mentions as "@localpart". Both forms are handled here. + return mentionR.MatchString(msgEvt.Body) || mentionR.MatchString(msgEvt.FormattedBody) } func mentionsUserInFormattedBody(formattedBody string, userID id.UserID) bool { @@ -941,6 +1053,32 @@ func (c *MatrixChannel) baseContext() context.Context { return context.Background() } +func (c *MatrixChannel) runRoomKindCacheJanitor(ctx context.Context) { + ticker := time.NewTicker(roomKindCacheCleanupPeriod) + defer ticker.Stop() + + for { + select { + case <-ctx.Done(): + return + case now := <-ticker.C: + c.roomKindCache.cleanupExpired(now) + } + } +} + +func (c *MatrixChannel) stripSelfMention(text string) string { + return stripUserMentionWithRegexp(text, c.client.UserID, c.localpartMentionR) +} + +func matrixMediaTempDir() (string, error) { + mediaDir := filepath.Join(os.TempDir(), matrixMediaTempDirName) + if err := os.MkdirAll(mediaDir, 0o700); err != nil { + return "", err + } + return mediaDir, nil +} + func matrixLocalpart(userID id.UserID) string { s := strings.TrimPrefix(userID.String(), "@") localpart, _, _ := strings.Cut(s, ":") @@ -948,17 +1086,27 @@ func matrixLocalpart(userID id.UserID) string { } func localpartMentionRegexp(localpart string) *regexp.Regexp { + localpart = strings.TrimSpace(localpart) + if localpart == "" { + return nil + } + + // Match Matrix mentions in plain text while avoiding false positives: + // "@picoclaw" and "@picoclaw:matrix.org" should match, + // "test@example.com" and "hellopicoclawworld" should not. pattern := `(?i)(^|[^[:alnum:]_])@` + regexp.QuoteMeta(localpart) + `(?::[A-Za-z0-9._:-]+)?([^[:alnum:]_]|$)` return regexp.MustCompile(pattern) } func stripUserMention(text string, userID id.UserID) string { + return stripUserMentionWithRegexp(text, userID, localpartMentionRegexp(matrixLocalpart(userID))) +} + +func stripUserMentionWithRegexp(text string, userID id.UserID, mentionR *regexp.Regexp) string { cleaned := strings.ReplaceAll(text, userID.String(), "") - localpart := matrixLocalpart(userID) - if localpart != "" { - re := localpartMentionRegexp(localpart) - cleaned = re.ReplaceAllString(cleaned, "$1$2") + if mentionR != nil { + cleaned = mentionR.ReplaceAllString(cleaned, "$1$2") } cleaned = strings.TrimSpace(cleaned) diff --git a/pkg/channels/matrix/matrix_test.go b/pkg/channels/matrix/matrix_test.go index 6a0ad03b8..4eb5ac083 100644 --- a/pkg/channels/matrix/matrix_test.go +++ b/pkg/channels/matrix/matrix_test.go @@ -2,7 +2,10 @@ package matrix import ( "context" + "os" + "path/filepath" "testing" + "time" "maunium.net/go/mautrix" "maunium.net/go/mautrix/event" @@ -116,6 +119,57 @@ func TestIsBotMentioned(t *testing.T) { } } +func TestRoomKindCache_ExpiresEntries(t *testing.T) { + cache := newRoomKindCache(4, 5*time.Second) + now := time.Unix(100, 0) + cache.set("!room:matrix.org", true, now) + + if got, ok := cache.get("!room:matrix.org", now.Add(2*time.Second)); !ok || !got { + t.Fatalf("expected cached group room before ttl, got ok=%v group=%v", ok, got) + } + + if _, ok := cache.get("!room:matrix.org", now.Add(6*time.Second)); ok { + t.Fatal("expected cache miss after ttl expiry") + } +} + +func TestRoomKindCache_EvictsOldestWhenFull(t *testing.T) { + cache := newRoomKindCache(2, time.Minute) + now := time.Unix(200, 0) + + cache.set("!room1:matrix.org", false, now) + cache.set("!room2:matrix.org", false, now.Add(1*time.Second)) + cache.set("!room3:matrix.org", true, now.Add(2*time.Second)) + + if _, ok := cache.get("!room1:matrix.org", now.Add(2*time.Second)); ok { + t.Fatal("expected oldest cache entry to be evicted") + } + if got, ok := cache.get("!room2:matrix.org", now.Add(2*time.Second)); !ok || got { + t.Fatalf("expected room2 to remain and be direct, got ok=%v group=%v", ok, got) + } + if got, ok := cache.get("!room3:matrix.org", now.Add(2*time.Second)); !ok || !got { + t.Fatalf("expected room3 to remain and be group, got ok=%v group=%v", ok, got) + } +} + +func TestMatrixMediaTempDir(t *testing.T) { + dir, err := matrixMediaTempDir() + if err != nil { + t.Fatalf("matrixMediaTempDir failed: %v", err) + } + if filepath.Base(dir) != matrixMediaTempDirName { + t.Fatalf("unexpected media dir base: %q", filepath.Base(dir)) + } + + info, err := os.Stat(dir) + if err != nil { + t.Fatalf("media dir not created: %v", err) + } + if !info.IsDir() { + t.Fatalf("expected directory, got mode=%v", info.Mode()) + } +} + func TestMatrixMediaExt(t *testing.T) { if got := matrixMediaExt("photo.png", "", "image"); got != ".png" { t.Fatalf("filename extension mismatch: got=%q", got) From fb2bfe4b3ce809a1f1afbddc5336eb6badf3a3d7 Mon Sep 17 00:00:00 2001 From: horsley Date: Sun, 8 Mar 2026 10:53:45 +0000 Subject: [PATCH 5/5] fix(matrix): satisfy golines in mention regex test --- pkg/channels/matrix/matrix_test.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/channels/matrix/matrix_test.go b/pkg/channels/matrix/matrix_test.go index 4eb5ac083..e76db0d3e 100644 --- a/pkg/channels/matrix/matrix_test.go +++ b/pkg/channels/matrix/matrix_test.go @@ -21,7 +21,10 @@ func TestMatrixLocalpartMentionRegexp(t *testing.T) { }{ {text: "@picoclaw hello", want: true}, {text: "hi @picoclaw:matrix.org", want: true}, - {text: "\u6b22\u8fce\u4e00\u4e0bpicoclaw\u5c0f\u9f99\u867e", want: false}, // historical false-positive case in PR #356 + { + text: "\u6b22\u8fce\u4e00\u4e0bpicoclaw\u5c0f\u9f99\u867e", + want: false, // historical false-positive case in PR #356 + }, {text: "mail test@example.com", want: false}, }