2019-05-31 14:36:18 +02:00
|
|
|
package threema
|
|
|
|
|
2019-05-31 18:26:15 +02:00
|
|
|
import (
|
2019-06-01 19:26:03 +02:00
|
|
|
"errors"
|
2019-05-31 19:39:29 +02:00
|
|
|
|
2019-05-31 18:26:15 +02:00
|
|
|
"github.com/o3ma/o3"
|
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
|
2019-06-02 10:41:19 +02:00
|
|
|
receive <-chan o3.ReceivedMsg
|
2019-06-01 01:43:48 +02:00
|
|
|
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-06-01 15:21:49 +02:00
|
|
|
func (t *Threema) getAccount(jid *models.JID) (*Account, error) {
|
2019-05-31 18:26:15 +02:00
|
|
|
if a, ok := t.accountJID[jid.String()]; ok {
|
2019-06-01 15:21:49 +02:00
|
|
|
return a, nil
|
2019-05-31 18:26:15 +02:00
|
|
|
}
|
|
|
|
account := models.AccountThreema{}
|
|
|
|
|
2019-06-01 19:26:03 +02:00
|
|
|
if database.Read == nil {
|
|
|
|
return nil, errors.New("no database connection")
|
|
|
|
}
|
|
|
|
|
2019-05-31 18:26:15 +02:00
|
|
|
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{})
|
|
|
|
if err != nil {
|
2019-06-01 15:21:49 +02:00
|
|
|
return nil, err
|
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}
|
|
|
|
a.Session = o3.NewSessionContext(tid)
|
2019-06-02 10:41:19 +02:00
|
|
|
a.send, a.receive, err = a.Session.Run()
|
2019-05-31 18:26:15 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2019-06-01 15:21:49 +02:00
|
|
|
return nil, err
|
2019-05-31 18:26:15 +02:00
|
|
|
}
|
|
|
|
|
2019-06-01 15:21:49 +02:00
|
|
|
a.XMPP = *jid
|
|
|
|
a.deliveredMSG = make(map[uint64]string)
|
|
|
|
a.readedMSG = make(map[uint64]string)
|
|
|
|
|
2019-06-02 10:41:19 +02:00
|
|
|
go a.receiver(t.out)
|
2019-05-31 19:39:29 +02:00
|
|
|
|
2019-05-31 18:26:15 +02:00
|
|
|
t.accountJID[jid.String()] = a
|
2019-06-01 15:21:49 +02:00
|
|
|
return a, nil
|
2019-05-31 18:26:15 +02:00
|
|
|
}
|