all repos — honk @ b162076c4d37bcff6770ff1ca9299b66d5752b2b

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	"strings"
 24	"time"
 25)
 26
 27type WhatAbout struct {
 28	ID        int64
 29	Name      string
 30	Display   string
 31	About     string
 32	Key       string
 33	URL       string
 34	SkinnyCSS bool
 35}
 36
 37type Honk struct {
 38	ID       int64
 39	UserID   int64
 40	Username string
 41	What     string
 42	Honker   string
 43	Handle   string
 44	Oonker   string
 45	Oondle   string
 46	XID      string
 47	RID      string
 48	Date     time.Time
 49	URL      string
 50	Noise    string
 51	Precis   string
 52	Format   string
 53	Convoy   string
 54	Audience []string
 55	Public   bool
 56	Whofore  int64
 57	Replies  []*Honk
 58	Flags    int64
 59	HTPrecis template.HTML
 60	HTML     template.HTML
 61	Style    string
 62	Open     string
 63	Donks    []*Donk
 64	Onts     []string
 65	Place    *Place
 66	Time     *Time
 67}
 68
 69type OldRevision struct {
 70	Precis string
 71	Noise  string
 72}
 73
 74const (
 75	flagIsAcked  = 1
 76	flagIsBonked = 2
 77	flagIsSaved  = 4
 78)
 79
 80func (honk *Honk) IsAcked() bool {
 81	return honk.Flags&flagIsAcked != 0
 82}
 83
 84func (honk *Honk) IsBonked() bool {
 85	return honk.Flags&flagIsBonked != 0
 86}
 87
 88func (honk *Honk) IsSaved() bool {
 89	return honk.Flags&flagIsSaved != 0
 90}
 91
 92type Donk struct {
 93	FileID int64
 94	XID    string
 95	Name   string
 96	Desc   string
 97	URL    string
 98	Media  string
 99	Local  bool
100}
101
102type Place struct {
103	Name      string
104	Latitude  float64
105	Longitude float64
106	Url       string
107}
108
109type Duration int64
110
111func (d Duration) String() string {
112	s := time.Duration(d).String()
113	if strings.HasSuffix(s, "m0s") {
114		s = s[:len(s)-2]
115	}
116	if strings.HasSuffix(s, "h0m") {
117		s = s[:len(s)-2]
118	}
119	return s
120}
121
122type Time struct {
123	StartTime time.Time
124	EndTime   time.Time
125	Duration  Duration
126}
127
128type Honker struct {
129	ID     int64
130	UserID int64
131	Name   string
132	XID    string
133	Handle string
134	Flavor string
135	Combos []string
136}
137
138var serverName string
139var iconName = "icon.png"
140var serverMsg template.HTML
141var aboutMsg template.HTML
142var loginMsg template.HTML
143
144func ElaborateUnitTests() {
145}
146
147func main() {
148	cmd := "run"
149	if len(os.Args) > 1 {
150		cmd = os.Args[1]
151	}
152	switch cmd {
153	case "init":
154		initdb()
155	case "upgrade":
156		upgradedb()
157	}
158	db := opendatabase()
159	dbversion := 0
160	getconfig("dbversion", &dbversion)
161	if dbversion != myVersion {
162		log.Fatal("incorrect database version. run upgrade.")
163	}
164	getconfig("servermsg", &serverMsg)
165	getconfig("aboutmsg", &aboutMsg)
166	getconfig("loginmsg", &loginMsg)
167	getconfig("servername", &serverName)
168	getconfig("usersep", &userSep)
169	getconfig("honksep", &honkSep)
170	prepareStatements(db)
171	switch cmd {
172	case "admin":
173		adminscreen()
174	case "debug":
175		if len(os.Args) != 3 {
176			log.Fatal("need an argument: debug (on|off)")
177		}
178		switch os.Args[2] {
179		case "on":
180			updateconfig("debug", 1)
181		case "off":
182			updateconfig("debug", 0)
183		default:
184			log.Fatal("argument must be on or off")
185		}
186	case "adduser":
187		adduser()
188	case "chpass":
189		chpass()
190	case "cleanup":
191		arg := "30"
192		if len(os.Args) > 2 {
193			arg = os.Args[2]
194		}
195		cleanupdb(arg)
196	case "ping":
197		if len(os.Args) < 4 {
198			fmt.Printf("usage: honk ping from to\n")
199			return
200		}
201		name := os.Args[2]
202		targ := os.Args[3]
203		user, err := butwhatabout(name)
204		if err != nil {
205			log.Printf("unknown user")
206			return
207		}
208		ping(user, targ)
209	case "run":
210		serve()
211	case "test":
212		ElaborateUnitTests()
213	default:
214		log.Fatal("unknown command")
215	}
216}