diff --git a/main.go b/main.go index 927ee2e..de82353 100644 --- a/main.go +++ b/main.go @@ -131,7 +131,18 @@ func main() { } 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) + } + } } }) diff --git a/plugins/beer.go b/plugins/beer.go index 679a63e..e5055ab 100644 --- a/plugins/beer.go +++ b/plugins/beer.go @@ -129,31 +129,24 @@ func (h *Beer) pretty(b BeerResp, random bool) string { ) } -// Respond to looking up of beer requests -func (h *Beer) 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 { - if h.match(post) { - beer := h.fix(post) - if beer != "" { - log.Printf("%s: responding to '%s'", h.Name(), ev.Sender) - brr, err := h.get(beer) - if err != nil { - SendText(c, ev.RoomID, fmt.Sprintf("sorry %s, I can't look for beer. (%s)", ev.Sender, err)) - } +// RespondText to looking up of beer requests +func (h *Beer) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) { + if h.match(post) { + beer := h.fix(post) + if beer != "" { + log.Printf("%s: responding to '%s'", h.Name(), ev.Sender) + brr, err := h.get(beer) + if err != nil { + SendText(c, ev.RoomID, fmt.Sprintf("sorry %s, I can't look for beer. (%s)", ev.Sender, err)) + } - switch { - case brr.Nhits == 0: - SendText(c, ev.RoomID, "¯\\_(ツ)_/¯") - case brr.Nhits == 1: - SendText(c, ev.RoomID, fmt.Sprintf("%s", h.pretty(*brr, false))) - case brr.Nhits > 1: - SendText(c, ev.RoomID, fmt.Sprintf("Found %d beers, here is a random one:\n%s", brr.Nhits, h.pretty(*brr, true))) - } - } - } + switch { + case brr.Nhits == 0: + SendText(c, ev.RoomID, "¯\\_(ツ)_/¯") + case brr.Nhits == 1: + SendText(c, ev.RoomID, fmt.Sprintf("%s", h.pretty(*brr, false))) + case brr.Nhits > 1: + SendText(c, ev.RoomID, fmt.Sprintf("Found %d beers, here is a random one:\n%s", brr.Nhits, h.pretty(*brr, true))) } } } diff --git a/plugins/botsnack.go b/plugins/botsnack.go index 3f6ce7e..1bd72d6 100644 --- a/plugins/botsnack.go +++ b/plugins/botsnack.go @@ -31,20 +31,13 @@ func (h *BotSnack) resp() string { } -// Respond to hi events -func (h *BotSnack) 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") - if ToMe(u, post) { - if h.match(post) { - log.Printf("%s: responding to '%s'", h.Name(), ev.Sender) - SendText(c, ev.RoomID, h.resp()) - } - } - } +// RespondText to hi events +func (h *BotSnack) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) { + u := NameRE.ReplaceAllString(user, "$1") + if ToMe(u, post) { + if h.match(post) { + log.Printf("%s: responding to '%s'", h.Name(), ev.Sender) + SendText(c, ev.RoomID, h.resp()) } } } diff --git a/plugins/hi.go b/plugins/hi.go index d53fed2..28f87c9 100644 --- a/plugins/hi.go +++ b/plugins/hi.go @@ -17,21 +17,14 @@ func (h *Hi) match(msg string) bool { 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) - SendText(c, ev.RoomID, fmt.Sprintf("hi %s!", s)) - } - } - } +// RespondText to hi events +func (h *Hi) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) { + 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) + SendText(c, ev.RoomID, fmt.Sprintf("hi %s!", s)) } } } diff --git a/plugins/highfive.go b/plugins/highfive.go index 6b32655..48d55c4 100644 --- a/plugins/highfive.go +++ b/plugins/highfive.go @@ -12,25 +12,18 @@ import ( 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) - SendText(c, ev.RoomID, fmt.Sprintf("\\o %s", s)) - } - if strings.Contains(post, "\\o") { - log.Printf("%s: responding to '%s'", h.Name(), ev.Sender) - SendText(c, ev.RoomID, fmt.Sprintf("%s o/", s)) - } - } - } +// RespondText to high five events +func (h *HighFive) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) { + 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) + SendText(c, ev.RoomID, fmt.Sprintf("\\o %s", s)) + } + if strings.Contains(post, "\\o") { + log.Printf("%s: responding to '%s'", h.Name(), ev.Sender) + SendText(c, ev.RoomID, fmt.Sprintf("%s o/", s)) } } } diff --git a/plugins/love.go b/plugins/love.go index 9c07cc9..6d97499 100644 --- a/plugins/love.go +++ b/plugins/love.go @@ -32,20 +32,13 @@ func (h *LoveYou) resp() string { } -// Respond to love events -func (h *LoveYou) 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") - if ToMe(u, post) { - if h.match(post) { - log.Printf("%s: responding to '%s'", h.Name(), ev.Sender) - SendText(c, ev.RoomID, h.resp()) - } - } - } +// RespondText to love events +func (h *LoveYou) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) { + u := NameRE.ReplaceAllString(user, "$1") + if ToMe(u, post) { + if h.match(post) { + log.Printf("%s: responding to '%s'", h.Name(), ev.Sender) + SendText(c, ev.RoomID, h.resp()) } } } diff --git a/plugins/plugins.go b/plugins/plugins.go index 8e775aa..2fa07bc 100644 --- a/plugins/plugins.go +++ b/plugins/plugins.go @@ -10,7 +10,8 @@ import ( // Plugin defines the functions a plugin must implement to be used by // mcchunkie. 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 } diff --git a/plugins/source.go b/plugins/source.go index 06665f2..5725baa 100644 --- a/plugins/source.go +++ b/plugins/source.go @@ -17,21 +17,14 @@ func (h *Source) match(msg string) bool { return re.MatchString(msg) } -// Respond to questions about TheSource™©®⑨ -func (h *Source) 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) - SendText(c, ev.RoomID, fmt.Sprintf("%s: %s ;D", s, "https://git.sr.ht/~qbit/mcchunkie")) - } - } - } +// RespondText to questions about TheSource™©®⑨ +func (h *Source) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) { + 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) + SendText(c, ev.RoomID, fmt.Sprintf("%s: %s ;D", s, "https://git.sr.ht/~qbit/mcchunkie")) } } } diff --git a/plugins/version.go b/plugins/version.go index bde6dd1..1180df5 100644 --- a/plugins/version.go +++ b/plugins/version.go @@ -22,21 +22,14 @@ func (v *Version) print(to string) string { return fmt.Sprintf("%s, I am written in Go, running on %s", to, runtime.GOOS) } -// Respond to version events -func (v *Version) 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 v.match(post) { - log.Printf("%s: responding to '%s'", v.Name(), ev.Sender) - SendText(c, ev.RoomID, v.print(s)) - } - } - } +// RespondText to version events +func (v *Version) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) { + u := NameRE.ReplaceAllString(user, "$1") + s := NameRE.ReplaceAllString(ev.Sender, "$1") + if ToMe(u, post) { + if v.match(post) { + log.Printf("%s: responding to '%s'", v.Name(), ev.Sender) + SendText(c, ev.RoomID, v.print(s)) } } }