hook2xmpp/runtime/xmpp.go

52 lines
1.1 KiB
Go
Raw Normal View History

2019-02-13 03:24:38 +01:00
package runtime
2017-06-09 10:55:47 +02:00
import (
2019-02-13 03:24:38 +01:00
"fmt"
2017-08-06 16:33:32 +02:00
2019-02-13 03:55:07 +01:00
"github.com/bdlm/log"
2017-06-09 10:55:47 +02:00
xmpp "github.com/mattn/go-xmpp"
)
func Start(client *xmpp.Client) {
for {
m, err := client.Recv()
if err != nil {
continue
}
switch v := m.(type) {
case xmpp.Chat:
if v.Type == "chat" {
2019-02-13 03:55:07 +01:00
log.Debugf("from %s: %s", v.Remote, v.Text)
2017-06-09 10:55:47 +02:00
}
if v.Type == "groupchat" {
}
case xmpp.Presence:
// do nothing
}
}
}
2019-02-13 19:51:23 +01:00
func NotifyImage(client *xmpp.Client, hook Hook, url string, desc string) {
2019-02-13 03:24:38 +01:00
msg := fmt.Sprintf(`<message to='%%s' type='%%s'>
<body>%s</body>
<x xmlns='jabber:x:oob'>
<url>%s</url>
2019-02-13 19:51:23 +01:00
<desc>%s</desc>
2019-02-13 03:24:38 +01:00
</x>
2019-02-13 19:51:23 +01:00
</message>`, url, url, desc)
2017-06-09 10:55:47 +02:00
2019-02-13 03:24:38 +01:00
for _, muc := range hook.NotifyMuc {
client.SendOrg(fmt.Sprintf(msg, muc, "groupchat"))
}
for _, user := range hook.NotifyUser {
client.SendOrg(fmt.Sprintf(msg, user, "chat"))
}
}
func Notify(client *xmpp.Client, hook Hook, msg string) {
2017-06-09 10:55:47 +02:00
for _, muc := range hook.NotifyMuc {
client.SendHtml(xmpp.Chat{Remote: muc, Type: "groupchat", Text: msg})
}
for _, user := range hook.NotifyUser {
client.SendHtml(xmpp.Chat{Remote: user, Type: "chat", Text: msg})
}
}