all repos — honk @ 5bdd51a32c506d15ec6d4a3e0b43c3fc91bd60da

my fork of honk

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	if develMode {
119		gogglesDoNothing()
120	}
121	getconfig("fasttimeout", &fastTimeout)
122	getconfig("slowtimeout", &slowTimeout)
123	getconfig("honkwindow", &honkwindow)
124	honkwindow *= 24 * time.Hour
125
126	prepareStatements(db)
127
128	switch cmd {
129	case "admin":
130		adminscreen()
131	case "import":
132		if len(args) != 4 {
133			errx("import username honk|mastodon|twitter srcdir")
134		}
135		importMain(args[1], args[2], args[3])
136	case "export":
137		if len(args) != 3 {
138			errx("export username destdir")
139		}
140		export(args[1], args[2])
141	case "devel":
142		if len(args) != 2 {
143			errx("need an argument: devel (on|off)")
144		}
145		switch args[1] {
146		case "on":
147			setconfig("devel", 1)
148		case "off":
149			setconfig("devel", 0)
150		default:
151			errx("argument must be on or off")
152		}
153	case "setconfig":
154		if len(args) != 3 {
155			errx("need an argument: setconfig key val")
156		}
157		var val interface{}
158		var err error
159		if val, err = strconv.Atoi(args[2]); err != nil {
160			val = args[2]
161		}
162		setconfig(args[1], val)
163	case "adduser":
164		adduser()
165	case "deluser":
166		if len(args) < 2 {
167			errx("usage: honk deluser username")
168		}
169		deluser(args[1])
170	case "chpass":
171		if len(args) < 2 {
172			errx("usage: honk chpass username")
173		}
174		chpass(args[1])
175	case "follow":
176		if len(args) < 3 {
177			errx("usage: honk follow username url")
178		}
179		user, err := butwhatabout(args[1])
180		if err != nil {
181			errx("user %s not found", args[1])
182		}
183		var meta HonkerMeta
184		mj, _ := jsonify(&meta)
185		honkerid, err := savehonker(user, args[2], "", "presub", "", mj)
186		if err != nil {
187			errx("had some trouble with that: %s", err)
188		}
189		followyou(user, honkerid, true)
190	case "unfollow":
191		if len(args) < 3 {
192			errx("usage: honk unfollow username url")
193		}
194		user, err := butwhatabout(args[1])
195		if err != nil {
196			errx("user not found")
197		}
198		row := db.QueryRow("select honkerid from honkers where xid = ? and userid = ? and flavor in ('sub')", args[2], user.ID)
199		var honkerid int64
200		err = row.Scan(&honkerid)
201		if err != nil {
202			errx("sorry couldn't find them")
203		}
204		unfollowyou(user, honkerid, true)
205	case "sendmsg":
206		if len(args) < 4 {
207			errx("usage: honk send username filename rcpt")
208		}
209		user, err := butwhatabout(args[1])
210		if err != nil {
211			errx("user %s not found", args[1])
212		}
213		data, err := os.ReadFile(args[2])
214		if err != nil {
215			errx("can't read file: %s", err)
216		}
217		deliverate(user.ID, args[3], data)
218	case "cleanup":
219		arg := "30"
220		if len(args) > 1 {
221			arg = args[1]
222		}
223		cleanupdb(arg)
224	case "unplug":
225		if len(args) < 2 {
226			errx("usage: honk unplug servername")
227		}
228		name := args[1]
229		unplugserver(name)
230	case "backup":
231		if len(args) < 2 {
232			errx("usage: honk backup dirname")
233		}
234		name := args[1]
235		svalbard(name)
236	case "ping":
237		if len(args) < 3 {
238			errx("usage: honk ping (from username) (to username or url)")
239		}
240		name := args[1]
241		targ := args[2]
242		user, err := butwhatabout(name)
243		if err != nil {
244			errx("unknown user %s", name)
245		}
246		ping(user, targ)
247	case "run":
248		serve()
249	case "backend":
250		backendServer()
251	case "test":
252		ElaborateUnitTests()
253	default:
254		errx("unknown command")
255	}
256}