all repos — honk @ dea3cb9d2c80622a1ff6273f60ac0fa87e0d3847

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