Skip to content

Commit

Permalink
Use new main function for v2 bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Jun 11, 2024
1 parent 67da642 commit ecd4f05
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 49 deletions.
43 changes: 22 additions & 21 deletions cmd/mautrix-signal-v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,32 @@
package main

import (
"os"

"go.mau.fi/util/dbutil"
"go.mau.fi/util/exerrors"
"go.mau.fi/util/exzerolog"
"gopkg.in/yaml.v3"
"maunium.net/go/mautrix/bridgev2"
"maunium.net/go/mautrix/bridgev2/bridgeconfig"
"maunium.net/go/mautrix/bridgev2/matrix"
"maunium.net/go/mautrix/bridgev2/matrix/mxmain"

"go.mau.fi/mautrix-signal/pkg/connector"
"go.mau.fi/mautrix-signal/pkg/signalmeow"
)

// Information to find out exactly which commit the bridge was built from.
// These are filled at build time with the -X linker flag.
var (
Tag = "unknown"
Commit = "unknown"
BuildTime = "unknown"
)

func main() {
var cfg bridgeconfig.Config
config := exerrors.Must(os.ReadFile("config.yaml"))
exerrors.PanicIfNotNil(yaml.Unmarshal(config, &cfg))
log := exerrors.Must(cfg.Logging.Compile())
exzerolog.SetupDefaults(log)
signalmeow.SetLogger(log.With().Str("component", "signalmeow").Logger())
db := exerrors.Must(dbutil.NewFromConfig("mautrix-signal", cfg.Database, dbutil.ZeroLogger(log.With().Str("db_section", "main").Logger())))
signalConnector := connector.NewConnector()
exerrors.PanicIfNotNil(cfg.Network.Decode(signalConnector.Config))
bridge := bridgev2.NewBridge("", db, *log, matrix.NewConnector(&cfg), signalConnector)
bridge.CommandPrefix = "!signal"
bridge.Start()
m := mxmain.BridgeMain{
Name: "mautrix-signal",
URL: "https://github.com/mautrix/signal",
Description: "A Matrix-Signal puppeting bridge.",
Version: "0.7.0",

Connector: connector.NewConnector(),
}
m.PostInit = func() {
signalmeow.SetLogger(m.Log.With().Str("component", "signalmeow").Logger())
}
m.InitVersion(Tag, Commit, BuildTime)
m.Run()
}
2 changes: 1 addition & 1 deletion config/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"maunium.net/go/mautrix/bridge/bridgeconfig"
)

func DoUpgrade(helper *up.Helper) {
func DoUpgrade(helper up.Helper) {
bridgeconfig.Upgrader.DoUpgrade(helper)

legacyDB, ok := helper.Get(up.Str, "appservice", "database")
Expand Down
16 changes: 8 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@ require (
github.com/mattn/go-pointer v0.0.1
github.com/mattn/go-sqlite3 v1.14.22
github.com/prometheus/client_golang v1.19.1
github.com/rs/zerolog v1.32.0
github.com/rs/zerolog v1.33.0
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
github.com/stretchr/testify v1.9.0
github.com/tidwall/gjson v1.17.1
go.mau.fi/util v0.4.3-0.20240516141139-2ebe792cd8f7
golang.org/x/crypto v0.23.0
golang.org/x/exp v0.0.0-20240525044651-4c93da0ed11d
golang.org/x/net v0.25.0
go.mau.fi/util v0.4.3-0.20240611114927-6ef09885dd97
golang.org/x/crypto v0.24.0
golang.org/x/exp v0.0.0-20240604190554-fc45aab8b7f8
golang.org/x/net v0.26.0
google.golang.org/protobuf v1.34.1
gopkg.in/yaml.v3 v3.0.1
maunium.net/go/mautrix v0.18.2-0.20240610192549-f9dccaaea049
maunium.net/go/mautrix v0.18.2-0.20240611120402-f97d365ea9eb
nhooyr.io/websocket v1.8.11
)

Expand All @@ -44,7 +43,8 @@ require (
github.com/tidwall/sjson v1.2.5 // indirect
github.com/yuin/goldmark v1.7.1 // indirect
go.mau.fi/zeroconfig v0.1.2 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/sys v0.21.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
)
28 changes: 14 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjR
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0=
github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
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.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
Expand All @@ -69,21 +69,21 @@ 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.7.1 h1:3bajkSilaCbjdKVsKdZjZCLBNPL9pYzrCakKaf4U49U=
github.com/yuin/goldmark v1.7.1/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
go.mau.fi/util v0.4.3-0.20240516141139-2ebe792cd8f7 h1:2hnc2iS7usHT3aqIQ8HVtKtPgic+13EVSdZ1m8UBL/E=
go.mau.fi/util v0.4.3-0.20240516141139-2ebe792cd8f7/go.mod h1:m+PJpPMadAW6cj3ldyuO5bLhFreWdwcu+3QTwYNGlGk=
go.mau.fi/util v0.4.3-0.20240611114927-6ef09885dd97 h1:btYXIv4Iqnboc9FQS99dh8XwMF2QftOhfTeh02K2b4o=
go.mau.fi/util v0.4.3-0.20240611114927-6ef09885dd97/go.mod h1:4etkIWotzgsWICu/1I34Y2LFFekINhFsyWYHXEsxXdY=
go.mau.fi/zeroconfig v0.1.2 h1:DKOydWnhPMn65GbXZOafgkPm11BvFashZWLct0dGFto=
go.mau.fi/zeroconfig v0.1.2/go.mod h1:NcSJkf180JT+1IId76PcMuLTNa1CzsFFZ0nBygIQM70=
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/exp v0.0.0-20240525044651-4c93da0ed11d h1:N0hmiNbwsSNwHBAvR3QB5w25pUwH4tK0Y/RltD1j1h4=
golang.org/x/exp v0.0.0-20240525044651-4c93da0ed11d/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
golang.org/x/exp v0.0.0-20240604190554-fc45aab8b7f8 h1:LoYXNGAShUG3m/ehNk4iFctuhGX/+R1ZpfJ4/ia80JM=
golang.org/x/exp v0.0.0-20240604190554-fc45aab8b7f8/go.mod h1:jj3sYF3dwk5D+ghuXyeI3r5MFf+NT2An6/9dOA95KSI=
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand All @@ -95,7 +95,7 @@ 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.18.2-0.20240610192549-f9dccaaea049 h1:keFLN2tVrt5UmNSm7Mjweue4r4t+WUKk0kmPcWfXUpQ=
maunium.net/go/mautrix v0.18.2-0.20240610192549-f9dccaaea049/go.mod h1:P/FV8cXY262MezYX7ViuhfzeJ0nK4+M8K6ZmxEC/aEA=
maunium.net/go/mautrix v0.18.2-0.20240611120402-f97d365ea9eb h1:PcUevUW1w5PCeqXha1fPIlk25XcdXrogpBsBLB/HbXo=
maunium.net/go/mautrix v0.18.2-0.20240611120402-f97d365ea9eb/go.mod h1:HLNcNm0J4ZDgaoMjoHv5Hc/GkA2FfVUR6KbW7A3Iwv0=
nhooyr.io/websocket v1.8.11 h1:f/qXNc2/3DpoSZkHt1DQu6rj4zGC8JmkkLkWss0MgN0=
nhooyr.io/websocket v1.8.11/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c=
39 changes: 34 additions & 5 deletions pkg/connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package connector

import (
"context"
_ "embed"
"fmt"
"strconv"
"strings"
Expand All @@ -26,10 +27,10 @@ import (

"github.com/google/uuid"
"github.com/rs/zerolog"
up "go.mau.fi/util/configupgrade"
"go.mau.fi/util/dbutil"
"go.mau.fi/util/variationselector"
"google.golang.org/protobuf/proto"

"maunium.net/go/mautrix/bridgev2"
"maunium.net/go/mautrix/bridgev2/database"
"maunium.net/go/mautrix/bridgev2/networkid"
Expand Down Expand Up @@ -94,12 +95,44 @@ type SignalConnector struct {
Config *SignalConfig
}

var _ bridgev2.NetworkConnector = (*SignalConnector)(nil)
var _ bridgev2.NetworkAPI = (*SignalClient)(nil)
var _ msgconv.PortalMethods = (*msgconvPortalMethods)(nil)

func NewConnector() *SignalConnector {
return &SignalConnector{
Config: &SignalConfig{},
}
}

//go:embed example-config.yaml
var ExampleConfig string

func (s *SignalConnector) GetName() bridgev2.BridgeName {
return bridgev2.BridgeName{
DisplayName: "Signal",
NetworkURL: "https://signal.org",
NetworkIcon: "mxc://maunium.net/wPJgTQbZOtpBFmDNkiNEMDUp",
NetworkID: "signal",
BeeperBridgeType: "signal",
DefaultPort: 29328,
}
}

func upgradeConfig(helper up.Helper) {
helper.Copy(up.Str, "displayname_template")
helper.Copy(up.Bool, "use_contact_avatars")
helper.Copy(up.Bool, "use_outdated_profiles")
helper.Copy(up.Bool, "number_in_topic")
helper.Copy(up.Str, "device_name")
helper.Copy(up.Str, "note_to_self_avatar")
helper.Copy(up.Str, "location_format")
}

func (s *SignalConnector) GetConfig() (string, any, up.Upgrader) {
return ExampleConfig, s.Config, up.SimpleUpgrader(upgradeConfig)
}

func (s *SignalConnector) Init(bridge *bridgev2.Bridge) {
var err error
s.Config.displaynameTemplate, err = template.New("displayname").Parse(s.Config.DisplaynameTemplate)
Expand Down Expand Up @@ -161,10 +194,6 @@ func (s *SignalConnector) Start(ctx context.Context) error {
return s.Store.Upgrade(ctx)
}

var _ bridgev2.NetworkConnector = (*SignalConnector)(nil)
var _ bridgev2.NetworkAPI = (*SignalClient)(nil)
var _ msgconv.PortalMethods = (*msgconvPortalMethods)(nil)

func (s *SignalConnector) LoadUserLogin(ctx context.Context, login *bridgev2.UserLogin) error {
aci, err := uuid.Parse(string(login.ID))
if err != nil {
Expand Down
21 changes: 21 additions & 0 deletions pkg/connector/example-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Displayname template for Signal users.
# {{.ProfileName}} - The Signal profile name set by the user.
# {{.ContactName}} - The name for the user from your phone's contact list. This is not safe on multi-user instances.
# {{.PhoneNumber}} - The phone number of the user.
# {{.UUID}} - The UUID of the Signal user.
# {{.AboutEmoji}} - The emoji set by the user in their profile.
displayname_template: '{{or .ProfileName .PhoneNumber "Unknown user"}}'
# Should avatars from the user's contact list be used? This is not safe on multi-user instances.
use_contact_avatars: false
# Should the bridge sync ghost user info even if profile fetching fails? This is not safe on multi-user instances.
use_outdated_profiles: false
# Should the Signal user's phone number be included in the room topic in private chat portal rooms?
number_in_topic: true
# Default device name that shows up in the Signal app.
device_name: mautrix-signal
# Avatar image for the Note to Self room.
note_to_self_avatar: mxc://maunium.net/REBIVrqjZwmaWpssCZpBlmlL
# Format for generating URLs from location messages for sending to Signal.
# Google Maps: 'https://www.google.com/maps/place/%[1]s,%[2]s'
# OpenStreetMap: 'https://www.openstreetmap.org/?mlat=%[1]s&mlon=%[2]s'
location_format: 'https://www.google.com/maps/place/%[1]s,%[2]s'

0 comments on commit ecd4f05

Please sign in to comment.