From 734896fe9356b7c08686e46ddac4633e25947267 Mon Sep 17 00:00:00 2001 From: Steve Date: Fri, 25 Jun 2021 13:58:04 -0400 Subject: [PATCH] add sync state tracking, commands --- .gitignore | 1 + lazystore.go | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 38 ++++++++++++++++++++++- 3 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 lazystore.go diff --git a/.gitignore b/.gitignore index f81fc0c..8d22f12 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ config.yaml +statedata config.yml diff --git a/lazystore.go b/lazystore.go new file mode 100644 index 0000000..713e5fc --- /dev/null +++ b/lazystore.go @@ -0,0 +1,85 @@ +package main + +import ( + "bytes" + "encoding/gob" + + "os" + + "maunium.net/go/mautrix" + "maunium.net/go/mautrix/id" +) + +type LazyMemStore struct { + mem *mautrix.InMemoryStore + NextBatch map[id.UserID]string + saveFile string +} + +func NewLazyMemStore(fileloc string) *LazyMemStore { + return &LazyMemStore{ + mem: mautrix.NewInMemoryStore(), + NextBatch: make(map[id.UserID]string), + saveFile: fileloc, + } +} + +func (l *LazyMemStore) SaveFilterID(userID id.UserID, filterID string) { + l.mem.SaveFilterID(userID, filterID) +} + +func (l *LazyMemStore) LoadFilterID(userID id.UserID) string { + return l.mem.LoadFilterID(userID) +} + +func (l *LazyMemStore) SaveNextBatch(userID id.UserID, nextBatchToken string) { + b := new(bytes.Buffer) + l.NextBatch[userID] = nextBatchToken + e := gob.NewEncoder(b) + err := e.Encode(l.NextBatch) + if err != nil { + panic(err) + } + if err := os.WriteFile(l.saveFile, b.Bytes(), 0666); err != nil { + panic(err) + } + +} + +func (l *LazyMemStore) LoadNextBatch(userID id.UserID) string { + + dat, err := os.ReadFile(l.saveFile) + if err != nil { + if os.IsNotExist(err) { + b := new(bytes.Buffer) + e := gob.NewEncoder(b) + err := e.Encode(l.NextBatch) + if err != nil { + panic(err) + } + if err := os.WriteFile(l.saveFile, b.Bytes(), 0666); err != nil { + panic(err) + } + dat, err = os.ReadFile(l.saveFile) + if err != nil { + panic(err) + } + } else { + panic(err) + } + } + d := gob.NewDecoder(bytes.NewBuffer(dat)) + err = d.Decode(&l.NextBatch) + if err != nil { + panic(err) + } + return l.NextBatch[userID] +} + +func (l *LazyMemStore) SaveRoom(room *mautrix.Room) { + l.mem.SaveRoom(room) +} + +func (l *LazyMemStore) LoadRoom(roomID id.RoomID) *mautrix.Room { + return l.mem.LoadRoom(roomID) +} diff --git a/main.go b/main.go index 2da5a1e..652763e 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" "log" + "strings" "time" "github.com/spf13/viper" @@ -15,6 +16,7 @@ var Username string var Password string var DimensionServer string var HomeserverDomain string +var GitCommit string func main() { viper.SetConfigName("config") @@ -30,11 +32,13 @@ func main() { Password = viper.GetString("password") DimensionServer = viper.GetString("dimension") HomeserverDomain = viper.GetString("domain") + var vtubers []*Vtuber log.Println("Logging into", Homeserver, "as", Username) client, err := mautrix.NewClient(Homeserver, "", "") if err != nil { panic(err) } + client.Store = NewLazyMemStore("statedata") _, err = client.Login(&mautrix.ReqLogin{ Type: "m.login.password", Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: Username}, @@ -48,6 +52,39 @@ func main() { syncer := client.Syncer.(*mautrix.DefaultSyncer) syncer.OnEventType(event.EventMessage, func(source mautrix.EventSource, evt *event.Event) { fmt.Printf("<%[1]s> %[4]s (%[2]s/%[3]s)\n", evt.Sender, evt.Type.String(), evt.ID, evt.Content.AsMessage().Body) + body := evt.Content.AsMessage().Body + body_s := strings.Split(body, " ") + if body_s[0] != "!simp" { + return + } + if len(body_s) < 2 { + return //nothing to parse + } + if evt.Sender == client.UserID { + return //ignore events from self + } + switch body_s[1] { + case "info": + // print info page + var infomsg string + vlist := []string{} + for _, vt := range vtubers { + vlist = append(vlist, vt.Name) + } + infomsg = fmt.Sprintf("Currently Simping For: \n%v", strings.Join(vlist, "\n")) + client.SendText(evt.RoomID, infomsg) + case "version": + // print version + client.SendText(evt.RoomID, "SimpBot version "+GitCommit) + case "reload": + //reload config + client.SendText(evt.RoomID, "unimplemented") + case "help": + client.SendText(evt.RoomID, "Supported commands: info,version") + default: + //command not found + client.SendText(evt.RoomID, "command not recognized") + } }) syncer.OnEventType(event.StateMember, func(source mautrix.EventSource, evt *event.Event) { fmt.Printf("<%[1]s> %[4]s (%[2]s/%[3]s)\n", evt.Sender, evt.Type.String(), evt.ID, evt.Content.AsMessage().Body) @@ -61,7 +98,6 @@ func main() { } }) var vtubersRaw []VtuberConfig - var vtubers []*Vtuber err = viper.UnmarshalKey("vtubers", &vtubersRaw) if err != nil { panic(err)