|
@@ -17,21 +17,24 @@
|
|
package main
|
|
package main
|
|
|
|
|
|
import (
|
|
import (
|
|
- "maunium.net/go/mautrix-whatsapp/database"
|
|
|
|
- log "maunium.net/go/maulogger"
|
|
|
|
|
|
+ "bytes"
|
|
|
|
+ "encoding/hex"
|
|
"fmt"
|
|
"fmt"
|
|
- "maunium.net/go/mautrix-whatsapp/types"
|
|
|
|
- "maunium.net/go/gomatrix"
|
|
|
|
|
|
+ "image"
|
|
|
|
+ "math/rand"
|
|
|
|
+ "mime"
|
|
|
|
+ "net/http"
|
|
"strings"
|
|
"strings"
|
|
- "maunium.net/go/mautrix-appservice"
|
|
|
|
- "github.com/Rhymen/go-whatsapp"
|
|
|
|
"sync"
|
|
"sync"
|
|
- "net/http"
|
|
|
|
- "maunium.net/go/mautrix-whatsapp/whatsapp-ext"
|
|
|
|
- "mime"
|
|
|
|
- "image"
|
|
|
|
- "bytes"
|
|
|
|
|
|
+
|
|
|
|
+ "github.com/Rhymen/go-whatsapp"
|
|
|
|
+ "maunium.net/go/gomatrix"
|
|
"maunium.net/go/gomatrix/format"
|
|
"maunium.net/go/gomatrix/format"
|
|
|
|
+ log "maunium.net/go/maulogger"
|
|
|
|
+ "maunium.net/go/mautrix-appservice"
|
|
|
|
+ "maunium.net/go/mautrix-whatsapp/database"
|
|
|
|
+ "maunium.net/go/mautrix-whatsapp/types"
|
|
|
|
+ "maunium.net/go/mautrix-whatsapp/whatsapp-ext"
|
|
)
|
|
)
|
|
|
|
|
|
func (user *User) GetPortalByMXID(mxid types.MatrixRoomID) *Portal {
|
|
func (user *User) GetPortalByMXID(mxid types.MatrixRoomID) *Portal {
|
|
@@ -221,7 +224,7 @@ func (portal *Portal) CreateMatrixRoom() error {
|
|
name := portal.Name
|
|
name := portal.Name
|
|
topic := portal.Topic
|
|
topic := portal.Topic
|
|
isPrivateChat := false
|
|
isPrivateChat := false
|
|
- if strings.HasSuffix(portal.JID, "s.whatsapp.net") {
|
|
|
|
|
|
+ if strings.HasSuffix(portal.JID, whatsapp_ext.NewUserSuffix) {
|
|
puppet := portal.user.GetPuppetByJID(portal.JID)
|
|
puppet := portal.user.GetPuppetByJID(portal.JID)
|
|
name = puppet.Displayname
|
|
name = puppet.Displayname
|
|
topic = "WhatsApp private chat"
|
|
topic = "WhatsApp private chat"
|
|
@@ -244,7 +247,7 @@ func (portal *Portal) CreateMatrixRoom() error {
|
|
}
|
|
}
|
|
|
|
|
|
func (portal *Portal) IsPrivateChat() bool {
|
|
func (portal *Portal) IsPrivateChat() bool {
|
|
- return strings.HasSuffix(portal.JID, puppetJIDStrippedSuffix)
|
|
|
|
|
|
+ return strings.HasSuffix(portal.JID, whatsapp_ext.NewUserSuffix)
|
|
}
|
|
}
|
|
|
|
|
|
func (portal *Portal) MainIntent() *appservice.IntentAPI {
|
|
func (portal *Portal) MainIntent() *appservice.IntentAPI {
|
|
@@ -282,13 +285,18 @@ func (portal *Portal) GetMessageIntent(info whatsapp.MessageInfo) *appservice.In
|
|
return puppet.Intent()
|
|
return puppet.Intent()
|
|
}
|
|
}
|
|
|
|
|
|
-func (portal *Portal) GetRelations(info whatsapp.MessageInfo) (reply gomatrix.RelatesTo) {
|
|
|
|
|
|
+func (portal *Portal) SetReply(content *gomatrix.Content, info whatsapp.MessageInfo) {
|
|
if len(info.QuotedMessageID) == 0 {
|
|
if len(info.QuotedMessageID) == 0 {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
message := portal.bridge.DB.Message.GetByJID(portal.Owner, info.QuotedMessageID)
|
|
message := portal.bridge.DB.Message.GetByJID(portal.Owner, info.QuotedMessageID)
|
|
if message != nil {
|
|
if message != nil {
|
|
- reply.InReplyTo.EventID = message.MXID
|
|
|
|
|
|
+ event, err := portal.MainIntent().GetEvent(portal.MXID, message.MXID)
|
|
|
|
+ if err != nil {
|
|
|
|
+ portal.log.Warnln("Failed to get reply target:", err)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ content.SetReply(event)
|
|
}
|
|
}
|
|
return
|
|
return
|
|
|
|
|
|
@@ -299,18 +307,24 @@ func (portal *Portal) HandleTextMessage(message whatsapp.TextMessage) {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
- portal.CreateMatrixRoom()
|
|
|
|
|
|
+ err := portal.CreateMatrixRoom()
|
|
|
|
+ if err != nil {
|
|
|
|
+ portal.log.Errorln("Failed to create portal room:", err)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
|
|
intent := portal.GetMessageIntent(message.Info)
|
|
intent := portal.GetMessageIntent(message.Info)
|
|
if intent == nil {
|
|
if intent == nil {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
- resp, err := intent.SendMassagedMessageEvent(portal.MXID, gomatrix.EventMessage, gomatrix.Content{
|
|
|
|
- Body: message.Text,
|
|
|
|
- MsgType: gomatrix.MsgText,
|
|
|
|
- RelatesTo: portal.GetRelations(message.Info),
|
|
|
|
- }, int64(message.Info.Timestamp*1000))
|
|
|
|
|
|
+ content := gomatrix.Content{
|
|
|
|
+ Body: message.Text,
|
|
|
|
+ MsgType: gomatrix.MsgText,
|
|
|
|
+ }
|
|
|
|
+ portal.SetReply(&content, message.Info)
|
|
|
|
+
|
|
|
|
+ resp, err := intent.SendMassagedMessageEvent(portal.MXID, gomatrix.EventMessage, content, int64(message.Info.Timestamp*1000))
|
|
if err != nil {
|
|
if err != nil {
|
|
portal.log.Errorfln("Failed to handle message %s: %v", message.Info.Id, err)
|
|
portal.log.Errorfln("Failed to handle message %s: %v", message.Info.Id, err)
|
|
return
|
|
return
|
|
@@ -324,7 +338,11 @@ func (portal *Portal) HandleMediaMessage(download func() ([]byte, error), thumbn
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
- portal.CreateMatrixRoom()
|
|
|
|
|
|
+ err := portal.CreateMatrixRoom()
|
|
|
|
+ if err != nil {
|
|
|
|
+ portal.log.Errorln("Failed to create portal room:", err)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
|
|
intent := portal.GetMessageIntent(info)
|
|
intent := portal.GetMessageIntent(info)
|
|
if intent == nil {
|
|
if intent == nil {
|
|
@@ -353,12 +371,12 @@ func (portal *Portal) HandleMediaMessage(download func() ([]byte, error), thumbn
|
|
content := gomatrix.Content{
|
|
content := gomatrix.Content{
|
|
Body: caption,
|
|
Body: caption,
|
|
URL: uploaded.ContentURI,
|
|
URL: uploaded.ContentURI,
|
|
- Info: gomatrix.FileInfo{
|
|
|
|
|
|
+ Info: &gomatrix.FileInfo{
|
|
Size: len(data),
|
|
Size: len(data),
|
|
MimeType: mimeType,
|
|
MimeType: mimeType,
|
|
},
|
|
},
|
|
- RelatesTo: portal.GetRelations(info),
|
|
|
|
}
|
|
}
|
|
|
|
+ portal.SetReply(&content, info)
|
|
|
|
|
|
if thumbnail != nil {
|
|
if thumbnail != nil {
|
|
thumbnailMime := http.DetectContentType(thumbnail)
|
|
thumbnailMime := http.DetectContentType(thumbnail)
|
|
@@ -422,6 +440,12 @@ var htmlParser = format.HTMLParser{
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func makeMessageID() string {
|
|
|
|
+ b := make([]byte, 10)
|
|
|
|
+ rand.Read(b)
|
|
|
|
+ return strings.ToUpper(hex.EncodeToString(b))
|
|
|
|
+}
|
|
|
|
+
|
|
func (portal *Portal) HandleMatrixMessage(evt *gomatrix.Event) {
|
|
func (portal *Portal) HandleMatrixMessage(evt *gomatrix.Event) {
|
|
var err error
|
|
var err error
|
|
switch evt.Content.MsgType {
|
|
switch evt.Content.MsgType {
|
|
@@ -430,18 +454,21 @@ func (portal *Portal) HandleMatrixMessage(evt *gomatrix.Event) {
|
|
if evt.Content.Format == gomatrix.FormatHTML {
|
|
if evt.Content.Format == gomatrix.FormatHTML {
|
|
text = htmlParser.Parse(evt.Content.FormattedBody)
|
|
text = htmlParser.Parse(evt.Content.FormattedBody)
|
|
}
|
|
}
|
|
|
|
+ id := makeMessageID()
|
|
err = portal.user.Conn.Send(whatsapp.TextMessage{
|
|
err = portal.user.Conn.Send(whatsapp.TextMessage{
|
|
Text: text,
|
|
Text: text,
|
|
Info: whatsapp.MessageInfo{
|
|
Info: whatsapp.MessageInfo{
|
|
|
|
+ Id: id,
|
|
RemoteJid: portal.JID,
|
|
RemoteJid: portal.JID,
|
|
},
|
|
},
|
|
})
|
|
})
|
|
|
|
+ portal.MarkHandled(id, evt.ID)
|
|
default:
|
|
default:
|
|
portal.log.Debugln("Unhandled Matrix event:", evt)
|
|
portal.log.Debugln("Unhandled Matrix event:", evt)
|
|
return
|
|
return
|
|
}
|
|
}
|
|
if err != nil {
|
|
if err != nil {
|
|
- portal.log.Errorln("Error handling Matrix event %s: %v", evt.ID, err)
|
|
|
|
|
|
+ portal.log.Errorfln("Error handling Matrix event %s: %v", evt.ID, err)
|
|
} else {
|
|
} else {
|
|
portal.log.Debugln("Handled Matrix event:", evt)
|
|
portal.log.Debugln("Handled Matrix event:", evt)
|
|
}
|
|
}
|