use Sender interface of gosrc.io/xmpp
This commit is contained in:
parent
2435b9b73b
commit
aa37652fdf
|
@ -30,7 +30,7 @@ func (r requestBody) String() string {
|
|||
}
|
||||
|
||||
func init() {
|
||||
runtime.HookRegister[hookType] = func(client *xmpp.Client, hooks []runtime.Hook) func(w http.ResponseWriter, r *http.Request) {
|
||||
runtime.HookRegister[hookType] = func(client xmpp.Sender, hooks []runtime.Hook) func(w http.ResponseWriter, r *http.Request) {
|
||||
log.WithField("type", hookType).Info("loaded")
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
logger := log.WithField("type", hookType)
|
||||
|
|
|
@ -21,7 +21,7 @@ var eventHeader = map[string]string{
|
|||
const hookType = "git"
|
||||
|
||||
func init() {
|
||||
runtime.HookRegister[hookType] = func(client *xmpp.Client, hooks []runtime.Hook) func(w http.ResponseWriter, r *http.Request) {
|
||||
runtime.HookRegister[hookType] = func(client xmpp.Sender, hooks []runtime.Hook) func(w http.ResponseWriter, r *http.Request) {
|
||||
log.WithField("type", hookType).Info("loaded")
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
logger := log.WithField("type", hookType)
|
||||
|
|
|
@ -24,7 +24,7 @@ var eventHeader = map[string]string{
|
|||
const hookType = "gitlab"
|
||||
|
||||
func init() {
|
||||
runtime.HookRegister[hookType] = func(client *xmpp.Client, hooks []runtime.Hook) func(w http.ResponseWriter, r *http.Request) {
|
||||
runtime.HookRegister[hookType] = func(client xmpp.Sender, hooks []runtime.Hook) func(w http.ResponseWriter, r *http.Request) {
|
||||
log.WithField("type", hookType).Info("loaded")
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
event := r.Header.Get("X-Gitlab-Event")
|
||||
|
|
|
@ -39,7 +39,7 @@ func (r requestBody) String() string {
|
|||
}
|
||||
|
||||
func init() {
|
||||
runtime.HookRegister[hookType] = func(client *xmpp.Client, hooks []runtime.Hook) func(w http.ResponseWriter, r *http.Request) {
|
||||
runtime.HookRegister[hookType] = func(client xmpp.Sender, hooks []runtime.Hook) func(w http.ResponseWriter, r *http.Request) {
|
||||
log.WithField("type", hookType).Info("loaded")
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
logger := log.WithField("type", hookType)
|
||||
|
|
3
main.go
3
main.go
|
@ -33,6 +33,7 @@ func main() {
|
|||
log.SetLevel(config.LogLevel)
|
||||
|
||||
router := xmpp.NewRouter()
|
||||
var err error
|
||||
client, err := xmpp.NewClient(xmpp.Config{
|
||||
Address: config.XMPP.Address,
|
||||
Jid: config.XMPP.JID,
|
||||
|
@ -69,7 +70,7 @@ func main() {
|
|||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||
sig := <-sigs
|
||||
|
||||
closeXMPP()
|
||||
closeXMPP(client)
|
||||
|
||||
srv.Close()
|
||||
|
||||
|
|
|
@ -17,7 +17,7 @@ import (
|
|||
const hookType = "prometheus"
|
||||
|
||||
func init() {
|
||||
runtime.HookRegister[hookType] = func(client *xmpp.Client, hooks []runtime.Hook) func(w http.ResponseWriter, r *http.Request) {
|
||||
runtime.HookRegister[hookType] = func(client xmpp.Sender, hooks []runtime.Hook) func(w http.ResponseWriter, r *http.Request) {
|
||||
log.WithField("type", hookType).Info("loaded")
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
logger := log.WithField("type", hookType)
|
||||
|
|
|
@ -6,7 +6,7 @@ import (
|
|||
"gosrc.io/xmpp"
|
||||
)
|
||||
|
||||
type HookHandler func(*xmpp.Client, []Hook) func(http.ResponseWriter, *http.Request)
|
||||
type HookHandler func(xmpp.Sender, []Hook) func(http.ResponseWriter, *http.Request)
|
||||
|
||||
var HookRegister map[string]HookHandler
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
"gosrc.io/xmpp/stanza"
|
||||
)
|
||||
|
||||
func NotifyImage(client *xmpp.Client, hook Hook, url string, desc string) {
|
||||
func NotifyImage(client xmpp.Sender, hook Hook, url string, desc string) {
|
||||
msg := stanza.Message{
|
||||
Attrs: stanza.Attrs{Type: stanza.MessageTypeGroupchat},
|
||||
Body: url,
|
||||
|
@ -38,7 +38,7 @@ func NotifyImage(client *xmpp.Client, hook Hook, url string, desc string) {
|
|||
}
|
||||
}
|
||||
|
||||
func Notify(client *xmpp.Client, hook Hook, text, html string) {
|
||||
func Notify(client xmpp.Sender, hook Hook, text, html string) {
|
||||
msg := stanza.Message{
|
||||
Attrs: stanza.Attrs{Type: stanza.MessageTypeGroupchat},
|
||||
Body: text,
|
||||
|
|
26
xmpp.go
26
xmpp.go
|
@ -6,10 +6,9 @@ import (
|
|||
"gosrc.io/xmpp/stanza"
|
||||
)
|
||||
|
||||
var client *xmpp.Client
|
||||
var mucs []string
|
||||
|
||||
func notify(text string) {
|
||||
func notify(c xmpp.Sender, text string) {
|
||||
msg := stanza.Message{
|
||||
Attrs: stanza.Attrs{Type: stanza.MessageTypeGroupchat},
|
||||
Body: text,
|
||||
|
@ -17,7 +16,7 @@ func notify(text string) {
|
|||
|
||||
for _, muc := range config.StartupNotifyMuc {
|
||||
msg.To = muc
|
||||
if err := client.Send(msg); err != nil {
|
||||
if err := c.Send(msg); err != nil {
|
||||
log.WithFields(map[string]interface{}{
|
||||
"muc": muc,
|
||||
"msg": text,
|
||||
|
@ -28,7 +27,7 @@ func notify(text string) {
|
|||
msg.Type = stanza.MessageTypeChat
|
||||
for _, user := range config.StartupNotifyUser {
|
||||
msg.To = user
|
||||
if err := client.Send(msg); err != nil {
|
||||
if err := c.Send(msg); err != nil {
|
||||
log.WithFields(map[string]interface{}{
|
||||
"user": user,
|
||||
"msg": text,
|
||||
|
@ -38,7 +37,7 @@ func notify(text string) {
|
|||
log.Infof("notify: %s", text)
|
||||
}
|
||||
|
||||
func joinMUC(to, nick string) error {
|
||||
func joinMUC(c xmpp.Sender, to, nick string) error {
|
||||
|
||||
toJID, err := xmpp.NewJid(to)
|
||||
if err != nil {
|
||||
|
@ -49,7 +48,7 @@ func joinMUC(to, nick string) error {
|
|||
|
||||
mucs = append(mucs, jid)
|
||||
|
||||
return client.Send(stanza.Presence{Attrs: stanza.Attrs{To: jid},
|
||||
return c.Send(stanza.Presence{Attrs: stanza.Attrs{To: jid},
|
||||
Extensions: []stanza.PresExtension{
|
||||
stanza.MucPresence{
|
||||
History: stanza.History{MaxStanzas: stanza.NewNullableInt(0)},
|
||||
|
@ -58,34 +57,33 @@ func joinMUC(to, nick string) error {
|
|||
|
||||
}
|
||||
|
||||
func postStartup(c xmpp.StreamClient) {
|
||||
func postStartup(c xmpp.Sender) {
|
||||
for _, muc := range config.StartupNotifyMuc {
|
||||
if err := joinMUC(muc, config.Nickname); err != nil {
|
||||
if err := joinMUC(c, muc, config.Nickname); err != nil {
|
||||
log.WithField("muc", muc).Errorf("error on joining muc: %s", err)
|
||||
}
|
||||
}
|
||||
for _, hooks := range config.Hooks {
|
||||
for _, hook := range hooks {
|
||||
for _, muc := range hook.NotifyMuc {
|
||||
if err := joinMUC(muc, config.Nickname); err != nil {
|
||||
if err := joinMUC(c, muc, config.Nickname); err != nil {
|
||||
log.WithField("muc", muc).Errorf("error on joining muc: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
notify("started hock2xmpp")
|
||||
notify(c, "started hock2xmpp")
|
||||
}
|
||||
|
||||
func closeXMPP() {
|
||||
notify("stopped of hock2xmpp")
|
||||
func closeXMPP(c xmpp.Sender) {
|
||||
notify(c, "stopped of hock2xmpp")
|
||||
|
||||
for _, muc := range mucs {
|
||||
if err := client.Send(stanza.Presence{Attrs: stanza.Attrs{
|
||||
if err := c.Send(stanza.Presence{Attrs: stanza.Attrs{
|
||||
To: muc,
|
||||
Type: stanza.PresenceTypeUnavailable,
|
||||
}}); err != nil {
|
||||
log.WithField("muc", muc).Errorf("error on leaving muc: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue