2019-06-02 00:50:54 +02:00
|
|
|
package threema
|
|
|
|
|
|
|
|
import (
|
2019-09-06 04:34:13 +02:00
|
|
|
"encoding/base32"
|
2019-06-02 00:50:54 +02:00
|
|
|
"strconv"
|
2019-09-06 04:34:13 +02:00
|
|
|
"strings"
|
2019-06-02 00:50:54 +02:00
|
|
|
|
|
|
|
"github.com/bdlm/log"
|
|
|
|
"github.com/o3ma/o3"
|
2019-06-28 03:03:38 +02:00
|
|
|
"gosrc.io/xmpp/stanza"
|
2019-06-02 00:50:54 +02:00
|
|
|
)
|
|
|
|
|
2019-06-28 03:03:38 +02:00
|
|
|
func (a *Account) Send(to string, msg stanza.Message) error {
|
2019-06-02 10:41:19 +02:00
|
|
|
m, err := a.sending(to, msg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if m != nil {
|
|
|
|
a.send <- m
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-06-28 03:03:38 +02:00
|
|
|
func (a *Account) sending(to string, msg stanza.Message) (o3.Message, error) {
|
2019-09-06 03:08:58 +02:00
|
|
|
from := string(a.AccountThreema.TID)
|
2019-06-06 22:54:45 +02:00
|
|
|
logger := log.WithFields(map[string]interface{}{
|
2019-09-06 03:08:58 +02:00
|
|
|
"from": a.XMPP.String(),
|
|
|
|
"from_t": from,
|
|
|
|
"to": to,
|
2019-06-06 22:54:45 +02:00
|
|
|
})
|
2019-09-06 03:57:27 +02:00
|
|
|
msg3ID := o3.NewMsgID()
|
|
|
|
header := &o3.MessageHeader{
|
|
|
|
Sender: o3.NewIDString(from),
|
|
|
|
ID: msg3ID,
|
|
|
|
Recipient: o3.NewIDString(to),
|
|
|
|
PubNick: a.ThreemaID.Nick,
|
|
|
|
}
|
2019-09-06 04:34:13 +02:00
|
|
|
var groupHeader *o3.GroupMessageHeader
|
|
|
|
if msg.Type == stanza.MessageTypeGroupchat {
|
|
|
|
toA := strings.SplitN(to, "-", 2)
|
|
|
|
gid, err := base32.StdEncoding.DecodeString(toA[1])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
groupHeader = &o3.GroupMessageHeader{
|
|
|
|
CreatorID: o3.NewIDString(toA[0]),
|
|
|
|
}
|
|
|
|
copy(groupHeader.GroupID[:], gid)
|
|
|
|
}
|
2019-06-06 22:05:33 +02:00
|
|
|
chatState := false
|
|
|
|
chatStateComposing := false
|
2019-06-06 22:54:45 +02:00
|
|
|
|
2019-06-06 22:05:33 +02:00
|
|
|
msgStateID := ""
|
|
|
|
msgStateRead := false
|
|
|
|
|
2019-06-02 00:50:54 +02:00
|
|
|
for _, el := range msg.Extensions {
|
|
|
|
switch ex := el.(type) {
|
2019-06-06 22:54:45 +02:00
|
|
|
|
2019-06-28 03:03:38 +02:00
|
|
|
case *stanza.StateActive:
|
2019-06-06 22:05:33 +02:00
|
|
|
chatState = true
|
2019-06-28 03:03:38 +02:00
|
|
|
case *stanza.StateComposing:
|
2019-06-06 22:05:33 +02:00
|
|
|
chatState = true
|
|
|
|
chatStateComposing = true
|
2019-06-28 03:03:38 +02:00
|
|
|
case *stanza.StateGone:
|
2019-06-06 22:05:33 +02:00
|
|
|
chatState = true
|
2019-06-28 03:03:38 +02:00
|
|
|
case *stanza.StateInactive:
|
2019-06-06 22:05:33 +02:00
|
|
|
chatState = true
|
2019-06-28 03:03:38 +02:00
|
|
|
case *stanza.StatePaused:
|
2019-06-06 22:05:33 +02:00
|
|
|
chatState = true
|
2019-06-06 22:54:45 +02:00
|
|
|
|
2019-06-28 03:03:38 +02:00
|
|
|
case *stanza.ReceiptReceived:
|
2019-06-06 22:05:33 +02:00
|
|
|
msgStateID = ex.ID
|
2019-06-28 03:03:38 +02:00
|
|
|
case *stanza.MarkReceived:
|
2019-06-06 22:05:33 +02:00
|
|
|
msgStateID = ex.ID
|
2019-06-06 22:54:45 +02:00
|
|
|
|
2019-06-28 03:03:38 +02:00
|
|
|
case *stanza.MarkDisplayed:
|
2019-06-06 22:05:33 +02:00
|
|
|
msgStateRead = true
|
|
|
|
msgStateID = ex.ID
|
2019-06-02 00:50:54 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-06 22:05:33 +02:00
|
|
|
if msg.Body == "" {
|
|
|
|
if msgStateID != "" {
|
|
|
|
id, err := strconv.ParseUint(msgStateID, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-06 03:08:58 +02:00
|
|
|
drm := &o3.DeliveryReceiptMessage{
|
2019-09-06 03:57:27 +02:00
|
|
|
MessageHeader: header,
|
|
|
|
Status: o3.MSGDELIVERED,
|
|
|
|
MessageID: id,
|
2019-06-06 22:05:33 +02:00
|
|
|
}
|
2019-08-30 17:28:21 +02:00
|
|
|
if msgStateRead {
|
|
|
|
drm.Status = o3.MSGREAD
|
2019-06-06 22:05:33 +02:00
|
|
|
}
|
2019-06-06 22:54:45 +02:00
|
|
|
logger.WithFields(map[string]interface{}{
|
2019-06-06 22:05:33 +02:00
|
|
|
"msg_id": id,
|
2019-08-30 17:28:21 +02:00
|
|
|
"type": drm.Status,
|
2019-06-06 22:05:33 +02:00
|
|
|
}).Debug("update status of threema message")
|
|
|
|
return drm, nil
|
2019-06-02 00:50:54 +02:00
|
|
|
}
|
2019-06-06 22:05:33 +02:00
|
|
|
if chatState {
|
2019-09-06 03:08:58 +02:00
|
|
|
tnm := &o3.TypingNotificationMessage{
|
2019-09-06 03:57:27 +02:00
|
|
|
MessageHeader: header,
|
2019-08-30 17:28:21 +02:00
|
|
|
}
|
2019-06-06 22:54:45 +02:00
|
|
|
if chatStateComposing {
|
|
|
|
tnm.OnOff = 0x1
|
|
|
|
}
|
|
|
|
logger.WithFields(map[string]interface{}{
|
2019-09-06 03:57:27 +02:00
|
|
|
"on": tnm.OnOff,
|
2019-09-06 03:08:58 +02:00
|
|
|
}).Debug("send typing")
|
|
|
|
return tnm, nil
|
2019-06-02 00:50:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-02 10:41:19 +02:00
|
|
|
// send text message
|
2019-09-06 03:08:58 +02:00
|
|
|
msg3 := &o3.TextMessage{
|
2019-09-06 03:57:27 +02:00
|
|
|
MessageHeader: header,
|
|
|
|
Body: msg.Body,
|
2019-06-02 00:50:54 +02:00
|
|
|
}
|
2019-09-06 04:34:13 +02:00
|
|
|
logger = logger.WithFields(map[string]interface{}{
|
2019-06-06 22:54:45 +02:00
|
|
|
"x_id": msg.Id,
|
2019-08-30 17:28:21 +02:00
|
|
|
"t_id": msg3ID,
|
2019-06-06 22:54:45 +02:00
|
|
|
"text": msg.Body,
|
2019-09-06 04:34:13 +02:00
|
|
|
})
|
|
|
|
if groupHeader != nil {
|
|
|
|
logger.Debug("send grouptext")
|
|
|
|
// TODO iterate of all occupants
|
|
|
|
return &o3.GroupTextMessage{
|
|
|
|
GroupMessageHeader: groupHeader,
|
|
|
|
TextMessage: msg3,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
a.deliveredMSG[msg3ID] = msg.Id
|
|
|
|
a.readedMSG[msg3ID] = msg.Id
|
|
|
|
logger.Debug("send text")
|
2019-06-02 10:41:19 +02:00
|
|
|
return msg3, nil
|
2019-06-02 00:50:54 +02:00
|
|
|
}
|