refactor a bit. this makes plugins a bit less blumpy

This commit is contained in:
Aaron Bieber 2020-02-02 19:43:26 -07:00
parent 41e6bf180c
commit 9d25ab6041
9 changed files with 81 additions and 118 deletions

13
main.go
View File

@ -131,7 +131,18 @@ func main() {
} }
for _, p := range plugins.Plugs { for _, p := range plugins.Plugs {
p.Respond(cli, ev, username) var post string
var ok bool
if post, ok = ev.Body(); !ok {
// Invaild body, for some reason
return
}
if mtype, ok := ev.MessageType(); ok {
switch mtype {
case "m.text":
p.RespondText(cli, ev, username, post)
}
}
} }
}) })

View File

@ -129,12 +129,8 @@ func (h *Beer) pretty(b BeerResp, random bool) string {
) )
} }
// Respond to looking up of beer requests // RespondText to looking up of beer requests
func (h *Beer) Respond(c *gomatrix.Client, ev *gomatrix.Event, user string) { func (h *Beer) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
if mtype, ok := ev.MessageType(); ok {
switch mtype {
case "m.text":
if post, ok := ev.Body(); ok {
if h.match(post) { if h.match(post) {
beer := h.fix(post) beer := h.fix(post)
if beer != "" { if beer != "" {
@ -154,9 +150,6 @@ func (h *Beer) Respond(c *gomatrix.Client, ev *gomatrix.Event, user string) {
} }
} }
} }
}
}
}
} }
// Name Beer! // Name Beer!

View File

@ -31,12 +31,8 @@ func (h *BotSnack) resp() string {
} }
// Respond to hi events // RespondText to hi events
func (h *BotSnack) Respond(c *gomatrix.Client, ev *gomatrix.Event, user string) { func (h *BotSnack) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
if mtype, ok := ev.MessageType(); ok {
switch mtype {
case "m.text":
if post, ok := ev.Body(); ok {
u := NameRE.ReplaceAllString(user, "$1") u := NameRE.ReplaceAllString(user, "$1")
if ToMe(u, post) { if ToMe(u, post) {
if h.match(post) { if h.match(post) {
@ -44,9 +40,6 @@ func (h *BotSnack) Respond(c *gomatrix.Client, ev *gomatrix.Event, user string)
SendText(c, ev.RoomID, h.resp()) SendText(c, ev.RoomID, h.resp())
} }
} }
}
}
}
} }
// Name hi // Name hi

View File

@ -17,12 +17,8 @@ func (h *Hi) match(msg string) bool {
return re.MatchString(msg) return re.MatchString(msg)
} }
// Respond to hi events // RespondText to hi events
func (h *Hi) Respond(c *gomatrix.Client, ev *gomatrix.Event, user string) { func (h *Hi) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
if mtype, ok := ev.MessageType(); ok {
switch mtype {
case "m.text":
if post, ok := ev.Body(); ok {
u := NameRE.ReplaceAllString(user, "$1") u := NameRE.ReplaceAllString(user, "$1")
s := NameRE.ReplaceAllString(ev.Sender, "$1") s := NameRE.ReplaceAllString(ev.Sender, "$1")
if ToMe(u, post) { if ToMe(u, post) {
@ -31,9 +27,6 @@ func (h *Hi) Respond(c *gomatrix.Client, ev *gomatrix.Event, user string) {
SendText(c, ev.RoomID, fmt.Sprintf("hi %s!", s)) SendText(c, ev.RoomID, fmt.Sprintf("hi %s!", s))
} }
} }
}
}
}
} }
// Name hi // Name hi

View File

@ -12,12 +12,8 @@ import (
type HighFive struct { type HighFive struct {
} }
// Respond to high five events // RespondText to high five events
func (h *HighFive) Respond(c *gomatrix.Client, ev *gomatrix.Event, user string) { func (h *HighFive) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
if mtype, ok := ev.MessageType(); ok {
switch mtype {
case "m.text":
if post, ok := ev.Body(); ok {
u := NameRE.ReplaceAllString(user, "$1") u := NameRE.ReplaceAllString(user, "$1")
s := NameRE.ReplaceAllString(ev.Sender, "$1") s := NameRE.ReplaceAllString(ev.Sender, "$1")
if ToMe(u, post) { if ToMe(u, post) {
@ -30,9 +26,6 @@ func (h *HighFive) Respond(c *gomatrix.Client, ev *gomatrix.Event, user string)
SendText(c, ev.RoomID, fmt.Sprintf("%s o/", s)) SendText(c, ev.RoomID, fmt.Sprintf("%s o/", s))
} }
} }
}
}
}
} }
// Name returns the name of the HighFive plugin // Name returns the name of the HighFive plugin

View File

@ -32,12 +32,8 @@ func (h *LoveYou) resp() string {
} }
// Respond to love events // RespondText to love events
func (h *LoveYou) Respond(c *gomatrix.Client, ev *gomatrix.Event, user string) { func (h *LoveYou) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
if mtype, ok := ev.MessageType(); ok {
switch mtype {
case "m.text":
if post, ok := ev.Body(); ok {
u := NameRE.ReplaceAllString(user, "$1") u := NameRE.ReplaceAllString(user, "$1")
if ToMe(u, post) { if ToMe(u, post) {
if h.match(post) { if h.match(post) {
@ -45,9 +41,6 @@ func (h *LoveYou) Respond(c *gomatrix.Client, ev *gomatrix.Event, user string) {
SendText(c, ev.RoomID, h.resp()) SendText(c, ev.RoomID, h.resp())
} }
} }
}
}
}
} }
// Name i love you // Name i love you

View File

@ -10,7 +10,8 @@ import (
// Plugin defines the functions a plugin must implement to be used by // Plugin defines the functions a plugin must implement to be used by
// mcchunkie. // mcchunkie.
type Plugin interface { type Plugin interface {
Respond(c *gomatrix.Client, ev *gomatrix.Event, user string) //Respond(c *gomatrix.Client, ev *gomatrix.Event, user string)
RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, path string)
Name() string Name() string
} }

View File

@ -17,12 +17,8 @@ func (h *Source) match(msg string) bool {
return re.MatchString(msg) return re.MatchString(msg)
} }
// Respond to questions about TheSource™©®⑨ // RespondText to questions about TheSource™©®⑨
func (h *Source) Respond(c *gomatrix.Client, ev *gomatrix.Event, user string) { func (h *Source) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
if mtype, ok := ev.MessageType(); ok {
switch mtype {
case "m.text":
if post, ok := ev.Body(); ok {
u := NameRE.ReplaceAllString(user, "$1") u := NameRE.ReplaceAllString(user, "$1")
s := NameRE.ReplaceAllString(ev.Sender, "$1") s := NameRE.ReplaceAllString(ev.Sender, "$1")
if ToMe(u, post) { if ToMe(u, post) {
@ -31,9 +27,6 @@ func (h *Source) Respond(c *gomatrix.Client, ev *gomatrix.Event, user string) {
SendText(c, ev.RoomID, fmt.Sprintf("%s: %s ;D", s, "https://git.sr.ht/~qbit/mcchunkie")) SendText(c, ev.RoomID, fmt.Sprintf("%s: %s ;D", s, "https://git.sr.ht/~qbit/mcchunkie"))
} }
} }
}
}
}
} }
// Name Source // Name Source

View File

@ -22,12 +22,8 @@ func (v *Version) print(to string) string {
return fmt.Sprintf("%s, I am written in Go, running on %s", to, runtime.GOOS) return fmt.Sprintf("%s, I am written in Go, running on %s", to, runtime.GOOS)
} }
// Respond to version events // RespondText to version events
func (v *Version) Respond(c *gomatrix.Client, ev *gomatrix.Event, user string) { func (v *Version) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
if mtype, ok := ev.MessageType(); ok {
switch mtype {
case "m.text":
if post, ok := ev.Body(); ok {
u := NameRE.ReplaceAllString(user, "$1") u := NameRE.ReplaceAllString(user, "$1")
s := NameRE.ReplaceAllString(ev.Sender, "$1") s := NameRE.ReplaceAllString(ev.Sender, "$1")
if ToMe(u, post) { if ToMe(u, post) {
@ -36,9 +32,6 @@ func (v *Version) Respond(c *gomatrix.Client, ev *gomatrix.Event, user string) {
SendText(c, ev.RoomID, v.print(s)) SendText(c, ev.RoomID, v.print(s))
} }
} }
}
}
}
} }
// Name Version // Name Version