all repos — honk @ v1.3.1

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