This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
thrempp/component/threema/main.go

122 lines
3.0 KiB
Go
Raw Normal View History

2019-05-31 10:07:40 +02:00
package threema
2019-05-31 13:53:44 +02:00
import (
"errors"
2019-05-31 14:36:18 +02:00
"strings"
"github.com/bdlm/log"
2019-06-28 03:03:38 +02:00
"gosrc.io/xmpp/stanza"
2019-05-31 14:36:18 +02:00
2019-05-31 19:39:29 +02:00
"dev.sum7.eu/genofire/golang-lib/database"
2019-08-08 15:48:43 +02:00
"dev.sum7.eu/sum7/thrempp/component"
"dev.sum7.eu/sum7/thrempp/models"
2019-05-31 13:53:44 +02:00
)
2019-05-31 10:07:40 +02:00
type Threema struct {
component.Component
2019-06-28 03:03:38 +02:00
out chan stanza.Packet
accountJID map[string]*Account
bot map[string]*Bot
httpUploadPath string
httpUploadURL string
2019-05-31 10:07:40 +02:00
}
func NewThreema(config map[string]interface{}) (component.Component, error) {
t := &Threema{
2019-06-28 03:03:38 +02:00
out: make(chan stanza.Packet),
2019-05-31 18:26:15 +02:00
accountJID: make(map[string]*Account),
bot: make(map[string]*Bot),
}
if pathI, ok := config["http_upload_path"]; ok {
if path, ok := pathI.(string); ok {
t.httpUploadPath = path
} else {
return nil, errors.New("wrong format of http_upload_path")
}
}
if urlI, ok := config["http_upload_url"]; ok {
if url, ok := urlI.(string); ok {
t.httpUploadURL = url
} else {
return nil, errors.New("wrong format of http_upload_url")
}
}
return t, nil
2019-05-31 10:07:40 +02:00
}
2019-06-28 03:03:38 +02:00
func (t *Threema) Connect() (chan stanza.Packet, error) {
2019-05-31 19:39:29 +02:00
var jids []*models.JID
database.Read.Find(&jids)
for _, jid := range jids {
2019-06-01 15:21:49 +02:00
logger := log.WithField("jid", jid.String())
a, err := t.getAccount(jid)
if err != nil {
logger.Warnf("unable to connect%s", err)
continue
}
logger = logger.WithField("threema", string(a.TID))
logger.Info("connected")
2019-05-31 19:39:29 +02:00
}
2019-05-31 14:36:18 +02:00
return t.out, nil
}
2019-06-28 03:03:38 +02:00
func (t *Threema) Send(packet stanza.Packet) {
2019-06-01 15:21:49 +02:00
if p := t.send(packet); p != nil {
t.out <- p
}
}
2019-06-28 03:03:38 +02:00
func (t *Threema) send(packet stanza.Packet) stanza.Packet {
2019-05-31 14:36:18 +02:00
switch p := packet.(type) {
2019-09-06 04:34:13 +02:00
case stanza.Presence:
log.Debug(p)
return nil
2019-06-28 03:03:38 +02:00
case stanza.Message:
from := models.ParseJID(p.Attrs.From)
to := models.ParseJID(p.Attrs.To)
2019-09-06 03:08:58 +02:00
if p.Attrs.Type == stanza.MessageTypeError {
msg := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, To: from.String()})
if p.Error.Text == "User session not found" {
msg.Body = "please join groupchat xmpp:" + to.String() + "?join"
}
return msg
}
2019-05-31 18:26:15 +02:00
if to.IsDomain() {
if from == nil {
log.Warn("receive message without sender")
return nil
}
2019-06-28 03:03:38 +02:00
msg := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, To: from.String()})
msg.Body = t.getBot(from).Handle(p.Body)
2019-06-01 15:21:49 +02:00
return msg
2019-05-31 18:26:15 +02:00
}
2019-06-01 15:21:49 +02:00
account, err := t.getAccount(from)
if err != nil {
2019-06-28 03:03:38 +02:00
msg := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, To: from.String()})
msg.Body = "It was not possible to send, because we have no account for you.\nPlease generate one, by sending `generate` to this gateway"
2019-06-01 15:21:49 +02:00
return msg
2019-05-31 18:26:15 +02:00
}
2019-09-06 03:08:58 +02:00
if to == nil {
log.WithFields(map[string]interface{}{
"from": from,
"to": to,
}).Panicf("no to found")
return nil
}
2019-05-31 18:26:15 +02:00
threemaID := strings.ToUpper(to.Local)
2019-06-01 01:43:48 +02:00
if err := account.Send(threemaID, p); err != nil {
2019-06-28 03:03:38 +02:00
msg := stanza.NewMessage(stanza.Attrs{Type: stanza.MessageTypeChat, To: from.String()})
2019-05-31 14:36:18 +02:00
msg.Body = err.Error()
2019-06-01 15:21:49 +02:00
return msg
2019-05-31 14:36:18 +02:00
}
default:
log.Warnf("unknown package: %v", p)
2019-05-31 14:36:18 +02:00
}
2019-06-01 15:21:49 +02:00
return nil
2019-05-31 13:53:44 +02:00
}
2019-05-31 10:07:40 +02:00
func init() {
component.AddComponent("threema", NewThreema)
}