From 27f4a7e6e22c6b2413f90ba359b5aa59d628c0fd Mon Sep 17 00:00:00 2001 From: Aaron Bieber Date: Tue, 11 Feb 2020 07:56:19 -0700 Subject: [PATCH] add Re and Descr functions so we can produce better docs --- README.md | 21 +++++++++++---------- main.go | 22 ++++++++++++++++------ plugins/beer.go | 12 +++++++++--- plugins/botsnack.go | 12 +++++++++++- plugins/hi.go | 12 +++++++++++- plugins/highfive.go | 22 ++++++++++++++++++++-- plugins/love.go | 12 +++++++++++- plugins/openbsd_man.go | 12 +++++++++--- plugins/plugins.go | 16 +++++++++++----- plugins/source.go | 12 +++++++++++- plugins/version.go | 12 +++++++++++- plugins/weather.go | 12 +++++++++--- plugins/welcome_back.go | 12 +++++++++++- 13 files changed, 151 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 0bb1424..f1d274c 100644 --- a/README.md +++ b/README.md @@ -2,14 +2,15 @@ [![builds.sr.ht status](https://builds.sr.ht/~qbit/mcchunkie.svg)](https://builds.sr.ht/~qbit/mcchunkie?) -| Plugin | Match | Purpose | -|----------|-------------------------------|----------------------------------------------------------------------------------------------------------------------------------------| -| beer | '^beer: ' | Queries [OpenDataSoft](https://public-us.opendatasoft.com/explore/dataset/open-beer-database/table/)'s beer database for a given beer. | -| botsnack | name: 'botsnack$' | Consumes a botsnack. This pleases mcchunkie and brings balance to the universe. | -| hi | name: '^hi' or 'hi$' | Friendly bots say hi. | -| highfive | name: 'o/' or '\o' | Everyone loves highfives. | -| love | name: 'i love you' | Spreading love where ever we can. | -| source | name: 'where is your source?' | Tell people where they can find more information on mcchunkie. | -| version | name: 'version$' | Bit of information about what we are. | - +|Plugin Name|Match|Description| +|Beer|`(?i)^beer: `|Queries [OpenDataSoft](https://public-us.opendatasoft.com/explore/dataset/open-beer-database/table/)'s beer database for a given beer.| +|BotSnack|`(?i)botsnack`|Consumes a botsnack. This pleases mcchunkie and brings balance to the universe.| +|HighFive|`o/|\o`|Everyone loves highfives.| +|Hi|`(?i)^hi|hi$`|Friendly bots say hi.| +|LoveYou|`(?i)i love you`|Spreading love where ever we can by responding when someone shows us love.| +|OpenBSDMan|`(?i)^man: ([1-9]?p?)\s?(\w+)$`|Produces a link to man.openbsd.org.| +|Source|`(?i)where is your (source|code)`|Tell people where they can find more information about myself.| +|Version|`(?i)version$`|Show a bit of information about what we are.| +|Wb|`(?i)^welcome back|welcome back$|^wb|wb$`|Respond to welcome back messages.| +|Weather|`(?i)^weather: (\d+)$`|Produce weather information for a given ZIP code. Data comes from openweathermap.org.| diff --git a/main.go b/main.go index 7f67ae8..e2ef32f 100644 --- a/main.go +++ b/main.go @@ -15,16 +15,18 @@ import ( func main() { var username, password, userID, accessToken, server, db, avatar string var key, value, get string - var setup bool + var setup, doc bool - flag.StringVar(&username, "user", "", "username to connect to matrix server with") + flag.BoolVar(&doc, "doc", false, "print plugin information and exit") + flag.BoolVar(&setup, "s", false, "setup account") + + flag.StringVar(&avatar, "avatar", "", "set the avatar of the bot to specified url") + flag.StringVar(&db, "db", "db", "full path to database directory") flag.StringVar(&get, "get", "", "grab an entry from the store") flag.StringVar(&key, "key", "", "create an entry in the data store listed under 'key'") - flag.StringVar(&value, "value", "", "set the value of 'key' to be stored") flag.StringVar(&server, "server", "", "matrix server") - flag.StringVar(&avatar, "avatar", "", "set the avatar of the bot to specified url") - flag.BoolVar(&setup, "s", false, "setup account") - flag.StringVar(&db, "db", "db", "full path to database directory") + flag.StringVar(&username, "user", "", "username to connect to matrix server with") + flag.StringVar(&value, "value", "", "set the value of 'key' to be stored") flag.Parse() @@ -44,6 +46,14 @@ func main() { os.Exit(0) } + if doc { + fmt.Println("|Plugin Name|Match|Description|") + for _, p := range plugins.Plugs { + fmt.Printf("|%s|`%s`|%s|\n", p.Name(), p.Re(), p.Descr()) + } + os.Exit(0) + } + if get != "" { val, err := store.Get(get) if err != nil { diff --git a/plugins/beer.go b/plugins/beer.go index b48fb9e..9b071c8 100644 --- a/plugins/beer.go +++ b/plugins/beer.go @@ -74,18 +74,24 @@ type Records struct { RecordTimestamp time.Time `json:"record_timestamp"` } -func (h *Beer) re() string { +// Descr describes this plugin +func (h *Beer) Descr() string { + return "Queries [OpenDataSoft](https://public-us.opendatasoft.com/explore/dataset/open-beer-database/table/)'s beer database for a given beer." +} + +// Re returns the beer matching string +func (h *Beer) Re() string { return `(?i)^beer: ` } func (h *Beer) fix(msg string) string { - re := regexp.MustCompile(h.re()) + re := regexp.MustCompile(h.Re()) return re.ReplaceAllString(msg, "$1") } // Match determines if we should call the response for Beer func (h *Beer) Match(user, msg string) bool { - re := regexp.MustCompile(h.re()) + re := regexp.MustCompile(h.Re()) return re.MatchString(msg) } diff --git a/plugins/botsnack.go b/plugins/botsnack.go index 320b217..792f968 100644 --- a/plugins/botsnack.go +++ b/plugins/botsnack.go @@ -13,9 +13,19 @@ import ( type BotSnack struct { } +// Descr returns a description +func (h *BotSnack) Descr() string { + return "Consumes a botsnack. This pleases mcchunkie and brings balance to the universe." +} + +// Re matches "botsnack" in a given string +func (h *BotSnack) Re() string { + return `(?i)botsnack` +} + // Match determines if we should execute BotSnack func (h *BotSnack) Match(user, msg string) bool { - re := regexp.MustCompile(`(?i)botsnack`) + re := regexp.MustCompile(h.Re()) return re.MatchString(msg) } diff --git a/plugins/hi.go b/plugins/hi.go index 4bebe2c..00f6c6b 100644 --- a/plugins/hi.go +++ b/plugins/hi.go @@ -12,9 +12,19 @@ import ( type Hi struct { } +// Descr describes this plugin +func (h *Hi) Descr() string { + return "Friendly bots say hi." +} + +// Re is the regex for matching hi messages. +func (h *Hi) Re() string { + return `(?i)^hi|hi$` +} + // Match determines if we are highfiving func (h *Hi) Match(user, msg string) bool { - re := regexp.MustCompile(`(?i)^hi|hi$`) + re := regexp.MustCompile(h.Re()) return re.MatchString(msg) && ToMe(user, msg) } diff --git a/plugins/highfive.go b/plugins/highfive.go index 3d5a3b1..3f50dfb 100644 --- a/plugins/highfive.go +++ b/plugins/highfive.go @@ -12,6 +12,24 @@ import ( type HighFive struct { } +func rightFive() string { + return "o/" +} + +func leftFive() string { + return "\\o" +} + +// Descr describes this plugin +func (h *HighFive) Descr() string { + return "Everyone loves highfives." +} + +// Re are the regexes that high five uses +func (h *HighFive) Re() string { + return fmt.Sprintf("%s|%s", rightFive(), leftFive()) +} + // SetStore we don't need a store here. func (h *HighFive) SetStore(s PluginStore) {} @@ -24,11 +42,11 @@ func (h *HighFive) Match(user, msg string) bool { func (h *HighFive) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) { s := NameRE.ReplaceAllString(ev.Sender, "$1") - if strings.Contains(post, "o/") { + if strings.Contains(post, rightFive()) { log.Printf("%s: responding to '%s'", h.Name(), ev.Sender) SendText(c, ev.RoomID, fmt.Sprintf("\\o %s", s)) } - if strings.Contains(post, "\\o") { + if strings.Contains(post, leftFive()) { 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 a84e52b..e9fd547 100644 --- a/plugins/love.go +++ b/plugins/love.go @@ -13,9 +13,19 @@ import ( type LoveYou struct { } +// Descr describes this plugin +func (h *LoveYou) Descr() string { + return "Spreading love where ever we can by responding when someone shows us love." +} + +// Re matches "i love you" +func (h *LoveYou) Re() string { + return `(?i)i love you` +} + // Match checks for 'i love you' and a reference to the bot name func (h *LoveYou) Match(user, msg string) bool { - re := regexp.MustCompile(`(?i)i love you`) + re := regexp.MustCompile(h.Re()) return re.MatchString(msg) && ToMe(user, msg) } diff --git a/plugins/openbsd_man.go b/plugins/openbsd_man.go index a68d323..7c605da 100644 --- a/plugins/openbsd_man.go +++ b/plugins/openbsd_man.go @@ -12,12 +12,18 @@ import ( type OpenBSDMan struct { } -func (h *OpenBSDMan) re() string { +// Descr describes this plugin +func (h *OpenBSDMan) Descr() string { + return "Produces a link to man.openbsd.org." +} + +// Re matches our man format +func (h *OpenBSDMan) Re() string { return `(?i)^man: ([1-9]?p?)\s?(\w+)$` } func (h *OpenBSDMan) fix(msg string) string { - re := regexp.MustCompile(h.re()) + re := regexp.MustCompile(h.Re()) resp := "" section := re.ReplaceAllString(msg, "$1") if section == msg { @@ -34,7 +40,7 @@ func (h *OpenBSDMan) fix(msg string) string { // Match checks for our man page re func (h *OpenBSDMan) Match(user, msg string) bool { - re := regexp.MustCompile(h.re()) + re := regexp.MustCompile(h.Re()) return re.MatchString(msg) } diff --git a/plugins/plugins.go b/plugins/plugins.go index e1a8d54..970caba 100644 --- a/plugins/plugins.go +++ b/plugins/plugins.go @@ -17,16 +17,22 @@ type PluginStore interface { // Plugin defines the interface a plugin must implement to be used by // mcchunkie. type Plugin interface { - // RespondText responds to a "m.text" event - RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, path string) - - // Name should return the human readable name of the bot - Name() string + // Descr returns a brief description of the plugin. + Descr() string // Match determines if the plugin's main Respond function should be // called Match(user, message string) bool + // Name should return the human readable name of the bot + Name() string + + // Re returns the regular expression that a plugin uses to "match" + Re() string + + // RespondText responds to a "m.text" event + RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, path string) + // SetStore exposes the top level MCStore to a plugin SetStore(s PluginStore) } diff --git a/plugins/source.go b/plugins/source.go index 2a33a1c..e5c6192 100644 --- a/plugins/source.go +++ b/plugins/source.go @@ -12,9 +12,19 @@ import ( type Source struct { } +// Descr describes this plugin +func (h *Source) Descr() string { + return "Tell people where they can find more information about myself." +} + +// Re matches the source code question +func (h *Source) Re() string { + return `(?i)where is your (source|code)` +} + // Match determins if someone is asking about the source code func (h *Source) Match(user, msg string) bool { - re := regexp.MustCompile(`(?i)where is your (source|code)`) + re := regexp.MustCompile(h.Re()) return re.MatchString(msg) && ToMe(user, msg) } diff --git a/plugins/version.go b/plugins/version.go index da6dbcf..c6ffd5b 100644 --- a/plugins/version.go +++ b/plugins/version.go @@ -13,10 +13,20 @@ import ( type Version struct { } +// Descr describes this plugin +func (v *Version) Descr() string { + return "Show a bit of information about what we are." +} + +// Re matches version +func (v *Version) Re() string { + return `(?i)version$` +} + // Match checks for "version" anywhere. Might want to tighten this one down at // some point func (v *Version) Match(user, msg string) bool { - re := regexp.MustCompile(`(?i)version$`) + re := regexp.MustCompile(v.Re()) return re.MatchString(msg) && ToMe(user, msg) } diff --git a/plugins/weather.go b/plugins/weather.go index e8463e3..1e64965 100644 --- a/plugins/weather.go +++ b/plugins/weather.go @@ -142,18 +142,24 @@ func (h *Weather) get(loc string) (*WeatherResp, error) { return w, nil } -func (h *Weather) re() string { +// Descr describes this plugin +func (h *Weather) Descr() string { + return "Produce weather information for a given ZIP code. Data comes from openweathermap.org." +} + +// Re is what our weather matches +func (h *Weather) Re() string { return `(?i)^weather: (\d+)$` } // Match checks for "weather: " messages func (h *Weather) Match(user, msg string) bool { - re := regexp.MustCompile(h.re()) + re := regexp.MustCompile(h.Re()) return re.MatchString(msg) } func (h *Weather) fix(msg string) string { - re := regexp.MustCompile(h.re()) + re := regexp.MustCompile(h.Re()) return re.ReplaceAllString(msg, "$1") } diff --git a/plugins/welcome_back.go b/plugins/welcome_back.go index 9564e52..251707e 100644 --- a/plugins/welcome_back.go +++ b/plugins/welcome_back.go @@ -12,9 +12,19 @@ import ( type Wb struct { } +// Descr describes this plugin +func (h *Wb) Descr() string { + return "Respond to welcome back messages." +} + +// Re checks for various welcome back things +func (h *Wb) Re() string { + return `(?i)^welcome back|welcome back$|^wb|wb$` +} + // Match determins if we are welcomed back func (h *Wb) Match(user, msg string) bool { - re := regexp.MustCompile(`(?i)^welcome back|welcome back$|^wb|wb$`) + re := regexp.MustCompile(h.Re()) return re.MatchString(msg) && ToMe(user, msg) }