2024-03-10 17:21:27 -05:00
|
|
|
// package main is the main package for the LapisDeploy program
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-03-15 18:27:02 -05:00
|
|
|
"fmt"
|
2024-03-10 17:21:27 -05:00
|
|
|
"log"
|
2024-03-10 22:05:15 -05:00
|
|
|
"strings"
|
2024-03-15 18:27:02 -05:00
|
|
|
"time"
|
2024-03-10 17:21:27 -05:00
|
|
|
|
|
|
|
"maunium.net/go/mautrix"
|
|
|
|
"maunium.net/go/mautrix/event"
|
2024-03-10 21:22:05 -05:00
|
|
|
"maunium.net/go/mautrix/format"
|
2024-03-15 18:27:02 -05:00
|
|
|
"maunium.net/go/mautrix/id"
|
2024-03-10 17:21:27 -05:00
|
|
|
)
|
|
|
|
|
2024-03-10 22:05:15 -05:00
|
|
|
// MatrixMessage stores information about a matrix message.
|
2024-03-10 21:22:05 -05:00
|
|
|
type MatrixMessage struct {
|
|
|
|
text string
|
|
|
|
}
|
|
|
|
|
2024-03-15 18:27:02 -05:00
|
|
|
// MatrixCommandCallback is a function that gets called when the bot is issued a command over Matrix
|
|
|
|
type MatrixCommandCallback func(ctx context.Context, evt *event.Event, args []string) string
|
|
|
|
|
2024-03-10 22:05:15 -05:00
|
|
|
// MatrixCommand stores information about a matrix chat command
|
|
|
|
type MatrixCommand struct {
|
2024-03-15 18:27:02 -05:00
|
|
|
cmd string
|
|
|
|
callback MatrixCommandCallback
|
2024-03-10 22:05:15 -05:00
|
|
|
description string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output queue
|
2024-03-10 21:22:05 -05:00
|
|
|
var MatrixOut []MatrixMessage
|
|
|
|
|
2024-03-10 22:05:15 -05:00
|
|
|
// ChatCommands
|
|
|
|
var Chatcommands []MatrixCommand
|
|
|
|
|
2024-03-10 21:22:05 -05:00
|
|
|
// sendMessage sends a message through Matrix
|
|
|
|
func sendMessage(msg MatrixMessage) {
|
|
|
|
MatrixOut = append(MatrixOut, msg)
|
|
|
|
}
|
|
|
|
|
2024-03-10 22:05:15 -05:00
|
|
|
// registerChatCommand
|
2024-03-15 18:27:02 -05:00
|
|
|
func registerChatCommand(cmd string, description string, callback MatrixCommandCallback) {
|
2024-03-10 22:05:15 -05:00
|
|
|
Chatcommands = append(Chatcommands, MatrixCommand{
|
2024-03-15 18:27:02 -05:00
|
|
|
cmd: cmd,
|
|
|
|
callback: callback,
|
2024-03-10 22:05:15 -05:00
|
|
|
description: description,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-03-10 21:22:05 -05:00
|
|
|
// initMatrix starts a Matrix bot.
|
2024-03-10 17:21:27 -05:00
|
|
|
func initMatrix() {
|
|
|
|
client, err := mautrix.NewClient(configuration.matrix.homeserver, "", "")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
resp, err := client.Login(context.Background(), &mautrix.ReqLogin{
|
2024-03-15 18:27:02 -05:00
|
|
|
Type: "m.login.password",
|
|
|
|
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: configuration.matrix.username},
|
|
|
|
Password: configuration.matrix.password,
|
2024-03-10 17:21:27 -05:00
|
|
|
StoreCredentials: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
log.Printf("[Matrix] Logged in as %s", resp.UserID)
|
|
|
|
|
|
|
|
syncer := client.Syncer.(*mautrix.DefaultSyncer)
|
2024-03-11 17:43:24 -05:00
|
|
|
client.Syncer.(mautrix.ExtensibleSyncer).OnSync(client.DontProcessOldEvents)
|
2024-03-10 17:21:27 -05:00
|
|
|
syncer.OnEventType(event.StateMember, func(ctx context.Context, evt *event.Event) {
|
|
|
|
if evt.GetStateKey() == client.UserID.String() && evt.Content.AsMember().Membership == event.MembershipInvite {
|
|
|
|
if evt.RoomID.String() == configuration.matrix.room_id {
|
|
|
|
_, err := client.JoinRoomByID(ctx, evt.RoomID)
|
|
|
|
if err == nil {
|
|
|
|
log.Printf("[Matrix] Joined room '%s' (Invited by '%s')", evt.RoomID.String(), evt.Sender.String())
|
|
|
|
} else {
|
|
|
|
log.Printf("[Matrix] Failed to join room '%s' after invite (Invited by '%s')", evt.RoomID.String(), evt.Sender.String())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Printf("[Matrix] Rejected invite to room '%s' (Invited by '%s')", evt.RoomID.String(), evt.Sender.String())
|
|
|
|
}
|
|
|
|
}
|
2024-03-15 18:27:02 -05:00
|
|
|
})
|
2024-03-10 22:05:15 -05:00
|
|
|
syncer.OnEventType(event.EventMessage, func(ctx context.Context, evt *event.Event) {
|
|
|
|
if evt.RoomID.String() == configuration.matrix.room_id {
|
2024-03-15 18:27:02 -05:00
|
|
|
prefixes := []string{"!", configuration.matrix.username + " "}
|
|
|
|
for _, prefix := range prefixes {
|
2024-03-10 22:40:45 -05:00
|
|
|
if strings.HasPrefix(evt.Content.AsMessage().Body, prefix) {
|
2024-03-15 18:27:02 -05:00
|
|
|
msg := evt.Content.AsMessage().Body
|
|
|
|
cmd := strings.TrimPrefix(strings.Split(msg, " ")[0], prefix)
|
2024-03-10 22:40:45 -05:00
|
|
|
found_command := false
|
|
|
|
for _, command := range Chatcommands {
|
2024-03-15 18:27:02 -05:00
|
|
|
if command.cmd == cmd {
|
2024-03-10 22:40:45 -05:00
|
|
|
found_command = true
|
2024-03-15 18:27:02 -05:00
|
|
|
out := command.callback(ctx, evt, strings.Split(strings.TrimPrefix(msg, prefix+cmd), " "))
|
2024-03-10 22:40:45 -05:00
|
|
|
content := format.RenderMarkdown(out, true, false)
|
|
|
|
content.SetReply(evt)
|
|
|
|
client.SendMessageEvent(ctx, evt.RoomID, event.EventMessage, content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found_command {
|
|
|
|
client.SendText(ctx, evt.RoomID, "🔴 Command not found")
|
|
|
|
}
|
|
|
|
break
|
2024-03-10 22:05:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2024-03-15 18:27:02 -05:00
|
|
|
registerChatCommand("help", "Show the help dialouge", func(ctx context.Context, evt *event.Event, args []string) string {
|
2024-03-10 22:05:15 -05:00
|
|
|
text := ""
|
2024-03-15 18:27:02 -05:00
|
|
|
for _, cmd := range Chatcommands {
|
2024-03-10 22:05:15 -05:00
|
|
|
text = fmt.Sprintf("%s- %s\n %s\n", text, cmd.cmd, cmd.description)
|
|
|
|
}
|
|
|
|
return text
|
|
|
|
})
|
2024-03-10 21:22:05 -05:00
|
|
|
|
2024-03-15 18:27:02 -05:00
|
|
|
registerChatCommand("sites", "Display all available sites", func(ctx context.Context, evt *event.Event, args []string) string {
|
2024-03-10 22:40:45 -05:00
|
|
|
text := "⚪️ Getting sites...\n"
|
|
|
|
sites, err := getAllSites()
|
|
|
|
if err.code != 0 {
|
2024-03-15 18:27:02 -05:00
|
|
|
return text + "🔴 Failed to get sites!"
|
2024-03-10 22:40:45 -05:00
|
|
|
}
|
|
|
|
for _, site := range sites {
|
2024-03-11 17:19:10 -05:00
|
|
|
text = fmt.Sprintf("%s- %s\n", text, site.name)
|
|
|
|
}
|
|
|
|
if len(sites) == 0 {
|
2024-03-15 18:27:02 -05:00
|
|
|
text = text + "*No sites found*"
|
2024-03-10 22:40:45 -05:00
|
|
|
}
|
|
|
|
return text
|
|
|
|
})
|
|
|
|
|
2024-03-15 19:19:19 -05:00
|
|
|
registerChatCommand("restart", "Restart a site.", func(ctx context.Context, evt *event.Event, args []string) string {
|
|
|
|
if len(args) < 2 {
|
|
|
|
return "🔴 Invalid site."
|
|
|
|
}
|
|
|
|
site, found, err := getSite(args[1])
|
|
|
|
if err != nil {
|
|
|
|
return "🔴 Failed to get site " + args[2] + "!"
|
|
|
|
} else if !found {
|
|
|
|
return "🔴 Invalid site."
|
|
|
|
}
|
|
|
|
text := "⚪️ Restarting server..."
|
|
|
|
derr := site.Restart()
|
|
|
|
if derr.code != 0 {
|
|
|
|
derr.SendOverMatrix()
|
|
|
|
return text + "\n🔴 Failed to restart site!"
|
|
|
|
}
|
|
|
|
return text + "\n🟢 Successfully restarted site."
|
|
|
|
})
|
|
|
|
|
2024-03-15 18:27:02 -05:00
|
|
|
go func() {
|
2024-03-10 22:52:07 -05:00
|
|
|
for range time.Tick(time.Second * 6) {
|
2024-03-10 21:22:05 -05:00
|
|
|
log.Printf("Scanning for queued messages...")
|
|
|
|
for _, msg := range MatrixOut {
|
|
|
|
_, err := client.SendMessageEvent(context.Background(), id.RoomID(configuration.matrix.room_id), event.EventMessage, format.RenderMarkdown(msg.text, true, false))
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[Matrix] ERROR : %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
log.Print("[Matrix] Sent queued message successfully.")
|
|
|
|
}
|
|
|
|
MatrixOut = []MatrixMessage{}
|
|
|
|
}
|
|
|
|
}()
|
2024-03-10 17:21:27 -05:00
|
|
|
|
2024-03-11 17:43:24 -05:00
|
|
|
go func() {
|
|
|
|
log.Print("[Matrix] Running!")
|
|
|
|
err = client.Sync()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2024-03-10 17:21:27 -05:00
|
|
|
}
|