2022-10-21 14:18:02 +00:00
|
|
|
// mautrix-groupme - A Matrix-GroupMe puppeting bridge.
|
|
|
|
// Copyright (C) 2022 Sumner Evans, Karmanyaah Malhotra
|
2018-08-16 12:59:18 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-08-24 16:46:14 +00:00
|
|
|
"bytes"
|
2021-02-21 05:58:50 +00:00
|
|
|
"context"
|
2020-10-05 19:38:34 +00:00
|
|
|
"errors"
|
2018-08-16 12:59:18 +00:00
|
|
|
"fmt"
|
2023-09-07 19:01:41 +00:00
|
|
|
"github.com/gabriel-vasile/mimetype"
|
2018-08-24 16:46:14 +00:00
|
|
|
"image"
|
2020-06-23 13:36:05 +00:00
|
|
|
"io/ioutil"
|
2021-03-15 18:29:06 +00:00
|
|
|
"math"
|
2018-08-24 16:46:14 +00:00
|
|
|
"net/http"
|
2021-03-07 16:46:25 +00:00
|
|
|
"net/url"
|
2021-03-15 18:29:06 +00:00
|
|
|
"strconv"
|
2018-08-18 19:57:08 +00:00
|
|
|
"strings"
|
2018-08-22 22:12:26 +00:00
|
|
|
"sync"
|
2019-05-22 13:46:18 +00:00
|
|
|
"time"
|
2018-08-24 16:46:14 +00:00
|
|
|
|
2019-11-10 19:22:11 +00:00
|
|
|
log "maunium.net/go/maulogger/v2"
|
2019-10-04 18:01:53 +00:00
|
|
|
|
2022-10-21 19:02:33 +00:00
|
|
|
"maunium.net/go/mautrix/bridge"
|
|
|
|
"maunium.net/go/mautrix/bridge/bridgeconfig"
|
2020-06-10 12:26:14 +00:00
|
|
|
"maunium.net/go/mautrix/crypto/attachment"
|
|
|
|
|
2023-09-19 03:15:46 +00:00
|
|
|
"gitea.watsonlabs.net/watsonb8/groupme-lib"
|
2019-05-15 22:59:36 +00:00
|
|
|
|
2019-01-11 19:17:31 +00:00
|
|
|
"maunium.net/go/mautrix"
|
2020-05-09 11:31:06 +00:00
|
|
|
"maunium.net/go/mautrix/appservice"
|
2020-05-08 19:32:22 +00:00
|
|
|
"maunium.net/go/mautrix/event"
|
|
|
|
"maunium.net/go/mautrix/id"
|
2019-01-11 19:17:31 +00:00
|
|
|
|
2022-10-21 14:48:03 +00:00
|
|
|
"github.com/beeper/groupme/database"
|
2022-10-21 19:02:33 +00:00
|
|
|
"github.com/beeper/groupme/groupmeext"
|
2018-08-16 12:59:18 +00:00
|
|
|
)
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
const MaxMessageAgeToCreatePortal = 5 * 60 // 5 minutes
|
2021-04-02 00:00:27 +00:00
|
|
|
const recentlyHandledLength = 100
|
|
|
|
|
|
|
|
type PortalMessage struct {
|
2021-05-01 22:22:02 +00:00
|
|
|
chat database.PortalKey
|
2021-04-02 00:00:27 +00:00
|
|
|
source *User
|
|
|
|
data *groupme.Message
|
|
|
|
timestamp uint64
|
|
|
|
}
|
|
|
|
|
2022-10-21 19:02:33 +00:00
|
|
|
type PortalMatrixMessage struct {
|
|
|
|
evt *event.Event
|
|
|
|
user *User
|
|
|
|
receivedAt time.Time
|
|
|
|
}
|
|
|
|
|
2021-04-02 00:00:27 +00:00
|
|
|
type Portal struct {
|
|
|
|
*database.Portal
|
|
|
|
|
2022-10-21 19:02:33 +00:00
|
|
|
bridge *GMBridge
|
2021-04-02 00:00:27 +00:00
|
|
|
log log.Logger
|
|
|
|
|
|
|
|
roomCreateLock sync.Mutex
|
|
|
|
|
|
|
|
recentlyHandled []string
|
|
|
|
recentlyHandledLock sync.Mutex
|
|
|
|
recentlyHandledIndex uint8
|
|
|
|
|
2022-10-21 19:02:33 +00:00
|
|
|
encryptLock sync.Mutex
|
2021-04-02 00:00:27 +00:00
|
|
|
backfilling bool
|
|
|
|
lastMessageTs uint64
|
|
|
|
|
|
|
|
privateChatBackfillInvitePuppet func()
|
|
|
|
|
2022-10-21 19:02:33 +00:00
|
|
|
messages chan PortalMessage
|
|
|
|
matrixMessages chan PortalMatrixMessage
|
2021-04-02 00:00:27 +00:00
|
|
|
|
|
|
|
hasRelaybot *bool
|
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
// Public Properties
|
|
|
|
|
|
|
|
func (portal *Portal) IsEncrypted() bool {
|
|
|
|
return portal.Encrypted
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) IsPrivateChat() bool {
|
|
|
|
return portal.Key.IsPrivate()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) IsStatusBroadcastRoom() bool {
|
|
|
|
return portal.Key.GMID == "status@broadcast"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) MainIntent() *appservice.IntentAPI {
|
|
|
|
if portal.IsPrivateChat() {
|
|
|
|
return portal.bridge.GetPuppetByGMID(portal.Key.GMID).DefaultIntent()
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
2023-09-07 19:01:41 +00:00
|
|
|
return portal.bridge.Bot
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
// Private Properties
|
|
|
|
|
2021-04-02 00:00:27 +00:00
|
|
|
func (portal *Portal) isRecentlyHandled(id groupme.ID) bool {
|
|
|
|
idStr := id.String()
|
|
|
|
for i := recentlyHandledLength - 1; i >= 0; i-- {
|
|
|
|
if portal.recentlyHandled[i] == idStr {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) isDuplicate(id groupme.ID) bool {
|
2022-10-21 19:02:33 +00:00
|
|
|
msg := portal.bridge.DB.Message.GetByGMID(portal.Key, id)
|
2021-04-02 00:00:27 +00:00
|
|
|
if msg != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (portal *Portal) wasMessageSent(sender *User, id string) bool {
|
|
|
|
return true
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
// Public Methods
|
|
|
|
|
|
|
|
func (portal *Portal) SyncDM(user *User, dm *groupme.Chat) {
|
|
|
|
if !portal.IsPrivateChat() {
|
|
|
|
return
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
2023-09-07 19:01:41 +00:00
|
|
|
portal.log.Infoln("Syncing portal for", user.MXID)
|
2021-04-02 00:00:27 +00:00
|
|
|
|
2023-09-21 16:58:36 +00:00
|
|
|
err := user.Conn.SubscribeToDM(context.TODO(), dm.LastMessage.ConversationID, user.Token)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorln("Subscribing failed, live metadata updates won't work", err)
|
|
|
|
}
|
2021-04-02 00:00:27 +00:00
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
if len(portal.MXID) == 0 {
|
2023-09-08 18:00:18 +00:00
|
|
|
puppet := portal.bridge.GetPuppetByGMID(portal.Key.GMID)
|
|
|
|
meta, err := portal.bridge.StateStore.TryGetMember("", puppet.MXID)
|
|
|
|
if err {
|
|
|
|
println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
|
|
|
|
user.log.Warn("Cannot find user information")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if portal.bridge.Config.Bridge.PrivateChatPortalMeta {
|
|
|
|
portal.Name = meta.Displayname
|
|
|
|
portal.AvatarURL = meta.AvatarURL.ParseOrIgnore()
|
|
|
|
portal.Avatar = meta.AvatarURL.ParseOrIgnore().String()
|
2023-09-07 19:01:41 +00:00
|
|
|
} else {
|
|
|
|
portal.Name = ""
|
|
|
|
}
|
|
|
|
portal.Topic = "GroupMe private chat"
|
2023-09-08 18:00:18 +00:00
|
|
|
e := portal.createMatrixRoom(user)
|
|
|
|
if e != nil {
|
2023-09-07 19:01:41 +00:00
|
|
|
portal.log.Errorln("Failed to create portal room:", err)
|
|
|
|
return
|
2021-05-01 22:22:02 +00:00
|
|
|
}
|
2021-04-02 00:00:27 +00:00
|
|
|
|
2023-09-08 18:00:18 +00:00
|
|
|
customPuppet := portal.bridge.GetPuppetByGMID(user.GMID)
|
2023-09-07 19:01:41 +00:00
|
|
|
if customPuppet != nil && customPuppet.CustomIntent() != nil {
|
|
|
|
_ = customPuppet.CustomIntent().EnsureJoined(portal.MXID)
|
|
|
|
}
|
2021-04-02 00:00:27 +00:00
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
if portal.bridge.Config.Bridge.Encryption.Default {
|
2023-09-08 18:00:18 +00:00
|
|
|
e = portal.bridge.Bot.EnsureJoined(portal.MXID)
|
|
|
|
if e != nil {
|
2023-09-07 19:01:41 +00:00
|
|
|
portal.log.Errorln("Failed to join created portal with bridge bot for e2be:", err)
|
|
|
|
}
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
2023-09-07 19:01:41 +00:00
|
|
|
user.UpdateDirectChats(map[id.UserID][]id.RoomID{puppet.MXID: {portal.MXID}})
|
|
|
|
|
|
|
|
} else {
|
|
|
|
portal.ensureUserInvited(user)
|
|
|
|
}
|
|
|
|
|
|
|
|
update := false
|
|
|
|
update = portal.UpdateAvatar(user, dm.OtherUser.AvatarURL, false) || update
|
|
|
|
|
|
|
|
if update {
|
|
|
|
portal.Update(nil)
|
|
|
|
portal.UpdateBridgeInfo()
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (portal *Portal) SyncGroup(user *User, group *groupme.Group) {
|
|
|
|
if portal.IsPrivateChat() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
portal.log.Infoln("Syncing portal for", user.MXID)
|
|
|
|
|
2023-09-21 16:58:36 +00:00
|
|
|
err := user.Conn.SubscribeToGroup(context.TODO(), portal.Key.GMID, user.Token)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorln("Subscribing failed, live metadata updates won't work", err)
|
|
|
|
}
|
2021-04-02 00:00:27 +00:00
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
if len(portal.MXID) == 0 {
|
|
|
|
portal.Name = group.Name
|
|
|
|
portal.Topic = group.Description
|
|
|
|
|
|
|
|
err := portal.createMatrixRoom(user)
|
2021-04-02 00:00:27 +00:00
|
|
|
if err != nil {
|
2023-09-07 19:01:41 +00:00
|
|
|
portal.log.Errorln("Failed to create portal room:", err)
|
|
|
|
return
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
2023-09-07 19:01:41 +00:00
|
|
|
portal.syncParticipants(group)
|
|
|
|
} else {
|
|
|
|
portal.ensureUserInvited(user)
|
|
|
|
}
|
2021-04-02 00:00:27 +00:00
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
update := false
|
|
|
|
update = portal.updateMetadata(user) || update
|
|
|
|
update = portal.UpdateAvatar(user, group.ImageURL, false) || update
|
|
|
|
|
|
|
|
if update {
|
|
|
|
portal.Update(nil)
|
|
|
|
portal.UpdateBridgeInfo()
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (portal *Portal) MarkEncrypted() {
|
|
|
|
portal.Encrypted = true
|
|
|
|
portal.Update(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) ReceiveMatrixEvent(user bridge.User, evt *event.Event) {
|
|
|
|
if user.GetPermissionLevel() >= bridgeconfig.PermissionLevelUser {
|
|
|
|
portal.matrixMessages <- PortalMatrixMessage{user: user.(*User), evt: evt, receivedAt: time.Now()}
|
|
|
|
}
|
2022-10-21 22:04:21 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 00:00:27 +00:00
|
|
|
func (portal *Portal) UpdateAvatar(user *User, avatar string, updateInfo bool) bool {
|
|
|
|
// if len(avatar) == 0 {
|
|
|
|
// var err error
|
|
|
|
// avatar, err = user.Conn.GetProfilePicThumb(portal.Key.JID)
|
|
|
|
// if err != nil {
|
|
|
|
// portal.log.Errorln(err)
|
|
|
|
// return false
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//TODO: duplicated code from puppet.UpdateAvatar
|
|
|
|
if len(avatar) == 0 {
|
|
|
|
if len(portal.Avatar) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
err := portal.MainIntent().SetAvatarURL(id.ContentURI{})
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Warnln("Failed to remove avatar:", err)
|
|
|
|
}
|
2022-10-21 14:35:03 +00:00
|
|
|
portal.AvatarURL = id.ContentURI{}
|
2021-04-02 00:00:27 +00:00
|
|
|
portal.Avatar = avatar
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if portal.Avatar == avatar {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO check its actually groupme?
|
|
|
|
response, err := http.Get(avatar + ".large")
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Warnln("Failed to download avatar:", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
image, err := ioutil.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Warnln("Failed to read downloaded avatar:", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
mime := response.Header.Get("Content-Type")
|
|
|
|
if len(mime) == 0 {
|
|
|
|
mime = http.DetectContentType(image)
|
|
|
|
}
|
|
|
|
resp, err := portal.MainIntent().UploadBytes(image, mime)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Warnln("Failed to upload avatar:", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-10-21 14:35:03 +00:00
|
|
|
portal.AvatarURL = resp.ContentURI
|
2021-04-02 00:00:27 +00:00
|
|
|
if len(portal.MXID) > 0 {
|
|
|
|
_, err = portal.MainIntent().SetRoomAvatar(portal.MXID, resp.ContentURI)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Warnln("Failed to set room topic:", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
portal.Avatar = avatar
|
|
|
|
if updateInfo {
|
|
|
|
portal.UpdateBridgeInfo()
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-10-21 19:02:33 +00:00
|
|
|
func (portal *Portal) UpdateName(name string, setBy groupme.ID, updateInfo bool) bool {
|
2021-04-02 00:00:27 +00:00
|
|
|
if portal.Name != name {
|
|
|
|
intent := portal.MainIntent()
|
|
|
|
if len(setBy) > 0 {
|
2022-10-21 19:02:33 +00:00
|
|
|
intent = portal.bridge.GetPuppetByGMID(setBy).IntentFor(portal)
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
|
|
|
_, err := intent.SetRoomName(portal.MXID, name)
|
|
|
|
if err == nil {
|
|
|
|
portal.Name = name
|
|
|
|
if updateInfo {
|
|
|
|
portal.UpdateBridgeInfo()
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
portal.log.Warnln("Failed to set room name:", err)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-10-21 19:02:33 +00:00
|
|
|
func (portal *Portal) UpdateTopic(topic string, setBy groupme.ID, updateInfo bool) bool {
|
2021-04-02 00:00:27 +00:00
|
|
|
if portal.Topic != topic {
|
|
|
|
intent := portal.MainIntent()
|
|
|
|
if len(setBy) > 0 {
|
2022-10-21 19:02:33 +00:00
|
|
|
intent = portal.bridge.GetPuppetByGMID(setBy).IntentFor(portal)
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
|
|
|
_, err := intent.SetRoomTopic(portal.MXID, topic)
|
|
|
|
if err == nil {
|
|
|
|
portal.Topic = topic
|
|
|
|
if updateInfo {
|
|
|
|
portal.UpdateBridgeInfo()
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
portal.log.Warnln("Failed to set room topic:", err)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (portal *Portal) updateMetadata(user *User) bool {
|
2021-04-02 00:00:27 +00:00
|
|
|
if portal.IsPrivateChat() {
|
|
|
|
return false
|
|
|
|
}
|
2023-09-20 21:06:04 +00:00
|
|
|
group, err := user.Client.ShowGroup(context.TODO(), groupme.ID(strings.Replace(portal.Key.GMID.String(),
|
|
|
|
groupmeext.NewUserSuffix, "", 1)), user.Token)
|
2021-04-02 00:00:27 +00:00
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorln(err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// if metadata.Status != 0 {
|
|
|
|
// 401: access denied
|
|
|
|
// 404: group does (no longer) exist
|
|
|
|
// 500: ??? happens with status@broadcast
|
|
|
|
|
|
|
|
// TODO: update the room, e.g. change priority level
|
|
|
|
// to send messages to moderator
|
|
|
|
//return false
|
|
|
|
// }
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
portal.syncParticipants(group)
|
2021-04-02 00:00:27 +00:00
|
|
|
update := false
|
|
|
|
update = portal.UpdateName(group.Name, "", false) || update
|
|
|
|
update = portal.UpdateTopic(group.Description, "", false) || update
|
|
|
|
|
|
|
|
// portal.RestrictMessageSending(metadata.Announce)
|
|
|
|
|
|
|
|
return update
|
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (portal *Portal) UpdateBridgeInfo() {
|
|
|
|
if len(portal.MXID) == 0 {
|
|
|
|
portal.log.Debugln("Not updating bridge info: no Matrix room created")
|
2021-04-02 00:00:27 +00:00
|
|
|
return
|
|
|
|
}
|
2023-09-07 19:01:41 +00:00
|
|
|
portal.log.Debugln("Updating bridge info...")
|
|
|
|
stateKey, content := portal.getBridgeInfo()
|
|
|
|
_, err := portal.MainIntent().SendStateEvent(portal.MXID, event.StateBridge, stateKey, content)
|
2021-04-02 00:00:27 +00:00
|
|
|
if err != nil {
|
2023-09-07 19:01:41 +00:00
|
|
|
portal.log.Warnln("Failed to update m.bridge:", err)
|
|
|
|
}
|
|
|
|
// TODO remove this once https://github.com/matrix-org/matrix-doc/pull/2346 is in spec
|
|
|
|
_, err = portal.MainIntent().SendStateEvent(portal.MXID, event.StateHalfShotBridge, stateKey, content)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Warnln("Failed to update uk.half-shot.bridge:", err)
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (portal *Portal) GetEncryptionEventContent() (evt *event.EncryptionEventContent) {
|
|
|
|
evt = &event.EncryptionEventContent{Algorithm: id.AlgorithmMegolmV1}
|
|
|
|
if rot := portal.bridge.Config.Bridge.Encryption.Rotation; rot.EnableCustom {
|
|
|
|
evt.RotationPeriodMillis = rot.Milliseconds
|
|
|
|
evt.RotationPeriodMessages = rot.Messages
|
|
|
|
}
|
|
|
|
return
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (portal *Portal) SetReply(content *event.MessageEventContent, msgID groupme.ID) {
|
|
|
|
if len(msgID) == 0 {
|
|
|
|
return
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
2023-09-07 19:01:41 +00:00
|
|
|
message := portal.bridge.DB.Message.GetByGMID(portal.Key, msgID)
|
|
|
|
if message != nil {
|
|
|
|
evt, err := portal.MainIntent().GetEvent(portal.MXID, message.MXID)
|
2021-04-02 00:00:27 +00:00
|
|
|
if err != nil {
|
2023-09-07 19:01:41 +00:00
|
|
|
portal.log.Warnln("Failed to get reply target:", err)
|
2021-04-02 00:00:27 +00:00
|
|
|
return
|
|
|
|
}
|
2023-09-07 19:01:41 +00:00
|
|
|
if evt.Type == event.EventEncrypted {
|
|
|
|
_ = evt.Content.ParseRaw(evt.Type)
|
|
|
|
decryptedEvt, err := portal.bridge.Crypto.Decrypt(evt)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Warnln("Failed to decrypt reply target:", err)
|
|
|
|
} else {
|
|
|
|
evt = decryptedEvt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ = evt.Content.ParseRaw(evt.Type)
|
|
|
|
content.SetReply(evt)
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
2023-09-07 19:01:41 +00:00
|
|
|
return
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) RestrictMessageSending(restrict bool) {
|
|
|
|
levels, err := portal.MainIntent().PowerLevels(portal.MXID)
|
|
|
|
if err != nil {
|
2023-09-07 19:01:41 +00:00
|
|
|
levels = portal.getBasePowerLevels()
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
newLevel := 0
|
|
|
|
if restrict {
|
|
|
|
newLevel = 50
|
|
|
|
}
|
|
|
|
|
|
|
|
if levels.EventsDefault == newLevel {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
levels.EventsDefault = newLevel
|
|
|
|
_, err = portal.MainIntent().SetPowerLevels(portal.MXID, levels)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorln("Failed to change power levels:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) RestrictMetadataChanges(restrict bool) {
|
|
|
|
levels, err := portal.MainIntent().PowerLevels(portal.MXID)
|
|
|
|
if err != nil {
|
2023-09-07 19:01:41 +00:00
|
|
|
levels = portal.getBasePowerLevels()
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
|
|
|
newLevel := 0
|
|
|
|
if restrict {
|
|
|
|
newLevel = 50
|
|
|
|
}
|
|
|
|
changed := false
|
|
|
|
changed = levels.EnsureEventLevel(event.StateRoomName, newLevel) || changed
|
|
|
|
changed = levels.EnsureEventLevel(event.StateRoomAvatar, newLevel) || changed
|
|
|
|
changed = levels.EnsureEventLevel(event.StateTopic, newLevel) || changed
|
|
|
|
if changed {
|
|
|
|
_, err = portal.MainIntent().SetPowerLevels(portal.MXID, levels)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorln("Failed to change power levels:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (portal *Portal) Delete() {
|
|
|
|
portal.Portal.Delete()
|
|
|
|
portal.bridge.portalsLock.Lock()
|
|
|
|
delete(portal.bridge.portalsByGMID, portal.Key)
|
|
|
|
if len(portal.MXID) > 0 {
|
|
|
|
delete(portal.bridge.portalsByMXID, portal.MXID)
|
2022-10-21 19:02:33 +00:00
|
|
|
}
|
2023-09-07 19:01:41 +00:00
|
|
|
portal.bridge.portalsLock.Unlock()
|
2022-10-21 19:02:33 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
// Handlers
|
2021-04-02 00:00:27 +00:00
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (portal *Portal) HandleTextMessage(source *User, message *groupme.Message) {
|
|
|
|
intent := portal.startHandling(source, message)
|
|
|
|
if intent == nil {
|
|
|
|
return
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
sendText := true
|
|
|
|
var sentID id.EventID
|
|
|
|
for _, a := range message.Attachments {
|
|
|
|
msg, text, err := portal.handleAttachment(intent, a, source, message)
|
2021-04-02 00:00:27 +00:00
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorfln("Failed to handle message %s: %v", "TODOID", err)
|
|
|
|
portal.sendMediaBridgeFailure(source, intent, *message, err)
|
|
|
|
continue
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
2023-09-07 19:01:41 +00:00
|
|
|
if msg == nil {
|
|
|
|
continue
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
2023-09-07 19:01:41 +00:00
|
|
|
resp, err := portal.sendMessage(intent, event.EventMessage, msg, nil, message.CreatedAt.ToTime().Unix())
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorfln("Failed to handle message %s: %v", "TODOID", err)
|
|
|
|
portal.sendMediaBridgeFailure(source, intent, *message, err)
|
|
|
|
continue
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
2023-09-07 19:01:41 +00:00
|
|
|
sentID = resp.EventID
|
2021-04-02 00:00:27 +00:00
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
sendText = sendText && text
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
// portal.SetReply(content, message.ContextInfo)
|
|
|
|
//TODO: mentions
|
|
|
|
content := &event.MessageEventContent{
|
|
|
|
Body: message.Text,
|
|
|
|
MsgType: event.MsgText,
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
_, _ = intent.UserTyping(portal.MXID, false, 0)
|
|
|
|
if sendText {
|
|
|
|
resp, err := portal.sendMessage(intent, event.EventMessage, content, nil, message.CreatedAt.ToTime().Unix())
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorfln("Failed to handle message %s: %v", message.ID, err)
|
|
|
|
return
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
2023-09-07 19:01:41 +00:00
|
|
|
sentID = resp.EventID
|
2021-04-02 00:00:27 +00:00
|
|
|
|
|
|
|
}
|
2023-09-07 19:01:41 +00:00
|
|
|
portal.finishHandling(source, message, sentID)
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (portal *Portal) HandleMatrixMessage(sender *User, evt *event.Event) {
|
|
|
|
portal.log.Debugfln("Received event %s", evt.ID)
|
|
|
|
info, sender := portal.convertMatrixMessage(sender, evt)
|
|
|
|
if info == nil {
|
2021-04-02 00:00:27 +00:00
|
|
|
return
|
|
|
|
}
|
2023-09-07 19:01:41 +00:00
|
|
|
for _, i := range info {
|
|
|
|
portal.log.Debugln("Sending event", evt.ID, "to GroupMe", info[0].ID)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
i, err = portal.sendRaw(sender, evt, info[0], -1) //TODO deal with multiple messages for longer messages
|
2021-04-02 00:00:27 +00:00
|
|
|
if err != nil {
|
2023-09-07 19:01:41 +00:00
|
|
|
portal.log.Warnln("Unable to handle message from Matrix", evt.ID)
|
|
|
|
//TODO handle deleted room and such
|
|
|
|
} else {
|
|
|
|
portal.markHandled(sender, i, evt.ID)
|
|
|
|
}
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (portal *Portal) HandleMatrixRedaction(sender *User, evt *event.Event) {
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (portal *Portal) HandleMatrixLeave(sender *User) {
|
|
|
|
if portal.IsPrivateChat() {
|
|
|
|
portal.log.Debugln("User left private chat portal, cleaning up and deleting...")
|
|
|
|
portal.Delete()
|
|
|
|
portal.cleanup(false)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
// TODO should we somehow deduplicate this call if this leave was sent by the bridge?
|
2023-09-20 21:06:04 +00:00
|
|
|
err := sender.Client.RemoveFromGroup(sender.GMID, portal.Key.GMID, sender.Token)
|
2023-09-07 19:01:41 +00:00
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorfln("Failed to leave group as %s: %v", sender.MXID, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
portal.cleanupIfEmpty()
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (portal *Portal) HandleMatrixKick(sender *User, evt *event.Event) {
|
|
|
|
portal.log.Debug("HandleMatrixKick")
|
2022-10-21 22:04:21 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (portal *Portal) HandleMatrixInvite(sender *User, evt *event.Event) {
|
|
|
|
portal.log.Debug("HandleMatrixInvite")
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (portal *Portal) handleMessageLoop() {
|
2023-09-11 21:44:58 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg := <-portal.messages:
|
|
|
|
portal.handleMessageLoopItem(msg)
|
|
|
|
case msg := <-portal.matrixMessages:
|
|
|
|
portal.HandleMatrixMessage(msg.user, msg.evt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) handleMessageLoopItem(msg PortalMessage) {
|
|
|
|
if len(portal.MXID) == 0 {
|
|
|
|
if msg.timestamp+MaxMessageAgeToCreatePortal < uint64(time.Now().Unix()) {
|
|
|
|
portal.log.Debugln("Not creating portal room for incoming message: message is too old")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
portal.log.Debugln("Creating Matrix room from incoming message")
|
|
|
|
err := portal.createMatrixRoom(msg.source)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorln("Failed to create portal room:", err)
|
|
|
|
return
|
2023-09-07 19:01:41 +00:00
|
|
|
}
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
2023-09-11 21:44:58 +00:00
|
|
|
portal.handleMessage(msg)
|
2023-09-07 19:01:41 +00:00
|
|
|
}
|
2022-10-21 19:02:33 +00:00
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (portal *Portal) handleMessage(msg PortalMessage) {
|
|
|
|
if len(portal.MXID) == 0 {
|
|
|
|
portal.log.Warnln("handleMessage called even though portal.MXID is empty")
|
|
|
|
return
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
2023-09-07 19:01:41 +00:00
|
|
|
portal.HandleTextMessage(msg.source, msg.data)
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) handleAttachment(intent *appservice.IntentAPI, attachment *groupme.Attachment, source *User, message *groupme.Message) (msg *event.MessageEventContent, sendText bool, err error) {
|
|
|
|
sendText = true
|
|
|
|
switch attachment.Type {
|
|
|
|
case "image":
|
2022-10-21 19:02:33 +00:00
|
|
|
imgData, mime, err := groupmeext.DownloadImage(attachment.URL)
|
2021-04-02 00:00:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, true, fmt.Errorf("failed to load media info: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var width, height int
|
|
|
|
if strings.HasPrefix(mime, "image/") {
|
|
|
|
cfg, _, _ := image.DecodeConfig(bytes.NewReader(*imgData))
|
|
|
|
width, height = cfg.Width, cfg.Height
|
|
|
|
}
|
|
|
|
data, uploadMimeType, file := portal.encryptFile(*imgData, mime)
|
|
|
|
|
2022-10-21 22:04:21 +00:00
|
|
|
uploaded, err := intent.UploadBytes(data, uploadMimeType)
|
2021-04-02 00:00:27 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, mautrix.MTooLarge) {
|
|
|
|
err = errors.New("homeserver rejected too large file")
|
|
|
|
} else if httpErr := err.(mautrix.HTTPError); httpErr.IsStatus(413) {
|
|
|
|
err = errors.New("proxy rejected too large file")
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("failed to upload media: %w", err)
|
|
|
|
}
|
|
|
|
return nil, true, err
|
|
|
|
}
|
|
|
|
attachmentUrl, _ := url.Parse(attachment.URL)
|
|
|
|
urlParts := strings.Split(attachmentUrl.Path, ".")
|
|
|
|
var fname1, fname2 string
|
|
|
|
if len(urlParts) == 2 {
|
|
|
|
fname1, fname2 = urlParts[1], urlParts[0]
|
|
|
|
} else if len(urlParts) > 2 {
|
|
|
|
fname1, fname2 = urlParts[2], urlParts[1]
|
2022-10-21 19:02:33 +00:00
|
|
|
} //TODO abstract groupme url parsing in groupmeext
|
2021-04-02 00:00:27 +00:00
|
|
|
fname := fmt.Sprintf("%s.%s", fname1, fname2)
|
|
|
|
|
|
|
|
content := &event.MessageEventContent{
|
|
|
|
Body: fname,
|
|
|
|
File: file,
|
|
|
|
Info: &event.FileInfo{
|
|
|
|
Size: len(data),
|
|
|
|
MimeType: mime,
|
|
|
|
Width: width,
|
|
|
|
Height: height,
|
|
|
|
//Duration: int(msg.length),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if content.File != nil {
|
|
|
|
content.File.URL = uploaded.ContentURI.CUString()
|
|
|
|
} else {
|
|
|
|
content.URL = uploaded.ContentURI.CUString()
|
|
|
|
}
|
|
|
|
//TODO thumbnail since groupme supports it anyway
|
|
|
|
content.MsgType = event.MsgImage
|
|
|
|
|
|
|
|
return content, true, nil
|
|
|
|
case "video":
|
2022-10-21 19:02:33 +00:00
|
|
|
vidContents, mime := groupmeext.DownloadVideo(attachment.VideoPreviewURL, attachment.URL, source.Token)
|
2021-04-02 00:00:27 +00:00
|
|
|
if mime == "" {
|
2023-09-19 03:15:46 +00:00
|
|
|
mime, _ = mimetype.Detect(vidContents)
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data, uploadMimeType, file := portal.encryptFile(vidContents, mime)
|
2022-10-21 22:04:21 +00:00
|
|
|
uploaded, err := intent.UploadBytes(data, uploadMimeType)
|
2021-04-02 00:00:27 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, mautrix.MTooLarge) {
|
|
|
|
err = errors.New("homeserver rejected too large file")
|
|
|
|
} else if httpErr := err.(mautrix.HTTPError); httpErr.IsStatus(413) {
|
|
|
|
err = errors.New("proxy rejected too large file")
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("failed to upload media: %w", err)
|
|
|
|
}
|
|
|
|
return nil, true, err
|
|
|
|
}
|
|
|
|
|
|
|
|
text := strings.Split(attachment.URL, "/")
|
|
|
|
content := &event.MessageEventContent{
|
|
|
|
Body: text[len(text)-1],
|
|
|
|
File: file,
|
|
|
|
Info: &event.FileInfo{
|
|
|
|
Size: len(data),
|
|
|
|
MimeType: mime,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if content.File != nil {
|
|
|
|
content.File.URL = uploaded.ContentURI.CUString()
|
|
|
|
} else {
|
|
|
|
content.URL = uploaded.ContentURI.CUString()
|
|
|
|
}
|
|
|
|
content.MsgType = event.MsgVideo
|
|
|
|
|
|
|
|
message.Text = strings.Replace(message.Text, attachment.URL, "", 1)
|
|
|
|
return content, true, nil
|
|
|
|
case "file":
|
2022-10-21 19:02:33 +00:00
|
|
|
fileData, fname, fmime := groupmeext.DownloadFile(portal.Key.GMID, attachment.FileID, source.Token)
|
2021-04-02 00:00:27 +00:00
|
|
|
if fmime == "" {
|
2023-09-19 03:15:46 +00:00
|
|
|
fmime, _ = mimetype.Detect(fileData)
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
|
|
|
data, uploadMimeType, file := portal.encryptFile(fileData, fmime)
|
|
|
|
|
2022-10-21 22:04:21 +00:00
|
|
|
uploaded, err := intent.UploadBytes(data, uploadMimeType)
|
2021-04-02 00:00:27 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, mautrix.MTooLarge) {
|
|
|
|
err = errors.New("homeserver rejected too large file")
|
|
|
|
} else if httpErr := err.(mautrix.HTTPError); httpErr.IsStatus(413) {
|
|
|
|
err = errors.New("proxy rejected too large file")
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("failed to upload media: %w", err)
|
|
|
|
}
|
|
|
|
return nil, true, err
|
|
|
|
}
|
|
|
|
|
|
|
|
content := &event.MessageEventContent{
|
|
|
|
Body: fname,
|
|
|
|
File: file,
|
|
|
|
Info: &event.FileInfo{
|
|
|
|
Size: len(data),
|
|
|
|
MimeType: fmime,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if content.File != nil {
|
|
|
|
content.File.URL = uploaded.ContentURI.CUString()
|
|
|
|
} else {
|
|
|
|
content.URL = uploaded.ContentURI.CUString()
|
|
|
|
}
|
|
|
|
//TODO thumbnail since groupme supports it anyway
|
|
|
|
if strings.HasPrefix(fmime, "image") {
|
|
|
|
content.MsgType = event.MsgImage
|
|
|
|
} else if strings.HasPrefix(fmime, "video") {
|
|
|
|
content.MsgType = event.MsgVideo
|
|
|
|
} else {
|
|
|
|
content.MsgType = event.MsgFile
|
|
|
|
}
|
|
|
|
|
|
|
|
return content, false, nil
|
|
|
|
case "location":
|
|
|
|
name := attachment.Name
|
|
|
|
lat, _ := strconv.ParseFloat(attachment.Latitude, 64)
|
|
|
|
lng, _ := strconv.ParseFloat(attachment.Longitude, 64)
|
|
|
|
latChar := 'N'
|
|
|
|
if lat < 0 {
|
|
|
|
latChar = 'S'
|
|
|
|
}
|
|
|
|
longChar := 'E'
|
|
|
|
if lng < 0 {
|
|
|
|
longChar = 'W'
|
|
|
|
}
|
|
|
|
formattedLoc := fmt.Sprintf("%.4f° %c %.4f° %c", math.Abs(lat), latChar, math.Abs(lng), longChar)
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
content := &event.MessageEventContent{
|
|
|
|
MsgType: event.MsgLocation,
|
|
|
|
Body: fmt.Sprintf("Location: %s\n%s", name, formattedLoc), //TODO link and stuff
|
|
|
|
GeoURI: fmt.Sprintf("geo:%.5f,%.5f", lat, lng),
|
|
|
|
}
|
|
|
|
|
|
|
|
return content, false, nil
|
|
|
|
case "reply":
|
|
|
|
fmt.Printf("%+v\n", attachment)
|
|
|
|
content := &event.MessageEventContent{
|
|
|
|
Body: message.Text,
|
|
|
|
MsgType: event.MsgText,
|
|
|
|
}
|
|
|
|
portal.SetReply(content, attachment.ReplyID)
|
|
|
|
return content, false, nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
portal.log.Warnln("Unable to handle groupme attachment type", attachment.Type)
|
|
|
|
return nil, true, fmt.Errorf("Unable to handle groupme attachment type %s", attachment.Type)
|
|
|
|
}
|
|
|
|
// return nil, true, errors.New("Unknown type")
|
|
|
|
}
|
|
|
|
|
2023-09-12 02:58:26 +00:00
|
|
|
// Private Methods
|
2023-09-07 19:01:41 +00:00
|
|
|
|
|
|
|
func (portal *Portal) createMatrixRoom(user *User) error {
|
|
|
|
portal.roomCreateLock.Lock()
|
|
|
|
defer portal.roomCreateLock.Unlock()
|
|
|
|
if len(portal.MXID) > 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
intent := portal.MainIntent()
|
|
|
|
if err := intent.EnsureRegistered(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
portal.log.Infoln("Creating Matrix room. Info source:", user.MXID)
|
|
|
|
|
|
|
|
bridgeInfoStateKey, bridgeInfo := portal.getBridgeInfo()
|
|
|
|
|
|
|
|
initialState := []*event.Event{{
|
|
|
|
Type: event.StatePowerLevels,
|
|
|
|
Content: event.Content{
|
|
|
|
Parsed: portal.getBasePowerLevels(),
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
Type: event.StateBridge,
|
|
|
|
Content: event.Content{Parsed: bridgeInfo},
|
|
|
|
StateKey: &bridgeInfoStateKey,
|
|
|
|
}, {
|
|
|
|
// TODO remove this once https://github.com/matrix-org/matrix-doc/pull/2346 is in spec
|
|
|
|
Type: event.StateHalfShotBridge,
|
|
|
|
Content: event.Content{Parsed: bridgeInfo},
|
|
|
|
StateKey: &bridgeInfoStateKey,
|
2023-09-15 21:49:25 +00:00
|
|
|
}, {
|
2023-09-15 03:35:20 +00:00
|
|
|
Type: event.StateSpaceParent,
|
2023-09-15 21:49:25 +00:00
|
|
|
StateKey: &user.SpaceId,
|
2023-09-15 03:35:20 +00:00
|
|
|
Content: event.Content{Parsed: &event.SpaceParentEventContent{
|
|
|
|
Via: []string{portal.bridge.AS.HomeserverDomain},
|
|
|
|
Canonical: true,
|
|
|
|
}},
|
2023-09-15 21:49:25 +00:00
|
|
|
}, {
|
2023-09-15 03:35:20 +00:00
|
|
|
Type: event.StateJoinRules,
|
|
|
|
Content: event.Content{Parsed: &event.JoinRulesEventContent{
|
|
|
|
JoinRule: event.JoinRuleRestricted,
|
|
|
|
Allow: []event.JoinRuleAllow{{
|
|
|
|
RoomID: id.RoomID(user.SpaceId),
|
|
|
|
Type: event.JoinRuleAllowRoomMembership,
|
|
|
|
}},
|
|
|
|
}},
|
2023-09-15 21:49:25 +00:00
|
|
|
}}
|
|
|
|
|
|
|
|
//initialState = append(initialState, event.Event)
|
2023-09-07 19:01:41 +00:00
|
|
|
if !portal.AvatarURL.IsEmpty() {
|
|
|
|
initialState = append(initialState, &event.Event{
|
|
|
|
Type: event.StateRoomAvatar,
|
|
|
|
Content: event.Content{
|
|
|
|
Parsed: event.RoomAvatarEventContent{URL: portal.AvatarURL},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
invite := []id.UserID{user.MXID}
|
|
|
|
|
2023-09-08 18:00:18 +00:00
|
|
|
// TODO only do this if not double puppeting
|
|
|
|
userPuppet := portal.bridge.GetPuppetByGMID(portal.Key.Receiver)
|
|
|
|
invite = append(invite, userPuppet.MXID)
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
if portal.bridge.Config.Bridge.Encryption.Default {
|
|
|
|
initialState = append(initialState, &event.Event{
|
|
|
|
Type: event.StateEncryption,
|
|
|
|
Content: event.Content{
|
|
|
|
Parsed: event.EncryptionEventContent{Algorithm: id.AlgorithmMegolmV1},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
portal.Encrypted = true
|
|
|
|
if portal.IsPrivateChat() {
|
|
|
|
invite = append(invite, portal.bridge.Bot.UserID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := intent.CreateRoom(&mautrix.ReqCreateRoom{
|
|
|
|
Visibility: "private",
|
|
|
|
Name: portal.Name,
|
|
|
|
Topic: portal.Topic,
|
|
|
|
Invite: invite,
|
|
|
|
Preset: "private_chat",
|
|
|
|
IsDirect: portal.IsPrivateChat(),
|
|
|
|
InitialState: initialState,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if len(resp.RoomID) == 0 {
|
|
|
|
return errors.New("Empty room ID")
|
|
|
|
}
|
|
|
|
portal.MXID = resp.RoomID
|
|
|
|
portal.Update(nil)
|
|
|
|
portal.bridge.portalsLock.Lock()
|
|
|
|
portal.bridge.portalsByMXID[portal.MXID] = portal
|
|
|
|
portal.bridge.portalsLock.Unlock()
|
|
|
|
|
|
|
|
// We set the memberships beforehand to make sure the encryption key exchange in initial backfill knows the users are here.
|
|
|
|
for _, user := range invite {
|
|
|
|
portal.bridge.StateStore.SetMembership(portal.MXID, user, event.MembershipInvite)
|
|
|
|
}
|
|
|
|
|
2023-09-15 03:35:20 +00:00
|
|
|
user.addPortalToSpace(portal)
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) markHandled(source *User, message *groupme.Message, mxid id.EventID) {
|
|
|
|
msg := portal.bridge.DB.Message.New()
|
|
|
|
msg.Chat = portal.Key
|
|
|
|
msg.GMID = message.ID
|
|
|
|
msg.MXID = mxid
|
|
|
|
msg.Timestamp = message.CreatedAt.ToTime()
|
|
|
|
if message.UserID == source.GMID {
|
|
|
|
msg.Sender = source.GMID
|
|
|
|
} else if portal.IsPrivateChat() {
|
|
|
|
msg.Sender = portal.Key.GMID
|
|
|
|
} else {
|
|
|
|
msg.Sender = message.SenderID
|
|
|
|
}
|
2023-09-11 22:32:11 +00:00
|
|
|
msg.Insert()
|
2023-09-07 19:01:41 +00:00
|
|
|
|
|
|
|
portal.recentlyHandledLock.Lock()
|
|
|
|
portal.recentlyHandled[0] = "" //FIFO queue being implemented here //TODO: is this efficent
|
|
|
|
portal.recentlyHandled = portal.recentlyHandled[1:]
|
|
|
|
portal.recentlyHandled = append(portal.recentlyHandled, message.ID.String())
|
|
|
|
portal.recentlyHandledLock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) getMessageIntent(user *User, info *groupme.Message) *appservice.IntentAPI {
|
|
|
|
if portal.IsPrivateChat() {
|
|
|
|
if info.UserID == user.GetGMID() { //from me
|
2023-09-12 02:58:26 +00:00
|
|
|
puppet := portal.bridge.GetPuppetByGMID(user.GMID)
|
|
|
|
if len(puppet.CustomMXID) > 0 {
|
|
|
|
return puppet.customIntent
|
|
|
|
}
|
|
|
|
return puppet.DefaultIntent()
|
2023-09-07 19:01:41 +00:00
|
|
|
}
|
|
|
|
return portal.MainIntent()
|
|
|
|
} else if len(info.UserID.String()) == 0 {
|
|
|
|
println("TODO weird uid stuff")
|
|
|
|
} else if info.UserID == user.GetGMID() { //from me
|
|
|
|
return portal.bridge.GetPuppetByGMID(user.GMID).IntentFor(portal)
|
|
|
|
}
|
|
|
|
return portal.bridge.GetPuppetByGMID(info.UserID).IntentFor(portal)
|
|
|
|
}
|
|
|
|
|
2023-09-12 02:58:26 +00:00
|
|
|
func (portal *Portal) getReactionIntent(gmid groupme.ID) *appservice.IntentAPI {
|
|
|
|
return portal.bridge.GetPuppetByGMID(gmid).IntentFor(portal)
|
2023-09-07 19:01:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) startHandling(source *User, info *groupme.Message) *appservice.IntentAPI {
|
|
|
|
// TODO these should all be trace logs
|
|
|
|
if portal.lastMessageTs > uint64(info.CreatedAt.ToTime().Unix()+1) {
|
|
|
|
portal.log.Debugfln("Not handling %s: message is older (%d) than last bridge message (%d)", info.ID, info.CreatedAt, portal.lastMessageTs)
|
|
|
|
} else if portal.isRecentlyHandled(info.ID) {
|
|
|
|
portal.log.Debugfln("Not handling %s: message was recently handled", info.ID)
|
|
|
|
} else if portal.isDuplicate(info.ID) {
|
|
|
|
portal.log.Debugfln("Not handling %s: message is duplicate", info.ID)
|
|
|
|
} else if info.System {
|
|
|
|
portal.log.Debugfln("Not handling %s: message is from system: %s", info.ID, info.Text)
|
|
|
|
} else {
|
|
|
|
portal.lastMessageTs = uint64(info.CreatedAt.ToTime().Unix())
|
|
|
|
intent := portal.getMessageIntent(source, info)
|
|
|
|
if intent != nil {
|
|
|
|
portal.log.Debugfln("Starting handling of %s (ts: %d)", info.ID, info.CreatedAt)
|
|
|
|
} else {
|
|
|
|
portal.log.Debugfln("Not handling %s: sender is not known", info.ID.String())
|
|
|
|
}
|
|
|
|
return intent
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) finishHandling(source *User, message *groupme.Message, mxid id.EventID) {
|
|
|
|
portal.markHandled(source, message, mxid)
|
|
|
|
portal.sendDeliveryReceipt(mxid)
|
|
|
|
portal.log.Debugln("Handled message", message.ID.String(), "->", mxid)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) userMXIDAction(user *User, fn func(mxid id.UserID)) {
|
|
|
|
if user == nil {
|
|
|
|
return
|
|
|
|
}
|
2021-04-02 00:00:27 +00:00
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
fn(user.MXID)
|
|
|
|
}
|
2021-04-21 18:40:57 +00:00
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (portal *Portal) ensureMXIDInvited(mxid id.UserID) {
|
|
|
|
err := portal.MainIntent().EnsureInvited(portal.MXID, mxid)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Warnfln("Failed to ensure %s is invited to %s: %v", mxid, portal.MXID, err)
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (portal *Portal) ensureUserInvited(user *User) bool {
|
|
|
|
return user.ensureInvited(portal.MainIntent(), portal.MXID, portal.IsPrivateChat())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) getBridgeInfoStateKey() string {
|
|
|
|
return fmt.Sprintf("com.beeper.groupme://groupme/%s", portal.Key.GMID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) getBridgeInfo() (string, event.BridgeEventContent) {
|
|
|
|
bridgeInfo := event.BridgeEventContent{
|
|
|
|
BridgeBot: portal.bridge.Bot.UserID,
|
|
|
|
Creator: portal.MainIntent().UserID,
|
|
|
|
Protocol: event.BridgeInfoSection{
|
|
|
|
ID: "groupme",
|
|
|
|
DisplayName: "GroupMe",
|
|
|
|
AvatarURL: portal.bridge.Config.AppService.Bot.ParsedAvatar.CUString(),
|
|
|
|
ExternalURL: "https://www.groupme.com/",
|
|
|
|
},
|
|
|
|
Channel: event.BridgeInfoSection{
|
|
|
|
ID: portal.Key.GMID.String(),
|
|
|
|
DisplayName: portal.Name,
|
|
|
|
AvatarURL: portal.AvatarURL.CUString(),
|
|
|
|
},
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
2023-09-07 19:01:41 +00:00
|
|
|
return portal.getBridgeInfoStateKey(), bridgeInfo
|
|
|
|
}
|
2021-04-02 00:00:27 +00:00
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (portal *Portal) sendReaction(intent *appservice.IntentAPI, eventID id.EventID, reaction string) (*mautrix.RespSendEvent, error) {
|
|
|
|
var content event.ReactionEventContent
|
|
|
|
content.RelatesTo = event.RelatesTo{
|
|
|
|
Type: event.RelAnnotation,
|
|
|
|
EventID: eventID,
|
|
|
|
Key: reaction,
|
|
|
|
}
|
|
|
|
return intent.SendMassagedMessageEvent(portal.MXID, event.EventReaction, &content, time.Now().UnixMilli())
|
|
|
|
}
|
2021-04-02 00:00:27 +00:00
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (portal *Portal) sendMainIntentMessage(content *event.MessageEventContent) (*mautrix.RespSendEvent, error) {
|
|
|
|
return portal.sendMessage(portal.MainIntent(), event.EventMessage, content, nil, 0)
|
|
|
|
}
|
2021-04-02 00:00:27 +00:00
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (portal *Portal) encrypt(intent *appservice.IntentAPI, content *event.Content, eventType event.Type) (event.Type, error) {
|
|
|
|
if !portal.Encrypted || portal.bridge.Crypto == nil {
|
|
|
|
return eventType, nil
|
|
|
|
}
|
|
|
|
intent.AddDoublePuppetValue(content)
|
|
|
|
// TODO maybe the locking should be inside mautrix-go?
|
|
|
|
portal.encryptLock.Lock()
|
|
|
|
defer portal.encryptLock.Unlock()
|
|
|
|
err := portal.bridge.Crypto.Encrypt(portal.MXID, eventType, content)
|
|
|
|
if err != nil {
|
|
|
|
return eventType, fmt.Errorf("failed to encrypt event: %w", err)
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
2023-09-07 19:01:41 +00:00
|
|
|
return event.EventEncrypted, nil
|
|
|
|
}
|
2021-04-02 00:00:27 +00:00
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (portal *Portal) sendMessage(intent *appservice.IntentAPI, eventType event.Type, content *event.MessageEventContent, extraContent map[string]any, timestamp int64) (*mautrix.RespSendEvent, error) {
|
|
|
|
wrappedContent := event.Content{Parsed: content, Raw: extraContent}
|
|
|
|
var err error
|
|
|
|
eventType, err = portal.encrypt(intent, &wrappedContent, eventType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, _ = intent.UserTyping(portal.MXID, false, 0)
|
2023-09-07 19:01:41 +00:00
|
|
|
if timestamp == 0 {
|
|
|
|
return intent.SendMessageEvent(portal.MXID, eventType, &wrappedContent)
|
|
|
|
} else {
|
|
|
|
return intent.SendMassagedMessageEvent(portal.MXID, eventType, &wrappedContent, timestamp)
|
2021-04-02 00:00:27 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-19 15:21:38 +00:00
|
|
|
|
2021-03-07 16:46:25 +00:00
|
|
|
func (portal *Portal) sendMediaBridgeFailure(source *User, intent *appservice.IntentAPI, message groupme.Message, bridgeErr error) {
|
|
|
|
portal.log.Errorfln("Failed to bridge media for %s: %v", message.UserID.String(), bridgeErr)
|
|
|
|
resp, err := portal.sendMessage(intent, event.EventMessage, &event.MessageEventContent{
|
|
|
|
MsgType: event.MsgNotice,
|
|
|
|
Body: "Failed to bridge media",
|
2022-10-21 19:02:33 +00:00
|
|
|
}, nil, int64(message.CreatedAt.ToTime().Unix()*1000))
|
2021-03-07 16:46:25 +00:00
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorfln("Failed to send media download error message for %s: %v", message.UserID.String(), err)
|
|
|
|
} else {
|
|
|
|
portal.finishHandling(source, &message, resp.EventID)
|
|
|
|
}
|
|
|
|
}
|
2020-05-24 14:46:19 +00:00
|
|
|
|
2020-06-10 12:26:14 +00:00
|
|
|
func (portal *Portal) encryptFile(data []byte, mimeType string) ([]byte, string, *event.EncryptedFileInfo) {
|
|
|
|
if !portal.Encrypted {
|
|
|
|
return data, mimeType, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
file := &event.EncryptedFileInfo{
|
|
|
|
EncryptedFile: *attachment.NewEncryptedFile(),
|
|
|
|
URL: "",
|
|
|
|
}
|
|
|
|
return file.Encrypt(data), "application/octet-stream", file
|
2020-06-20 19:24:27 +00:00
|
|
|
}
|
2020-06-10 12:26:14 +00:00
|
|
|
|
2020-06-25 20:58:35 +00:00
|
|
|
func (portal *Portal) tryKickUser(userID id.UserID, intent *appservice.IntentAPI) error {
|
|
|
|
_, err := intent.KickUser(portal.MXID, &mautrix.ReqKickUser{UserID: userID})
|
|
|
|
if err != nil {
|
|
|
|
httpErr, ok := err.(mautrix.HTTPError)
|
|
|
|
if ok && httpErr.RespError != nil && httpErr.RespError.ErrCode == "M_FORBIDDEN" {
|
|
|
|
_, err = portal.MainIntent().KickUser(portal.MXID, &mautrix.ReqKickUser{UserID: userID})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (portal *Portal) removeUser(isSameUser bool, kicker *appservice.IntentAPI, target id.UserID, targetIntent *appservice.IntentAPI) {
|
|
|
|
if !isSameUser || targetIntent == nil {
|
|
|
|
err := portal.tryKickUser(target, kicker)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Warnfln("Failed to kick %s from %s: %v", target, portal.MXID, err)
|
|
|
|
if targetIntent != nil {
|
|
|
|
_, _ = targetIntent.LeaveRoom(portal.MXID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_, err := targetIntent.LeaveRoom(portal.MXID)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Warnfln("Failed to leave portal as %s: %v", target, err)
|
|
|
|
_, _ = portal.MainIntent().KickUser(portal.MXID, &mautrix.ReqKickUser{UserID: target})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 03:02:33 +00:00
|
|
|
func (portal *Portal) convertMatrixMessage(sender *User, evt *event.Event) ([]*groupme.Message, *User) {
|
|
|
|
content, ok := evt.Content.Parsed.(*event.MessageEventContent)
|
|
|
|
if !ok {
|
|
|
|
portal.log.Debugfln("Failed to handle event %s: unexpected parsed content type %T", evt.ID, evt.Content.Parsed)
|
|
|
|
return nil, sender
|
|
|
|
}
|
|
|
|
|
|
|
|
info := groupme.Message{
|
2021-05-01 22:22:02 +00:00
|
|
|
GroupID: groupme.ID(portal.Key.String()),
|
|
|
|
ConversationID: groupme.ID(portal.Key.String()),
|
|
|
|
ChatID: groupme.ID(portal.Key.String()),
|
2022-10-21 19:02:33 +00:00
|
|
|
RecipientID: groupme.ID(portal.Key.GMID),
|
2021-02-23 03:02:33 +00:00
|
|
|
}
|
|
|
|
relaybotFormatted := false
|
2022-10-21 22:04:21 +00:00
|
|
|
|
2021-02-23 03:02:33 +00:00
|
|
|
if evt.Type == event.EventSticker {
|
|
|
|
content.MsgType = event.MsgImage
|
|
|
|
} else if content.MsgType == event.MsgImage && content.GetInfo().MimeType == "image/gif" {
|
|
|
|
content.MsgType = event.MsgVideo
|
|
|
|
}
|
2021-02-13 05:53:35 +00:00
|
|
|
|
2021-02-23 03:02:33 +00:00
|
|
|
switch content.MsgType {
|
|
|
|
case event.MsgText, event.MsgEmote, event.MsgNotice:
|
|
|
|
text := content.Body
|
|
|
|
if content.Format == event.FormatHTML {
|
2023-03-20 00:31:16 +00:00
|
|
|
text = portal.parseMatrixHTML(content)
|
2021-02-23 03:02:33 +00:00
|
|
|
}
|
|
|
|
if content.MsgType == event.MsgEmote && !relaybotFormatted {
|
|
|
|
text = "/me " + text
|
|
|
|
}
|
|
|
|
info.Text = text
|
2021-02-13 05:53:35 +00:00
|
|
|
|
2021-02-23 03:02:33 +00:00
|
|
|
default:
|
|
|
|
portal.log.Debugln("Unhandled Matrix event %s: unknown msgtype %s", evt.ID, content.MsgType)
|
|
|
|
return nil, sender
|
|
|
|
}
|
|
|
|
return []*groupme.Message{&info}, sender
|
2020-05-24 14:28:30 +00:00
|
|
|
}
|
|
|
|
|
2021-05-01 22:22:02 +00:00
|
|
|
func (portal *Portal) sendRaw(sender *User, evt *event.Event, info *groupme.Message, retries int) (*groupme.Message, error) {
|
|
|
|
if retries == -1 {
|
|
|
|
retries = 2
|
|
|
|
}
|
|
|
|
|
|
|
|
var m *groupme.Message
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if portal.IsPrivateChat() {
|
2023-09-20 21:06:04 +00:00
|
|
|
m, err = sender.Client.CreateDirectMessage(context.TODO(), info, sender.Token)
|
2021-05-01 22:22:02 +00:00
|
|
|
} else {
|
2023-09-20 21:06:04 +00:00
|
|
|
m, err = sender.Client.CreateMessage(context.TODO(), info.GroupID, info, sender.Token)
|
2021-05-01 22:22:02 +00:00
|
|
|
}
|
2021-02-23 03:02:33 +00:00
|
|
|
|
|
|
|
id := ""
|
|
|
|
if m != nil {
|
|
|
|
id = m.ID.String()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Warnln(err, id, info.GroupID.String())
|
2021-05-01 22:22:02 +00:00
|
|
|
|
|
|
|
if retries > 0 {
|
|
|
|
return portal.sendRaw(sender, evt, info, retries-1)
|
|
|
|
}
|
2021-02-27 22:38:47 +00:00
|
|
|
}
|
|
|
|
return m, nil
|
2018-08-18 19:57:08 +00:00
|
|
|
}
|
2019-05-15 22:59:36 +00:00
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (portal *Portal) getMatrixUsers() ([]id.UserID, error) {
|
2020-06-25 20:33:11 +00:00
|
|
|
members, err := portal.MainIntent().JoinedMembers(portal.MXID)
|
|
|
|
if err != nil {
|
2020-10-05 19:38:34 +00:00
|
|
|
return nil, fmt.Errorf("failed to get member list: %w", err)
|
2020-06-25 20:33:11 +00:00
|
|
|
}
|
|
|
|
var users []id.UserID
|
|
|
|
for userID := range members.Joined {
|
|
|
|
_, isPuppet := portal.bridge.ParsePuppetMXID(userID)
|
|
|
|
if !isPuppet && userID != portal.bridge.Bot.UserID {
|
|
|
|
users = append(users, userID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return users, nil
|
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (portal *Portal) cleanupIfEmpty() {
|
|
|
|
users, err := portal.getMatrixUsers()
|
2020-06-25 20:33:11 +00:00
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorfln("Failed to get Matrix user list to determine if portal needs to be cleaned up: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(users) == 0 {
|
|
|
|
portal.log.Infoln("Room seems to be empty, cleaning up...")
|
|
|
|
portal.Delete()
|
2023-09-07 19:01:41 +00:00
|
|
|
portal.cleanup(false)
|
2020-06-25 20:33:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (portal *Portal) cleanup(puppetsOnly bool) {
|
2019-05-16 17:14:32 +00:00
|
|
|
if len(portal.MXID) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if portal.IsPrivateChat() {
|
|
|
|
_, err := portal.MainIntent().LeaveRoom(portal.MXID)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Warnln("Failed to leave private chat portal with main intent:", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
intent := portal.MainIntent()
|
|
|
|
members, err := intent.JoinedMembers(portal.MXID)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorln("Failed to get portal members for cleanup:", err)
|
|
|
|
return
|
|
|
|
}
|
2020-09-24 12:25:36 +00:00
|
|
|
for member := range members.Joined {
|
2019-05-21 20:44:14 +00:00
|
|
|
if member == intent.UserID {
|
|
|
|
continue
|
|
|
|
}
|
2019-05-16 17:14:32 +00:00
|
|
|
puppet := portal.bridge.GetPuppetByMXID(member)
|
|
|
|
if puppet != nil {
|
2019-05-23 23:33:26 +00:00
|
|
|
_, err = puppet.DefaultIntent().LeaveRoom(portal.MXID)
|
2019-05-21 18:06:27 +00:00
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorln("Error leaving as puppet while cleaning up portal:", err)
|
|
|
|
}
|
2019-05-16 17:14:32 +00:00
|
|
|
} else if !puppetsOnly {
|
|
|
|
_, err = intent.KickUser(portal.MXID, &mautrix.ReqKickUser{UserID: member, Reason: "Deleting portal"})
|
2019-05-21 18:06:27 +00:00
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorln("Error kicking user while cleaning up portal:", err)
|
|
|
|
}
|
2019-05-16 17:14:32 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-21 20:44:14 +00:00
|
|
|
_, err = intent.LeaveRoom(portal.MXID)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorln("Error leaving with main intent while cleaning up portal:", err)
|
|
|
|
}
|
2019-05-16 17:14:32 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (portal *Portal) getBasePowerLevels() *event.PowerLevelsEventContent {
|
|
|
|
anyone := 0
|
|
|
|
nope := 99
|
|
|
|
invite := 50
|
|
|
|
if portal.bridge.Config.Bridge.AllowUserInvite {
|
|
|
|
invite = 0
|
|
|
|
}
|
|
|
|
return &event.PowerLevelsEventContent{
|
|
|
|
UsersDefault: anyone,
|
|
|
|
EventsDefault: anyone,
|
|
|
|
RedactPtr: &anyone,
|
|
|
|
StateDefaultPtr: &nope,
|
|
|
|
BanPtr: &nope,
|
|
|
|
InvitePtr: &invite,
|
|
|
|
Users: map[id.UserID]int{
|
|
|
|
portal.MainIntent().UserID: 100,
|
|
|
|
},
|
|
|
|
Events: map[string]int{
|
|
|
|
event.StateRoomName.Type: anyone,
|
|
|
|
event.StateRoomAvatar.Type: anyone,
|
|
|
|
event.StateTopic.Type: anyone,
|
|
|
|
},
|
2021-02-28 00:57:42 +00:00
|
|
|
}
|
2019-05-16 17:14:32 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
func (portal *Portal) syncParticipants(group *groupme.Group) {
|
|
|
|
changed := false
|
|
|
|
levels, err := portal.MainIntent().PowerLevels(portal.MXID)
|
|
|
|
if err != nil {
|
|
|
|
levels = portal.getBasePowerLevels()
|
|
|
|
changed = true
|
|
|
|
}
|
|
|
|
participantMap := make(map[groupme.ID]bool)
|
|
|
|
for _, participant := range group.Members {
|
|
|
|
participantMap[participant.UserID] = true
|
|
|
|
user := portal.bridge.GetUserByGMID(participant.UserID)
|
|
|
|
portal.userMXIDAction(user, portal.ensureMXIDInvited)
|
2020-06-25 20:58:35 +00:00
|
|
|
|
2023-09-07 19:01:41 +00:00
|
|
|
puppet := portal.bridge.GetPuppetByGMID(participant.UserID)
|
|
|
|
err := puppet.IntentFor(portal).EnsureJoined(portal.MXID)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Warnfln("Failed to make puppet of %s join %s: %v", participant.ID.String(), portal.MXID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedLevel := 0
|
|
|
|
changed = levels.EnsureUserLevel(puppet.MXID, expectedLevel) || changed
|
|
|
|
if user != nil {
|
|
|
|
changed = levels.EnsureUserLevel(user.MXID, expectedLevel) || changed
|
|
|
|
}
|
2023-09-12 02:58:26 +00:00
|
|
|
if len(puppet.Displayname) == 0 {
|
|
|
|
puppet.Sync(nil, participant, false, false)
|
|
|
|
}
|
2023-09-07 19:01:41 +00:00
|
|
|
}
|
|
|
|
if changed {
|
|
|
|
_, err = portal.MainIntent().SetPowerLevels(portal.MXID, levels)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Errorln("Failed to change power levels:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
members, err := portal.MainIntent().JoinedMembers(portal.MXID)
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Warnln("Failed to get member list:", err)
|
|
|
|
} else {
|
|
|
|
for member := range members.Joined {
|
|
|
|
jid, ok := portal.bridge.ParsePuppetMXID(member)
|
|
|
|
if ok {
|
|
|
|
_, shouldBePresent := participantMap[jid]
|
|
|
|
if !shouldBePresent {
|
|
|
|
_, err := portal.MainIntent().KickUser(portal.MXID, &mautrix.ReqKickUser{
|
|
|
|
UserID: member,
|
|
|
|
Reason: "User had left this GroupMe chat",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
portal.log.Warnfln("Failed to kick user %s who had left: %v", member, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-25 20:58:35 +00:00
|
|
|
}
|