diff --git a/component/threema/account.go b/component/threema/account.go new file mode 100644 index 0000000..f5e4726 --- /dev/null +++ b/component/threema/account.go @@ -0,0 +1,18 @@ +package threema + +import "errors" + +type ThreemaAccount struct { + ID string +} + +func (t *Threema) getAccount(jid string) *ThreemaAccount { + return &ThreemaAccount{} +} + +func (a *ThreemaAccount) Send(to string, msg string) error { + if a.ID == "" { + return errors.New("It was not possible to send, becaouse we have no account for you.\nPlease generate one, by sending `generate` to gateway") + } + return nil +} diff --git a/component/threema/main.go b/component/threema/main.go index 349e215..545d3d4 100644 --- a/component/threema/main.go +++ b/component/threema/main.go @@ -1,12 +1,17 @@ package threema import ( - "dev.sum7.eu/genofire/thrempp/component" + "strings" + + "github.com/bdlm/log" "gosrc.io/xmpp" + + "dev.sum7.eu/genofire/thrempp/component" ) type Threema struct { component.Component + out chan xmpp.Packet } func NewThreema(config map[string]interface{}) (component.Component, error) { @@ -14,8 +19,28 @@ func NewThreema(config map[string]interface{}) (component.Component, error) { } func (t *Threema) Connect() (chan xmpp.Packet, error) { - c := make(chan xmpp.Packet) - return c, nil + t.out = make(chan xmpp.Packet) + return t.out, nil +} +func (t *Threema) Send(packet xmpp.Packet) { + switch p := packet.(type) { + case xmpp.Message: + attrs := p.PacketAttrs + account := t.getAccount(attrs.From) + log.WithFields(map[string]interface{}{ + "from": attrs.From, + "to": attrs.To, + }).Debug(p.Body) + threemaID := strings.ToUpper(strings.Split(attrs.To, "@")[0]) + err := account.Send(threemaID, p.Body) + if err != nil { + msg := xmpp.NewMessage("chat", "", attrs.From, "", "en") + msg.Body = err.Error() + t.out <- msg + } + default: + log.Warnf("unkown package%v", p) + } } func init() { diff --git a/component/xmpp.go b/component/xmpp.go index 1edf43c..77fd576 100644 --- a/component/xmpp.go +++ b/component/xmpp.go @@ -33,26 +33,47 @@ func (c *Config) Start() error { return nil } -func (c *Config) recieve(chan xmpp.Packet) { +func (c *Config) recieve(packets chan xmpp.Packet) { + logger := log.WithField("type", c.Type) + for packet := range packets { + switch p := packet.(type) { + case xmpp.Message: + if p.PacketAttrs.From == "" { + p.PacketAttrs.From = c.Host + } else { + p.PacketAttrs.From += "@" + c.Host + } + loggerMSG := logger.WithFields(map[string]interface{}{ + "from": p.PacketAttrs.From, + "to": p.PacketAttrs.To, + }) + loggerMSG.Debug(p.Body) + c.xmpp.Send(p) + default: + log.Warn("ignoring packet:", packet) + } + } } func (c *Config) sender() { logger := log.WithField("type", c.Type) for { - logger.Debug("wait fo recieve") packet, err := c.xmpp.ReadPacket() if err != nil { logger.Panicf("connection closed%s", err) return } - logger.Debug("recieve") switch p := packet.(type) { case xmpp.IQ: attrs := p.PacketAttrs + loggerIQ := logger.WithFields(map[string]interface{}{ + "from": attrs.From, + "to": attrs.To, + }) switch inner := p.Payload[0].(type) { case *xmpp.DiscoInfo: - logger.Debug("Disco Info") + loggerIQ.Debug("Disco Info") if p.Type == "get" { iq := xmpp.NewIQ("result", attrs.To, attrs.From, attrs.Id, "en") var identity xmpp.Identity @@ -76,7 +97,7 @@ func (c *Config) sender() { _ = c.xmpp.Send(iq) } case *xmpp.DiscoItems: - logger.Debug("DiscoItems") + loggerIQ.Debug("DiscoItems") if p.Type == "get" { iq := xmpp.NewIQ("result", attrs.To, attrs.From, attrs.Id, "en") @@ -103,7 +124,7 @@ func (c *Config) sender() { } case xmpp.Message: - logger.Info("Received message:", p.Body) + c.comp.Send(packet) case xmpp.Presence: logger.Info("Received presence:", p.Type)