recieve + startup connect
This commit is contained in:
parent
90c5f4c5e1
commit
edb91f6254
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Reference in New Issue