This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
thrempp/component/threema/account.go

155 lines
3.7 KiB
Go
Raw Normal View History

2019-05-31 14:36:18 +02:00
package threema
2019-05-31 18:26:15 +02:00
import (
2019-05-31 19:39:29 +02:00
"strconv"
"github.com/bdlm/log"
2019-05-31 18:26:15 +02:00
"github.com/o3ma/o3"
2019-05-31 19:39:29 +02:00
"gosrc.io/xmpp"
2019-05-31 14:36:18 +02:00
2019-05-31 18:26:15 +02:00
"dev.sum7.eu/genofire/golang-lib/database"
"dev.sum7.eu/genofire/thrempp/models"
)
2019-05-31 14:36:18 +02:00
2019-05-31 18:26:15 +02:00
type Account struct {
models.AccountThreema
2019-06-01 01:43:48 +02:00
Session o3.SessionContext
send chan<- o3.Message
recieve <-chan o3.ReceivedMsg
deliveredMSG map[uint64]string
2019-06-01 02:32:34 +02:00
readedMSG map[uint64]string
2019-05-31 14:36:18 +02:00
}
2019-05-31 18:26:15 +02:00
func (t *Threema) getAccount(jid *models.JID) *Account {
if a, ok := t.accountJID[jid.String()]; ok {
return a
}
account := models.AccountThreema{}
database.Read.Where("xmpp_id = (?)",
database.Read.Table(jid.TableName()).Select("id").Where(map[string]interface{}{
"local": jid.Local,
"domain": jid.Domain,
}).QueryExpr()).First(&account)
var lsk [32]byte
copy(lsk[:], account.LSK[:])
tid, err := o3.NewThreemaID(string(account.TID), lsk, o3.AddressBook{})
// TODO error handling
if err != nil {
return nil
2019-05-31 14:36:18 +02:00
}
2019-05-31 18:26:15 +02:00
tid.Nick = o3.NewPubNick("xmpp:" + jid.String())
a := &Account{AccountThreema: account}
2019-05-31 19:39:29 +02:00
a.XMPP = *jid
2019-05-31 18:26:15 +02:00
a.Session = o3.NewSessionContext(tid)
a.send, a.recieve, err = a.Session.Run()
2019-06-01 01:43:48 +02:00
a.deliveredMSG = make(map[uint64]string)
2019-06-01 02:32:34 +02:00
a.readedMSG = make(map[uint64]string)
2019-05-31 18:26:15 +02:00
// TODO error handling
if err != nil {
return nil
}
2019-05-31 19:39:29 +02:00
go a.reciever(t.out)
2019-05-31 18:26:15 +02:00
t.accountJID[jid.String()] = a
return a
}
2019-05-31 19:39:29 +02:00
func (a *Account) reciever(out chan<- xmpp.Packet) {
for receivedMessage := range a.recieve {
if receivedMessage.Err != nil {
log.Warnf("Error Receiving Message: %s\n", receivedMessage.Err)
continue
}
switch msg := receivedMessage.Msg.(type) {
case o3.TextMessage:
sender := msg.Sender().String()
if string(a.TID) == sender {
continue
}
xMSG := xmpp.NewMessage("chat", sender, a.XMPP.String(), strconv.FormatUint(msg.ID(), 10), "en")
xMSG.Body = msg.Text()
2019-06-01 01:43:48 +02:00
xMSG.Extensions = append(xMSG.Extensions, xmpp.ReceiptRequest{})
2019-06-01 02:32:34 +02:00
xMSG.Extensions = append(xMSG.Extensions, xmpp.ChatMarkerMarkable{})
2019-05-31 19:39:29 +02:00
out <- xMSG
2019-06-01 02:32:34 +02:00
2019-05-31 19:39:29 +02:00
case o3.DeliveryReceiptMessage:
2019-06-01 02:32:34 +02:00
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")
}
2019-06-01 01:43:48 +02:00
}
2019-05-31 19:39:29 +02:00
2019-06-01 02:32:34 +02:00
if len(xMSG.Extensions) > 0 {
out <- xMSG
}
2019-05-31 19:39:29 +02:00
}
}
}
2019-06-01 01:43:48 +02:00
func (a *Account) Send(to string, msg xmpp.Message) error {
2019-06-01 02:32:34 +02:00
msgID := ""
readed := false
2019-06-01 01:43:48 +02:00
for _, el := range msg.Extensions {
switch ex := el.(type) {
case *xmpp.ReceiptReceived:
2019-06-01 02:32:34 +02:00
msgID = ex.Id
case *xmpp.ChatMarkerReceived:
msgID = ex.Id
case *xmpp.ChatMarkerDisplayed:
readed = true
msgID = ex.Id
2019-06-01 01:43:48 +02:00
}
}
2019-06-01 02:32:34 +02:00
if msgID != "" {
id, err := strconv.ParseUint(msgID, 10, 64)
2019-06-01 01:43:48 +02:00
if err != nil {
return err
}
2019-06-01 02:32:34 +02:00
msgType := o3.MSGDELIVERED
if readed {
msgType = o3.MSGREAD
}
drm, err := o3.NewDeliveryReceiptMessage(&a.Session, to, id, msgType)
2019-06-01 01:43:48 +02:00
if err != nil {
return err
}
a.send <- drm
log.WithFields(map[string]interface{}{
"tid": to,
"msg_id": id,
2019-06-01 02:32:34 +02:00
"type": msgType,
2019-06-01 02:59:00 +02:00
}).Debug("update status of threema message")
2019-06-01 01:43:48 +02:00
return nil
}
msg3, err := o3.NewTextMessage(&a.Session, to, msg.Body)
if err != nil {
return err
}
a.deliveredMSG[msg3.ID()] = msg.Id
2019-06-01 02:32:34 +02:00
a.readedMSG[msg3.ID()] = msg.Id
2019-06-01 01:43:48 +02:00
a.send <- msg3
return nil
2019-05-31 14:36:18 +02:00
}