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

59 lines
1.1 KiB
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-10-03 23:44:42 +02:00
func (c *Config) fixAddr(addr string) string {
if addr == "" {
return c.Host
}
if strings.Contains(addr, "{{DOMAIN}}") {
return strings.Replace(addr, "{{DOMAIN}}", c.Host, 1)
}
if !strings.Contains(addr, "@") {
return addr + "@" + c.Host
}
return addr
}
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-10-03 23:44:42 +02:00
case stanza.Presence:
p.From = c.fixAddr(p.From)
if p.To != "" {
p.To = c.fixAddr(p.To)
}
return p
2019-06-28 03:03:38 +02:00
case stanza.Message:
2019-10-03 23:44:42 +02:00
p.From = c.fixAddr(p.From)
if p.To != "" {
p.To = c.fixAddr(p.To)
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
}
}