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