fix - join as postup

This commit is contained in:
Martin/Geno 2019-06-24 17:44:48 +02:00
parent 8b8d136779
commit c3d91aea98
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
2 changed files with 31 additions and 31 deletions

View File

@ -6,6 +6,7 @@ import (
"github.com/bdlm/log" "github.com/bdlm/log"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
"gosrc.io/xmpp" "gosrc.io/xmpp"
"gosrc.io/xmpp/stanza"
"dev.sum7.eu/genofire/logmania/bot" "dev.sum7.eu/genofire/logmania/bot"
"dev.sum7.eu/genofire/logmania/database" "dev.sum7.eu/genofire/logmania/database"
@ -25,12 +26,13 @@ type Output struct {
defaults []*database.Notify defaults []*database.Notify
channels map[string]bool channels map[string]bool
bot *bot.Bot bot *bot.Bot
client *xmpp.Client client xmpp.Sender
botOut chan interface{} botOut chan interface{}
logOut chan interface{} logOut chan interface{}
} }
type OutputConfig struct { type OutputConfig struct {
Address string `mapstructure:"address"`
JID string `mapstructure:"jid"` JID string `mapstructure:"jid"`
Password string `mapstructure:"password"` Password string `mapstructure:"password"`
Defaults map[string]bool `mapstructure:"default"` Defaults map[string]bool `mapstructure:"default"`
@ -53,6 +55,7 @@ func Init(configInterface interface{}, db *database.DB, bot *bot.Bot) output.Out
router.HandleFunc("presence", out.recvPresence) router.HandleFunc("presence", out.recvPresence)
client, err := xmpp.NewClient(xmpp.Config{ client, err := xmpp.NewClient(xmpp.Config{
Address: config.Address,
Jid: config.JID, Jid: config.JID,
Password: config.Password, Password: config.Password,
}, router) }, router)
@ -61,30 +64,16 @@ func Init(configInterface interface{}, db *database.DB, bot *bot.Bot) output.Out
logger.Error(err) logger.Error(err)
return nil return nil
} }
out.client = client cm := xmpp.NewStreamManager(client, func(c xmpp.Sender) {
cm := xmpp.NewStreamManager(client, nil)
go func() {
cm.Run()
log.Panic("closed connection")
}()
logger.WithField("jid", config.JID).Info("startup")
for to, muc := range config.Defaults { for to, muc := range config.Defaults {
var def *database.Notify def := &database.Notify{
pro := proto Protocol: proto,
if muc {
pro = protoGroup
}
if dbNotify, ok := db.NotifiesByAddress[pro+":"+to]; ok {
def = dbNotify
} else {
def = &database.Notify{
Protocol: pro,
To: to, To: to,
RegexIn: make(map[string]*regexp.Regexp), RegexIn: make(map[string]*regexp.Regexp),
MaxPrioIn: log.DebugLevel, MaxPrioIn: log.DebugLevel,
} }
if muc {
def.Protocol = protoGroup
out.Join(to) out.Join(to)
} }
out.defaults = append(out.defaults, def) out.defaults = append(out.defaults, def)
@ -94,6 +83,15 @@ func Init(configInterface interface{}, db *database.DB, bot *bot.Bot) output.Out
out.Join(toAddresses.To) out.Join(toAddresses.To)
} }
} }
logger.Info("join muc after connect")
})
go func() {
cm.Run()
log.Panic("closed connection")
}()
out.client = client
logger.WithField("jid", config.JID).Info("startup")
return out return out
} }
@ -108,9 +106,9 @@ func (out *Output) Close() {
logger.Error("xmpp could generate jid to leave ", jid, " error:", err) logger.Error("xmpp could generate jid to leave ", jid, " error:", err)
} }
toJID.Resource = nickname toJID.Resource = nickname
err = out.client.Send(xmpp.Presence{Attrs: xmpp.Attrs{ err = out.client.Send(stanza.Presence{Attrs: stanza.Attrs{
To: toJID.Full(), To: toJID.Full(),
Type: xmpp.PresenceTypeUnavailable, Type: stanza.PresenceTypeUnavailable,
}}) }})
if err != nil { if err != nil {
logger.Error("xmpp could not leave ", toJID.Full(), " error:", err) logger.Error("xmpp could not leave ", toJID.Full(), " error:", err)

View File

@ -15,11 +15,13 @@ func (out *Output) Join(to string) {
logger.Error("xmpp could not generate jid to join ", to, " error:", err) logger.Error("xmpp could not generate jid to join ", to, " error:", err)
return return
} }
maxstanzas := 0
toJID.Resource = nickname toJID.Resource = nickname
err = out.client.Send(xmpp.Presence{Attrs: xmpp.Attrs{To: toJID.Full()}, err = out.client.Send(xmpp.Presence{Attrs: xmpp.Attrs{To: toJID.Full()},
Extensions: []xmpp.PresExtension{ Extensions: []xmpp.PresExtension{
xmpp.MucPresence{ xmpp.MucPresence{
History: xmpp.History{MaxStanzas: 0}, History: xmpp.History{MaxStanzas: &maxstanzas},
}}, }},
}) })
if err != nil { if err != nil {