some basic plugins

This commit is contained in:
Aaron Bieber 2020-01-31 19:58:17 -07:00
parent bf3e56ed99
commit 9eb077d501
2 changed files with 83 additions and 0 deletions

42
plugins/hi.go Normal file
View File

@ -0,0 +1,42 @@
package plugins
import (
"fmt"
"log"
"regexp"
"github.com/matrix-org/gomatrix"
)
// Hi responds to hi messages
type Hi struct {
}
func (h *Hi) match(msg string) bool {
re := regexp.MustCompile(`^hi|hi$`)
return re.MatchString(msg)
}
// Respond to hi events
func (h *Hi) Respond(c *gomatrix.Client, ev *gomatrix.Event, user string) {
if mtype, ok := ev.MessageType(); ok {
switch mtype {
case "m.text":
if post, ok := ev.Body(); ok {
u := NameRE.ReplaceAllString(user, "$1")
s := NameRE.ReplaceAllString(ev.Sender, "$1")
if ToMe(u, post) {
if h.match(post) {
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
SendMessage(c, ev.RoomID, fmt.Sprintf("hi %s!", s))
}
}
}
}
}
}
// Name hi
func (h *Hi) Name() string {
return "Hi"
}

41
plugins/highfive.go Normal file
View File

@ -0,0 +1,41 @@
package plugins
import (
"fmt"
"log"
"strings"
"github.com/matrix-org/gomatrix"
)
// HighFive high fives!
type HighFive struct {
}
// Respond to high five events
func (h *HighFive) Respond(c *gomatrix.Client, ev *gomatrix.Event, user string) {
if mtype, ok := ev.MessageType(); ok {
switch mtype {
case "m.text":
if post, ok := ev.Body(); ok {
u := NameRE.ReplaceAllString(user, "$1")
s := NameRE.ReplaceAllString(ev.Sender, "$1")
if ToMe(u, post) {
if strings.Contains(post, "o/") {
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
SendMessage(c, ev.RoomID, fmt.Sprintf("\\o %s", s))
}
if strings.Contains(post, "\\o") {
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
SendMessage(c, ev.RoomID, fmt.Sprintf("%s o/", s))
}
}
}
}
}
}
// Name returns the name of the HighFive plugin
func (h *HighFive) Name() string {
return "HighFive"
}