From d22ab34d543aaaef63831f31923c63d5519d0f5c Mon Sep 17 00:00:00 2001 From: Aaron Bieber Date: Wed, 5 Feb 2020 22:04:04 -0700 Subject: [PATCH] Share store with plugins --- main.go | 21 +++++++++++---------- plugins/beer.go | 3 +++ plugins/botsnack.go | 3 +++ plugins/hi.go | 3 +++ plugins/highfive.go | 3 +++ plugins/love.go | 3 +++ plugins/openbsd_man.go | 3 +++ plugins/plugins.go | 7 +++++++ plugins/source.go | 3 +++ plugins/version.go | 3 +++ store.go | 18 ++++++++++-------- 11 files changed, 52 insertions(+), 18 deletions(-) diff --git a/main.go b/main.go index ddaeac3..35810cf 100644 --- a/main.go +++ b/main.go @@ -60,12 +60,12 @@ func main() { } if key != "" && value != "" { - store.set(key, value) + store.Set(key, value) os.Exit(0) } if get != "" { - val, err := store.get(get) + val, err := store.Get(get) if err != nil { log.Fatalf("%s\n", err) } @@ -74,13 +74,13 @@ func main() { } if server == "" { - server, err = store.get("server") + server, err = store.Get("server") if server == "" { log.Fatalln("please specify a server") } } else { - store.set("server", server) + store.Set("server", server) } log.Printf("connecting to %s\n", server) @@ -111,16 +111,16 @@ func main() { // No longer need tty now that we have our info pledge("stdio unveil rpath wpath cpath flock dns inet") - store.set("username", username) - store.set("access_token", resp.AccessToken) - store.set("user_id", resp.UserID) + store.Set("username", username) + store.Set("access_token", resp.AccessToken) + store.Set("user_id", resp.UserID) accessToken = resp.AccessToken userID = resp.UserID } else { - username, _ = store.get("username") - accessToken, _ = store.get("access_token") - userID, _ = store.get("user_id") + username, _ = store.Get("username") + accessToken, _ = store.Get("access_token") + userID, _ = store.Get("user_id") } cli.SetCredentials(userID, accessToken) @@ -164,6 +164,7 @@ func main() { if mtype, ok := ev.MessageType(); ok { switch mtype { case "m.text": + p.SetStore(store) p.RespondText(cli, ev, username, post) } } diff --git a/plugins/beer.go b/plugins/beer.go index e5055ab..5e2e00e 100644 --- a/plugins/beer.go +++ b/plugins/beer.go @@ -129,6 +129,9 @@ func (h *Beer) pretty(b BeerResp, random bool) string { ) } +// SetStore we don't need a store here. +func (h *Beer) SetStore(s PluginStore) { return } + // RespondText to looking up of beer requests func (h *Beer) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) { if h.match(post) { diff --git a/plugins/botsnack.go b/plugins/botsnack.go index 93ef753..ce73f4f 100644 --- a/plugins/botsnack.go +++ b/plugins/botsnack.go @@ -31,6 +31,9 @@ func (h *BotSnack) resp() string { } +// SetStore we don't need a store, so just return +func (h *BotSnack) SetStore(s PluginStore) { return } + // RespondText to botsnack events func (h *BotSnack) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) { u := NameRE.ReplaceAllString(user, "$1") diff --git a/plugins/hi.go b/plugins/hi.go index 28f87c9..eb6d72d 100644 --- a/plugins/hi.go +++ b/plugins/hi.go @@ -17,6 +17,9 @@ func (h *Hi) match(msg string) bool { return re.MatchString(msg) } +// SetStore we don't need a store here +func (h *Hi) SetStore(s PluginStore) { return } + // RespondText to hi events func (h *Hi) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) { u := NameRE.ReplaceAllString(user, "$1") diff --git a/plugins/highfive.go b/plugins/highfive.go index 48d55c4..d41593a 100644 --- a/plugins/highfive.go +++ b/plugins/highfive.go @@ -12,6 +12,9 @@ import ( type HighFive struct { } +// SetStore we don't need a store here. +func (h *HighFive) SetStore(s PluginStore) { return } + // RespondText to high five events func (h *HighFive) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) { u := NameRE.ReplaceAllString(user, "$1") diff --git a/plugins/love.go b/plugins/love.go index 6d97499..467ef58 100644 --- a/plugins/love.go +++ b/plugins/love.go @@ -32,6 +32,9 @@ func (h *LoveYou) resp() string { } +// SetStore we don't need a store, so just return +func (h *LoveYou) SetStore(s PluginStore) { return } + // RespondText to love events func (h *LoveYou) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) { u := NameRE.ReplaceAllString(user, "$1") diff --git a/plugins/openbsd_man.go b/plugins/openbsd_man.go index 4f502a5..33ee794 100644 --- a/plugins/openbsd_man.go +++ b/plugins/openbsd_man.go @@ -37,6 +37,9 @@ func (h *OpenBSDMan) match(msg string) bool { return re.MatchString(msg) } +// SetStore does nothing in OpenBSDMan +func (h *OpenBSDMan) SetStore(s PluginStore) { return } + // RespondText sends back a man page. func (h *OpenBSDMan) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) { if h.match(post) { diff --git a/plugins/plugins.go b/plugins/plugins.go index 34b6511..a704218 100644 --- a/plugins/plugins.go +++ b/plugins/plugins.go @@ -7,12 +7,19 @@ import ( "github.com/matrix-org/gomatrix" ) +// PluginStore matches MCStore so that the main store can be used by plugins. +type PluginStore interface { + Set(key, values string) + Get(key string) (string, error) +} + // 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) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, path string) Name() string + SetStore(s PluginStore) } // NameRE matches the "friendly" name. This is typically used in tab diff --git a/plugins/source.go b/plugins/source.go index 5725baa..d5599ef 100644 --- a/plugins/source.go +++ b/plugins/source.go @@ -17,6 +17,9 @@ func (h *Source) match(msg string) bool { return re.MatchString(msg) } +// SetStore does nothing in here +func (h *Source) SetStore(s PluginStore) { return } + // RespondText to questions about TheSource™©®⑨ func (h *Source) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) { u := NameRE.ReplaceAllString(user, "$1") diff --git a/plugins/version.go b/plugins/version.go index 1180df5..b8deac2 100644 --- a/plugins/version.go +++ b/plugins/version.go @@ -22,6 +22,9 @@ func (v *Version) print(to string) string { return fmt.Sprintf("%s, I am written in Go, running on %s", to, runtime.GOOS) } +// SetStore does nothing in here +func (h *Version) SetStore(s PluginStore) { return } + // RespondText to version events func (v *Version) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) { u := NameRE.ReplaceAllString(user, "$1") diff --git a/store.go b/store.go index 5dd0c20..1cc003c 100644 --- a/store.go +++ b/store.go @@ -29,12 +29,14 @@ func NewStore(path string) (*MCStore, error) { return s, nil } -func (s *MCStore) set(key string, value string) { +// Set takes a key value pair and shoves it in a db. +func (s *MCStore) Set(key string, value string) { v := []byte(value) s.db.Write(key, v) } -func (s *MCStore) get(key string) (string, error) { +// Get retrives a value from the db +func (s *MCStore) Get(key string) (string, error) { b, err := s.db.Read(key) return string(b), err } @@ -62,36 +64,36 @@ func (s *MCStore) decodeRoom(room []byte) (*gomatrix.Room, error) { // SaveFilterID exposed for gomatrix func (s *MCStore) SaveFilterID(userID, filterID string) { - s.set(fmt.Sprintf("filter_%s", userID), filterID) + s.Set(fmt.Sprintf("filter_%s", userID), filterID) } // LoadFilterID exposed for gomatrix func (s *MCStore) LoadFilterID(userID string) string { - filter, _ := s.get(fmt.Sprintf("filter_%s", userID)) + filter, _ := s.Get(fmt.Sprintf("filter_%s", userID)) return string(filter) } // SaveNextBatch exposed for gomatrix func (s *MCStore) SaveNextBatch(userID, nextBatchToken string) { - s.set(fmt.Sprintf("batch_%s", userID), nextBatchToken) + s.Set(fmt.Sprintf("batch_%s", userID), nextBatchToken) } // LoadNextBatch exposed for gomatrix func (s *MCStore) LoadNextBatch(userID string) string { - batch, _ := s.get(fmt.Sprintf("batch_%s", userID)) + batch, _ := s.Get(fmt.Sprintf("batch_%s", userID)) return string(batch) } // SaveRoom exposed for gomatrix func (s *MCStore) SaveRoom(room *gomatrix.Room) { b, _ := s.encodeRoom(room) - s.set(fmt.Sprintf("room_%s", room.ID), string(b)) + s.Set(fmt.Sprintf("room_%s", room.ID), string(b)) } // LoadRoom exposed for gomatrix func (s *MCStore) LoadRoom(roomID string) *gomatrix.Room { - b, _ := s.get(fmt.Sprintf("room_%s", roomID)) + b, _ := s.Get(fmt.Sprintf("room_%s", roomID)) room, _ := s.decodeRoom([]byte(b)) return room }