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) {
|
2019-06-06 22:54:45 +02:00
|
|
|
logger := log.WithFields(map[string]interface{}{
|
|
|
|
"from": a.XMPP.String(),
|
|
|
|
"to": to,
|
|
|
|
})
|
|
|
|
|
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
|
|
|
|
|
|
|
case *xmpp.StateActive:
|
2019-06-06 22:05:33 +02:00
|
|
|
chatState = true
|
2019-06-06 22:54:45 +02:00
|
|
|
case *xmpp.StateComposing:
|
2019-06-06 22:05:33 +02:00
|
|
|
chatState = true
|
|
|
|
chatStateComposing = true
|
2019-06-06 22:54:45 +02:00
|
|
|
case *xmpp.StateGone:
|
2019-06-06 22:05:33 +02:00
|
|
|
chatState = true
|
2019-06-06 22:54:45 +02:00
|
|
|
case *xmpp.StateInactive:
|
2019-06-06 22:05:33 +02:00
|
|
|
chatState = true
|
2019-06-06 22:54:45 +02:00
|
|
|
case *xmpp.StatePaused:
|
2019-06-06 22:05:33 +02:00
|
|
|
chatState = true
|
2019-06-06 22:54:45 +02:00
|
|
|
|
|
|
|
case *xmpp.ReceiptReceived:
|
2019-06-06 22:05:33 +02:00
|
|
|
msgStateID = ex.ID
|
2019-06-06 22:54:45 +02:00
|
|
|
case *xmpp.MarkReceived:
|
2019-06-06 22:05:33 +02:00
|
|
|
msgStateID = ex.ID
|
2019-06-06 22:54:45 +02:00
|
|
|
|
|
|
|
case *xmpp.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
|
|
|
|
}
|
|
|
|
msgType := o3.MSGDELIVERED
|
|
|
|
if msgStateRead {
|
|
|
|
msgType = o3.MSGREAD
|
|
|
|
}
|
|
|
|
drm, err := o3.NewDeliveryReceiptMessage(&a.Session, to, id, msgType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-06 22:54:45 +02:00
|
|
|
logger.WithFields(map[string]interface{}{
|
2019-06-06 22:05:33 +02:00
|
|
|
"msg_id": id,
|
|
|
|
"type": msgType,
|
|
|
|
}).Debug("update status of threema message")
|
|
|
|
return drm, nil
|
2019-06-02 00:50:54 +02:00
|
|
|
}
|
2019-06-06 22:54:45 +02:00
|
|
|
|
2019-06-06 22:05:33 +02:00
|
|
|
if chatState {
|
2019-06-06 22:54:45 +02:00
|
|
|
tnm := o3.TypingNotificationMessage{}
|
|
|
|
if chatStateComposing {
|
|
|
|
tnm.OnOff = 0x1
|
|
|
|
}
|
|
|
|
logger.WithFields(map[string]interface{}{
|
|
|
|
"state": chatStateComposing,
|
|
|
|
}).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-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-06 22:54:45 +02:00
|
|
|
logger.WithFields(map[string]interface{}{
|
|
|
|
"x_id": msg.Id,
|
|
|
|
"t_id": msg3.ID(),
|
|
|
|
"text": msg.Body,
|
|
|
|
}).Debug("send text")
|
2019-06-02 10:41:19 +02:00
|
|
|
return msg3, nil
|
2019-06-02 00:50:54 +02:00
|
|
|
}
|