2020-02-03 16:19:04 -07:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"regexp"
|
|
|
|
|
|
|
|
"github.com/matrix-org/gomatrix"
|
|
|
|
)
|
|
|
|
|
|
|
|
// OpenBSDMan responds to beer requests
|
|
|
|
type OpenBSDMan struct {
|
|
|
|
}
|
|
|
|
|
2020-02-05 17:34:41 -07:00
|
|
|
func (h *OpenBSDMan) re() string {
|
|
|
|
return `(?i)^man: ([1-9]?p?)\s?(\w+)$`
|
|
|
|
}
|
|
|
|
|
2020-02-03 16:19:04 -07:00
|
|
|
func (h *OpenBSDMan) fix(msg string) string {
|
2020-02-05 17:34:41 -07:00
|
|
|
re := regexp.MustCompile(h.re())
|
2020-02-05 07:55:39 -07:00
|
|
|
resp := ""
|
|
|
|
section := re.ReplaceAllString(msg, "$1")
|
2020-02-05 17:34:41 -07:00
|
|
|
if section == msg {
|
|
|
|
return ""
|
|
|
|
}
|
2020-02-05 07:55:39 -07:00
|
|
|
if section != "" {
|
|
|
|
resp = re.ReplaceAllString(msg, "$2.$1")
|
|
|
|
} else {
|
|
|
|
resp = re.ReplaceAllString(msg, "$2")
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp
|
2020-02-03 16:19:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *OpenBSDMan) match(msg string) bool {
|
2020-02-05 17:34:41 -07:00
|
|
|
re := regexp.MustCompile(h.re())
|
2020-02-03 16:19:04 -07:00
|
|
|
return re.MatchString(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RespondText sends back a man page.
|
|
|
|
func (h *OpenBSDMan) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
|
|
|
|
if h.match(post) {
|
|
|
|
page := h.fix(post)
|
|
|
|
if page != "" {
|
|
|
|
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
|
|
|
|
SendText(c, ev.RoomID, fmt.Sprintf("https://man.openbsd.org/%s", page))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name OpenBSDMan!
|
|
|
|
func (h *OpenBSDMan) Name() string {
|
|
|
|
return "OpenBSDMan"
|
|
|
|
}
|