recieve + startup connect

This commit is contained in:
Martin/Geno 2019-05-31 19:39:29 +02:00
parent 90c5f4c5e1
commit edb91f6254
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
3 changed files with 45 additions and 7 deletions

View File

@ -1,7 +1,11 @@
package threema
import (
"strconv"
"github.com/bdlm/log"
"github.com/o3ma/o3"
"gosrc.io/xmpp"
"dev.sum7.eu/genofire/golang-lib/database"
@ -37,6 +41,7 @@ func (t *Threema) getAccount(jid *models.JID) *Account {
tid.Nick = o3.NewPubNick("xmpp:" + jid.String())
a := &Account{AccountThreema: account}
a.XMPP = *jid
a.Session = o3.NewSessionContext(tid)
a.send, a.recieve, err = a.Session.Run()
@ -45,11 +50,35 @@ func (t *Threema) getAccount(jid *models.JID) *Account {
return nil
}
go a.reciever(t.out)
t.accountJID[jid.String()] = a
t.accountTID[string(a.TID)] = a
return a
}
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()
out <- xMSG
case o3.DeliveryReceiptMessage:
// msg.MsgID()
}
}
}
func (a *Account) Send(to string, msg string) error {
return a.Session.SendTextMessage(to, msg, a.send)
}

View File

@ -6,6 +6,8 @@ import (
"github.com/bdlm/log"
"gosrc.io/xmpp"
"dev.sum7.eu/genofire/golang-lib/database"
"dev.sum7.eu/genofire/thrempp/component"
"dev.sum7.eu/genofire/thrempp/models"
)
@ -18,16 +20,23 @@ type Threema struct {
}
func NewThreema(config map[string]interface{}) (component.Component, error) {
t := &Threema{
return &Threema{
out: make(chan xmpp.Packet),
accountJID: make(map[string]*Account),
accountTID: make(map[string]*Account),
}
// TODO load accounts on startup
return t, nil
}, nil
}
func (t *Threema) Connect() (chan xmpp.Packet, error) {
var jids []*models.JID
database.Read.Find(&jids)
for _, jid := range jids {
a := t.getAccount(jid)
log.WithFields(map[string]interface{}{
"jid": jid.String(),
"threema": string(a.TID),
}).Debug("connected")
}
return t.out, nil
}
func (t *Threema) Send(packet xmpp.Packet) {

View File

@ -113,7 +113,7 @@ func (c *Config) sender() {
_ = c.xmpp.Send(iq)
}
default:
logger.Warn("ignoring iq packet", inner)
logger.Debug("ignoring iq packet", inner)
xError := xmpp.Err{
Code: 501,
Reason: "feature-not-implemented",
@ -127,10 +127,10 @@ func (c *Config) sender() {
c.comp.Send(packet)
case xmpp.Presence:
logger.Info("Received presence:", p.Type)
logger.Debug("Received presence:", p.Type)
default:
logger.Warn("ignoring packet:", packet)
logger.Debug("ignoring packet:", packet)
}
}
}