diff options
Diffstat (limited to 'web.go')
-rw-r--r-- | web.go | 139 |
1 files changed, 128 insertions, 11 deletions
@@ -5,12 +5,14 @@ import ( "html/template" "log" "net/http" + "strconv" "strings" "github.com/gorilla/mux" "github.com/gorilla/sessions" "golang.org/x/crypto/argon2" + "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector" "git.deuxfleurs.fr/Deuxfleurs/easybridge/mxlib" ) @@ -30,6 +32,9 @@ func StartWeb() { r := mux.NewRouter() r.HandleFunc("/", handleHome) r.HandleFunc("/logout", handleLogout) + r.HandleFunc("/add/{protocol}", handleAdd) + r.HandleFunc("/edit/{account}", handleEdit) + r.HandleFunc("/delete/{account}", handleDelete) staticfiles := http.FileServer(http.Dir("static")) r.Handle("/static/{file:.*}", http.StripPrefix("/static/", staticfiles)) @@ -178,19 +183,131 @@ func handleLogin(w http.ResponseWriter, r *http.Request) *LoginInfo { } } -func LoadDbAccounts(mxid string, key *[32]byte) { - var allAccounts []DbAccountConfig - db.Where(&DbAccountConfig{MxUserID: mxid}).Find(&allAccounts) - for _, acct := range allAccounts { - config, err := decryptAccountConfig(acct.Config, key) - if err != nil { - ezbrSystemSendf("Could not decrypt stored configuration for account %s", acct.Name) - continue +// ---- + +func handleAdd(w http.ResponseWriter, r *http.Request) { + login := checkLogin(w, r) + if login == nil { + return + } + + protocol := mux.Vars(r)["protocol"] + + configForm(w, r, login, "", protocol, map[string]string{}) +} + +func handleEdit(w http.ResponseWriter, r *http.Request) { + login := checkLogin(w, r) + if login == nil { + return + } + + account := mux.Vars(r)["account"] + acct := FindAccount(login.MxId, account) + if acct == nil { + http.Error(w, "No such account", http.StatusNotFound) + return + } + + configForm(w, r, login, account, acct.Protocol, acct.Config) +} + +type ConfigFormData struct { + ErrorMessage string + + Name string + NameEditable bool + InvalidName bool + + Protocol string + + Config map[string]string + Errors map[string]string + Schema connector.ConfigSchema +} + +func configForm(w http.ResponseWriter, r *http.Request, + login *LoginInfo, name string, protocol string, + prevConfig map[string]string) { + templateConfig := template.Must(template.ParseFiles("templates/layout.html", "templates/config.html")) + + data := &ConfigFormData{ + Name: name, + NameEditable: (name == ""), + Protocol: protocol, + Config: map[string]string{}, + Errors: map[string]string{}, + Schema: connector.Protocols[protocol], + } + for k, v := range prevConfig { + data.Config[k] = v + } + for _, sch := range data.Schema { + if _, ok := data.Config[sch.Name]; !ok && sch.Default != "" { + data.Config[sch.Name] = sch.Default } + } - err = SetAccount(mxid, acct.Name, acct.Protocol, config) - if err != nil { - ezbrSystemSendf("Could not setup account %s: %s", acct.Name, err.Error()) + if r.Method == "POST" { + ok := true + r.ParseForm() + + if data.NameEditable { + data.Name = strings.Join(r.Form["name"], "") + if data.Name == "" { + ok = false + data.InvalidName = true + } + } + + for _, schema := range data.Schema { + field := schema.Name + data.Config[field] = strings.Join(r.Form[field], "") + if data.Config[field] == "" { + if schema.Required { + ok = false + data.Errors[field] = "This field is required" + } + } else if schema.FixedValue != "" { + if data.Config[field] != schema.FixedValue { + ok = false + data.Errors[field] = "This field must be equal to " + schema.FixedValue + } + } else if schema.IsBoolean { + if data.Config[field] != "false" && data.Config[field] != "true" { + ok = false + data.Errors[field] = "This field must be 'true' or 'false'" + } + } else if schema.IsNumeric { + _, err := strconv.Atoi(data.Config[field]) + if err != nil { + ok = false + data.Errors[field] = "This field must be a valid number" + } + } + } + + if ok { + var entry DbAccountConfig + db.Where(&DbAccountConfig{ + MxUserID: login.MxId, + Name: data.Name, + }).Assign(&DbAccountConfig{ + Protocol: protocol, + Config: encryptAccountConfig(data.Config, userKeys[login.MxId]), + }).FirstOrCreate(&entry) + + err := SetAccount(login.MxId, data.Name, protocol, data.Config) + if err == nil { + http.Redirect(w, r, "/", http.StatusFound) + return + } + data.ErrorMessage = err.Error() } } + + templateConfig.Execute(w, data) +} + +func handleDelete(w http.ResponseWriter, r *http.Request) { } |