Compare commits
4 Commits
ca33731826
...
83b9dd8f21
Author | SHA1 | Date |
---|---|---|
vilmibm | 83b9dd8f21 | |
vilmibm | 5082290d15 | |
vilmibm | 65c9f89f75 | |
vilmibm | 051326330a |
10
Makefile
10
Makefile
|
@ -1,6 +1,7 @@
|
||||||
all: cmds external
|
all: cmds external
|
||||||
|
|
||||||
install: all
|
install: all
|
||||||
|
cp bin/tma /town/bin/
|
||||||
cp bin/launcher /usr/local/bin/town
|
cp bin/launcher /usr/local/bin/town
|
||||||
cp bin/stats /town/bin/
|
cp bin/stats /town/bin/
|
||||||
cp bin/contrib /town/bin/
|
cp bin/contrib /town/bin/
|
||||||
|
@ -12,6 +13,7 @@ install: all
|
||||||
cp bin/help /town/bin/external/
|
cp bin/help /town/bin/external/
|
||||||
cp bin/welcome /town/bin/external/
|
cp bin/welcome /town/bin/external/
|
||||||
cp bin/signup /town/bin/external/
|
cp bin/signup /town/bin/external/
|
||||||
|
cp bin/review /town/bin/
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf bin
|
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
|
bin/registeruser: external/cmd/helpers/registeruser/main.go bin
|
||||||
go build -o bin/registeruser ./external/cmd/helpers/registeruser
|
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
|
bin/launcher: cmd/launcher/main.go bin
|
||||||
go build -o bin/launcher ./cmd/launcher
|
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
|
bin/request: cmd/request/main.go bin
|
||||||
go build -o bin/request ./cmd/request
|
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:
|
bin:
|
||||||
mkdir -p bin
|
mkdir -p bin
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"git.tilde.town/tildetown/town/email"
|
"git.tilde.town/tildetown/town/email"
|
||||||
"git.tilde.town/tildetown/town/invites"
|
"git.tilde.town/tildetown/town/invites"
|
||||||
|
@ -44,7 +45,7 @@ func loadPassword() (string, error) {
|
||||||
return "", errors.New("smtp password file was empty")
|
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 {
|
func sendInviteEmail(invite invites.Invite) error {
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,13 +4,15 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"net"
|
||||||
|
"net/mail"
|
||||||
"net/smtp"
|
"net/smtp"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
from = "root@tilde.town"
|
SMTPHost = "smtp.migadu.com"
|
||||||
SMTPHost = "smtp.zoho.com"
|
|
||||||
SMTPPort = 465
|
SMTPPort = 465
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -38,61 +40,79 @@ func NewExternalMailer(pw string) *ExternalMailer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *ExternalMailer) Send(address, subject, body string) error {
|
func (m *ExternalMailer) Send(address, subj, body string) error {
|
||||||
headers := map[string]string{
|
from := mail.Address{"Tilde Town Admins", "root@tilde.town"}
|
||||||
"From": from,
|
to := mail.Address{"", address}
|
||||||
"To": address,
|
|
||||||
"Subject": subject,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Setup headers
|
||||||
|
headers := make(map[string]string)
|
||||||
|
headers["From"] = from.String()
|
||||||
|
headers["To"] = to.String()
|
||||||
|
headers["Subject"] = subj
|
||||||
|
|
||||||
|
// Setup message
|
||||||
message := ""
|
message := ""
|
||||||
for k, v := range headers {
|
for k, v := range headers {
|
||||||
message += fmt.Sprintf("%s: %s\r\n", k, v)
|
message += fmt.Sprintf("%s: %s\r\n", k, v)
|
||||||
}
|
}
|
||||||
message += "\r\n" + body
|
message += "\r\n" + body
|
||||||
|
|
||||||
auth := smtp.PlainAuth("", from, m.Password, SMTPHost)
|
// Connect to the SMTP Server
|
||||||
|
servername := fmt.Sprintf("%s:%d", SMTPHost, SMTPPort)
|
||||||
|
|
||||||
server := fmt.Sprintf("%s:%d", SMTPHost, SMTPPort)
|
host, _, _ := net.SplitHostPort(servername)
|
||||||
|
|
||||||
tlsconf := &tls.Config{
|
auth := smtp.PlainAuth("", "root@tilde.town", m.Password, host)
|
||||||
|
|
||||||
|
// TLS config
|
||||||
|
tlsconfig := &tls.Config{
|
||||||
InsecureSkipVerify: true,
|
InsecureSkipVerify: true,
|
||||||
ServerName: server,
|
ServerName: host,
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err := tls.Dial("tcp", server, tlsconf)
|
// Here is the key, you need to call tls.Dial instead of smtp.Dial
|
||||||
|
// for smtp servers running on 465 that require an ssl connection
|
||||||
|
// from the very beginning (no starttls)
|
||||||
|
conn, err := tls.Dial("tcp", servername, tlsconfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("failed dial: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
c, err := smtp.NewClient(conn, SMTPHost)
|
c, err := smtp.NewClient(conn, host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("failed to make smtp client: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Auth
|
||||||
if err = c.Auth(auth); err != nil {
|
if err = c.Auth(auth); err != nil {
|
||||||
return fmt.Errorf("auth failed for smtp: %w", err)
|
return fmt.Errorf("failed to make auth: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = c.Mail(from); err != nil {
|
// To && From
|
||||||
return err
|
if err = c.Mail(from.Address); err != nil {
|
||||||
|
return fmt.Errorf("failed to create mail: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = c.Rcpt(address); err != nil {
|
if err = c.Rcpt(to.Address); err != nil {
|
||||||
return err
|
return fmt.Errorf("failed to add rcpt: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Data
|
||||||
w, err := c.Data()
|
w, err := c.Data()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("failed to send data: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = w.Write([]byte(message))
|
_, err = w.Write([]byte(message))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("failed to write: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = w.Close()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to close: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Close()
|
|
||||||
c.Quit()
|
c.Quit()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"git.tilde.town/tildetown/town/email"
|
"git.tilde.town/tildetown/town/email"
|
||||||
)
|
)
|
||||||
|
@ -41,7 +42,7 @@ func loadPassword() (string, error) {
|
||||||
return "", errors.New("smtp password file was empty")
|
return "", errors.New("smtp password file was empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
return string(pw[0:n]), nil
|
return strings.TrimSpace(string(pw[0:n])), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendAuthCodeEmail(ac AuthCode) error {
|
func sendAuthCodeEmail(ac AuthCode) error {
|
||||||
|
|
Loading…
Reference in New Issue