Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ var products: [Product] = [
name: "IMessage",
targets: ["IMessage"]
),
.library(
name: "IMessageBridgeKit",
type: .dynamic,
targets: ["IMessageBridgeKit"]
),
.executable(name: "imessage-cli", targets: ["IMessageCLI"]),
.executable(name: "IMDatabaseTestBench", targets: ["IMDatabaseTestBench"]),
]
Expand Down Expand Up @@ -102,6 +107,15 @@ var targets: [Target] = [
],
path: "src/IMessage/Sources/IMessage"
),
.target(
name: "IMessageBridgeKit",
dependencies: [
"IMessage",
"IMessageCore",
"PlatformSDK",
],
path: "src/IMessage/Sources/IMessageBridgeKit"
),
.target(
name: "IMessagePrivateSPI",
path: "src/IMessage/Sources/IMessagePrivateSPI",
Expand Down
20 changes: 20 additions & 0 deletions bridge-readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# mautrix-imessage bridgev2

This repo includes a bridgev2 Matrix bridge entrypoint at `cmd/mautrix-imessage`.

The bridge is integrated with the Swift iMessage runtime through `IMessageBridgeKit`, a Swift dynamic library with a C ABI. It does not shell out to `imessage-cli`.

## Build

```sh
swift build --product IMessageBridgeKit
swift_bin="$(swift build --show-bin-path)"
CGO_LDFLAGS="-L${swift_bin} -Wl,-rpath,${swift_bin}" \
go build -tags nocrypto ./cmd/mautrix-imessage
```

The `nocrypto` tag avoids linking mautrix's optional libolm dependency while still keeping cgo enabled for the Swift bridge library.

The bridge currently supports local-login, startup chat/message sync, live state-sync events, Matrix text/file sends, replies, edits, unsend, reactions, read receipts, and typing.

It must run on macOS with the usual platform-imessage permissions for Messages data and UI automation.
26 changes: 26 additions & 0 deletions cmd/mautrix-imessage/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"maunium.net/go/mautrix/bridgev2/matrix/mxmain"

"github.com/beeper/platform-imessage/pkg/connector"
)

var (
Tag = "unknown"
Commit = "unknown"
BuildTime = "unknown"
)

var m = mxmain.BridgeMain{
Name: "mautrix-imessage",
URL: "https://github.com/beeper/platform-imessage",
Description: "A Matrix-iMessage bridge using platform-imessage and mautrix bridgev2.",
Version: "0.1.0",
Connector: &connector.Connector{},
}

func main() {
m.InitVersion(Tag, Commit, BuildTime)
m.Run()
}
39 changes: 39 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module github.com/beeper/platform-imessage

go 1.25.0

toolchain go1.26.2

require (
github.com/rs/zerolog v1.35.1
go.mau.fi/util v0.9.9-0.20260511124621-9241e81bdf25
maunium.net/go/mautrix v0.27.1-0.20260513120123-5fba7e3afae4
)

require (
filippo.io/edwards25519 v1.2.0 // indirect
github.com/coder/websocket v1.8.14 // indirect
github.com/coreos/go-systemd/v22 v22.7.0 // indirect
github.com/lib/pq v1.12.3 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-sqlite3 v1.14.44 // indirect
github.com/petermattis/goid v0.0.0-20260330135022-df67b199bc81 // indirect
github.com/rs/xid v1.6.0 // indirect
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect
github.com/tidwall/gjson v1.18.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tidwall/sjson v1.2.5 // indirect
github.com/yuin/goldmark v1.8.2 // indirect
go.mau.fi/zeroconfig v0.2.0 // indirect
golang.org/x/crypto v0.50.0 // indirect
golang.org/x/exp v0.0.0-20260410095643-746e56fc9e2f // indirect
golang.org/x/net v0.53.0 // indirect
golang.org/x/sync v0.20.0 // indirect
golang.org/x/sys v0.43.0 // indirect
golang.org/x/text v0.36.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
maunium.net/go/mauflag v1.0.0 // indirect
)
69 changes: 69 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
filippo.io/edwards25519 v1.2.0 h1:crnVqOiS4jqYleHd9vaKZ+HKtHfllngJIiOpNpoJsjo=
filippo.io/edwards25519 v1.2.0/go.mod h1:xzAOLCNug/yB62zG1bQ8uziwrIqIuxhctzJT18Q77mc=
github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
github.com/coder/websocket v1.8.14 h1:9L0p0iKiNOibykf283eHkKUHHrpG7f65OE3BhhO7v9g=
github.com/coder/websocket v1.8.14/go.mod h1:NX3SzP+inril6yawo5CQXx8+fk145lPDC6pumgx0mVg=
github.com/coreos/go-systemd/v22 v22.7.0 h1:LAEzFkke61DFROc7zNLX/WA2i5J8gYqe0rSj9KI28KA=
github.com/coreos/go-systemd/v22 v22.7.0/go.mod h1:xNUYtjHu2EDXbsxz1i41wouACIwT7Ybq9o0BQhMwD0w=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/lib/pq v1.12.3 h1:tTWxr2YLKwIvK90ZXEw8GP7UFHtcbTtty8zsI+YjrfQ=
github.com/lib/pq v1.12.3/go.mod h1:/p+8NSbOcwzAEI7wiMXFlgydTwcgTr3OSKMsD2BitpA=
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.44 h1:3VSe+xafpbzsLbdr2AWlAZk9yRHiBhTBakioXaCKTF8=
github.com/mattn/go-sqlite3 v1.14.44/go.mod h1:pjEuOr8IwzLJP2MfGeTb0A35jauH+C2kbHKBr7yXKVQ=
github.com/petermattis/goid v0.0.0-20260330135022-df67b199bc81 h1:WDsQxOJDy0N1VRAjXLpi8sCEZRSGarLWQevDxpTBRrM=
github.com/petermattis/goid v0.0.0-20260330135022-df67b199bc81/go.mod h1:pxMtw7cyUw6B2bRH0ZBANSPg+AoSud1I1iyJHI69jH4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
github.com/rs/zerolog v1.35.1 h1:m7xQeoiLIiV0BCEY4Hs+j2NG4Gp2o2KPKmhnnLiazKI=
github.com/rs/zerolog v1.35.1/go.mod h1:EjML9kdfa/RMA7h/6z6pYmq1ykOuA8/mjWaEvGI+jcw=
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
github.com/yuin/goldmark v1.8.2 h1:kEGpgqJXdgbkhcOgBxkC0X0PmoPG1ZyoZ117rDVp4zE=
github.com/yuin/goldmark v1.8.2/go.mod h1:ip/1k0VRfGynBgxOz0yCqHrbZXhcjxyuS66Brc7iBKg=
go.mau.fi/util v0.9.9-0.20260511124621-9241e81bdf25 h1:YPEmc+li7TF6C9AdRTcSLMb6yCHdF27/wNT7kFLIVNg=
go.mau.fi/util v0.9.9-0.20260511124621-9241e81bdf25/go.mod h1:jE9FfhbgEgAwxei6lomO9v8zdCIATcquONUu4vjRwSs=
go.mau.fi/zeroconfig v0.2.0 h1:e/OGEERqVRRKlgaro7E6bh8xXiKFSXB3eNNIud7FUjU=
go.mau.fi/zeroconfig v0.2.0/go.mod h1:J0Vn0prHNOm493oZoQ84kq83ZaNCYZnq+noI1b1eN8w=
golang.org/x/crypto v0.50.0 h1:zO47/JPrL6vsNkINmLoo/PH1gcxpls50DNogFvB5ZGI=
golang.org/x/crypto v0.50.0/go.mod h1:3muZ7vA7PBCE6xgPX7nkzzjiUq87kRItoJQM1Yo8S+Q=
golang.org/x/exp v0.0.0-20260410095643-746e56fc9e2f h1:W3F4c+6OLc6H2lb//N1q4WpJkhzJCK5J6kUi1NTVXfM=
golang.org/x/exp v0.0.0-20260410095643-746e56fc9e2f/go.mod h1:J1xhfL/vlindoeF/aINzNzt2Bket5bjo9sdOYzOsU80=
golang.org/x/net v0.53.0 h1:d+qAbo5L0orcWAr0a9JweQpjXF19LMXJE8Ey7hwOdUA=
golang.org/x/net v0.53.0/go.mod h1:JvMuJH7rrdiCfbeHoo3fCQU24Lf5JJwT9W3sJFulfgs=
golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4=
golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.43.0 h1:Rlag2XtaFTxp19wS8MXlJwTvoh8ArU6ezoyFsMyCTNI=
golang.org/x/sys v0.43.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/text v0.36.0 h1:JfKh3XmcRPqZPKevfXVpI1wXPTqbkE5f7JA92a55Yxg=
golang.org/x/text v0.36.0/go.mod h1:NIdBknypM8iqVmPiuco0Dh6P5Jcdk8lJL0CUebqK164=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
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/mauflag v1.0.0 h1:YiaRc0tEI3toYtJMRIfjP+jklH45uDHtT80nUamyD4M=
maunium.net/go/mauflag v1.0.0/go.mod h1:nLivPOpTpHnpzEh8jEdSL9UqO9+/KBJFmNRlwKfkPeA=
maunium.net/go/mautrix v0.27.1-0.20260513120123-5fba7e3afae4 h1:zNC9eVAhw8FhKpM3AxNAh/iy75UEYX91uJUvqqAYlvo=
maunium.net/go/mautrix v0.27.1-0.20260513120123-5fba7e3afae4/go.mod h1:3sOGhXi3P1V6/NruTA0gujkvTypXVUraWktCuTGyDuM=
99 changes: 99 additions & 0 deletions pkg/connector/backfill.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package connector

import (
"context"

"github.com/beeper/platform-imessage/pkg/imessage"
"github.com/beeper/platform-imessage/pkg/imessageid"
"maunium.net/go/mautrix/bridgev2"
"maunium.net/go/mautrix/bridgev2/database"
"maunium.net/go/mautrix/bridgev2/networkid"
)

var _ bridgev2.BackfillingNetworkAPI = (*Client)(nil)

func (c *Client) FetchMessages(ctx context.Context, params bridgev2.FetchMessagesParams) (*bridgev2.FetchMessagesResponse, error) {
threadID := string(params.Portal.ID)
pagination := backfillPagination(params)
page, err := c.IM.Messages(threadID, pagination)
if err != nil {
return nil, err
}

messages := make([]*bridgev2.BackfillMessage, 0, len(page.Items))
for i := len(page.Items) - 1; i >= 0; i-- {
msg := page.Items[i]
if msg.ThreadID == "" {
msg.ThreadID = threadID
}
converted, err := c.convertMessageFromIMessage(ctx, params.Portal, c.Main.Bridge.Bot, msg)
if err != nil {
return nil, err
}
reactions := make([]*bridgev2.BackfillReaction, 0, len(msg.Reactions))
for _, reaction := range msg.Reactions {
reactions = append(reactions, &bridgev2.BackfillReaction{
Sender: bridgev2.EventSender{Sender: imessageid.MakeUserID(reaction.ParticipantID)},
EmojiID: networkid.EmojiID(reaction.ID),
Emoji: reaction.ReactionKey,
})
}
messages = append(messages, &bridgev2.BackfillMessage{
ConvertedMessage: converted,
Sender: c.sender(msg),
ID: imessageid.MakeMessageID(msg.ID),
TxnID: networkid.TransactionID(msg.ID),
Timestamp: messageTimestamp(msg),
StreamOrder: msg.Timestamp,
Reactions: reactions,
})
}

return &bridgev2.FetchMessagesResponse{
Messages: messages,
Cursor: networkid.PaginationCursor(nextBackfillCursor(page, messages)),
HasMore: page.HasMore,
Forward: params.Forward,
}, nil
}

func backfillPagination(params bridgev2.FetchMessagesParams) *imessage.Pagination {
cursor := string(params.Cursor)
if cursor == "" {
cursor = cursorFromMessage(params.AnchorMessage)
}
if cursor == "" {
return nil
}
direction := "before"
if params.Forward {
direction = "after"
}
return &imessage.Pagination{
Cursor: cursor,
Direction: direction,
}
}

func cursorFromMessage(msg *database.Message) string {
if msg == nil {
return ""
}
if meta, ok := msg.Metadata.(*imessageid.MessageMetadata); ok && meta.Cursor != "" {
return meta.Cursor
}
return string(msg.ID)
}

func nextBackfillCursor(page *imessage.Page[imessage.Message], messages []*bridgev2.BackfillMessage) string {
Comment thread
indent[bot] marked this conversation as resolved.
Outdated
if page == nil {
return ""
}
if page.OldestCursor != "" {
return page.OldestCursor
}
if len(messages) == 0 {
return ""
}
return string(messages[0].ID)
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
84 changes: 84 additions & 0 deletions pkg/connector/capabilities.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package connector

import (
"context"
"time"

"go.mau.fi/util/jsontime"
"go.mau.fi/util/ptr"
"maunium.net/go/mautrix/bridgev2"
"maunium.net/go/mautrix/event"
)

const maxTextLength = 200_000
const maxFileSize = 100 * 1024 * 1024

var generalCaps = &bridgev2.NetworkGeneralCapabilities{
ImplicitReadReceipts: true,
Provisioning: bridgev2.ProvisioningCapabilities{
ResolveIdentifier: bridgev2.ResolveIdentifierCapabilities{
CreateDM: true,
AnyPhone: true,
ContactList: true,
Search: true,
},
},
}

func (c *Connector) GetCapabilities() *bridgev2.NetworkGeneralCapabilities {
return generalCaps
}

func (c *Connector) GetBridgeInfoVersion() (info, capabilities int) {
return 1, 1
}

var roomCaps = &event.RoomFeatures{
ID: "com.beeper.imessage.capabilities.2026_06_04",
Formatting: map[event.FormattingFeature]event.CapabilitySupportLevel{
event.FmtBold: event.CapLevelDropped,
event.FmtItalic: event.CapLevelDropped,
event.FmtStrikethrough: event.CapLevelDropped,
event.FmtInlineLink: event.CapLevelDropped,
event.FmtUserLink: event.CapLevelDropped,
},
File: map[event.CapabilityMsgType]*event.FileFeatures{
event.MsgImage: {
MimeTypes: map[string]event.CapabilitySupportLevel{"*/*": event.CapLevelFullySupported},
Caption: event.CapLevelDropped,
MaxSize: maxFileSize,
},
event.MsgVideo: {
MimeTypes: map[string]event.CapabilitySupportLevel{"*/*": event.CapLevelFullySupported},
Caption: event.CapLevelDropped,
MaxSize: maxFileSize,
},
event.MsgAudio: {
MimeTypes: map[string]event.CapabilitySupportLevel{"*/*": event.CapLevelFullySupported},
Caption: event.CapLevelDropped,
MaxSize: maxFileSize,
},
event.MsgFile: {
MimeTypes: map[string]event.CapabilitySupportLevel{"*/*": event.CapLevelFullySupported},
Caption: event.CapLevelDropped,
MaxSize: maxFileSize,
},
},
MaxTextLength: maxTextLength,
Reply: event.CapLevelFullySupported,
Edit: event.CapLevelFullySupported,
EditMaxAge: ptr.Ptr(jsontime.S(15 * time.Minute)),
Delete: event.CapLevelFullySupported,
DeleteForMe: false,
DeleteMaxAge: ptr.Ptr(jsontime.S(2 * time.Minute)),
Reaction: event.CapLevelFullySupported,
ReactionCount: 1,
ReadReceipts: true,
TypingNotifications: true,
MarkAsUnread: true,
DeleteChat: true,
}

func (c *Client) GetCapabilities(ctx context.Context, portal *bridgev2.Portal) *event.RoomFeatures {
return roomCaps.Clone()
}
Loading