73 lines
1.6 KiB
Go
73 lines
1.6 KiB
Go
package threema
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/o3ma/o3"
|
|
"gosrc.io/xmpp/stanza"
|
|
|
|
"dev.sum7.eu/genofire/golang-lib/database"
|
|
|
|
"dev.sum7.eu/sum7/thrempp/models"
|
|
)
|
|
|
|
type Account struct {
|
|
models.AccountThreema
|
|
threema *Threema
|
|
ThreemaID *o3.ThreemaID
|
|
send chan<- o3.Message
|
|
receive <-chan o3.ReceivedMsg
|
|
xmpp chan<- stanza.Packet
|
|
deliveredMSG map[uint64]string
|
|
readedMSG map[uint64]string
|
|
XMPPResource map[string]map[string]bool
|
|
}
|
|
|
|
func (t *Threema) getAccount(jid *models.JID) (*Account, error) {
|
|
if a, ok := t.accountJID[jid.String()]; ok {
|
|
return a, nil
|
|
}
|
|
account := models.AccountThreema{}
|
|
|
|
if database.Read == nil {
|
|
return nil, errors.New("no database connection")
|
|
}
|
|
|
|
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 {
|
|
return nil, err
|
|
}
|
|
tid.Nick = o3.NewPubNick("xmpp:" + jid.String())
|
|
|
|
a := &Account{
|
|
AccountThreema: account,
|
|
ThreemaID: &tid,
|
|
threema: t,
|
|
xmpp: t.out,
|
|
XMPPResource: make(map[string]map[string]bool),
|
|
deliveredMSG: make(map[uint64]string),
|
|
readedMSG: make(map[uint64]string),
|
|
}
|
|
session := o3.NewSessionContext(tid)
|
|
a.send, a.receive, err = session.Run()
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
a.XMPP = *jid
|
|
|
|
go a.receiver()
|
|
|
|
t.accountJID[jid.String()] = a
|
|
return a, nil
|
|
}
|