all repos — honk @ 4c029becd5c6f0d50f121adad810146ba0c612d0

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