Skip to content

Commit

Permalink
Merge branch '42wim:master' into cross-platform-delete
Browse files Browse the repository at this point in the history
  • Loading branch information
yousefmansy1 authored Mar 10, 2023
2 parents e5c3536 + 08779c2 commit 6d16a31
Show file tree
Hide file tree
Showing 384 changed files with 142,696 additions and 117,138 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
test-build-upload:
strategy:
matrix:
go-version: [1.18.x]
go-version: [1.20.x]
platform: [ubuntu-latest]
runs-on: ${{ matrix.platform }}
steps:
Expand All @@ -39,19 +39,19 @@ jobs:
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags "-s -X github.com/42wim/matterbridge/version.GitHash=$(git log --pretty=format:'%h' -n 1)" -o output/win/matterbridge-$VERSION-windows-amd64.exe
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags "-s -X github.com/42wim/matterbridge/version.GitHash=$(git log --pretty=format:'%h' -n 1)" -o output/mac/matterbridge-$VERSION-darwin-amd64
- name: Upload linux 64-bit
if: startsWith(matrix.go-version,'1.18')
if: startsWith(matrix.go-version,'1.20')
uses: actions/upload-artifact@v2
with:
name: matterbridge-linux-64bit
path: output/lin
- name: Upload windows 64-bit
if: startsWith(matrix.go-version,'1.18')
if: startsWith(matrix.go-version,'1.20')
uses: actions/upload-artifact@v2
with:
name: matterbridge-windows-64bit
path: output/win
- name: Upload darwin 64-bit
if: startsWith(matrix.go-version,'1.18')
if: startsWith(matrix.go-version,'1.20')
uses: actions/upload-artifact@v2
with:
name: matterbridge-darwin-64bit
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ And more...

### 3rd party via matterbridge api

- [Delta Chat](https://github.com/deltachat-bot/matterdelta)
- [Discourse](https://github.com/DeclanHoare/matterbabble)
- [Facebook messenger](https://github.com/powerjungle/fbridge-asyncio)
- [Facebook messenger](https://github.com/VictorNine/fbridge)
Expand Down
13 changes: 11 additions & 2 deletions bridge/telegram/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ func (b *Btelegram) handleUsername(rmsg *config.Message, message *tgbotapi.Messa
rmsg.Username = message.From.FirstName
}
if b.GetBool("UseFullName") {
rmsg.Username = message.From.FirstName + " " + message.From.LastName
if message.From.FirstName != "" && message.From.LastName != "" {
rmsg.Username = message.From.FirstName + " " + message.From.LastName
}
}
if rmsg.Username == "" {
rmsg.Username = message.From.UserName
Expand All @@ -148,7 +150,9 @@ func (b *Btelegram) handleUsername(rmsg *config.Message, message *tgbotapi.Messa
rmsg.Username = message.SenderChat.FirstName
}
if b.GetBool("UseFullName") {
rmsg.Username = message.SenderChat.FirstName + " " + message.SenderChat.LastName
if message.SenderChat.FirstName != "" && message.SenderChat.LastName != "" {
rmsg.Username = message.SenderChat.FirstName + " " + message.SenderChat.LastName
}
}

if rmsg.Username == "" || rmsg.Username == "Channel_Bot" {
Expand All @@ -164,6 +168,11 @@ func (b *Btelegram) handleUsername(rmsg *config.Message, message *tgbotapi.Messa
}
}

// Fallback on author signature (used in "channel" type of chat)
if rmsg.Username == "" && message.AuthorSignature != "" {
rmsg.Username = message.AuthorSignature
}

// if we really didn't find a username, set it to unknown
if rmsg.Username == "" {
rmsg.Username = unknownUser
Expand Down
39 changes: 31 additions & 8 deletions bridge/whatsappmulti/handlers.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build whatsappmulti
// +build whatsappmulti

package bwhatsapp
Expand Down Expand Up @@ -63,6 +64,10 @@ func (b *Bwhatsapp) handleTextMessage(messageInfo types.MessageInfo, msg *proto.
// nolint:nestif
if msg.GetExtendedTextMessage() == nil {
text = msg.GetConversation()
} else if msg.GetExtendedTextMessage().GetContextInfo() == nil {
// Handle pure text message with a link preview
// A pure text message with a link preview acts as an extended text message but will not contain any context info
text = msg.GetExtendedTextMessage().GetText()
} else {
text = msg.GetExtendedTextMessage().GetText()
ci := msg.GetExtendedTextMessage().GetContextInfo()
Expand All @@ -85,6 +90,12 @@ func (b *Bwhatsapp) handleTextMessage(messageInfo types.MessageInfo, msg *proto.
}
}

parentID := ""
if msg.GetExtendedTextMessage() != nil {
ci := msg.GetExtendedTextMessage().GetContextInfo()
parentID = getParentIdFromCtx(ci)
}

rmsg := config.Message{
UserID: senderJID.String(),
Username: senderName,
Expand All @@ -93,8 +104,8 @@ func (b *Bwhatsapp) handleTextMessage(messageInfo types.MessageInfo, msg *proto.
Account: b.Account,
Protocol: b.Protocol,
Extra: make(map[string][]interface{}),
// ParentID: TODO, // TODO handle thread replies // map from Info.QuotedMessageID string
ID: messageInfo.ID,
ID: getMessageIdFormat(senderJID, messageInfo.ID),
ParentID: parentID,
}

if avatarURL, exists := b.userAvatars[senderJID.String()]; exists {
Expand Down Expand Up @@ -126,7 +137,8 @@ func (b *Bwhatsapp) handleImageMessage(msg *events.Message) {
Account: b.Account,
Protocol: b.Protocol,
Extra: make(map[string][]interface{}),
ID: msg.Info.ID,
ID: getMessageIdFormat(senderJID, msg.Info.ID),
ParentID: getParentIdFromCtx(ci),
}

if avatarURL, exists := b.userAvatars[senderJID.String()]; exists {
Expand Down Expand Up @@ -189,7 +201,8 @@ func (b *Bwhatsapp) handleVideoMessage(msg *events.Message) {
Account: b.Account,
Protocol: b.Protocol,
Extra: make(map[string][]interface{}),
ID: msg.Info.ID,
ID: getMessageIdFormat(senderJID, msg.Info.ID),
ParentID: getParentIdFromCtx(ci),
}

if avatarURL, exists := b.userAvatars[senderJID.String()]; exists {
Expand All @@ -207,7 +220,16 @@ func (b *Bwhatsapp) handleVideoMessage(msg *events.Message) {
fileExt = append(fileExt, ".mp4")
}

filename := fmt.Sprintf("%v%v", msg.Info.ID, fileExt[0])
// Prefer .mp4 extension, otherwise fallback to first index
fileExtIndex := 0
for i, n := range fileExt {
if ".mp4" == n {
fileExtIndex = i
break
}
}

filename := fmt.Sprintf("%v%v", msg.Info.ID, fileExt[fileExtIndex])

b.Log.Debugf("Trying to download %s with size %#v and type %s", filename, imsg.GetFileLength(), imsg.GetMimetype())

Expand Down Expand Up @@ -238,15 +260,15 @@ func (b *Bwhatsapp) handleAudioMessage(msg *events.Message) {
if senderJID == (types.JID{}) && ci.Participant != nil {
senderJID = types.NewJID(ci.GetParticipant(), types.DefaultUserServer)
}

rmsg := config.Message{
UserID: senderJID.String(),
Username: senderName,
Channel: msg.Info.Chat.String(),
Account: b.Account,
Protocol: b.Protocol,
Extra: make(map[string][]interface{}),
ID: msg.Info.ID,
ID: getMessageIdFormat(senderJID, msg.Info.ID),
ParentID: getParentIdFromCtx(ci),
}

if avatarURL, exists := b.userAvatars[senderJID.String()]; exists {
Expand Down Expand Up @@ -303,7 +325,8 @@ func (b *Bwhatsapp) handleDocumentMessage(msg *events.Message) {
Account: b.Account,
Protocol: b.Protocol,
Extra: make(map[string][]interface{}),
ID: msg.Info.ID,
ID: getMessageIdFormat(senderJID, msg.Info.ID),
ParentID: getParentIdFromCtx(ci),
}

if avatarURL, exists := b.userAvatars[senderJID.String()]; exists {
Expand Down
63 changes: 63 additions & 0 deletions bridge/whatsappmulti/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import (
"fmt"
"strings"

goproto "google.golang.org/protobuf/proto"

"go.mau.fi/whatsmeow"
"go.mau.fi/whatsmeow/binary/proto"
"go.mau.fi/whatsmeow/store"
"go.mau.fi/whatsmeow/store/sqlstore"
"go.mau.fi/whatsmeow/types"
Expand Down Expand Up @@ -122,3 +125,63 @@ func (b *Bwhatsapp) getDevice() (*store.Device, error) {

return device, nil
}

func (b *Bwhatsapp) getNewReplyContext(parentID string) (*proto.ContextInfo, error) {
replyInfo, err := b.parseMessageID(parentID)

if err != nil {
return nil, err
}

sender := fmt.Sprintf("%s@%s", replyInfo.Sender.User, replyInfo.Sender.Server)
ctx := &proto.ContextInfo{
StanzaId: &replyInfo.MessageID,
Participant: &sender,
QuotedMessage: &proto.Message{Conversation: goproto.String("")},
}

return ctx, nil
}

func (b *Bwhatsapp) parseMessageID(id string) (*Replyable, error) {
// No message ID in case action is executed on a message sent before the bridge was started
// and then the bridge cache doesn't have this message ID mapped
if id == "" {
return &Replyable{MessageID: id}, nil
}

replyInfo := strings.Split(id, "/")

if len(replyInfo) == 2 {
sender, err := types.ParseJID(replyInfo[0])

if err == nil {
return &Replyable{
MessageID: types.MessageID(replyInfo[1]),
Sender: sender,
}, nil
}
}

err := fmt.Errorf("MessageID does not match format of {senderJID}:{messageID} : \"%s\"", id)

return &Replyable{MessageID: id}, err
}

func getParentIdFromCtx(ci *proto.ContextInfo) string {
if ci != nil && ci.StanzaId != nil {
senderJid, err := types.ParseJID(*ci.Participant)

if err == nil {
return getMessageIdFormat(senderJid, *ci.StanzaId)
}
}

return ""
}

func getMessageIdFormat(jid types.JID, messageID string) string {
// we're crafting our own JID str as AD JID format messes with how stuff looks on a webclient
jidStr := fmt.Sprintf("%s@%s", jid.User, jid.Server)
return fmt.Sprintf("%s/%s", jidStr, messageID)
}
Loading

0 comments on commit 6d16a31

Please sign in to comment.