all repos — honk @ 23ab4900a5a1e516690d51654a0ef122e9fa4d48

my fork of honk

honk.go (view raw)

  1//
  2// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
  3//
  4// Permission to use, copy, modify, and distribute this software for any
  5// purpose with or without fee is hereby granted, provided that the above
  6// copyright notice and this permission notice appear in all copies.
  7//
  8// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 10// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 11// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 12// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 13// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 14// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 15
 16package main
 17
 18import (
 19	"fmt"
 20	"html/template"
 21	"log"
 22	"os"
 23	"time"
 24)
 25
 26type WhatAbout struct {
 27	ID        int64
 28	Name      string
 29	Display   string
 30	About     string
 31	Key       string
 32	URL       string
 33	SkinnyCSS bool
 34}
 35
 36type Honk struct {
 37	ID       int64
 38	UserID   int64
 39	Username string
 40	What     string
 41	Honker   string
 42	Handle   string
 43	Oonker   string
 44	Oondle   string
 45	XID      string
 46	RID      string
 47	Date     time.Time
 48	URL      string
 49	Noise    string
 50	Precis   string
 51	Format   string
 52	Convoy   string
 53	Audience []string
 54	Public   bool
 55	Whofore  int64
 56	Replies  []*Honk
 57	Flags    int64
 58	HTML     template.HTML
 59	Style    string
 60	Open     string
 61	Donks    []*Donk
 62	Onts     []string
 63}
 64
 65const (
 66	flagIsAcked  = 1
 67	flagIsBonked = 2
 68)
 69
 70func (honk *Honk) IsAcked() bool {
 71	return honk.Flags&flagIsAcked != 0
 72}
 73
 74func (honk *Honk) IsBonked() bool {
 75	return honk.Flags&flagIsBonked != 0
 76}
 77
 78type Donk struct {
 79	FileID  int64
 80	XID     string
 81	Name    string
 82	Desc    string
 83	URL     string
 84	Media   string
 85	Local   bool
 86	Content []byte
 87}
 88
 89type Honker struct {
 90	ID     int64
 91	UserID int64
 92	Name   string
 93	XID    string
 94	Handle string
 95	Flavor string
 96	Combos []string
 97}
 98
 99type Zonker struct {
100	ID        int64
101	Name      string
102	Wherefore string
103}
104
105var serverName string
106var iconName = "icon.png"
107var serverMsg = "Things happen."
108
109func ElaborateUnitTests() {
110}
111
112func main() {
113	cmd := "run"
114	if len(os.Args) > 1 {
115		cmd = os.Args[1]
116	}
117	switch cmd {
118	case "init":
119		initdb()
120	case "upgrade":
121		upgradedb()
122	}
123	db := opendatabase()
124	dbversion := 0
125	getconfig("dbversion", &dbversion)
126	if dbversion != myVersion {
127		log.Fatal("incorrect database version. run upgrade.")
128	}
129	getconfig("servermsg", &serverMsg)
130	getconfig("servername", &serverName)
131	getconfig("usersep", &userSep)
132	getconfig("honksep", &honkSep)
133	getconfig("dnf", &donotfedafterdark)
134	prepareStatements(db)
135	switch cmd {
136	case "adduser":
137		adduser()
138	case "cleanup":
139		arg := "30"
140		if len(os.Args) > 2 {
141			arg = os.Args[2]
142		}
143		cleanupdb(arg)
144	case "ping":
145		if len(os.Args) < 4 {
146			fmt.Printf("usage: honk ping from to\n")
147			return
148		}
149		name := os.Args[2]
150		targ := os.Args[3]
151		user, err := butwhatabout(name)
152		if err != nil {
153			log.Printf("unknown user")
154			return
155		}
156		ping(user, targ)
157	case "peep":
158		peeppeep()
159	case "run":
160		serve()
161	case "test":
162		ElaborateUnitTests()
163	default:
164		log.Fatal("unknown command")
165	}
166}