From dbb376d883fadbf34b9263a702d9ac3ab38c5162 Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Sun, 20 Oct 2019 17:02:55 -0400 Subject: [PATCH] move all the messages into the database --- docs/honk.8 | 6 +++--- honk.go | 10 +++++----- upgradedb.go | 25 ++++++++++++++++++++++++- util.go | 41 +++++++++++++++++++---------------------- 4 files changed, 51 insertions(+), 31 deletions(-) diff --git a/docs/honk.8 b/docs/honk.8 index 3ea246a..3fce09a 100644 --- a/docs/honk.8 +++ b/docs/honk.8 @@ -83,9 +83,9 @@ file. .Pp Custom messages may be set by editing the database. (Sorry.) -.Dl insert into config values ('servermsg', 'message'); -.Dl insert into config values ('aboutmsg', 'message'); -.Dl insert into config values ('loginmsg', 'message'); +.Dl update config set value = 'message' where key = 'servermsg'; +.Dl update config set value = 'message' where key = 'aboutmsg'; +.Dl update config set value = 'message' where key = 'loginmsg'; .Pp .Ss ADMIN New users can be added with the diff --git a/honk.go b/honk.go index 52de461..cac60a6 100644 --- a/honk.go +++ b/honk.go @@ -137,9 +137,9 @@ type Honker struct { var serverName string var iconName = "icon.png" -var serverMsg = template.HTML(`

Things happen.

`) -var aboutMsg = template.HTML(`

What is honk?

Honk is amazing!`) -var loginMsg = template.HTML(`

login

`) +var serverMsg template.HTML +var aboutMsg template.HTML +var loginMsg template.HTML func ElaborateUnitTests() { } @@ -175,9 +175,9 @@ func main() { } switch os.Args[2] { case "on": - saveconfig("debug", 1) + updateconfig("debug", 1) case "off": - saveconfig("debug", 0) + updateconfig("debug", 0) default: log.Fatal("argument must be on or off") } diff --git a/upgradedb.go b/upgradedb.go index 3aa45b4..d419992 100644 --- a/upgradedb.go +++ b/upgradedb.go @@ -24,7 +24,7 @@ import ( "time" ) -var myVersion = 26 +var myVersion = 27 func doordie(db *sql.DB, s string, args ...interface{}) { _, err := db.Exec(s, args...) @@ -280,6 +280,29 @@ func upgradedb() { doordie(db, "update config set value = 26 where key = 'dbversion'") fallthrough case 26: + s := "" + getconfig("servermsg", &s) + if s == "" { + setconfig("servermsg", serverMsg) + } + s = "" + getconfig("aboutmsg", &s) + if s == "" { + setconfig("aboutmsg", aboutMsg) + } + s = "" + getconfig("loginmsg", &s) + if s == "" { + setconfig("loginmsg", loginMsg) + } + d := -1 + getconfig("debug", &d) + if d == -1 { + setconfig("debug", 0) + } + doordie(db, "update config set value = 27 where key = 'dbversion'") + fallthrough + case 27: default: log.Fatalf("can't upgrade unknown version %d", dbversion) diff --git a/util.go b/util.go index 3ecc8de..3b304a2 100644 --- a/util.go +++ b/util.go @@ -88,6 +88,7 @@ func initdb() { if err != nil { log.Fatal(err) } + alreadyopendb = db defer func() { os.Remove(dbname) os.Exit(1) @@ -129,11 +130,7 @@ func initdb() { log.Print("that's way too short") return } - _, err = db.Exec("insert into config (key, value) values (?, ?)", "listenaddr", addr) - if err != nil { - log.Print(err) - return - } + setconfig("listenaddr", addr) fmt.Printf("server name: ") addr, err = r.ReadString('\n') if err != nil { @@ -145,24 +142,17 @@ func initdb() { log.Print("that's way too short") return } - _, err = db.Exec("insert into config (key, value) values (?, ?)", "servername", addr) - if err != nil { - log.Print(err) - return - } + setconfig("servername", addr) var randbytes [16]byte rand.Read(randbytes[:]) key := fmt.Sprintf("%x", randbytes) - _, err = db.Exec("insert into config (key, value) values (?, ?)", "csrfkey", key) - if err != nil { - log.Print(err) - return - } - _, err = db.Exec("insert into config (key, value) values (?, ?)", "dbversion", myVersion) - if err != nil { - log.Print(err) - return - } + setconfig("csrfkey", key) + setconfig("dbversion", myVersion) + + setconfig("servermsg", "

Things happen.

") + setconfig("aboutmsg", "

What is honk?

\n

Honk is amazing!") + setconfig("loginmsg", "

login

") + setconfig("debug", 0) initblobdb() @@ -374,9 +364,16 @@ func getconfig(key string, value interface{}) error { return err } -func saveconfig(key string, val interface{}) { +func setconfig(key string, val interface{}) error { db := opendatabase() - db.Exec("update config set value = ? where key = ?", val, key) + _, err := db.Exec("insert into config (key, value) values (?, ?)", key, val) + return err +} + +func updateconfig(key string, val interface{}) error { + db := opendatabase() + _, err := db.Exec("update config set value = ? where key = ?", val, key) + return err } func openListener() (net.Listener, error) {