all repos — honk @ 0a3b3d52608fb9abda0651b5353421571102d8d0

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