Share store with plugins
This commit is contained in:
parent
ee2db932cf
commit
d22ab34d54
21
main.go
21
main.go
@ -60,12 +60,12 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if key != "" && value != "" {
|
if key != "" && value != "" {
|
||||||
store.set(key, value)
|
store.Set(key, value)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
if get != "" {
|
if get != "" {
|
||||||
val, err := store.get(get)
|
val, err := store.Get(get)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("%s\n", err)
|
log.Fatalf("%s\n", err)
|
||||||
}
|
}
|
||||||
@ -74,13 +74,13 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if server == "" {
|
if server == "" {
|
||||||
server, err = store.get("server")
|
server, err = store.Get("server")
|
||||||
if server == "" {
|
if server == "" {
|
||||||
log.Fatalln("please specify a server")
|
log.Fatalln("please specify a server")
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
store.set("server", server)
|
store.Set("server", server)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("connecting to %s\n", server)
|
log.Printf("connecting to %s\n", server)
|
||||||
@ -111,16 +111,16 @@ func main() {
|
|||||||
// No longer need tty now that we have our info
|
// No longer need tty now that we have our info
|
||||||
pledge("stdio unveil rpath wpath cpath flock dns inet")
|
pledge("stdio unveil rpath wpath cpath flock dns inet")
|
||||||
|
|
||||||
store.set("username", username)
|
store.Set("username", username)
|
||||||
store.set("access_token", resp.AccessToken)
|
store.Set("access_token", resp.AccessToken)
|
||||||
store.set("user_id", resp.UserID)
|
store.Set("user_id", resp.UserID)
|
||||||
|
|
||||||
accessToken = resp.AccessToken
|
accessToken = resp.AccessToken
|
||||||
userID = resp.UserID
|
userID = resp.UserID
|
||||||
} else {
|
} else {
|
||||||
username, _ = store.get("username")
|
username, _ = store.Get("username")
|
||||||
accessToken, _ = store.get("access_token")
|
accessToken, _ = store.Get("access_token")
|
||||||
userID, _ = store.get("user_id")
|
userID, _ = store.Get("user_id")
|
||||||
}
|
}
|
||||||
|
|
||||||
cli.SetCredentials(userID, accessToken)
|
cli.SetCredentials(userID, accessToken)
|
||||||
@ -164,6 +164,7 @@ func main() {
|
|||||||
if mtype, ok := ev.MessageType(); ok {
|
if mtype, ok := ev.MessageType(); ok {
|
||||||
switch mtype {
|
switch mtype {
|
||||||
case "m.text":
|
case "m.text":
|
||||||
|
p.SetStore(store)
|
||||||
p.RespondText(cli, ev, username, post)
|
p.RespondText(cli, ev, username, post)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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
|
// RespondText to looking up of beer requests
|
||||||
func (h *Beer) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
|
func (h *Beer) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
|
||||||
if h.match(post) {
|
if h.match(post) {
|
||||||
|
@ -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
|
// RespondText to botsnack events
|
||||||
func (h *BotSnack) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
|
func (h *BotSnack) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
|
||||||
u := NameRE.ReplaceAllString(user, "$1")
|
u := NameRE.ReplaceAllString(user, "$1")
|
||||||
|
@ -17,6 +17,9 @@ func (h *Hi) match(msg string) bool {
|
|||||||
return re.MatchString(msg)
|
return re.MatchString(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetStore we don't need a store here
|
||||||
|
func (h *Hi) SetStore(s PluginStore) { return }
|
||||||
|
|
||||||
// RespondText to hi events
|
// RespondText to hi events
|
||||||
func (h *Hi) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
|
func (h *Hi) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
|
||||||
u := NameRE.ReplaceAllString(user, "$1")
|
u := NameRE.ReplaceAllString(user, "$1")
|
||||||
|
@ -12,6 +12,9 @@ import (
|
|||||||
type HighFive struct {
|
type HighFive struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetStore we don't need a store here.
|
||||||
|
func (h *HighFive) SetStore(s PluginStore) { return }
|
||||||
|
|
||||||
// RespondText to high five events
|
// RespondText to high five events
|
||||||
func (h *HighFive) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
|
func (h *HighFive) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
|
||||||
u := NameRE.ReplaceAllString(user, "$1")
|
u := NameRE.ReplaceAllString(user, "$1")
|
||||||
|
@ -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
|
// RespondText to love events
|
||||||
func (h *LoveYou) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
|
func (h *LoveYou) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
|
||||||
u := NameRE.ReplaceAllString(user, "$1")
|
u := NameRE.ReplaceAllString(user, "$1")
|
||||||
|
@ -37,6 +37,9 @@ func (h *OpenBSDMan) match(msg string) bool {
|
|||||||
return re.MatchString(msg)
|
return re.MatchString(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetStore does nothing in OpenBSDMan
|
||||||
|
func (h *OpenBSDMan) SetStore(s PluginStore) { return }
|
||||||
|
|
||||||
// RespondText sends back a man page.
|
// RespondText sends back a man page.
|
||||||
func (h *OpenBSDMan) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
|
func (h *OpenBSDMan) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
|
||||||
if h.match(post) {
|
if h.match(post) {
|
||||||
|
@ -7,12 +7,19 @@ import (
|
|||||||
"github.com/matrix-org/gomatrix"
|
"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
|
// Plugin defines the functions a plugin must implement to be used by
|
||||||
// mcchunkie.
|
// mcchunkie.
|
||||||
type Plugin interface {
|
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)
|
RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, path string)
|
||||||
Name() string
|
Name() string
|
||||||
|
SetStore(s PluginStore)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NameRE matches the "friendly" name. This is typically used in tab
|
// NameRE matches the "friendly" name. This is typically used in tab
|
||||||
|
@ -17,6 +17,9 @@ func (h *Source) match(msg string) bool {
|
|||||||
return re.MatchString(msg)
|
return re.MatchString(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetStore does nothing in here
|
||||||
|
func (h *Source) SetStore(s PluginStore) { return }
|
||||||
|
|
||||||
// RespondText to questions about TheSource™©®⑨
|
// RespondText to questions about TheSource™©®⑨
|
||||||
func (h *Source) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
|
func (h *Source) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
|
||||||
u := NameRE.ReplaceAllString(user, "$1")
|
u := NameRE.ReplaceAllString(user, "$1")
|
||||||
|
@ -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)
|
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
|
// RespondText to version events
|
||||||
func (v *Version) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
|
func (v *Version) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
|
||||||
u := NameRE.ReplaceAllString(user, "$1")
|
u := NameRE.ReplaceAllString(user, "$1")
|
||||||
|
18
store.go
18
store.go
@ -29,12 +29,14 @@ func NewStore(path string) (*MCStore, error) {
|
|||||||
return s, nil
|
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)
|
v := []byte(value)
|
||||||
s.db.Write(key, v)
|
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)
|
b, err := s.db.Read(key)
|
||||||
return string(b), err
|
return string(b), err
|
||||||
}
|
}
|
||||||
@ -62,36 +64,36 @@ func (s *MCStore) decodeRoom(room []byte) (*gomatrix.Room, error) {
|
|||||||
|
|
||||||
// SaveFilterID exposed for gomatrix
|
// SaveFilterID exposed for gomatrix
|
||||||
func (s *MCStore) SaveFilterID(userID, filterID string) {
|
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
|
// LoadFilterID exposed for gomatrix
|
||||||
func (s *MCStore) LoadFilterID(userID string) string {
|
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)
|
return string(filter)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SaveNextBatch exposed for gomatrix
|
// SaveNextBatch exposed for gomatrix
|
||||||
func (s *MCStore) SaveNextBatch(userID, nextBatchToken string) {
|
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
|
// LoadNextBatch exposed for gomatrix
|
||||||
func (s *MCStore) LoadNextBatch(userID string) string {
|
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)
|
return string(batch)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SaveRoom exposed for gomatrix
|
// SaveRoom exposed for gomatrix
|
||||||
func (s *MCStore) SaveRoom(room *gomatrix.Room) {
|
func (s *MCStore) SaveRoom(room *gomatrix.Room) {
|
||||||
b, _ := s.encodeRoom(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
|
// LoadRoom exposed for gomatrix
|
||||||
func (s *MCStore) LoadRoom(roomID string) *gomatrix.Room {
|
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))
|
room, _ := s.decodeRoom([]byte(b))
|
||||||
return room
|
return room
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user