stuff. WIP tma tool

trunk
vilmibm 2024-08-30 02:06:24 +00:00
parent 5082290d15
commit 83b9dd8f21
4 changed files with 112 additions and 69 deletions

View File

@ -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

View File

@ -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 {

101
cmd/tma/main.go 100644
View File

@ -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) <lookup>")
}
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)
}
}

View File

@ -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