2019-06-02 00:50:54 +02:00
|
|
|
package threema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/bdlm/log"
|
|
|
|
"github.com/o3ma/o3"
|
|
|
|
"gosrc.io/xmpp"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (a *Account) Send(to string, msg xmpp.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
|
|
|
|
}
|
|
|
|
func (a *Account) sending(to string, msg xmpp.Message) (o3.Message, error) {
|
|
|
|
// handle delivered / readed
|
2019-06-02 00:50:54 +02:00
|
|
|
msgID := ""
|
|
|
|
readed := false
|
2019-06-06 20:53:06 +02:00
|
|
|
composing := false
|
|
|
|
state := false
|
2019-06-02 00:50:54 +02:00
|
|
|
for _, el := range msg.Extensions {
|
|
|
|
switch ex := el.(type) {
|
2019-06-06 20:53:06 +02:00
|
|
|
case xmpp.StateComposing:
|
|
|
|
composing = true
|
|
|
|
state = true
|
|
|
|
case xmpp.StateInactive:
|
|
|
|
state = true
|
|
|
|
case xmpp.StateActive:
|
|
|
|
state = true
|
|
|
|
case xmpp.StateGone:
|
|
|
|
state = true
|
2019-06-05 04:30:00 +02:00
|
|
|
case xmpp.ReceiptReceived:
|
|
|
|
msgID = ex.ID
|
|
|
|
case xmpp.MarkReceived:
|
|
|
|
msgID = ex.ID
|
|
|
|
case xmpp.MarkDisplayed:
|
2019-06-02 00:50:54 +02:00
|
|
|
readed = true
|
2019-06-05 04:30:00 +02:00
|
|
|
msgID = ex.ID
|
2019-06-02 00:50:54 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-06 20:53:06 +02:00
|
|
|
if composing {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if state && msg.Body == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2019-06-02 00:50:54 +02:00
|
|
|
if msgID != "" {
|
|
|
|
id, err := strconv.ParseUint(msgID, 10, 64)
|
|
|
|
if err != nil {
|
2019-06-02 10:41:19 +02:00
|
|
|
return nil, err
|
2019-06-02 00:50:54 +02:00
|
|
|
}
|
|
|
|
msgType := o3.MSGDELIVERED
|
|
|
|
if readed {
|
|
|
|
msgType = o3.MSGREAD
|
|
|
|
}
|
|
|
|
drm, err := o3.NewDeliveryReceiptMessage(&a.Session, to, id, msgType)
|
|
|
|
if err != nil {
|
2019-06-02 10:41:19 +02:00
|
|
|
return nil, err
|
2019-06-02 00:50:54 +02:00
|
|
|
}
|
|
|
|
log.WithFields(map[string]interface{}{
|
|
|
|
"tid": to,
|
|
|
|
"msg_id": id,
|
|
|
|
"type": msgType,
|
|
|
|
}).Debug("update status of threema message")
|
2019-06-02 10:41:19 +02:00
|
|
|
return drm, nil
|
2019-06-02 00:50:54 +02:00
|
|
|
}
|
|
|
|
|
2019-06-02 10:41:19 +02:00
|
|
|
// send text message
|
2019-06-02 00:50:54 +02:00
|
|
|
msg3, err := o3.NewTextMessage(&a.Session, to, msg.Body)
|
|
|
|
if err != nil {
|
2019-06-02 10:41:19 +02:00
|
|
|
return nil, err
|
2019-06-02 00:50:54 +02:00
|
|
|
}
|
|
|
|
a.deliveredMSG[msg3.ID()] = msg.Id
|
|
|
|
a.readedMSG[msg3.ID()] = msg.Id
|
2019-06-02 10:41:19 +02:00
|
|
|
return msg3, nil
|
2019-06-02 00:50:54 +02:00
|
|
|
}
|