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 "strings"
24 "time"
25)
26
27type WhatAbout struct {
28 ID int64
29 Name string
30 Display string
31 About string
32 Key string
33 URL string
34 SkinnyCSS bool
35}
36
37type Honk struct {
38 ID int64
39 UserID int64
40 Username string
41 What string
42 Honker string
43 Handle string
44 Oonker string
45 Oondle string
46 XID string
47 RID string
48 Date time.Time
49 URL string
50 Noise string
51 Precis string
52 Format string
53 Convoy string
54 Audience []string
55 Public bool
56 Whofore int64
57 Replies []*Honk
58 Flags int64
59 HTPrecis template.HTML
60 HTML template.HTML
61 Style string
62 Open string
63 Donks []*Donk
64 Onts []string
65 Place *Place
66 Time *Time
67}
68
69type OldRevision struct {
70 Precis string
71 Noise string
72}
73
74const (
75 flagIsAcked = 1
76 flagIsBonked = 2
77)
78
79func (honk *Honk) IsAcked() bool {
80 return honk.Flags&flagIsAcked != 0
81}
82
83func (honk *Honk) IsBonked() bool {
84 return honk.Flags&flagIsBonked != 0
85}
86
87type Donk struct {
88 FileID int64
89 XID string
90 Name string
91 Desc string
92 URL string
93 Media string
94 Local bool
95 Content []byte
96}
97
98type Place struct {
99 Name string
100 Latitude float64
101 Longitude float64
102 Url string
103}
104
105type Duration int64
106
107func (d Duration) String() string {
108 s := time.Duration(d).String()
109 if strings.HasSuffix(s, "m0s") {
110 s = s[:len(s)-2]
111 }
112 if strings.HasSuffix(s, "h0m") {
113 s = s[:len(s)-2]
114 }
115 return s
116}
117
118type Time struct {
119 StartTime time.Time
120 EndTime time.Time
121 Duration Duration
122}
123
124type Honker struct {
125 ID int64
126 UserID int64
127 Name string
128 XID string
129 Handle string
130 Flavor string
131 Combos []string
132}
133
134type Zonker struct {
135 ID int64
136 Name string
137 Wherefore string
138}
139
140var serverName string
141var iconName = "icon.png"
142var serverMsg = "Things happen."
143
144func ElaborateUnitTests() {
145}
146
147func main() {
148 cmd := "run"
149 if len(os.Args) > 1 {
150 cmd = os.Args[1]
151 }
152 switch cmd {
153 case "init":
154 initdb()
155 case "upgrade":
156 upgradedb()
157 }
158 db := opendatabase()
159 dbversion := 0
160 getconfig("dbversion", &dbversion)
161 if dbversion != myVersion {
162 log.Fatal("incorrect database version. run upgrade.")
163 }
164 getconfig("servermsg", &serverMsg)
165 getconfig("servername", &serverName)
166 getconfig("usersep", &userSep)
167 getconfig("honksep", &honkSep)
168 prepareStatements(db)
169 switch cmd {
170 case "adduser":
171 adduser()
172 case "cleanup":
173 arg := "30"
174 if len(os.Args) > 2 {
175 arg = os.Args[2]
176 }
177 cleanupdb(arg)
178 case "ping":
179 if len(os.Args) < 4 {
180 fmt.Printf("usage: honk ping from to\n")
181 return
182 }
183 name := os.Args[2]
184 targ := os.Args[3]
185 user, err := butwhatabout(name)
186 if err != nil {
187 log.Printf("unknown user")
188 return
189 }
190 ping(user, targ)
191 case "peep":
192 peeppeep()
193 case "run":
194 serve()
195 case "test":
196 ElaborateUnitTests()
197 default:
198 log.Fatal("unknown command")
199 }
200}