2019-05-31 10:07:40 +02:00
|
|
|
package threema
|
|
|
|
|
2019-05-31 13:53:44 +02:00
|
|
|
import (
|
2019-05-31 14:36:18 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/bdlm/log"
|
2019-05-31 13:53:44 +02:00
|
|
|
"gosrc.io/xmpp"
|
2019-05-31 14:36:18 +02:00
|
|
|
|
2019-05-31 19:39:29 +02:00
|
|
|
"dev.sum7.eu/genofire/golang-lib/database"
|
|
|
|
|
2019-05-31 14:36:18 +02:00
|
|
|
"dev.sum7.eu/genofire/thrempp/component"
|
2019-05-31 18:26:15 +02:00
|
|
|
"dev.sum7.eu/genofire/thrempp/models"
|
2019-05-31 13:53:44 +02:00
|
|
|
)
|
2019-05-31 10:07:40 +02:00
|
|
|
|
|
|
|
type Threema struct {
|
|
|
|
component.Component
|
2019-05-31 18:26:15 +02:00
|
|
|
out chan xmpp.Packet
|
|
|
|
accountJID map[string]*Account
|
2019-05-31 10:07:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewThreema(config map[string]interface{}) (component.Component, error) {
|
2019-05-31 19:39:29 +02:00
|
|
|
return &Threema{
|
2019-05-31 18:26:15 +02:00
|
|
|
out: make(chan xmpp.Packet),
|
|
|
|
accountJID: make(map[string]*Account),
|
2019-05-31 19:39:29 +02:00
|
|
|
}, nil
|
2019-05-31 10:07:40 +02:00
|
|
|
}
|
|
|
|
|
2019-05-31 13:53:44 +02:00
|
|
|
func (t *Threema) Connect() (chan xmpp.Packet, error) {
|
2019-05-31 19:39:29 +02:00
|
|
|
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),
|
2019-06-01 01:43:48 +02:00
|
|
|
}).Info("connected")
|
2019-05-31 19:39:29 +02:00
|
|
|
}
|
2019-05-31 14:36:18 +02:00
|
|
|
return t.out, nil
|
|
|
|
}
|
|
|
|
func (t *Threema) Send(packet xmpp.Packet) {
|
|
|
|
switch p := packet.(type) {
|
|
|
|
case xmpp.Message:
|
2019-05-31 18:26:15 +02:00
|
|
|
from := models.ParseJID(p.PacketAttrs.From)
|
|
|
|
to := models.ParseJID(p.PacketAttrs.To)
|
|
|
|
|
|
|
|
if to.IsDomain() {
|
|
|
|
msg := xmpp.NewMessage("chat", "", from.String(), "", "en")
|
|
|
|
msg.Body = t.Bot(from, p.Body)
|
|
|
|
t.out <- msg
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
account := t.getAccount(from)
|
|
|
|
if account == nil {
|
|
|
|
msg := xmpp.NewMessage("chat", "", from.String(), "", "en")
|
|
|
|
msg.Body = "It was not possible to send, becouse we have no account for you.\nPlease generate one, by sending `generate` to this gateway"
|
|
|
|
t.out <- msg
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
threemaID := strings.ToUpper(to.Local)
|
2019-06-01 01:43:48 +02:00
|
|
|
if err := account.Send(threemaID, p); err != nil {
|
2019-05-31 18:26:15 +02:00
|
|
|
msg := xmpp.NewMessage("chat", "", from.String(), "", "en")
|
2019-05-31 14:36:18 +02:00
|
|
|
msg.Body = err.Error()
|
|
|
|
t.out <- msg
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
log.Warnf("unkown package%v", p)
|
|
|
|
}
|
2019-05-31 13:53:44 +02:00
|
|
|
}
|
|
|
|
|
2019-05-31 10:07:40 +02:00
|
|
|
func init() {
|
|
|
|
component.AddComponent("threema", NewThreema)
|
|
|
|
}
|