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}
101
102type Place struct {
103 Name string
104 Latitude float64
105 Longitude float64
106 Url string
107}
108
109type Duration int64
110
111func (d Duration) String() string {
112 s := time.Duration(d).String()
113 if strings.HasSuffix(s, "m0s") {
114 s = s[:len(s)-2]
115 }
116 if strings.HasSuffix(s, "h0m") {
117 s = s[:len(s)-2]
118 }
119 return s
120}
121
122type Time struct {
123 StartTime time.Time
124 EndTime time.Time
125 Duration Duration
126}
127
128type Honker struct {
129 ID int64
130 UserID int64
131 Name string
132 XID string
133 Handle string
134 Flavor string
135 Combos []string
136}
137
138var serverName string
139var iconName = "icon.png"
140var serverMsg = "Things happen."
141
142func ElaborateUnitTests() {
143}
144
145func main() {
146 cmd := "run"
147 if len(os.Args) > 1 {
148 cmd = os.Args[1]
149 }
150 switch cmd {
151 case "init":
152 initdb()
153 case "upgrade":
154 upgradedb()
155 }
156 db := opendatabase()
157 dbversion := 0
158 getconfig("dbversion", &dbversion)
159 if dbversion != myVersion {
160 log.Fatal("incorrect database version. run upgrade.")
161 }
162 getconfig("servermsg", &serverMsg)
163 getconfig("servername", &serverName)
164 getconfig("usersep", &userSep)
165 getconfig("honksep", &honkSep)
166 prepareStatements(db)
167 switch cmd {
168 case "adduser":
169 adduser()
170 case "chpass":
171 chpass()
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 "run":
192 serve()
193 case "test":
194 ElaborateUnitTests()
195 default:
196 log.Fatal("unknown command")
197 }
198}