2019-06-02 00:50:54 +02:00
|
|
|
package threema
|
|
|
|
|
|
|
|
import (
|
2019-06-02 21:39:21 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-06-02 00:50:54 +02:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/bdlm/log"
|
|
|
|
"github.com/o3ma/o3"
|
|
|
|
"gosrc.io/xmpp"
|
|
|
|
)
|
|
|
|
|
2019-06-02 10:41:19 +02:00
|
|
|
func (a *Account) receiver(out chan<- xmpp.Packet) {
|
|
|
|
for receivedMessage := range a.receive {
|
2019-06-02 21:39:21 +02:00
|
|
|
sender := receivedMessage.Msg.Sender().String()
|
|
|
|
if string(a.TID) == sender {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if p, err := a.receiving(receivedMessage); err != nil {
|
|
|
|
xMSG := xmpp.NewMessage("chat", sender, a.XMPP.String(), "", "en")
|
|
|
|
xMSG.Body = fmt.Sprintf("error on decoding message: %s\n%v", err, receivedMessage.Msg.Serialize())
|
|
|
|
out <- xMSG
|
|
|
|
} else if p != nil {
|
2019-06-02 00:50:54 +02:00
|
|
|
out <- p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-02 21:39:21 +02:00
|
|
|
|
|
|
|
func requestExtensions(xMSG *xmpp.Message) {
|
|
|
|
xMSG.Extensions = append(xMSG.Extensions, xmpp.ReceiptRequest{})
|
|
|
|
xMSG.Extensions = append(xMSG.Extensions, xmpp.ChatMarkerMarkable{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Account) receiving(receivedMessage o3.ReceivedMsg) (xmpp.Packet, error) {
|
2019-06-02 00:50:54 +02:00
|
|
|
if receivedMessage.Err != nil {
|
|
|
|
log.Warnf("Error Receiving Message: %s\n", receivedMessage.Err)
|
2019-06-02 21:39:21 +02:00
|
|
|
return nil, receivedMessage.Err
|
2019-06-02 00:50:54 +02:00
|
|
|
}
|
|
|
|
switch msg := receivedMessage.Msg.(type) {
|
|
|
|
case o3.TextMessage:
|
|
|
|
sender := msg.Sender().String()
|
|
|
|
xMSG := xmpp.NewMessage("chat", sender, a.XMPP.String(), strconv.FormatUint(msg.ID(), 10), "en")
|
|
|
|
xMSG.Body = msg.Text()
|
2019-06-02 21:39:21 +02:00
|
|
|
requestExtensions(&xMSG)
|
|
|
|
return xMSG, nil
|
|
|
|
|
|
|
|
case o3.ImageMessage:
|
|
|
|
if a.threema.httpUploadPath == "" {
|
|
|
|
return nil, errors.New("no place to store files at transport configurated")
|
|
|
|
}
|
|
|
|
data, err := msg.GetImageData(a.Session)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("unable to read data from message: %s", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
xMSG, err := a.FileToXMPP(msg.Sender().String(), msg.ID(), "jpg", data)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("unable to create data from message: %s", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
xMSG.Type = "chat"
|
|
|
|
requestExtensions(&xMSG)
|
|
|
|
return xMSG, nil
|
2019-06-02 00:50:54 +02:00
|
|
|
|
|
|
|
case o3.DeliveryReceiptMessage:
|
|
|
|
msgID := msg.MsgID()
|
|
|
|
xMSG := xmpp.NewMessage("chat", msg.Sender().String(), a.XMPP.String(), "", "en")
|
|
|
|
|
|
|
|
if msg.Status() == o3.MSGDELIVERED {
|
|
|
|
if id, ok := a.deliveredMSG[msgID]; ok {
|
|
|
|
xMSG.Extensions = append(xMSG.Extensions, xmpp.ReceiptReceived{Id: id})
|
|
|
|
xMSG.Extensions = append(xMSG.Extensions, xmpp.ChatMarkerReceived{Id: id})
|
|
|
|
delete(a.deliveredMSG, msgID)
|
|
|
|
} else {
|
|
|
|
log.Warnf("found not id in cache to announce received on xmpp side")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if msg.Status() == o3.MSGREAD {
|
|
|
|
if id, ok := a.readedMSG[msgID]; ok {
|
|
|
|
xMSG.Extensions = append(xMSG.Extensions, xmpp.ChatMarkerDisplayed{Id: id})
|
|
|
|
delete(a.readedMSG, msgID)
|
|
|
|
} else {
|
|
|
|
log.Warnf("found not id in cache to announce readed on xmpp side")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(xMSG.Extensions) > 0 {
|
2019-06-02 21:39:21 +02:00
|
|
|
return xMSG, nil
|
2019-06-02 00:50:54 +02:00
|
|
|
}
|
2019-06-02 21:39:21 +02:00
|
|
|
return nil, nil
|
2019-06-02 00:50:54 +02:00
|
|
|
}
|
2019-06-02 21:39:21 +02:00
|
|
|
return nil, errors.New("not known data format")
|
2019-06-02 00:50:54 +02:00
|
|
|
}
|