12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178 |
- package bridge
- import (
- "bytes"
- "fmt"
- "strings"
- "sync"
- "time"
- "github.com/bwmarrin/discordgo"
- log "maunium.net/go/maulogger/v2"
- "maunium.net/go/mautrix"
- "maunium.net/go/mautrix/appservice"
- "maunium.net/go/mautrix/event"
- "maunium.net/go/mautrix/id"
- "go.mau.fi/mautrix-discord/database"
- )
- type portalDiscordMessage struct {
- msg interface{}
- user *User
- }
- type portalMatrixMessage struct {
- evt *event.Event
- user *User
- }
- type Portal struct {
- *database.Portal
- bridge *Bridge
- log log.Logger
- roomCreateLock sync.Mutex
- encryptLock sync.Mutex
- discordMessages chan portalDiscordMessage
- matrixMessages chan portalMatrixMessage
- }
- var (
- portalCreationDummyEvent = event.Type{Type: "fi.mau.dummy.portal_created", Class: event.MessageEventType}
- )
- func (b *Bridge) loadPortal(dbPortal *database.Portal, key *database.PortalKey) *Portal {
- // If we weren't given a portal we'll attempt to create it if a key was
- // provided.
- if dbPortal == nil {
- if key == nil {
- return nil
- }
- dbPortal = b.db.Portal.New()
- dbPortal.Key = *key
- dbPortal.Insert()
- }
- portal := b.NewPortal(dbPortal)
- // No need to lock, it is assumed that our callers have already acquired
- // the lock.
- b.portalsByID[portal.Key] = portal
- if portal.MXID != "" {
- b.portalsByMXID[portal.MXID] = portal
- }
- return portal
- }
- func (b *Bridge) GetPortalByMXID(mxid id.RoomID) *Portal {
- b.portalsLock.Lock()
- defer b.portalsLock.Unlock()
- portal, ok := b.portalsByMXID[mxid]
- if !ok {
- return b.loadPortal(b.db.Portal.GetByMXID(mxid), nil)
- }
- return portal
- }
- func (b *Bridge) GetPortalByID(key database.PortalKey) *Portal {
- b.portalsLock.Lock()
- defer b.portalsLock.Unlock()
- portal, ok := b.portalsByID[key]
- if !ok {
- return b.loadPortal(b.db.Portal.GetByID(key), &key)
- }
- return portal
- }
- func (b *Bridge) GetAllPortals() []*Portal {
- return b.dbPortalsToPortals(b.db.Portal.GetAll())
- }
- func (b *Bridge) GetAllPortalsByID(id string) []*Portal {
- return b.dbPortalsToPortals(b.db.Portal.GetAllByID(id))
- }
- func (b *Bridge) dbPortalsToPortals(dbPortals []*database.Portal) []*Portal {
- b.portalsLock.Lock()
- defer b.portalsLock.Unlock()
- output := make([]*Portal, len(dbPortals))
- for index, dbPortal := range dbPortals {
- if dbPortal == nil {
- continue
- }
- portal, ok := b.portalsByID[dbPortal.Key]
- if !ok {
- portal = b.loadPortal(dbPortal, nil)
- }
- output[index] = portal
- }
- return output
- }
- func (b *Bridge) NewPortal(dbPortal *database.Portal) *Portal {
- portal := &Portal{
- Portal: dbPortal,
- bridge: b,
- log: b.log.Sub(fmt.Sprintf("Portal/%s", dbPortal.Key)),
- discordMessages: make(chan portalDiscordMessage, b.Config.Bridge.PortalMessageBuffer),
- matrixMessages: make(chan portalMatrixMessage, b.Config.Bridge.PortalMessageBuffer),
- }
- go portal.messageLoop()
- return portal
- }
- func (p *Portal) handleMatrixInvite(sender *User, evt *event.Event) {
- // Look up an existing puppet or create a new one.
- puppet := p.bridge.GetPuppetByMXID(id.UserID(evt.GetStateKey()))
- if puppet != nil {
- p.log.Infoln("no puppet for %v", sender)
- // Open a conversation on the discord side?
- }
- p.log.Infoln("matrixInvite: puppet:", puppet)
- }
- func (p *Portal) messageLoop() {
- for {
- select {
- case msg := <-p.matrixMessages:
- p.handleMatrixMessages(msg)
- case msg := <-p.discordMessages:
- p.handleDiscordMessages(msg)
- }
- }
- }
- func (p *Portal) IsPrivateChat() bool {
- return p.Type == discordgo.ChannelTypeDM
- }
- func (p *Portal) MainIntent() *appservice.IntentAPI {
- if p.IsPrivateChat() && p.DMUser != "" {
- return p.bridge.GetPuppetByID(p.DMUser).DefaultIntent()
- }
- return p.bridge.bot
- }
- func (p *Portal) createMatrixRoom(user *User, channel *discordgo.Channel) error {
- p.roomCreateLock.Lock()
- defer p.roomCreateLock.Unlock()
- // If we have a matrix id the room should exist so we have nothing to do.
- if p.MXID != "" {
- return nil
- }
- p.Type = channel.Type
- if p.Type == discordgo.ChannelTypeDM {
- p.DMUser = channel.Recipients[0].ID
- }
- intent := p.MainIntent()
- if err := intent.EnsureRegistered(); err != nil {
- return err
- }
- name, err := p.bridge.Config.Bridge.FormatChannelname(channel, user.Session)
- if err != nil {
- p.log.Warnfln("failed to format name, proceeding with generic name: %v", err)
- p.Name = channel.Name
- } else {
- p.Name = name
- }
- p.Topic = channel.Topic
- // TODO: get avatars figured out
- // p.Avatar = puppet.Avatar
- // p.AvatarURL = puppet.AvatarURL
- p.log.Infoln("Creating Matrix room for channel:", p.Portal.Key.ChannelID)
- initialState := []*event.Event{}
- creationContent := make(map[string]interface{})
- creationContent["m.federate"] = false
- var invite []id.UserID
- if p.bridge.Config.Bridge.Encryption.Default {
- initialState = append(initialState, &event.Event{
- Type: event.StateEncryption,
- Content: event.Content{
- Parsed: event.EncryptionEventContent{Algorithm: id.AlgorithmMegolmV1},
- },
- })
- p.Encrypted = true
- if p.IsPrivateChat() {
- invite = append(invite, p.bridge.bot.UserID)
- }
- }
- resp, err := intent.CreateRoom(&mautrix.ReqCreateRoom{
- Visibility: "private",
- Name: p.Name,
- Topic: p.Topic,
- Invite: invite,
- Preset: "private_chat",
- IsDirect: p.IsPrivateChat(),
- InitialState: initialState,
- CreationContent: creationContent,
- })
- if err != nil {
- p.log.Warnln("Failed to create room:", err)
- return err
- }
- p.MXID = resp.RoomID
- p.Update()
- p.bridge.portalsLock.Lock()
- p.bridge.portalsByMXID[p.MXID] = p
- p.bridge.portalsLock.Unlock()
- p.ensureUserInvited(user)
- user.syncChatDoublePuppetDetails(p, true)
- p.syncParticipants(user, channel.Recipients)
- if p.IsPrivateChat() {
- puppet := user.bridge.GetPuppetByID(p.Key.Receiver)
- chats := map[id.UserID][]id.RoomID{puppet.MXID: {p.MXID}}
- user.updateDirectChats(chats)
- }
- firstEventResp, err := p.MainIntent().SendMessageEvent(p.MXID, portalCreationDummyEvent, struct{}{})
- if err != nil {
- p.log.Errorln("Failed to send dummy event to mark portal creation:", err)
- } else {
- p.FirstEventID = firstEventResp.EventID
- p.Update()
- }
- return nil
- }
- func (p *Portal) handleDiscordMessages(msg portalDiscordMessage) {
- if p.MXID == "" {
- discordMsg, ok := msg.msg.(*discordgo.MessageCreate)
- if !ok {
- p.log.Warnln("Can't create Matrix room from non new message event")
- return
- }
- p.log.Debugln("Creating Matrix room from incoming message")
- channel, err := msg.user.Session.Channel(discordMsg.ChannelID)
- if err != nil {
- p.log.Errorln("Failed to find channel for message:", err)
- return
- }
- if err := p.createMatrixRoom(msg.user, channel); err != nil {
- p.log.Errorln("Failed to create portal room:", err)
- return
- }
- }
- switch msg.msg.(type) {
- case *discordgo.MessageCreate:
- p.handleDiscordMessageCreate(msg.user, msg.msg.(*discordgo.MessageCreate).Message)
- case *discordgo.MessageUpdate:
- p.handleDiscordMessagesUpdate(msg.user, msg.msg.(*discordgo.MessageUpdate).Message)
- case *discordgo.MessageDelete:
- p.handleDiscordMessageDelete(msg.user, msg.msg.(*discordgo.MessageDelete).Message)
- case *discordgo.MessageReactionAdd:
- p.handleDiscordReaction(msg.user, msg.msg.(*discordgo.MessageReactionAdd).MessageReaction, true)
- case *discordgo.MessageReactionRemove:
- p.handleDiscordReaction(msg.user, msg.msg.(*discordgo.MessageReactionRemove).MessageReaction, false)
- default:
- p.log.Warnln("unknown message type")
- }
- }
- func (p *Portal) ensureUserInvited(user *User) bool {
- return user.ensureInvited(p.MainIntent(), p.MXID, p.IsPrivateChat())
- }
- func (p *Portal) markMessageHandled(msg *database.Message, discordID string, mxid id.EventID, authorID string, timestamp time.Time) *database.Message {
- if msg == nil {
- msg := p.bridge.db.Message.New()
- msg.Channel = p.Key
- msg.DiscordID = discordID
- msg.MatrixID = mxid
- msg.AuthorID = authorID
- msg.Timestamp = timestamp
- msg.Insert()
- } else {
- msg.UpdateMatrixID(mxid)
- }
- return msg
- }
- func (p *Portal) sendMediaFailedMessage(intent *appservice.IntentAPI, bridgeErr error) {
- content := &event.MessageEventContent{
- Body: fmt.Sprintf("Failed to bridge media: %v", bridgeErr),
- MsgType: event.MsgNotice,
- }
- _, err := p.sendMatrixMessage(intent, event.EventMessage, content, nil, time.Now().UTC().UnixMilli())
- if err != nil {
- p.log.Warnfln("failed to send error message to matrix: %v", err)
- }
- }
- func (p *Portal) handleDiscordAttachment(intent *appservice.IntentAPI, msgID string, attachment *discordgo.MessageAttachment) {
- // var captionContent *event.MessageEventContent
- // if attachment.Description != "" {
- // captionContent = &event.MessageEventContent{
- // Body: attachment.Description,
- // MsgType: event.MsgNotice,
- // }
- // }
- // p.log.Debugfln("captionContent: %#v", captionContent)
- content := &event.MessageEventContent{
- Body: attachment.Filename,
- Info: &event.FileInfo{
- Height: attachment.Height,
- MimeType: attachment.ContentType,
- Width: attachment.Width,
- // This gets overwritten later after the file is uploaded to the homeserver
- Size: attachment.Size,
- },
- }
- switch strings.ToLower(strings.Split(attachment.ContentType, "/")[0]) {
- case "audio":
- content.MsgType = event.MsgAudio
- case "image":
- content.MsgType = event.MsgImage
- case "video":
- content.MsgType = event.MsgVideo
- default:
- content.MsgType = event.MsgFile
- }
- data, err := p.downloadDiscordAttachment(attachment.URL)
- if err != nil {
- p.sendMediaFailedMessage(intent, err)
- return
- }
- err = p.uploadMatrixAttachment(intent, data, content)
- if err != nil {
- p.sendMediaFailedMessage(intent, err)
- return
- }
- resp, err := p.sendMatrixMessage(intent, event.EventMessage, content, nil, time.Now().UTC().UnixMilli())
- if err != nil {
- p.log.Warnfln("failed to send media message to matrix: %v", err)
- }
- dbAttachment := p.bridge.db.Attachment.New()
- dbAttachment.Channel = p.Key
- dbAttachment.DiscordMessageID = msgID
- dbAttachment.DiscordAttachmentID = attachment.ID
- dbAttachment.MatrixEventID = resp.EventID
- dbAttachment.Insert()
- }
- func (p *Portal) handleDiscordMessageCreate(user *User, msg *discordgo.Message) {
- if p.MXID == "" {
- p.log.Warnln("handle message called without a valid portal")
- return
- }
- // Handle room name changes
- if msg.Type == discordgo.MessageTypeChannelNameChange {
- channel, err := user.Session.Channel(msg.ChannelID)
- if err != nil {
- p.log.Errorf("Failed to find the channel for portal %s", p.Key)
- return
- }
- name, err := p.bridge.Config.Bridge.FormatChannelname(channel, user.Session)
- if err != nil {
- p.log.Errorf("Failed to format name for portal %s", p.Key)
- return
- }
- p.Name = name
- p.Update()
- p.MainIntent().SetRoomName(p.MXID, name)
- return
- }
- // Handle normal message
- existing := p.bridge.db.Message.GetByDiscordID(p.Key, msg.ID)
- if existing != nil {
- p.log.Debugln("not handling duplicate message", msg.ID)
- return
- }
- puppet := p.bridge.GetPuppetByID(msg.Author.ID)
- puppet.SyncContact(user)
- intent := puppet.IntentFor(p)
- if msg.Content != "" {
- content := &event.MessageEventContent{
- Body: msg.Content,
- MsgType: event.MsgText,
- }
- if msg.MessageReference != nil {
- key := database.PortalKey{msg.MessageReference.ChannelID, user.ID}
- existing := p.bridge.db.Message.GetByDiscordID(key, msg.MessageReference.MessageID)
- if existing != nil && existing.MatrixID != "" {
- content.RelatesTo = &event.RelatesTo{
- Type: event.RelReply,
- EventID: existing.MatrixID,
- }
- }
- }
- resp, err := p.sendMatrixMessage(intent, event.EventMessage, content, nil, time.Now().UTC().UnixMilli())
- if err != nil {
- p.log.Warnfln("failed to send message %q to matrix: %v", msg.ID, err)
- return
- }
- ts, _ := msg.Timestamp.Parse()
- p.markMessageHandled(existing, msg.ID, resp.EventID, msg.Author.ID, ts)
- }
- // now run through any attachments the message has
- for _, attachment := range msg.Attachments {
- p.handleDiscordAttachment(intent, msg.ID, attachment)
- }
- }
- func (p *Portal) handleDiscordMessagesUpdate(user *User, msg *discordgo.Message) {
- if p.MXID == "" {
- p.log.Warnln("handle message called without a valid portal")
- return
- }
- // There's a few scenarios where the author is nil but I haven't figured
- // them all out yet.
- if msg.Author == nil {
- // If the server has to lookup opengraph previews it'll send the
- // message through without the preview and then add the preview later
- // via a message update. However, when it does this there is no author
- // as it's just the server, so for the moment we'll ignore this to
- // avoid a crash.
- if len(msg.Embeds) > 0 {
- p.log.Debugln("ignoring update for opengraph attachment")
- return
- }
- p.log.Errorfln("author is nil: %#v", msg)
- }
- intent := p.bridge.GetPuppetByID(msg.Author.ID).IntentFor(p)
- existing := p.bridge.db.Message.GetByDiscordID(p.Key, msg.ID)
- if existing == nil {
- // Due to the differences in Discord and Matrix attachment handling,
- // existing will return nil if the original message was empty as we
- // don't store/save those messages so we can determine when we're
- // working against an attachment and do the attachment lookup instead.
- // Find all the existing attachments and drop them in a map so we can
- // figure out which, if any have been deleted and clean them up on the
- // matrix side.
- attachmentMap := map[string]*database.Attachment{}
- attachments := p.bridge.db.Attachment.GetAllByDiscordMessageID(p.Key, msg.ID)
- for _, attachment := range attachments {
- attachmentMap[attachment.DiscordAttachmentID] = attachment
- }
- // Now run through the list of attachments on this message and remove
- // them from the map.
- for _, attachment := range msg.Attachments {
- if _, found := attachmentMap[attachment.ID]; found {
- delete(attachmentMap, attachment.ID)
- }
- }
- // Finally run through any attachments still in the map and delete them
- // on the matrix side and our database.
- for _, attachment := range attachmentMap {
- _, err := intent.RedactEvent(p.MXID, attachment.MatrixEventID)
- if err != nil {
- p.log.Warnfln("Failed to remove attachment %s: %v", attachment.MatrixEventID, err)
- }
- attachment.Delete()
- }
- return
- }
- content := &event.MessageEventContent{
- Body: msg.Content,
- MsgType: event.MsgText,
- }
- content.SetEdit(existing.MatrixID)
- resp, err := p.sendMatrixMessage(intent, event.EventMessage, content, nil, time.Now().UTC().UnixMilli())
- if err != nil {
- p.log.Warnfln("failed to send message %q to matrix: %v", msg.ID, err)
- return
- }
- ts, _ := msg.Timestamp.Parse()
- p.markMessageHandled(existing, msg.ID, resp.EventID, msg.Author.ID, ts)
- }
- func (p *Portal) handleDiscordMessageDelete(user *User, msg *discordgo.Message) {
- // The discord delete message object is pretty empty and doesn't include
- // the author so we have to use the DMUser from the portal that was added
- // at creation time if we're a DM. We'll might have similar issues when we
- // add guild message support, but we'll cross that bridge when we get
- // there.
- // Find the message that we're working with. This could correctly return
- // nil if the message was just one or more attachments.
- existing := p.bridge.db.Message.GetByDiscordID(p.Key, msg.ID)
- var intent *appservice.IntentAPI
- if p.Type == discordgo.ChannelTypeDM {
- intent = p.bridge.GetPuppetByID(p.DMUser).IntentFor(p)
- } else {
- intent = p.MainIntent()
- }
- if existing != nil {
- _, err := intent.RedactEvent(p.MXID, existing.MatrixID)
- if err != nil {
- p.log.Warnfln("Failed to remove message %s: %v", existing.MatrixID, err)
- }
- existing.Delete()
- }
- // Now delete all of the existing attachments.
- attachments := p.bridge.db.Attachment.GetAllByDiscordMessageID(p.Key, msg.ID)
- for _, attachment := range attachments {
- _, err := intent.RedactEvent(p.MXID, attachment.MatrixEventID)
- if err != nil {
- p.log.Warnfln("Failed to remove attachment %s: %v", attachment.MatrixEventID, err)
- }
- attachment.Delete()
- }
- }
- func (p *Portal) syncParticipants(source *User, participants []*discordgo.User) {
- for _, participant := range participants {
- puppet := p.bridge.GetPuppetByID(participant.ID)
- puppet.SyncContact(source)
- user := p.bridge.GetUserByID(participant.ID)
- if user != nil {
- p.ensureUserInvited(user)
- }
- if user == nil || !puppet.IntentFor(p).IsCustomPuppet {
- if err := puppet.IntentFor(p).EnsureJoined(p.MXID); err != nil {
- p.log.Warnfln("Failed to make puppet of %s join %s: %v", participant.ID, p.MXID, err)
- }
- }
- }
- }
- func (portal *Portal) encrypt(content *event.Content, eventType event.Type) (event.Type, error) {
- if portal.Encrypted && portal.bridge.crypto != nil {
- // TODO maybe the locking should be inside mautrix-go?
- portal.encryptLock.Lock()
- encrypted, err := portal.bridge.crypto.Encrypt(portal.MXID, eventType, *content)
- portal.encryptLock.Unlock()
- if err != nil {
- return eventType, fmt.Errorf("failed to encrypt event: %w", err)
- }
- eventType = event.EventEncrypted
- content.Parsed = encrypted
- }
- return eventType, nil
- }
- const doublePuppetKey = "fi.mau.double_puppet_source"
- const doublePuppetValue = "mautrix-discord"
- func (portal *Portal) sendMatrixMessage(intent *appservice.IntentAPI, eventType event.Type, content *event.MessageEventContent, extraContent map[string]interface{}, timestamp int64) (*mautrix.RespSendEvent, error) {
- wrappedContent := event.Content{Parsed: content, Raw: extraContent}
- if timestamp != 0 && intent.IsCustomPuppet {
- if wrappedContent.Raw == nil {
- wrappedContent.Raw = map[string]interface{}{}
- }
- if intent.IsCustomPuppet {
- wrappedContent.Raw[doublePuppetKey] = doublePuppetValue
- }
- }
- var err error
- eventType, err = portal.encrypt(&wrappedContent, eventType)
- if err != nil {
- return nil, err
- }
- if eventType == event.EventEncrypted {
- // Clear other custom keys if the event was encrypted, but keep the double puppet identifier
- if intent.IsCustomPuppet {
- wrappedContent.Raw = map[string]interface{}{doublePuppetKey: doublePuppetValue}
- } else {
- wrappedContent.Raw = nil
- }
- }
- _, _ = intent.UserTyping(portal.MXID, false, 0)
- if timestamp == 0 {
- return intent.SendMessageEvent(portal.MXID, eventType, &wrappedContent)
- } else {
- return intent.SendMassagedMessageEvent(portal.MXID, eventType, &wrappedContent, timestamp)
- }
- }
- func (p *Portal) handleMatrixMessages(msg portalMatrixMessage) {
- switch msg.evt.Type {
- case event.EventMessage:
- p.handleMatrixMessage(msg.user, msg.evt)
- default:
- p.log.Debugln("unknown event type", msg.evt.Type)
- }
- }
- func (p *Portal) handleMatrixMessage(sender *User, evt *event.Event) {
- if p.IsPrivateChat() && sender.ID != p.Key.Receiver {
- return
- }
- existing := p.bridge.db.Message.GetByMatrixID(p.Key, evt.ID)
- if existing != nil {
- p.log.Debugln("not handling duplicate message", evt.ID)
- return
- }
- content, ok := evt.Content.Parsed.(*event.MessageEventContent)
- if !ok {
- p.log.Debugfln("Failed to handle event %s: unexpected parsed content type %T", evt.ID, evt.Content.Parsed)
- return
- }
- if content.RelatesTo != nil && content.RelatesTo.Type == event.RelReplace {
- existing := p.bridge.db.Message.GetByMatrixID(p.Key, content.RelatesTo.EventID)
- if existing != nil && existing.DiscordID != "" {
- // we don't have anything to save for the update message right now
- // as we're not tracking edited timestamps.
- _, err := sender.Session.ChannelMessageEdit(p.Key.ChannelID,
- existing.DiscordID, content.NewContent.Body)
- if err != nil {
- p.log.Errorln("Failed to update message %s: %v", existing.DiscordID, err)
- return
- }
- }
- return
- }
- var msg *discordgo.Message
- var err error
- switch content.MsgType {
- case event.MsgText, event.MsgEmote, event.MsgNotice:
- sent := false
- if content.RelatesTo != nil && content.RelatesTo.Type == event.RelReply {
- existing := p.bridge.db.Message.GetByMatrixID(
- p.Key,
- content.RelatesTo.EventID,
- )
- if existing != nil && existing.DiscordID != "" {
- msg, err = sender.Session.ChannelMessageSendReply(
- p.Key.ChannelID,
- content.Body,
- &discordgo.MessageReference{
- ChannelID: p.Key.ChannelID,
- MessageID: existing.DiscordID,
- },
- )
- if err == nil {
- sent = true
- }
- }
- }
- if !sent {
- msg, err = sender.Session.ChannelMessageSend(p.Key.ChannelID, content.Body)
- }
- case event.MsgAudio, event.MsgFile, event.MsgImage, event.MsgVideo:
- data, err := p.downloadMatrixAttachment(evt.ID, content)
- if err != nil {
- p.log.Errorfln("Failed to download matrix attachment: %v", err)
- return
- }
- msgSend := &discordgo.MessageSend{
- Files: []*discordgo.File{
- &discordgo.File{
- Name: content.Body,
- ContentType: content.Info.MimeType,
- Reader: bytes.NewReader(data),
- },
- },
- }
- msg, err = sender.Session.ChannelMessageSendComplex(p.Key.ChannelID, msgSend)
- default:
- p.log.Warnln("unknown message type:", content.MsgType)
- return
- }
- if err != nil {
- p.log.Errorfln("Failed to send message: %v", err)
- return
- }
- if msg != nil {
- dbMsg := p.bridge.db.Message.New()
- dbMsg.Channel = p.Key
- dbMsg.DiscordID = msg.ID
- dbMsg.MatrixID = evt.ID
- dbMsg.AuthorID = sender.ID
- dbMsg.Timestamp = time.Now()
- dbMsg.Insert()
- }
- }
- func (p *Portal) handleMatrixLeave(sender *User) {
- p.log.Debugln("User left private chat portal, cleaning up and deleting...")
- p.delete()
- p.cleanup(false)
- // TODO: figure out how to close a dm from the API.
- p.cleanupIfEmpty()
- }
- func (p *Portal) leave(sender *User) {
- if p.MXID == "" {
- return
- }
- intent := p.bridge.GetPuppetByID(sender.ID).IntentFor(p)
- intent.LeaveRoom(p.MXID)
- }
- func (p *Portal) delete() {
- p.Portal.Delete()
- p.bridge.portalsLock.Lock()
- delete(p.bridge.portalsByID, p.Key)
- if p.MXID != "" {
- delete(p.bridge.portalsByMXID, p.MXID)
- }
- p.bridge.portalsLock.Unlock()
- }
- func (p *Portal) cleanupIfEmpty() {
- users, err := p.getMatrixUsers()
- if err != nil {
- p.log.Errorfln("Failed to get Matrix user list to determine if portal needs to be cleaned up: %v", err)
- return
- }
- if len(users) == 0 {
- p.log.Infoln("Room seems to be empty, cleaning up...")
- p.delete()
- p.cleanup(false)
- }
- }
- func (p *Portal) cleanup(puppetsOnly bool) {
- if p.MXID != "" {
- return
- }
- if p.IsPrivateChat() {
- _, err := p.MainIntent().LeaveRoom(p.MXID)
- if err != nil {
- p.log.Warnln("Failed to leave private chat portal with main intent:", err)
- }
- return
- }
- intent := p.MainIntent()
- members, err := intent.JoinedMembers(p.MXID)
- if err != nil {
- p.log.Errorln("Failed to get portal members for cleanup:", err)
- return
- }
- for member := range members.Joined {
- if member == intent.UserID {
- continue
- }
- puppet := p.bridge.GetPuppetByMXID(member)
- if p != nil {
- _, err = puppet.DefaultIntent().LeaveRoom(p.MXID)
- if err != nil {
- p.log.Errorln("Error leaving as puppet while cleaning up portal:", err)
- }
- } else if !puppetsOnly {
- _, err = intent.KickUser(p.MXID, &mautrix.ReqKickUser{UserID: member, Reason: "Deleting portal"})
- if err != nil {
- p.log.Errorln("Error kicking user while cleaning up portal:", err)
- }
- }
- }
- _, err = intent.LeaveRoom(p.MXID)
- if err != nil {
- p.log.Errorln("Error leaving with main intent while cleaning up portal:", err)
- }
- }
- func (p *Portal) getMatrixUsers() ([]id.UserID, error) {
- members, err := p.MainIntent().JoinedMembers(p.MXID)
- if err != nil {
- return nil, fmt.Errorf("failed to get member list: %w", err)
- }
- var users []id.UserID
- for userID := range members.Joined {
- _, isPuppet := p.bridge.ParsePuppetMXID(userID)
- if !isPuppet && userID != p.bridge.bot.UserID {
- users = append(users, userID)
- }
- }
- return users, nil
- }
- func (p *Portal) handleMatrixKick(sender *User, target *Puppet) {
- // TODO: need to learn how to make this happen as discordgo proper doesn't
- // support group dms and it looks like it's a binary blob.
- }
- func (p *Portal) handleMatrixReaction(evt *event.Event) {
- user := p.bridge.GetUserByMXID(evt.Sender)
- if user == nil {
- p.log.Errorf("failed to find user for %s", evt.Sender)
- return
- }
- if user.ID != p.Key.Receiver {
- return
- }
- reaction := evt.Content.AsReaction()
- if reaction.RelatesTo.Type != event.RelAnnotation {
- p.log.Errorfln("Ignoring reaction %s due to unknown m.relates_to data", evt.ID)
- return
- }
- var discordID string
- msg := p.bridge.db.Message.GetByMatrixID(p.Key, reaction.RelatesTo.EventID)
- // Due to the differences in attachments between Discord and Matrix, if a
- // user reacts to a media message on discord our lookup above will fail
- // because the relation of matrix media messages to attachments in handled
- // in the attachments table instead of messages so we need to check that
- // before continuing.
- //
- // This also leads to interesting problems when a Discord message comes in
- // with multiple attachments. A user can react to each one individually on
- // Matrix, which will cause us to send it twice. Discord tends to ignore
- // this, but if the user removes one of them, discord removes it and now
- // they're out of sync. Perhaps we should add a counter to the reactions
- // table to keep them in sync and to avoid sending duplicates to Discord.
- if msg == nil {
- attachment := p.bridge.db.Attachment.GetByMatrixID(p.Key, reaction.RelatesTo.EventID)
- discordID = attachment.DiscordMessageID
- } else {
- if msg.DiscordID == "" {
- p.log.Debugf("Message %s has not yet been sent to discord", reaction.RelatesTo.EventID)
- return
- }
- discordID = msg.DiscordID
- }
- // Figure out if this is a custom emoji or not.
- emojiID := reaction.RelatesTo.Key
- if strings.HasPrefix(emojiID, "mxc://") {
- uri, _ := id.ParseContentURI(emojiID)
- emoji := p.bridge.db.Emoji.GetByMatrixURL(uri)
- if emoji == nil {
- p.log.Errorfln("failed to find emoji for %s", emojiID)
- return
- }
- emojiID = emoji.APIName()
- }
- err := user.Session.MessageReactionAdd(p.Key.ChannelID, discordID, emojiID)
- if err != nil {
- p.log.Debugf("Failed to send reaction %s id:%s: %v", p.Key, discordID, err)
- return
- }
- dbReaction := p.bridge.db.Reaction.New()
- dbReaction.Channel.ChannelID = p.Key.ChannelID
- dbReaction.Channel.Receiver = p.Key.Receiver
- dbReaction.MatrixEventID = evt.ID
- dbReaction.DiscordMessageID = discordID
- dbReaction.AuthorID = user.ID
- dbReaction.MatrixName = reaction.RelatesTo.Key
- dbReaction.DiscordID = emojiID
- dbReaction.Insert()
- }
- func (p *Portal) handleDiscordReaction(user *User, reaction *discordgo.MessageReaction, add bool) {
- intent := p.bridge.GetPuppetByID(reaction.UserID).IntentFor(p)
- var discordID string
- var matrixID string
- if reaction.Emoji.ID != "" {
- dbEmoji := p.bridge.db.Emoji.GetByDiscordID(reaction.Emoji.ID)
- if dbEmoji == nil {
- data, mimeType, err := p.downloadDiscordEmoji(reaction.Emoji.ID, reaction.Emoji.Animated)
- if err != nil {
- p.log.Warnfln("Failed to download emoji %s from discord: %v", reaction.Emoji.ID, err)
- return
- }
- uri, err := p.uploadMatrixEmoji(intent, data, mimeType)
- if err != nil {
- p.log.Warnfln("Failed to upload discord emoji %s to homeserver: %v", reaction.Emoji.ID, err)
- return
- }
- dbEmoji = p.bridge.db.Emoji.New()
- dbEmoji.DiscordID = reaction.Emoji.ID
- dbEmoji.DiscordName = reaction.Emoji.Name
- dbEmoji.MatrixURL = uri
- dbEmoji.Insert()
- }
- discordID = dbEmoji.DiscordID
- matrixID = dbEmoji.MatrixURL.String()
- } else {
- discordID = reaction.Emoji.Name
- matrixID = reaction.Emoji.Name
- }
- // Find the message that we're working with.
- message := p.bridge.db.Message.GetByDiscordID(p.Key, reaction.MessageID)
- if message == nil {
- p.log.Debugfln("failed to add reaction to message %s: message not found", reaction.MessageID)
- return
- }
- // Lookup an existing reaction
- existing := p.bridge.db.Reaction.GetByDiscordID(p.Key, message.DiscordID, discordID)
- if !add {
- if existing == nil {
- p.log.Debugln("Failed to remove reaction for unknown message", reaction.MessageID)
- return
- }
- _, err := intent.RedactEvent(p.MXID, existing.MatrixEventID)
- if err != nil {
- p.log.Warnfln("Failed to remove reaction from %s: %v", p.MXID, err)
- }
- existing.Delete()
- return
- }
- content := event.Content{Parsed: &event.ReactionEventContent{
- RelatesTo: event.RelatesTo{
- EventID: message.MatrixID,
- Type: event.RelAnnotation,
- Key: matrixID,
- },
- }}
- resp, err := intent.Client.SendMessageEvent(p.MXID, event.EventReaction, &content)
- if err != nil {
- p.log.Errorfln("failed to send reaction from %s: %v", reaction.MessageID, err)
- return
- }
- if existing == nil {
- dbReaction := p.bridge.db.Reaction.New()
- dbReaction.Channel = p.Key
- dbReaction.DiscordMessageID = message.DiscordID
- dbReaction.MatrixEventID = resp.EventID
- dbReaction.AuthorID = reaction.UserID
- dbReaction.MatrixName = matrixID
- dbReaction.DiscordID = discordID
- dbReaction.Insert()
- }
- }
- func (p *Portal) handleMatrixRedaction(evt *event.Event) {
- user := p.bridge.GetUserByMXID(evt.Sender)
- if user.ID != p.Key.Receiver {
- return
- }
- // First look if we're redacting a message
- message := p.bridge.db.Message.GetByMatrixID(p.Key, evt.Redacts)
- if message != nil {
- if message.DiscordID != "" {
- err := user.Session.ChannelMessageDelete(p.Key.ChannelID, message.DiscordID)
- if err != nil {
- p.log.Debugfln("Failed to delete discord message %s: %v", message.DiscordID, err)
- } else {
- message.Delete()
- }
- }
- return
- }
- // Now check if it's a reaction.
- reaction := p.bridge.db.Reaction.GetByMatrixID(p.Key, evt.Redacts)
- if reaction != nil {
- if reaction.DiscordID != "" {
- err := user.Session.MessageReactionRemove(p.Key.ChannelID, reaction.DiscordMessageID, reaction.DiscordID, reaction.AuthorID)
- if err != nil {
- p.log.Debugfln("Failed to delete reaction %s for message %s: %v", reaction.DiscordID, reaction.DiscordMessageID, err)
- } else {
- reaction.Delete()
- }
- }
- return
- }
- p.log.Warnfln("Failed to redact %s@%s: no event found", p.Key, evt.Redacts)
- }
- func (p *Portal) update(user *User, channel *discordgo.Channel) {
- name, err := p.bridge.Config.Bridge.FormatChannelname(channel, user.Session)
- if err != nil {
- p.log.Warnln("Failed to format channel name, using existing:", err)
- } else {
- p.Name = name
- }
- intent := p.MainIntent()
- if p.Name != name {
- _, err = intent.SetRoomName(p.MXID, p.Name)
- if err != nil {
- p.log.Warnln("Failed to update room name:", err)
- }
- }
- if p.Topic != channel.Topic {
- p.Topic = channel.Topic
- _, err = intent.SetRoomTopic(p.MXID, p.Topic)
- if err != nil {
- p.log.Warnln("Failed to update room topic:", err)
- }
- }
- if p.Avatar != channel.Icon {
- p.Avatar = channel.Icon
- var url string
- if p.Type == discordgo.ChannelTypeDM {
- dmUser, err := user.Session.User(p.DMUser)
- if err != nil {
- p.log.Warnln("failed to lookup the dmuser", err)
- } else {
- url = dmUser.AvatarURL("")
- }
- } else {
- url = discordgo.EndpointGroupIcon(channel.ID, channel.Icon)
- }
- p.AvatarURL = id.ContentURI{}
- if url != "" {
- uri, err := uploadAvatar(intent, url)
- if err != nil {
- p.log.Warnf("failed to upload avatar", err)
- } else {
- p.AvatarURL = uri
- }
- }
- intent.SetRoomAvatar(p.MXID, p.AvatarURL)
- }
- p.Update()
- p.log.Debugln("portal updated")
- }
|