chore: matrix command infrastructure

This commit is contained in:
0x1a8510f2 2023-06-10 02:36:50 +01:00
parent de958fdec0
commit a00212db51
Signed by: 0x1a8510f2
GPG Key ID: 1C692E355D76775D
5 changed files with 48 additions and 14 deletions

6
cmd/pc3/commands.go Normal file
View File

@ -0,0 +1,6 @@
package main
func ExecCmd(command string) (*string, error) {
a := "Test"
return &a, nil
}

View File

@ -157,6 +157,7 @@ mainloop:
// Stop Matrix bot.
stopMatrixBot()
matrixBotWait.Wait()
client.Logout()
os.Exit(0)
}

View File

@ -5,12 +5,14 @@ import (
"crypto/rand"
"errors"
"fmt"
"strings"
"sync"
"time"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/crypto/cryptohelper"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/format"
"maunium.net/go/mautrix/id"
)
@ -23,6 +25,7 @@ func MatrixBotRunStartup(client *mautrix.Client, c Config) {
for _, room := range rooms.JoinedRooms {
if room.String() != c.adminRoom {
client.LeaveRoom(room)
client.ForgetRoom(room)
}
}
}
@ -30,23 +33,44 @@ func MatrixBotRunStartup(client *mautrix.Client, c Config) {
func MatrixBotEventHandlerSetUp(client *mautrix.Client, c Config) {
syncer := client.Syncer.(*mautrix.DefaultSyncer)
// Messages.
syncer.OnEventType(event.EventMessage, func(source mautrix.EventSource, evt *event.Event) {
if evt.RoomID == id.RoomID(c.adminRoom) {
client.SendMessageEvent(evt.RoomID, event.EventReaction, &map[string]any{
"m.relates_to": map[string]any{
"event_id": evt.ID,
"key": "🌊",
"rel_type": "m.annotation",
},
})
// Mark any messages in the admin room as read.
client.SendReceipt(evt.RoomID, evt.ID, event.ReceiptTypeRead, nil)
if message, ok := evt.Content.Parsed.(*event.MessageEventContent); ok {
if command := strings.TrimPrefix(message.Body, "!wmp "); command != message.Body {
// If the message starts with the command prefix, start
// typing to indicate that we're processing the message.
defer client.UserTyping(evt.RoomID, false, time.Microsecond*1)
client.UserTyping(evt.RoomID, true, time.Minute*1)
if replyText, err := ExecCmd(command); replyText != nil {
// If there is a reply, send it.
reply := format.RenderMarkdown(*replyText, true, true)
reply.SetReply(evt)
client.SendMessageEvent(evt.RoomID, event.EventMessage, reply)
} else if err != nil {
// If there is no reply but there is an error, react with nack.
client.SendReaction(evt.RoomID, evt.ID, "❌")
} else {
// Otherwise, react with ack.
client.SendReaction(evt.RoomID, evt.ID, "✅")
}
}
}
}
})
// Invites.
syncer.OnEventType(event.StateMember, func(source mautrix.EventSource, evt *event.Event) {
if evt.GetStateKey() == client.UserID.String() && evt.Content.AsMember().Membership == event.MembershipInvite {
if evt.RoomID == id.RoomID(c.adminRoom) {
client.JoinRoomByID(evt.RoomID)
} else {
client.LeaveRoom(evt.RoomID)
client.ForgetRoom(evt.RoomID)
}
}
})
@ -78,7 +102,7 @@ func MatrixBotInit(ctx context.Context, c Config, wg *sync.WaitGroup) *mautrix.C
StoreHomeserverURL: true,
}
// Set the client crypto helper in order to automatically encrypt outgoing messages.
// Set up the client crypto helper in order to automatically encrypt outgoing messages.
err = cryptoHelper.Init()
if err != nil {
panic(err)

5
go.mod
View File

@ -9,7 +9,7 @@ require (
github.com/gorilla/mux v1.8.0
github.com/gorilla/websocket v1.5.0
github.com/matrix-org/pinecone v0.11.1-0.20230210171230-8c3b24f2649a
github.com/mattn/go-sqlite3 v1.14.16
github.com/mattn/go-sqlite3 v1.14.17
maunium.net/go/mautrix v0.15.2
)
@ -21,7 +21,7 @@ require (
github.com/klauspost/compress v1.16.5 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/onsi/ginkgo/v2 v2.9.7 // indirect
github.com/onsi/ginkgo/v2 v2.10.0 // indirect
github.com/quic-go/qtls-go1-19 v0.3.2 // indirect
github.com/quic-go/qtls-go1-20 v0.2.2 // indirect
github.com/quic-go/quic-go v0.34.0 // indirect
@ -31,6 +31,7 @@ require (
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tidwall/sjson v1.2.5 // indirect
github.com/x448/float16 v0.8.4 // indirect
github.com/yuin/goldmark v1.5.4 // indirect
go.uber.org/atomic v1.11.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect

10
go.sum
View File

@ -64,14 +64,14 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/onsi/ginkgo/v2 v2.9.7 h1:06xGQy5www2oN160RtEZoTvnP2sPhEfePYmCDc2szss=
github.com/onsi/ginkgo/v2 v2.9.7/go.mod h1:cxrmXWykAwTwhQsJOPfdIDiJ+l2RYq7U8hFU+M/1uw0=
github.com/onsi/ginkgo/v2 v2.10.0 h1:sfUl4qgLdvkChZrWCYndY2EAu9BRIw1YphNAzy1VNWs=
github.com/onsi/ginkgo/v2 v2.10.0/go.mod h1:UDQOh5wbQUlMnkLfVaIUMtQ1Vus92oM+P2JX1aulgcE=
github.com/onsi/gomega v1.27.7 h1:fVih9JD6ogIiHUN6ePK7HJidyEDpWGVB5mzM7cWNXoU=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@ -107,6 +107,8 @@ github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLY
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.5.4 h1:2uY/xC0roWy8IBEGLgB1ywIoEJFGmRrX21YQcvGZzjU=
github.com/yuin/goldmark v1.5.4/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=