Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit e5bbcd2dbd4fed3dd7a347bb746109a5a198082b Author: Lucki <Lucki1@hotmail.de> Date: Tue Sep 06 00:46:52 2022 +0000 Add Matrix username spoofing (#1875) * Matrix username spoofing * Add config sample diff --git a/bridge/matrix/helpers.go b/bridge/matrix/helpers.go index b8e848a..5a91f74 100644 --- a/bridge/matrix/helpers.go +++ b/bridge/matrix/helpers.go @@ -517 +517 @@ func interface2Struct(in interface{}, out interface{}) error {   return json.Unmarshal(jsonObj, out)  }   -// getDisplayName retrieves the displayName for mxid, querying the homserver if the mxid is not in the cache. +// getDisplayName retrieves the displayName for mxid, querying the homeserver if the mxid is not in the cache.  func (b *Bmatrix) getDisplayName(mxid string) string {   if b.GetBool("UseUserName") {   return mxid[1:] diff --git a/bridge/matrix/matrix.go b/bridge/matrix/matrix.go index 702f4c8..49fc33b 100644 --- a/bridge/matrix/matrix.go +++ b/bridge/matrix/matrix.go @@ -14812 +14837 @@ func (b *Bmatrix) Send(msg config.Message) (string, error) {     username := newMatrixUsername(msg.Username)   + body := username.plain + msg.Text + formattedBody := username.formatted + helper.ParseMarkdown(msg.Text) + + if b.GetBool("SpoofUsername") { + // https://spec.matrix.org/v1.3/client-server-api/#mroommember + type stateMember struct { + AvatarURL string `json:"avatar_url,omitempty"` + DisplayName string `json:"displayname"` + Membership string `json:"membership"` + } + + // TODO: reset username afterwards with DisplayName: null ? + m := stateMember{ + AvatarURL: "", + DisplayName: username.plain, + Membership: "join", + } + + _, err := b.mc.SendStateEvent(channel, "m.room.member", b.UserID, m) + if err == nil { + body = msg.Text + formattedBody = helper.ParseMarkdown(msg.Text) + } + } +   // Make a action /me of the message   if msg.Event == config.EventUserAction {   m := matrix.TextMessage{   MsgType: "m.emote", - Body: username.plain + msg.Text, - FormattedBody: username.formatted + helper.ParseMarkdown(msg.Text), + Body: body, + FormattedBody: formattedBody,   Format: "org.matrix.custom.html",   }   @@ -22410 +24910 @@ func (b *Bmatrix) Send(msg config.Message) (string, error) {   if msg.ID != "" {   rmsg := EditedMessage{   TextMessage: matrix.TextMessage{ - Body: username.plain + msg.Text, + Body: body,   MsgType: "m.text",   Format: "org.matrix.custom.html", - FormattedBody: username.formatted + helper.ParseMarkdown(msg.Text), + FormattedBody: formattedBody,   },   }   @@ -2668 +2918 @@ func (b *Bmatrix) Send(msg config.Message) (string, error) {   if msg.Event == config.EventJoinLeave {   m := matrix.TextMessage{   MsgType: "m.notice", - Body: username.plain + msg.Text, - FormattedBody: username.formatted + msg.Text, + Body: body, + FormattedBody: formattedBody,   Format: "org.matrix.custom.html",   }   @@ -2978 +3228 @@ func (b *Bmatrix) Send(msg config.Message) (string, error) {   m := ReplyMessage{   TextMessage: matrix.TextMessage{   MsgType: "m.text", - Body: username.plain + msg.Text, - FormattedBody: username.formatted + helper.ParseMarkdown(msg.Text), + Body: body, + FormattedBody: formattedBody,   Format: "org.matrix.custom.html",   },   } @@ -3387 +3637 @@ func (b *Bmatrix) Send(msg config.Message) (string, error) {   )     err = b.retry(func() error { - resp, err = b.mc.SendText(channel, username.plain+msg.Text) + resp, err = b.mc.SendText(channel, body)     return err   }) @@ -3568 +3817 @@ func (b *Bmatrix) Send(msg config.Message) (string, error) {   )     err = b.retry(func() error { - resp, err = b.mc.SendFormattedText(channel, username.plain+msg.Text, - username.formatted+helper.ParseMarkdown(msg.Text)) + resp, err = b.mc.SendFormattedText(channel, body, formattedBody)     return err   }) diff --git a/matterbridge.toml.sample b/matterbridge.toml.sample index 41ab44b..97b27d7 100644 --- a/matterbridge.toml.sample +++ b/matterbridge.toml.sample @@ -13906 +139011 @@ RemoteNickFormat="[{PROTOCOL}] <{NICK}> "  #OPTIONAL (default false)  ShowJoinPart=false   +#Rename the bot in the current room to the username of the message +#This will make an additional API request per message and will probably count towards rate limits +#OPTIONAL (default false) +SpoofUsername=false +  #StripNick only allows alphanumerical nicks. See https://github.com/42wim/matterbridge/issues/285  #It will strip other characters from the nick  #OPTIONAL (default false)