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/send.go

43 lines
871 B
Go
Raw Normal View History

2019-06-01 04:38:35 +02:00
package component
import (
2019-09-06 03:08:58 +02:00
"strings"
2019-06-01 04:38:35 +02:00
"github.com/bdlm/log"
2019-06-28 03:03:38 +02:00
"gosrc.io/xmpp/stanza"
2019-06-01 04:38:35 +02:00
)
2019-06-28 03:03:38 +02:00
func (c *Config) sender(packets chan stanza.Packet) {
2019-09-06 03:08:58 +02:00
log.Debugf("start xmpp sender for: %s", c.Host)
2019-06-01 04:38:35 +02:00
for packet := range packets {
if p := c.sending(packet); p != nil {
2019-06-01 04:38:35 +02:00
c.xmpp.Send(p)
}
}
}
2019-06-28 03:03:38 +02:00
func (c *Config) sending(packet stanza.Packet) stanza.Packet {
2019-06-01 04:38:35 +02:00
logger := log.WithField("type", c.Type)
switch p := packet.(type) {
2019-06-28 03:03:38 +02:00
case stanza.Message:
if p.From == "" {
p.From = c.Host
2019-09-06 03:08:58 +02:00
} else if strings.Contains(p.From, "{{DOMAIN}}") {
p.From = strings.Replace(p.From, "{{DOMAIN}}", c.Host, 1)
2019-06-01 04:38:35 +02:00
} else {
2019-06-28 03:03:38 +02:00
p.From += "@" + c.Host
2019-06-01 04:38:35 +02:00
}
2019-06-06 21:20:51 +02:00
if c.XMPPDebug {
2019-06-06 20:53:06 +02:00
logger.WithFields(map[string]interface{}{
2019-06-28 03:03:38 +02:00
"from": p.From,
"to": p.To,
"id": p.Id,
2019-06-06 20:53:06 +02:00
}).Debug(p.XMPPFormat())
}
2019-06-01 04:38:35 +02:00
return p
default:
log.Warn("ignoring packet:", packet)
return nil
}
}