Share store with plugins

This commit is contained in:
Aaron Bieber 2020-02-05 22:04:04 -07:00
parent ee2db932cf
commit d22ab34d54
11 changed files with 52 additions and 18 deletions

21
main.go
View File

@ -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)
}
}

View File

@ -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) {

View File

@ -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")

View File

@ -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")

View File

@ -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")

View File

@ -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")

View File

@ -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) {

View File

@ -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

View File

@ -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")

View File

@ -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")

View File

@ -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
}