add sync state tracking, commands
This commit is contained in:
parent
0e7396c977
commit
734896fe93
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
config.yaml
|
config.yaml
|
||||||
|
statedata
|
||||||
config.yml
|
config.yml
|
||||||
|
85
lazystore.go
Normal file
85
lazystore.go
Normal file
@ -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)
|
||||||
|
}
|
38
main.go
38
main.go
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
@ -15,6 +16,7 @@ var Username string
|
|||||||
var Password string
|
var Password string
|
||||||
var DimensionServer string
|
var DimensionServer string
|
||||||
var HomeserverDomain string
|
var HomeserverDomain string
|
||||||
|
var GitCommit string
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
viper.SetConfigName("config")
|
viper.SetConfigName("config")
|
||||||
@ -30,11 +32,13 @@ func main() {
|
|||||||
Password = viper.GetString("password")
|
Password = viper.GetString("password")
|
||||||
DimensionServer = viper.GetString("dimension")
|
DimensionServer = viper.GetString("dimension")
|
||||||
HomeserverDomain = viper.GetString("domain")
|
HomeserverDomain = viper.GetString("domain")
|
||||||
|
var vtubers []*Vtuber
|
||||||
log.Println("Logging into", Homeserver, "as", Username)
|
log.Println("Logging into", Homeserver, "as", Username)
|
||||||
client, err := mautrix.NewClient(Homeserver, "", "")
|
client, err := mautrix.NewClient(Homeserver, "", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
client.Store = NewLazyMemStore("statedata")
|
||||||
_, err = client.Login(&mautrix.ReqLogin{
|
_, err = client.Login(&mautrix.ReqLogin{
|
||||||
Type: "m.login.password",
|
Type: "m.login.password",
|
||||||
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: Username},
|
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: Username},
|
||||||
@ -48,6 +52,39 @@ func main() {
|
|||||||
syncer := client.Syncer.(*mautrix.DefaultSyncer)
|
syncer := client.Syncer.(*mautrix.DefaultSyncer)
|
||||||
syncer.OnEventType(event.EventMessage, func(source mautrix.EventSource, evt *event.Event) {
|
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)
|
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) {
|
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)
|
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 vtubersRaw []VtuberConfig
|
||||||
var vtubers []*Vtuber
|
|
||||||
err = viper.UnmarshalKey("vtubers", &vtubersRaw)
|
err = viper.UnmarshalKey("vtubers", &vtubersRaw)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
Loading…
Reference in New Issue
Block a user