Skip to content

Commit

Permalink
add support for matrix -> signal location messages
Browse files Browse the repository at this point in the history
  • Loading branch information
maltee1 committed Apr 22, 2024
1 parent 703beca commit 3adbb99
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 13 deletions.
23 changes: 12 additions & 11 deletions config/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,18 @@ type BridgeConfig struct {

PortalMessageBuffer int `yaml:"portal_message_buffer"`

PersonalFilteringSpaces bool `yaml:"personal_filtering_spaces"`
BridgeNotices bool `yaml:"bridge_notices"`
DeliveryReceipts bool `yaml:"delivery_receipts"`
MessageStatusEvents bool `yaml:"message_status_events"`
MessageErrorNotices bool `yaml:"message_error_notices"`
SyncDirectChatList bool `yaml:"sync_direct_chat_list"`
ResendBridgeInfo bool `yaml:"resend_bridge_info"`
PublicPortals bool `yaml:"public_portals"`
CaptionInMessage bool `yaml:"caption_in_message"`
FederateRooms bool `yaml:"federate_rooms"`
BridgeMatrixLeave bool `yaml:"bridge_matrix_leave"`
PersonalFilteringSpaces bool `yaml:"personal_filtering_spaces"`
BridgeNotices bool `yaml:"bridge_notices"`
DeliveryReceipts bool `yaml:"delivery_receipts"`
MessageStatusEvents bool `yaml:"message_status_events"`
MessageErrorNotices bool `yaml:"message_error_notices"`
SyncDirectChatList bool `yaml:"sync_direct_chat_list"`
ResendBridgeInfo bool `yaml:"resend_bridge_info"`
PublicPortals bool `yaml:"public_portals"`
CaptionInMessage bool `yaml:"caption_in_message"`
LocationFormat string `yaml:"location_format"`
FederateRooms bool `yaml:"federate_rooms"`
BridgeMatrixLeave bool `yaml:"bridge_matrix_leave"`

DoublePuppetConfig bridgeconfig.DoublePuppetConfig `yaml:",inline"`

Expand Down
1 change: 1 addition & 0 deletions config/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func DoUpgrade(helper *up.Helper) {
helper.Copy(up.Bool, "bridge", "resend_bridge_info")
helper.Copy(up.Bool, "bridge", "public_portals")
helper.Copy(up.Bool, "bridge", "caption_in_message")
helper.Copy(up.Str, "bridge", "location_format")
helper.Copy(up.Bool, "bridge", "federate_rooms")
helper.Copy(up.Map, "bridge", "double_puppet_server_map")
helper.Copy(up.Bool, "bridge", "double_puppet_allow_discovery")
Expand Down
4 changes: 4 additions & 0 deletions example-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ bridge:
# Send captions in the same message as images. This will send data compatible with both MSC2530.
# This is currently not supported in most clients.
caption_in_message: false
# 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]'
location_format: 'https://www.google.com/maps/place/%[1]s,%[2]s'
# Whether or not created rooms should have federation enabled.
# If false, created portal rooms will never be federated.
federate_rooms: true
Expand Down
31 changes: 29 additions & 2 deletions msgconv/from-matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ import (
"context"
"errors"
"fmt"
"strconv"
"strings"
"time"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"go.mau.fi/util/exerrors"
"go.mau.fi/util/exmime"
"go.mau.fi/util/ffmpeg"
Expand Down Expand Up @@ -109,8 +112,12 @@ func (mc *MessageConverter) ToSignal(ctx context.Context, evt *event.Event, cont
Emoji: emoji,
}
case event.MsgLocation:
// TODO implement
fallthrough
lat, lon, err := parseGeoURI(content.GeoURI)
if err != nil {
log.Err(err).Msg("Failed to pars geo URI")
}
locationString := fmt.Sprintf(mc.LocationFormat, lat, lon)
dm.Body = &locationString
default:
return nil, fmt.Errorf("%w %s", ErrUnsupportedMsgType, content.MsgType)
}
Expand Down Expand Up @@ -190,3 +197,23 @@ func (mc *MessageConverter) convertFileToSignal(ctx context.Context, evt *event.
}
return att, nil
}

func parseGeoURI(uri string) (lat, long string, err error) {
if !strings.HasPrefix(uri, "geo:") {
err = fmt.Errorf("uri doesn't have geo: prefix")
return
}
// Remove geo: prefix and anything after ;
coordinates := strings.Split(strings.TrimPrefix(uri, "geo:"), ";")[0]
splitCoordinates := strings.Split(coordinates, ",")
if len(splitCoordinates) != 2 {
err = fmt.Errorf("didn't find exactly two numbers separated by a comma")
} else if _, err = strconv.ParseFloat(splitCoordinates[0], 64); err != nil {
err = fmt.Errorf("latitude is not a number: %w", err)
} else if _, err = strconv.ParseFloat(splitCoordinates[1], 64); err != nil {
err = fmt.Errorf("longitude is not a number: %w", err)
}
lat = splitCoordinates[0]
long = splitCoordinates[1]
return
}
2 changes: 2 additions & 0 deletions msgconv/msgconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ type MessageConverter struct {
ConvertGIFToAPNG bool
MaxFileSize int64
AsyncFiles bool

LocationFormat string
}

func (mc *MessageConverter) IsPrivateChat(ctx context.Context) bool {
Expand Down
1 change: 1 addition & 0 deletions portal.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ func (br *SignalBridge) NewPortal(dbPortal *database.Portal) *Portal {
MatrixFmtParams: matrixFormatParams,
ConvertVoiceMessages: true,
MaxFileSize: br.MediaConfig.UploadSize,
LocationFormat: br.Config.Bridge.LocationFormat,
}
go portal.messageLoop()

Expand Down

0 comments on commit 3adbb99

Please sign in to comment.