main.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 "flag"
20 "fmt"
21 "html/template"
22 golog "log"
23 "log/syslog"
24 notrand "math/rand"
25 "os"
26 "strconv"
27 "time"
28
29 "humungus.tedunangst.com/r/webs/log"
30)
31
32var softwareVersion = "develop"
33
34func init() {
35 notrand.Seed(time.Now().Unix())
36}
37
38var serverName string
39var serverPrefix string
40var masqName string
41var dataDir = "."
42var viewDir = "."
43var iconName = "icon.png"
44var serverMsg template.HTML
45var aboutMsg template.HTML
46var loginMsg template.HTML
47
48func ElaborateUnitTests() {
49}
50
51func unplugserver(hostname string) {
52 db := opendatabase()
53 xid := fmt.Sprintf("%%https://%s/%%", hostname)
54 db.Exec("delete from honkers where xid like ? and flavor = 'dub'", xid)
55 db.Exec("delete from doovers where rcpt like ?", xid)
56}
57
58func reexecArgs(cmd string) []string {
59 args := []string{"-datadir", dataDir}
60 args = append(args, log.Args()...)
61 args = append(args, cmd)
62 return args
63}
64
65var elog, ilog, dlog *golog.Logger
66
67func errx(msg string, args ...interface{}) {
68 fmt.Fprintf(os.Stderr, msg+"\n", args...)
69 os.Exit(1)
70}
71
72func main() {
73 flag.StringVar(&dataDir, "datadir", dataDir, "data directory")
74 flag.StringVar(&viewDir, "viewdir", viewDir, "view directory")
75 flag.Parse()
76
77 log.Init(log.Options{Progname: "honk", Facility: syslog.LOG_UUCP})
78 elog = log.E
79 ilog = log.I
80 dlog = log.D
81
82 if os.Geteuid() == 0 {
83 elog.Fatalf("do not run honk as root")
84 }
85
86 args := flag.Args()
87 cmd := "run"
88 if len(args) > 0 {
89 cmd = args[0]
90 }
91 switch cmd {
92 case "init":
93 initdb()
94 case "upgrade":
95 upgradedb()
96 case "version":
97 fmt.Println(softwareVersion)
98 os.Exit(0)
99 }
100 db := opendatabase()
101 dbversion := 0
102 getconfig("dbversion", &dbversion)
103 if dbversion != myVersion {
104 elog.Fatal("incorrect database version. run upgrade.")
105 }
106 getconfig("servermsg", &serverMsg)
107 getconfig("aboutmsg", &aboutMsg)
108 getconfig("loginmsg", &loginMsg)
109 getconfig("servername", &serverName)
110 getconfig("masqname", &masqName)
111 if masqName == "" {
112 masqName = serverName
113 }
114 serverPrefix = fmt.Sprintf("https://%s/", serverName)
115 getconfig("usersep", &userSep)
116 getconfig("honksep", &honkSep)
117 getconfig("devel", &develMode)
118 getconfig("fasttimeout", &fastTimeout)
119 getconfig("slowtimeout", &slowTimeout)
120 getconfig("honkwindow", &honkwindow)
121 honkwindow *= 24 * time.Hour
122 getconfig("signgets", &signGets)
123 prepareStatements(db)
124 switch cmd {
125 case "admin":
126 adminscreen()
127 case "import":
128 if len(args) != 4 {
129 errx("import username honk|mastodon|twitter srcdir")
130 }
131 importMain(args[1], args[2], args[3])
132 case "export":
133 if len(args) != 3 {
134 errx("export username destdir")
135 }
136 export(args[1], args[2])
137 case "devel":
138 if len(args) != 2 {
139 errx("need an argument: devel (on|off)")
140 }
141 switch args[1] {
142 case "on":
143 setconfig("devel", 1)
144 case "off":
145 setconfig("devel", 0)
146 default:
147 errx("argument must be on or off")
148 }
149 case "setconfig":
150 if len(args) != 3 {
151 errx("need an argument: setconfig key val")
152 }
153 var val interface{}
154 var err error
155 if val, err = strconv.Atoi(args[2]); err != nil {
156 val = args[2]
157 }
158 setconfig(args[1], val)
159 case "adduser":
160 adduser()
161 case "deluser":
162 if len(args) < 2 {
163 errx("usage: honk deluser username")
164 }
165 deluser(args[1])
166 case "chpass":
167 if len(args) < 2 {
168 errx("usage: honk chpass username")
169 }
170 chpass(args[1])
171 case "follow":
172 if len(args) < 3 {
173 errx("usage: honk follow username url")
174 }
175 user, err := butwhatabout(args[1])
176 if err != nil {
177 errx("user %s not found", args[1])
178 }
179 var meta HonkerMeta
180 mj, _ := jsonify(&meta)
181 honkerid, err := savehonker(user, args[2], "", "presub", "", mj)
182 if err != nil {
183 errx("had some trouble with that: %s", err)
184 }
185 followyou(user, honkerid, true)
186 case "unfollow":
187 if len(args) < 3 {
188 errx("usage: honk unfollow username url")
189 }
190 user, err := butwhatabout(args[1])
191 if err != nil {
192 errx("user not found")
193 }
194 row := db.QueryRow("select honkerid from honkers where xid = ? and userid = ? and flavor in ('sub')", args[2], user.ID)
195 var honkerid int64
196 err = row.Scan(&honkerid)
197 if err != nil {
198 errx("sorry couldn't find them")
199 }
200 unfollowyou(user, honkerid, true)
201 case "sendmsg":
202 if len(args) < 4 {
203 errx("usage: honk send username filename rcpt")
204 }
205 user, err := butwhatabout(args[1])
206 if err != nil {
207 errx("user %s not found", args[1])
208 }
209 data, err := os.ReadFile(args[2])
210 if err != nil {
211 errx("can't read file: %s", err)
212 }
213 deliverate(user.ID, args[3], data)
214 case "cleanup":
215 arg := "30"
216 if len(args) > 1 {
217 arg = args[1]
218 }
219 cleanupdb(arg)
220 case "unplug":
221 if len(args) < 2 {
222 errx("usage: honk unplug servername")
223 }
224 name := args[1]
225 unplugserver(name)
226 case "backup":
227 if len(args) < 2 {
228 errx("usage: honk backup dirname")
229 }
230 name := args[1]
231 svalbard(name)
232 case "ping":
233 if len(args) < 3 {
234 errx("usage: honk ping (from username) (to username or url)")
235 }
236 name := args[1]
237 targ := args[2]
238 user, err := butwhatabout(name)
239 if err != nil {
240 errx("unknown user %s", name)
241 }
242 ping(user, targ)
243 case "run":
244 serve()
245 case "backend":
246 backendServer()
247 case "test":
248 ElaborateUnitTests()
249 default:
250 errx("unknown command")
251 }
252}