From 83b9dd8f214c8e5e13e5c87a8fb65b3762a1fc9b Mon Sep 17 00:00:00 2001 From: vilmibm Date: Fri, 30 Aug 2024 02:06:24 +0000 Subject: [PATCH] stuff. WIP tma tool --- Makefile | 10 ++++- cmd/review/email.go | 3 +- cmd/tma/main.go | 101 ++++++++++++++++++++++++++++++++++++++++++++ scripts/tma | 67 ----------------------------- 4 files changed, 112 insertions(+), 69 deletions(-) create mode 100644 cmd/tma/main.go delete mode 100755 scripts/tma diff --git a/Makefile b/Makefile index 2b052a6..6f311cb 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ all: cmds external install: all + cp bin/tma /town/bin/ cp bin/launcher /usr/local/bin/town cp bin/stats /town/bin/ cp bin/contrib /town/bin/ @@ -12,6 +13,7 @@ install: all cp bin/help /town/bin/external/ cp bin/welcome /town/bin/external/ cp bin/signup /town/bin/external/ + cp bin/review /town/bin/ clean: rm -rf bin @@ -41,7 +43,7 @@ bin/emailtouser: external/cmd/helpers/emailtouser/main.go bin bin/registeruser: external/cmd/helpers/registeruser/main.go bin go build -o bin/registeruser ./external/cmd/helpers/registeruser -cmds: bin/launcher bin/stats bin/contrib bin/request +cmds: bin/launcher bin/stats bin/contrib bin/request bin/review bin/tma bin/launcher: cmd/launcher/main.go bin go build -o bin/launcher ./cmd/launcher @@ -55,5 +57,11 @@ bin/contrib: cmd/contrib/main.go bin bin/request: cmd/request/main.go bin go build -o bin/request ./cmd/request +bin/review: cmd/review/main.go bin + go build -o bin/review ./cmd/review + +bin/tma: cmd/tma/main.go bin + go build -o bin/tma ./cmd/tma + bin: mkdir -p bin diff --git a/cmd/review/email.go b/cmd/review/email.go index 3afc1ec..7258474 100644 --- a/cmd/review/email.go +++ b/cmd/review/email.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "os" + "strings" "git.tilde.town/tildetown/town/email" "git.tilde.town/tildetown/town/invites" @@ -44,7 +45,7 @@ func loadPassword() (string, error) { return "", errors.New("smtp password file was empty") } - return string(pw[0:n]), nil + return strings.TrimSpace(string(pw[0:n])), nil } func sendInviteEmail(invite invites.Invite) error { diff --git a/cmd/tma/main.go b/cmd/tma/main.go new file mode 100644 index 0000000..12b29ea --- /dev/null +++ b/cmd/tma/main.go @@ -0,0 +1,101 @@ +package main + +import ( + "errors" + "fmt" + "os" + "strconv" + "time" + + "database/sql" + + "git.tilde.town/tildetown/town/towndb" + + _ "github.com/mattn/go-sqlite3" +) + +type dbs struct { + Invites *sql.DB + Signups *sql.DB + Users *sql.DB +} + +func connect() (*dbs, error) { + users, err := sql.Open("sqlite3", "/town/var/town.db?mode=r") + if err != nil { + return nil, err + } + + signups, err := sql.Open("sqlite3", "/town/var/signups/signups.db?mode=r") + if err != nil { + return nil, err + } + + invites, err := sql.Open("sqlite3", "/town/var/invites/invites.db?mode=r") + if err != nil { + return nil, err + } + + return &dbs{ + Invites: invites, + Signups: signups, + Users: users, + }, nil +} + +func _main(argv []string) error { + if len(argv) < 3 { + return errors.New("want two args (user|email) ") + } + + switch argv[1] { + case "user": + return userLookup(argv[2]) + case "email": + return emailLookup(argv[2]) + default: + return fmt.Errorf("idk %s", argv[2]) + } +} + +func userLookup(username string) error { + dbs, err := connect() + if err != nil { + return err + } + + stmt, err := dbs.Users.Prepare("SELECT id,created,username,state,admin FROM users WHERE username = ?") + if err != nil { + return fmt.Errorf("user select prepare failed: %w", err) + } + var created string + tu := towndb.TownUser{} + err = stmt.QueryRow(username).Scan(&tu.ID, &created, &tu.Username, &tu.State, &tu.IsAdmin) + if err != nil { + return fmt.Errorf("failed to select user: %w", err) + } + i, err := strconv.ParseInt(created, 10, 64) + if err != nil { + return fmt.Errorf("time what? %w", err) + } + tu.Created = time.Unix(i, 0) + + fmt.Println("id,created,username,state,admin") + fmt.Printf("%d,%s,%s,%s,%v\n", tu.ID, tu.Created, tu.Username, tu.State, tu.IsAdmin) + + // TODO rest + + return nil +} + +func emailLookup(address string) error { + // TODO + return nil +} + +func main() { + if err := _main(os.Args); err != nil { + fmt.Fprintf(os.Stderr, "error: %s\n", err.Error()) + os.Exit(1) + } +} diff --git a/scripts/tma b/scripts/tma deleted file mode 100755 index bdcd236..0000000 --- a/scripts/tma +++ /dev/null @@ -1,67 +0,0 @@ -#!/bin/bash - -set -e - -signups="/town/var/signups/signups.db" -invites="/town/var/invites/invites.db" -users="/town/var/town.db" -bold=$(tput bold) -plain=$(tput sgr0) - -which="${1}" - -header() { - echo $bold - echo $1 - echo $plain -} - -case $which in - user) - username="${2}" - if [ "$username" == "" ]; then - echo "gimme a username" - exit - fi - - header "i looked in $users and found:" - - sqlite3 $users "select * from users where username='$username'" - - header "their emails are:" - - sqlite3 $users "select * from emails where userid in (select id from users where username='$username')" - - header "i looked in $invites and found:" - - sqlite3 $users "select address from emails where userid in (select id from users where username='$username')" | xargs printf "select * from invites where email='%s'" | sqlite3 $invites - - header "i looked in $signups and found:" - - sqlite3 $users "select address from emails where userid in (select id from users where username='$username')" | xargs printf "select * from signups where email like '%%%s%%'" | sqlite3 $signups - ;; - email) - email="${2}" - if [ "$email" == "" ]; then - echo "gimme an email" - exit - fi - - header "i looked in $signups and found:" - - sqlite3 $signups "select * from signups where email like '%$email%'"; - - header "i looked in $invites and found:" - - sqlite3 $invites "select * from invites where email='$email'" - - header "i looked in $users and found:" - - sqlite3 $users "select * from users where id in (select userid from emails where address='$email')" - ;; - *) - echo "what do you want to know about?" - echo "i can tell you about: user, email" - exit 1 - ;; -esac