From edb91f62542b4af2d047fdfbf9482a9886afda38 Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Fri, 31 May 2019 19:39:29 +0200 Subject: [PATCH] recieve + startup connect --- component/threema/account.go | 29 +++++++++++++++++++++++++++++ component/threema/main.go | 17 +++++++++++++---- component/xmpp.go | 6 +++--- 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/component/threema/account.go b/component/threema/account.go index c7ee62f..b3d2631 100644 --- a/component/threema/account.go +++ b/component/threema/account.go @@ -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) } diff --git a/component/threema/main.go b/component/threema/main.go index 7715a20..46015ba 100644 --- a/component/threema/main.go +++ b/component/threema/main.go @@ -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) { diff --git a/component/xmpp.go b/component/xmpp.go index 77fd576..0ae5ada 100644 --- a/component/xmpp.go +++ b/component/xmpp.go @@ -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) } } }