all repos — honk @ 7c6194408fe5a50f8f0bdcc1aadb6723cd1c4956

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	Url       string
 96}
 97
 98type Honker struct {
 99	ID     int64
100	UserID int64
101	Name   string
102	XID    string
103	Handle string
104	Flavor string
105	Combos []string
106}
107
108type Zonker struct {
109	ID        int64
110	Name      string
111	Wherefore string
112}
113
114var serverName string
115var iconName = "icon.png"
116var serverMsg = "Things happen."
117
118func ElaborateUnitTests() {
119}
120
121func main() {
122	cmd := "run"
123	if len(os.Args) > 1 {
124		cmd = os.Args[1]
125	}
126	switch cmd {
127	case "init":
128		initdb()
129	case "upgrade":
130		upgradedb()
131	}
132	db := opendatabase()
133	dbversion := 0
134	getconfig("dbversion", &dbversion)
135	if dbversion != myVersion {
136		log.Fatal("incorrect database version. run upgrade.")
137	}
138	getconfig("servermsg", &serverMsg)
139	getconfig("servername", &serverName)
140	getconfig("usersep", &userSep)
141	getconfig("honksep", &honkSep)
142	prepareStatements(db)
143	switch cmd {
144	case "adduser":
145		adduser()
146	case "cleanup":
147		arg := "30"
148		if len(os.Args) > 2 {
149			arg = os.Args[2]
150		}
151		cleanupdb(arg)
152	case "ping":
153		if len(os.Args) < 4 {
154			fmt.Printf("usage: honk ping from to\n")
155			return
156		}
157		name := os.Args[2]
158		targ := os.Args[3]
159		user, err := butwhatabout(name)
160		if err != nil {
161			log.Printf("unknown user")
162			return
163		}
164		ping(user, targ)
165	case "peep":
166		peeppeep()
167	case "run":
168		serve()
169	case "test":
170		ElaborateUnitTests()
171	default:
172		log.Fatal("unknown command")
173	}
174}