all repos — honk @ 36d540382ff0f43fc2ff1b3c07bacf608c6e9c7a

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	HTPrecis template.HTML
 59	HTML     template.HTML
 60	Style    string
 61	Open     string
 62	Donks    []*Donk
 63	Onts     []string
 64	Place    *Place
 65}
 66
 67type OldRevision struct {
 68	Precis string
 69	Noise  string
 70}
 71
 72const (
 73	flagIsAcked  = 1
 74	flagIsBonked = 2
 75)
 76
 77func (honk *Honk) IsAcked() bool {
 78	return honk.Flags&flagIsAcked != 0
 79}
 80
 81func (honk *Honk) IsBonked() bool {
 82	return honk.Flags&flagIsBonked != 0
 83}
 84
 85type Donk struct {
 86	FileID  int64
 87	XID     string
 88	Name    string
 89	Desc    string
 90	URL     string
 91	Media   string
 92	Local   bool
 93	Content []byte
 94}
 95
 96type Place struct {
 97	Name      string
 98	Latitude  float64
 99	Longitude float64
100	Url       string
101}
102
103type Honker struct {
104	ID     int64
105	UserID int64
106	Name   string
107	XID    string
108	Handle string
109	Flavor string
110	Combos []string
111}
112
113type Zonker struct {
114	ID        int64
115	Name      string
116	Wherefore string
117}
118
119var serverName string
120var iconName = "icon.png"
121var serverMsg = "Things happen."
122
123func ElaborateUnitTests() {
124}
125
126func main() {
127	cmd := "run"
128	if len(os.Args) > 1 {
129		cmd = os.Args[1]
130	}
131	switch cmd {
132	case "init":
133		initdb()
134	case "upgrade":
135		upgradedb()
136	}
137	db := opendatabase()
138	dbversion := 0
139	getconfig("dbversion", &dbversion)
140	if dbversion != myVersion {
141		log.Fatal("incorrect database version. run upgrade.")
142	}
143	getconfig("servermsg", &serverMsg)
144	getconfig("servername", &serverName)
145	getconfig("usersep", &userSep)
146	getconfig("honksep", &honkSep)
147	prepareStatements(db)
148	switch cmd {
149	case "adduser":
150		adduser()
151	case "cleanup":
152		arg := "30"
153		if len(os.Args) > 2 {
154			arg = os.Args[2]
155		}
156		cleanupdb(arg)
157	case "ping":
158		if len(os.Args) < 4 {
159			fmt.Printf("usage: honk ping from to\n")
160			return
161		}
162		name := os.Args[2]
163		targ := os.Args[3]
164		user, err := butwhatabout(name)
165		if err != nil {
166			log.Printf("unknown user")
167			return
168		}
169		ping(user, targ)
170	case "peep":
171		peeppeep()
172	case "run":
173		serve()
174	case "test":
175		ElaborateUnitTests()
176	default:
177		log.Fatal("unknown command")
178	}
179}