Add matrix.disable to config file (fix #47)

This commit is contained in:
Noah 2024-04-16 10:21:34 -05:00
parent a4ae6f77c7
commit dea25cd2f9
2 changed files with 40 additions and 15 deletions

View File

@ -17,6 +17,7 @@ type Configuration struct {
username string username string
password string password string
room_id string room_id string
disabled bool
} }
} }
@ -46,6 +47,19 @@ func getIntFromTable(L *lua.LState, lobj lua.LValue, key string, out *int64) (bo
return true, nil return true, nil
} }
// getBoolFromTable retrieves a bool from a table on the Lua stack using the key passed in.
//
// If the value is nil, 'out' is unchanged.
func getBoolFromTable(L *lua.LState, lobj lua.LValue, key string, out *bool) (bool, error) {
lv := L.GetTable(lobj, lua.LString(key))
if boolean, ok := lv.(lua.LBool); ok { // Value is boolean
*out = bool(boolean)
} else if _, ok := lv.(*lua.LNilType); !ok { // Value is not a bool, and is not nil
return true, errors.New("'" + key + "' is not a bool")
}
return true, nil
}
// getTableFromTable retrieves a nested table from the Lua stack using the key passed in. // getTableFromTable retrieves a nested table from the Lua stack using the key passed in.
func getTableFromTable(L *lua.LState, lobj lua.LValue, key string, out *lua.LValue) bool { func getTableFromTable(L *lua.LState, lobj lua.LValue, key string, out *lua.LValue) bool {
lv := L.GetTable(lobj, lua.LString(key)) lv := L.GetTable(lobj, lua.LString(key))
@ -80,9 +94,14 @@ func parseConfig(path string, config *Configuration) {
// Matrix config // Matrix config
var matrix lua.LValue var matrix lua.LValue
getTableFromTable(L, table, "matrix", &matrix) // Get the 'matrix' sub table getTableFromTable(L, table, "matrix", &matrix) // Get the 'matrix' sub table
if matrix.Type().String() != "table" { // Make sure 'config.matrix' is of type 'table' // Make sure 'config.matrix' is of type 'table'
if matrix_type := matrix.Type().String(); matrix_type != "table" && matrix_type != "nil" {
log.Fatalf("config.matrix is of type : '%s', not 'table'", matrix.Type().String()) log.Fatalf("config.matrix is of type : '%s', not 'table'", matrix.Type().String())
} else if matrix_type != "nil" {
if _, err := getBoolFromTable(L, matrix, "disabled", &config.matrix.disabled); err != nil {
panic(err)
} }
if !config.matrix.disabled { // Only load the rest of the matrix config if matrix isn't disabled
if _, err := getStringFromTable(L, matrix, "homeserver", &config.matrix.homeserver); err != nil { if _, err := getStringFromTable(L, matrix, "homeserver", &config.matrix.homeserver); err != nil {
panic(err) panic(err)
} }
@ -95,4 +114,8 @@ func parseConfig(path string, config *Configuration) {
if _, err := getStringFromTable(L, matrix, "room_id", &config.matrix.room_id); err != nil { if _, err := getStringFromTable(L, matrix, "room_id", &config.matrix.room_id); err != nil {
panic(err) panic(err)
} }
}
} else if matrix_type == "nil" { // Assume config.matrix.disabled is true if 'matrix' is missing from the config file
config.matrix.disabled = true
}
} }

View File

@ -105,6 +105,8 @@ func main() {
} }
parseConfig(*config_path, &configuration) // Parse JSON configuration file parseConfig(*config_path, &configuration) // Parse JSON configuration file
// Disable Matrix if it's disabled in either the config or specified on CLI
configuration.matrix.disabled = *disable_matrix || configuration.matrix.disabled
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { // Webhook receiver http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { // Webhook receiver
var data map[string]interface{} var data map[string]interface{}
@ -125,7 +127,7 @@ func main() {
log.Printf("Starting Lapis Deploy on port %d...", configuration.port) log.Printf("Starting Lapis Deploy on port %d...", configuration.port)
startAllSites() // Start all the servers startAllSites() // Start all the servers
if !*disable_matrix { // Only start Matrix bot if --disable-matrix isn't passed if !configuration.matrix.disabled { // Only start Matrix bot if --disable-matrix isn't passed
go initMatrix() // Start Matrix stuff in a goroutine go initMatrix() // Start Matrix stuff in a goroutine
} else { } else {
log.Print("Matrix bot is disabled!") log.Print("Matrix bot is disabled!")