1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-09-16 07:56:24 -07:00

Add two command line options, possibly more to come

This commit is contained in:
Joshua Merrell 2018-10-09 21:55:35 -07:00
parent dc3b48e3aa
commit 2ae43dd030
3 changed files with 64 additions and 12 deletions

View file

@ -1,7 +1,10 @@
package main
import (
"flag"
"fmt"
"os"
"strings"
)
var version string
@ -14,9 +17,10 @@ func main() {
}
}()
initConfig()
printf(0, "Starting gochan v%s.%s, using verbosity level %d\n", config.Version, buildtimeString, config.Verbosity)
println(0, "Config file loaded. Connecting to database...")
connectToSQLServer()
parseCommandLine()
printf(0, "Starting gochan v%s.%s, using verbosity level %d\n", config.Version, buildtimeString, config.Verbosity)
println(0, "Loading and parsing templates...")
if err := initTemplates(); err != nil {
handleError(0, customError(err))
@ -30,3 +34,47 @@ func main() {
}
initServer()
}
func parseCommandLine() {
var newstaff string
var delstaff string
var rank int
var err error
flag.StringVar(&newstaff, "newstaff", "", "<newusername>:<newpassword>")
flag.StringVar(&delstaff, "delstaff", "", "<username>")
flag.IntVar(&rank, "rank", 0, "New staff member rank, to be used with -newstaff or -delstaff")
flag.Parse()
if newstaff != "" {
arr := strings.Split(newstaff, ":")
if len(arr) < 2 || delstaff != "" {
flag.Usage()
os.Exit(2)
}
printf(0, "Creating new staff: '%s', with password: '%s' and rank: %d\n", arr[0], arr[1], rank)
if err = newStaff(arr[0], arr[1], rank); err != nil {
handleError(0, "%s\n", err.Error())
os.Exit(2)
}
os.Exit(0)
}
if delstaff != "" {
if newstaff != "" {
flag.Usage()
os.Exit(2)
}
printf(0, "Are you sure you want to delete the staff account '%s'?[y/N]: ", delstaff)
var answer string
fmt.Scanln(&answer)
answer = strings.ToLower(answer)
if answer == "y" || answer == "yes" {
if err = deleteStaff(delstaff); err != nil {
printf(0, "Error deleting '%s': %s\n", delstaff, err.Error())
os.Exit(2)
}
} else {
println(0, "Not deleting.")
}
os.Exit(0)
}
}

View file

@ -118,6 +118,17 @@ func getStaffRank(request *http.Request) int {
return staff.Rank
}
func newStaff(username string, password string, rank int) error {
_, err := execSQL("INSERT INTO `"+config.DBprefix+"staff` (`username`, `password_checksum`, `rank`) VALUES(?,?,?)",
&username, bcryptSum(password), &rank)
return err
}
func deleteStaff(username string) error {
_, err := execSQL("DELETE FROM `"+config.DBprefix+"staff` WHERE `username` = ?", username)
return err
}
func createSession(key string, username string, password string, request *http.Request, writer http.ResponseWriter) int {
//returns 0 for successful, 1 for password mismatch, and 2 for other
domain := request.Host
@ -1015,16 +1026,12 @@ var manage_functions = map[string]ManageFunction{
if do == "add" {
newUsername := request.FormValue("username")
newPassword := request.FormValue("password")
newRank := request.FormValue("rank")
if _, err := execSQL("INSERT INTO `"+config.DBprefix+"staff` (`username`, `password_checksum`, `rank`) VALUES(?,?,?)",
&newUsername, bcryptSum(newPassword), &newRank,
); err != nil {
newRank, _ := strconv.Atoi(request.FormValue("rank"))
if err := newStaff(newUsername, newPassword, newRank); err != nil {
serveErrorPage(writer, handleError(1, err.Error()))
}
} else if do == "del" && request.FormValue("username") != "" {
if _, err = execSQL("DELETE FROM `"+config.DBprefix+"staff` WHERE `username` = ?",
request.FormValue("username"),
); err != nil {
if err = deleteStaff(request.FormValue("username")); err != nil {
serveErrorPage(writer, handleError(1, err.Error()))
}
}

View file

@ -44,8 +44,6 @@ func connectToSQLServer() {
os.Exit(2)
}
printf(0, "Starting initial setup...")
initialSQLStr := string(initialSQLBytes)
initialSQLStr = strings.NewReplacer("DBNAME", config.DBname, "DBPREFIX", config.DBprefix).Replace(initialSQLStr)
initialSQLArr := strings.Split(initialSQLStr, ";")
@ -88,7 +86,6 @@ func connectToSQLServer() {
}
}
checkDeprecatedSchema()
println(0, "complete.")
}
/*