|
|
|
@ -2,9 +2,10 @@ package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
|
|
"github.com/Rhymen/go-whatsapp"
|
|
|
|
|
"github.com/hugot/go-deltachat/deltachat"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type MessageHandler struct {
|
|
|
|
@ -16,48 +17,98 @@ type MessageAction func() error
|
|
|
|
|
|
|
|
|
|
func MakeTextMessageAction(b *BridgeContext, m whatsapp.TextMessage) MessageAction {
|
|
|
|
|
return func() error {
|
|
|
|
|
JID := m.Info.RemoteJid
|
|
|
|
|
if b.MessageWasSent(m.Info.Id) {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wasSent, err := b.MessageTracker.WasSent(m.Info.Id)
|
|
|
|
|
JID := m.Info.RemoteJid
|
|
|
|
|
DCID, err := b.GetOrCreateDCIDForJID(JID)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println(err)
|
|
|
|
|
b.SendLog(err.Error())
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Messgae has already been sent
|
|
|
|
|
if wasSent == true {
|
|
|
|
|
senderName := DetermineSenderName(b, m.Info)
|
|
|
|
|
|
|
|
|
|
b.DCContext.SendTextMessage(
|
|
|
|
|
DCID,
|
|
|
|
|
fmt.Sprintf("%s:\n%s", senderName, m.Text),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return b.MessageTracker.MarkSent(&m.Info.Id)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func MakeImageMessageAction(b *BridgeContext, m whatsapp.ImageMessage) MessageAction {
|
|
|
|
|
return func() error {
|
|
|
|
|
if b.MessageWasSent(m.Info.Id) {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JID := m.Info.RemoteJid
|
|
|
|
|
DCID, err := b.GetOrCreateDCIDForJID(JID)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println(err)
|
|
|
|
|
b.SendLog(err.Error())
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
senderName := m.Info.Source.GetParticipant()
|
|
|
|
|
senderName := DetermineSenderName(b, m.Info)
|
|
|
|
|
|
|
|
|
|
// No participant probably means that this isn't a group chat.
|
|
|
|
|
if senderName == "" {
|
|
|
|
|
senderName = m.Info.RemoteJid
|
|
|
|
|
imageData, err := m.Download()
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
b.SendLog(err.Error())
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if m.Info.FromMe == true {
|
|
|
|
|
senderName = b.DCContext.GetContact(b.DCUserID).GetDisplayName()
|
|
|
|
|
tmpFile, err := ioutil.TempFile(
|
|
|
|
|
b.Config.App.DataFolder+"/tmp",
|
|
|
|
|
"XXXXXXX-img",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
b.SendLog(err.Error())
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
contact, ok := b.WhappConn.Store.Contacts[senderName]
|
|
|
|
|
if ok {
|
|
|
|
|
senderName = contact.Name
|
|
|
|
|
err = ioutil.WriteFile(tmpFile.Name(), imageData, 0600)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
b.SendLog(err.Error())
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b.DCContext.SendTextMessage(
|
|
|
|
|
DCID,
|
|
|
|
|
fmt.Sprintf("%s:\n%s", senderName, m.Text),
|
|
|
|
|
)
|
|
|
|
|
message := b.DCContext.NewMessage(deltachat.DC_MSG_IMAGE)
|
|
|
|
|
message.SetText(fmt.Sprintf("%s:\n%s", senderName, m.Caption))
|
|
|
|
|
message.SetFile(tmpFile.Name(), m.Type)
|
|
|
|
|
|
|
|
|
|
b.DCContext.SendMessage(DCID, message)
|
|
|
|
|
|
|
|
|
|
return b.MessageTracker.MarkSent(&JID)
|
|
|
|
|
return b.MessageTracker.MarkSent(&m.Info.Id)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////
|
|
|
|
|
// Helpers
|
|
|
|
|
|
|
|
|
|
func DetermineSenderName(b *BridgeContext, info whatsapp.MessageInfo) string {
|
|
|
|
|
senderName := info.Source.GetParticipant()
|
|
|
|
|
|
|
|
|
|
// No participant probably means that this isn't a group chat.
|
|
|
|
|
if senderName == "" {
|
|
|
|
|
senderName = info.RemoteJid
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if info.FromMe == true {
|
|
|
|
|
return b.DCContext.GetContact(b.DCUserID).GetDisplayName()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
contact, ok := b.WhappConn.Store.Contacts[senderName]
|
|
|
|
|
if ok {
|
|
|
|
|
senderName = contact.Name
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return senderName
|
|
|
|
|
}
|
|
|
|
|