2024-04-10 15:59:41 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"maunium.net/go/mautrix/event"
|
|
|
|
)
|
|
|
|
|
|
|
|
var Chatcommands = []MatrixCommand{}
|
|
|
|
|
|
|
|
func registerChatCommands() {
|
|
|
|
Chatcommands = []MatrixCommand{
|
|
|
|
{
|
|
|
|
cmd: "help",
|
|
|
|
description: "Show the help dialouge",
|
|
|
|
callback: func(ctx context.Context, evt *event.Event, args []string) string {
|
|
|
|
text := ""
|
|
|
|
for _, cmd := range Chatcommands {
|
|
|
|
text = fmt.Sprintf("%s- %s\n %s\n", text, cmd.cmd, cmd.description)
|
|
|
|
}
|
|
|
|
return text
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cmd: "sites",
|
|
|
|
description: "Display all available sites",
|
|
|
|
callback: func(ctx context.Context, evt *event.Event, args []string) string {
|
2024-04-12 11:19:43 -05:00
|
|
|
text := Circles["white"] + "Getting sites...\n"
|
2024-04-10 15:59:41 -05:00
|
|
|
sites, err := getAllSites()
|
|
|
|
if err.code != 0 {
|
2024-04-12 11:19:43 -05:00
|
|
|
return text + Circles["red"] + "Failed to get sites!"
|
2024-04-10 15:59:41 -05:00
|
|
|
}
|
|
|
|
for _, site := range sites {
|
|
|
|
text = fmt.Sprintf("%s- %s\n", text, site.getName())
|
|
|
|
}
|
|
|
|
if len(sites) == 0 {
|
|
|
|
text = text + "*No sites found*"
|
|
|
|
}
|
|
|
|
return text
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cmd: "restart",
|
|
|
|
description: "Restart a site.",
|
|
|
|
callback: func(ctx context.Context, evt *event.Event, args []string) string {
|
|
|
|
if len(args) < 2 {
|
2024-04-12 11:19:43 -05:00
|
|
|
return Circles["red"] + "Invalid site."
|
2024-04-10 15:59:41 -05:00
|
|
|
}
|
|
|
|
site, found, err := getSite(args[1])
|
|
|
|
if err != nil {
|
2024-04-12 11:19:43 -05:00
|
|
|
return Circles["red"] + "Failed to get site " + args[2] + "!"
|
2024-04-10 15:59:41 -05:00
|
|
|
} else if !found {
|
2024-04-12 11:19:43 -05:00
|
|
|
return Circles["red"] + "Invalid site."
|
2024-04-10 15:59:41 -05:00
|
|
|
}
|
2024-04-12 11:19:43 -05:00
|
|
|
text := Circles["white"] + "Restarting server..."
|
2024-04-10 15:59:41 -05:00
|
|
|
derr := site.Restart()
|
|
|
|
if derr.code != 0 {
|
|
|
|
derr.SendOverMatrix()
|
2024-04-12 11:19:43 -05:00
|
|
|
return text + "\n" + Circles["red"] + "Failed to restart site!"
|
2024-04-10 15:59:41 -05:00
|
|
|
}
|
2024-04-12 11:19:43 -05:00
|
|
|
return text + "\n" + Circles["green"] + "Successfully restarted site."
|
2024-04-10 15:59:41 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cmd: "start",
|
|
|
|
description: "Start a site.",
|
|
|
|
callback: func(ctx context.Context, evt *event.Event, args []string) string {
|
|
|
|
if len(args) < 2 {
|
2024-04-12 11:19:43 -05:00
|
|
|
return Circles["red"] + "Invalid site."
|
2024-04-10 15:59:41 -05:00
|
|
|
}
|
|
|
|
site, found, err := getSite(args[1])
|
|
|
|
if err != nil {
|
2024-04-12 11:19:43 -05:00
|
|
|
return Circles["red"] + "Failed to get site " + args[2] + "!"
|
2024-04-10 15:59:41 -05:00
|
|
|
} else if !found {
|
2024-04-12 11:19:43 -05:00
|
|
|
return Circles["red"] + "Invalid site."
|
2024-04-10 15:59:41 -05:00
|
|
|
}
|
|
|
|
|
2024-04-12 11:19:43 -05:00
|
|
|
text := Circles["white"] + "Starting server..."
|
2024-04-10 15:59:41 -05:00
|
|
|
if derr := site.Start(); derr.code != 0 {
|
|
|
|
derr.SendOverMatrix()
|
2024-04-12 11:19:43 -05:00
|
|
|
return text + "\n" + Circles["red"] + "Failed to stop site!"
|
2024-04-10 15:59:41 -05:00
|
|
|
}
|
2024-04-12 11:19:43 -05:00
|
|
|
return text + "\n" + Circles["green"] + "Successfully started site."
|
2024-04-10 15:59:41 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cmd: "stop",
|
|
|
|
description: "Stop a site.",
|
|
|
|
callback: func(ctx context.Context, evt *event.Event, args []string) string {
|
|
|
|
if len(args) < 2 {
|
2024-04-12 11:19:43 -05:00
|
|
|
return Circles["red"] + "Invalid site."
|
2024-04-10 15:59:41 -05:00
|
|
|
}
|
|
|
|
site, found, err := getSite(args[1])
|
|
|
|
if err != nil {
|
2024-04-12 11:19:43 -05:00
|
|
|
return Circles["red"] + "Failed to get site " + args[2] + "!"
|
2024-04-10 15:59:41 -05:00
|
|
|
} else if !found {
|
2024-04-12 11:19:43 -05:00
|
|
|
return Circles["red"] + "Invalid site."
|
2024-04-10 15:59:41 -05:00
|
|
|
}
|
2024-04-12 11:19:43 -05:00
|
|
|
text := Circles["white"] + "Stopping server..."
|
2024-04-10 15:59:41 -05:00
|
|
|
if derr := site.Stop(); derr.code != 0 {
|
|
|
|
derr.SendOverMatrix()
|
2024-04-12 11:19:43 -05:00
|
|
|
return text + "\n" + Circles["red"] + "Failed to stop site!"
|
2024-04-10 15:59:41 -05:00
|
|
|
}
|
2024-04-12 11:19:43 -05:00
|
|
|
return text + "\n" + Circles["green"] + "Successfully stopped site."
|
2024-04-10 15:59:41 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|