all repos — honk @ 6daf6f8d9bf6caaa697f9cf699255081916017cf

my fork of honk

web.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	"bytes"
  20	"fmt"
  21	"html"
  22	"html/template"
  23	"io"
  24	"io/ioutil"
  25	"log"
  26	notrand "math/rand"
  27	"net/http"
  28	"net/url"
  29	"os"
  30	"sort"
  31	"strconv"
  32	"strings"
  33	"time"
  34
  35	"github.com/gorilla/mux"
  36	"humungus.tedunangst.com/r/webs/css"
  37	"humungus.tedunangst.com/r/webs/htfilter"
  38	"humungus.tedunangst.com/r/webs/httpsig"
  39	"humungus.tedunangst.com/r/webs/image"
  40	"humungus.tedunangst.com/r/webs/junk"
  41	"humungus.tedunangst.com/r/webs/login"
  42	"humungus.tedunangst.com/r/webs/rss"
  43	"humungus.tedunangst.com/r/webs/templates"
  44)
  45
  46var readviews *templates.Template
  47
  48var userSep = "u"
  49var honkSep = "h"
  50
  51var donotfedafterdark = make(map[string]bool)
  52
  53func stealthed(r *http.Request) bool {
  54	addr := r.Header.Get("X-Forwarded-For")
  55	fake := donotfedafterdark[addr]
  56	if fake {
  57		log.Printf("faking 404 for %s", addr)
  58	}
  59	return fake
  60}
  61
  62func getuserstyle(u *login.UserInfo) template.CSS {
  63	if u == nil {
  64		return ""
  65	}
  66	user, _ := butwhatabout(u.Username)
  67	if user.SkinnyCSS {
  68		return "main { max-width: 700px; }"
  69	}
  70	return ""
  71}
  72
  73func getInfo(r *http.Request) map[string]interface{} {
  74	u := login.GetUserInfo(r)
  75	templinfo := make(map[string]interface{})
  76	templinfo["StyleParam"] = getstyleparam("views/style.css")
  77	templinfo["LocalStyleParam"] = getstyleparam("views/local.css")
  78	templinfo["UserStyle"] = getuserstyle(u)
  79	templinfo["ServerName"] = serverName
  80	templinfo["IconName"] = iconName
  81	templinfo["UserInfo"] = u
  82	templinfo["UserSep"] = userSep
  83	return templinfo
  84}
  85
  86func homepage(w http.ResponseWriter, r *http.Request) {
  87	templinfo := getInfo(r)
  88	u := login.GetUserInfo(r)
  89	var honks []*Honk
  90	var userid int64 = -1
  91	if r.URL.Path == "/front" || u == nil {
  92		honks = getpublichonks()
  93	} else {
  94		userid = u.UserID
  95		if r.URL.Path == "/atme" {
  96			templinfo["PageName"] = "atme"
  97			honks = gethonksforme(userid)
  98		} else {
  99			templinfo["PageName"] = "home"
 100			honks = gethonksforuser(userid)
 101			honks = osmosis(honks, userid)
 102		}
 103		if len(honks) > 0 {
 104			templinfo["TopXID"] = honks[0].XID
 105		}
 106		templinfo["HonkCSRF"] = login.GetCSRF("honkhonk", r)
 107	}
 108
 109	templinfo["ShowRSS"] = true
 110	templinfo["ServerMessage"] = serverMsg
 111	honkpage(w, u, honks, templinfo)
 112}
 113
 114func showfunzone(w http.ResponseWriter, r *http.Request) {
 115	var emunames, memenames []string
 116	dir, err := os.Open("emus")
 117	if err == nil {
 118		emunames, _ = dir.Readdirnames(0)
 119		dir.Close()
 120	}
 121	for i, e := range emunames {
 122		if len(e) > 4 {
 123			emunames[i] = e[:len(e)-4]
 124		}
 125	}
 126	dir, err = os.Open("memes")
 127	if err == nil {
 128		memenames, _ = dir.Readdirnames(0)
 129		dir.Close()
 130	}
 131	templinfo := getInfo(r)
 132	templinfo["Emus"] = emunames
 133	templinfo["Memes"] = memenames
 134	err = readviews.Execute(w, "funzone.html", templinfo)
 135	if err != nil {
 136		log.Print(err)
 137	}
 138}
 139
 140func showrss(w http.ResponseWriter, r *http.Request) {
 141	name := mux.Vars(r)["name"]
 142
 143	var honks []*Honk
 144	if name != "" {
 145		honks = gethonksbyuser(name, false)
 146	} else {
 147		honks = getpublichonks()
 148	}
 149	if len(honks) > 20 {
 150		honks = honks[0:20]
 151	}
 152	reverbolate(-1, honks)
 153
 154	home := fmt.Sprintf("https://%s/", serverName)
 155	base := home
 156	if name != "" {
 157		home += "u/" + name
 158		name += " "
 159	}
 160	feed := rss.Feed{
 161		Title:       name + "honk",
 162		Link:        home,
 163		Description: name + "honk rss",
 164		Image: &rss.Image{
 165			URL:   base + "icon.png",
 166			Title: name + "honk rss",
 167			Link:  home,
 168		},
 169	}
 170	var modtime time.Time
 171	for _, honk := range honks {
 172		if !firstclass(honk) {
 173			continue
 174		}
 175		desc := string(honk.HTML)
 176		for _, d := range honk.Donks {
 177			desc += fmt.Sprintf(`<p><a href="%s">Attachment: %s</a>`,
 178				d.URL, html.EscapeString(d.Name))
 179		}
 180
 181		feed.Items = append(feed.Items, &rss.Item{
 182			Title:       fmt.Sprintf("%s %s %s", honk.Username, honk.What, honk.XID),
 183			Description: rss.CData{desc},
 184			Link:        honk.URL,
 185			PubDate:     honk.Date.Format(time.RFC1123),
 186			Guid:        &rss.Guid{IsPermaLink: true, Value: honk.URL},
 187		})
 188		if honk.Date.After(modtime) {
 189			modtime = honk.Date
 190		}
 191	}
 192	w.Header().Set("Cache-Control", "max-age=300")
 193	w.Header().Set("Last-Modified", modtime.Format(http.TimeFormat))
 194
 195	err := feed.Write(w)
 196	if err != nil {
 197		log.Printf("error writing rss: %s", err)
 198	}
 199}
 200
 201func crappola(j junk.Junk) bool {
 202	t, _ := j.GetString("type")
 203	a, _ := j.GetString("actor")
 204	o, _ := j.GetString("object")
 205	if t == "Delete" && a == o {
 206		log.Printf("crappola from %s", a)
 207		return true
 208	}
 209	return false
 210}
 211
 212func ping(user *WhatAbout, who string) {
 213	box, err := getboxes(who)
 214	if err != nil {
 215		log.Printf("no inbox for ping: %s", err)
 216		return
 217	}
 218	j := junk.New()
 219	j["@context"] = itiswhatitis
 220	j["type"] = "Ping"
 221	j["id"] = user.URL + "/ping/" + xfiltrate()
 222	j["actor"] = user.URL
 223	j["to"] = who
 224	keyname, key := ziggy(user.Name)
 225	err = PostJunk(keyname, key, box.In, j)
 226	if err != nil {
 227		log.Printf("can't send ping: %s", err)
 228		return
 229	}
 230	log.Printf("sent ping to %s: %s", who, j["id"])
 231}
 232
 233func pong(user *WhatAbout, who string, obj string) {
 234	box, err := getboxes(who)
 235	if err != nil {
 236		log.Printf("no inbox for pong %s : %s", who, err)
 237		return
 238	}
 239	j := junk.New()
 240	j["@context"] = itiswhatitis
 241	j["type"] = "Pong"
 242	j["id"] = user.URL + "/pong/" + xfiltrate()
 243	j["actor"] = user.URL
 244	j["to"] = who
 245	j["object"] = obj
 246	keyname, key := ziggy(user.Name)
 247	err = PostJunk(keyname, key, box.In, j)
 248	if err != nil {
 249		log.Printf("can't send pong: %s", err)
 250		return
 251	}
 252}
 253
 254func inbox(w http.ResponseWriter, r *http.Request) {
 255	name := mux.Vars(r)["name"]
 256	user, err := butwhatabout(name)
 257	if err != nil {
 258		http.NotFound(w, r)
 259		return
 260	}
 261	var buf bytes.Buffer
 262	io.Copy(&buf, r.Body)
 263	payload := buf.Bytes()
 264	j, err := junk.Read(bytes.NewReader(payload))
 265	if err != nil {
 266		log.Printf("bad payload: %s", err)
 267		io.WriteString(os.Stdout, "bad payload\n")
 268		os.Stdout.Write(payload)
 269		io.WriteString(os.Stdout, "\n")
 270		return
 271	}
 272	if crappola(j) {
 273		return
 274	}
 275	keyname, err := httpsig.VerifyRequest(r, payload, zaggy)
 276	if err != nil {
 277		log.Printf("inbox message failed signature: %s", err)
 278		if keyname != "" {
 279			keyname, err = makeitworksomehowwithoutregardforkeycontinuity(keyname, r, payload)
 280			if err != nil {
 281				log.Printf("still failed: %s", err)
 282			}
 283		}
 284		if err != nil {
 285			return
 286		}
 287	}
 288	what, _ := j.GetString("type")
 289	if what == "Like" {
 290		return
 291	}
 292	who, _ := j.GetString("actor")
 293	origin := keymatch(keyname, who)
 294	if origin == "" {
 295		log.Printf("keyname actor mismatch: %s <> %s", keyname, who)
 296		return
 297	}
 298	objid, _ := j.GetString("id")
 299	if thoudostbitethythumb(user.ID, []string{who}, objid) {
 300		log.Printf("ignoring thumb sucker %s", who)
 301		return
 302	}
 303	switch what {
 304	case "Ping":
 305		obj, _ := j.GetString("id")
 306		log.Printf("ping from %s: %s", who, obj)
 307		pong(user, who, obj)
 308	case "Pong":
 309		obj, _ := j.GetString("object")
 310		log.Printf("pong from %s: %s", who, obj)
 311	case "Follow":
 312		obj, _ := j.GetString("object")
 313		if obj == user.URL {
 314			log.Printf("updating honker follow: %s", who)
 315			stmtSaveDub.Exec(user.ID, who, who, "dub")
 316			go rubadubdub(user, j)
 317		} else {
 318			log.Printf("can't follow %s", obj)
 319		}
 320	case "Accept":
 321		log.Printf("updating honker accept: %s", who)
 322		_, err = stmtUpdateFlavor.Exec("sub", user.ID, who, "presub")
 323		if err != nil {
 324			log.Printf("error updating honker: %s", err)
 325			return
 326		}
 327	case "Update":
 328		obj, ok := j.GetMap("object")
 329		if ok {
 330			what, _ := obj.GetString("type")
 331			switch what {
 332			case "Person":
 333				return
 334			case "Question":
 335				return
 336			case "Note":
 337				go consumeactivity(user, j, origin)
 338				return
 339			}
 340		}
 341		log.Printf("unknown Update activity")
 342		fd, _ := os.OpenFile("savedinbox.json", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
 343		j.Write(fd)
 344		io.WriteString(fd, "\n")
 345		fd.Close()
 346
 347	case "Undo":
 348		obj, ok := j.GetMap("object")
 349		if !ok {
 350			log.Printf("unknown undo no object")
 351		} else {
 352			what, _ := obj.GetString("type")
 353			switch what {
 354			case "Follow":
 355				log.Printf("updating honker undo: %s", who)
 356				_, err = stmtUpdateFlavor.Exec("undub", user.ID, who, "dub")
 357				if err != nil {
 358					log.Printf("error updating honker: %s", err)
 359					return
 360				}
 361			case "Announce":
 362				xid, _ := obj.GetString("object")
 363				log.Printf("undo announce: %s", xid)
 364			case "Like":
 365			default:
 366				log.Printf("unknown undo: %s", what)
 367			}
 368		}
 369	default:
 370		go consumeactivity(user, j, origin)
 371	}
 372}
 373
 374func ximport(w http.ResponseWriter, r *http.Request) {
 375	xid := r.FormValue("xid")
 376	p, _ := investigate(xid)
 377	if p != nil {
 378		xid = p.XID
 379	}
 380	j, err := GetJunk(xid)
 381	if err != nil {
 382		http.Error(w, "error getting external object", http.StatusInternalServerError)
 383		log.Printf("error getting external object: %s", err)
 384		return
 385	}
 386	log.Printf("importing %s", xid)
 387	u := login.GetUserInfo(r)
 388	user, _ := butwhatabout(u.Username)
 389
 390	what, _ := j.GetString("type")
 391	if isactor(what) {
 392		outbox, _ := j.GetString("outbox")
 393		gimmexonks(user, outbox)
 394		http.Redirect(w, r, "/h?xid="+url.QueryEscape(xid), http.StatusSeeOther)
 395		return
 396	}
 397	xonk := xonkxonk(user, j, originate(xid))
 398	convoy := ""
 399	if xonk != nil {
 400		convoy = xonk.Convoy
 401		savexonk(xonk)
 402	}
 403	http.Redirect(w, r, "/t?c="+url.QueryEscape(convoy), http.StatusSeeOther)
 404}
 405
 406func xzone(w http.ResponseWriter, r *http.Request) {
 407	u := login.GetUserInfo(r)
 408	rows, err := stmtRecentHonkers.Query(u.UserID, u.UserID)
 409	if err != nil {
 410		log.Printf("query err: %s", err)
 411		return
 412	}
 413	defer rows.Close()
 414	var honkers []Honker
 415	for rows.Next() {
 416		var xid string
 417		rows.Scan(&xid)
 418		honkers = append(honkers, Honker{XID: xid})
 419	}
 420	rows.Close()
 421	for i, _ := range honkers {
 422		_, honkers[i].Handle = handles(honkers[i].XID)
 423	}
 424	templinfo := getInfo(r)
 425	templinfo["XCSRF"] = login.GetCSRF("ximport", r)
 426	templinfo["Honkers"] = honkers
 427	err = readviews.Execute(w, "xzone.html", templinfo)
 428	if err != nil {
 429		log.Print(err)
 430	}
 431}
 432
 433func outbox(w http.ResponseWriter, r *http.Request) {
 434	name := mux.Vars(r)["name"]
 435	user, err := butwhatabout(name)
 436	if err != nil {
 437		http.NotFound(w, r)
 438		return
 439	}
 440	if stealthed(r) {
 441		http.NotFound(w, r)
 442		return
 443	}
 444
 445	honks := gethonksbyuser(name, false)
 446	if len(honks) > 20 {
 447		honks = honks[0:20]
 448	}
 449
 450	var jonks []junk.Junk
 451	for _, h := range honks {
 452		j, _ := jonkjonk(user, h)
 453		jonks = append(jonks, j)
 454	}
 455
 456	j := junk.New()
 457	j["@context"] = itiswhatitis
 458	j["id"] = user.URL + "/outbox"
 459	j["type"] = "OrderedCollection"
 460	j["totalItems"] = len(jonks)
 461	j["orderedItems"] = jonks
 462
 463	w.Header().Set("Content-Type", theonetruename)
 464	j.Write(w)
 465}
 466
 467func emptiness(w http.ResponseWriter, r *http.Request) {
 468	name := mux.Vars(r)["name"]
 469	user, err := butwhatabout(name)
 470	if err != nil {
 471		http.NotFound(w, r)
 472		return
 473	}
 474	colname := "/followers"
 475	if strings.HasSuffix(r.URL.Path, "/following") {
 476		colname = "/following"
 477	}
 478	j := junk.New()
 479	j["@context"] = itiswhatitis
 480	j["id"] = user.URL + colname
 481	j["type"] = "OrderedCollection"
 482	j["totalItems"] = 0
 483	j["orderedItems"] = []junk.Junk{}
 484
 485	w.Header().Set("Content-Type", theonetruename)
 486	j.Write(w)
 487}
 488
 489func showuser(w http.ResponseWriter, r *http.Request) {
 490	name := mux.Vars(r)["name"]
 491	user, err := butwhatabout(name)
 492	if err != nil {
 493		log.Printf("user not found %s: %s", name, err)
 494		http.NotFound(w, r)
 495		return
 496	}
 497	if friendorfoe(r.Header.Get("Accept")) {
 498		j := asjonker(user)
 499		w.Header().Set("Content-Type", theonetruename)
 500		j.Write(w)
 501		return
 502	}
 503	u := login.GetUserInfo(r)
 504	honks := gethonksbyuser(name, u != nil && u.Username == name)
 505	templinfo := getInfo(r)
 506	filt := htfilter.New()
 507	templinfo["Name"] = user.Name
 508	whatabout := user.About
 509	whatabout = obfusbreak(user.About)
 510	templinfo["WhatAbout"], _ = filt.String(whatabout)
 511	templinfo["ServerMessage"] = ""
 512	templinfo["HonkCSRF"] = login.GetCSRF("honkhonk", r)
 513	honkpage(w, u, honks, templinfo)
 514}
 515
 516func showhonker(w http.ResponseWriter, r *http.Request) {
 517	u := login.GetUserInfo(r)
 518	name := mux.Vars(r)["name"]
 519	var honks []*Honk
 520	if name == "" {
 521		name = r.FormValue("xid")
 522		honks = gethonksbyxonker(u.UserID, name)
 523	} else {
 524		honks = gethonksbyhonker(u.UserID, name)
 525	}
 526	name = html.EscapeString(name)
 527	msg := fmt.Sprintf(`honks by honker: <a href="%s" ref="noreferrer">%s</a>`, name, name)
 528	templinfo := getInfo(r)
 529	templinfo["PageName"] = "honker"
 530	templinfo["ServerMessage"] = template.HTML(msg)
 531	templinfo["HonkCSRF"] = login.GetCSRF("honkhonk", r)
 532	honkpage(w, u, honks, templinfo)
 533}
 534
 535func showcombo(w http.ResponseWriter, r *http.Request) {
 536	name := mux.Vars(r)["name"]
 537	u := login.GetUserInfo(r)
 538	honks := gethonksbycombo(u.UserID, name)
 539	honks = osmosis(honks, u.UserID)
 540	templinfo := getInfo(r)
 541	templinfo["PageName"] = "combo"
 542	templinfo["ServerMessage"] = "honks by combo: " + name
 543	templinfo["HonkCSRF"] = login.GetCSRF("honkhonk", r)
 544	honkpage(w, u, honks, templinfo)
 545}
 546func showconvoy(w http.ResponseWriter, r *http.Request) {
 547	c := r.FormValue("c")
 548	u := login.GetUserInfo(r)
 549	honks := gethonksbyconvoy(u.UserID, c)
 550	templinfo := getInfo(r)
 551	templinfo["ServerMessage"] = "honks in convoy: " + c
 552	templinfo["HonkCSRF"] = login.GetCSRF("honkhonk", r)
 553	honkpage(w, u, honks, templinfo)
 554}
 555func showsearch(w http.ResponseWriter, r *http.Request) {
 556	q := r.FormValue("q")
 557	u := login.GetUserInfo(r)
 558	honks := gethonksbysearch(u.UserID, q)
 559	templinfo := getInfo(r)
 560	templinfo["ServerMessage"] = "honks for search: " + q
 561	templinfo["HonkCSRF"] = login.GetCSRF("honkhonk", r)
 562	honkpage(w, u, honks, templinfo)
 563}
 564func showontology(w http.ResponseWriter, r *http.Request) {
 565	name := mux.Vars(r)["name"]
 566	u := login.GetUserInfo(r)
 567	var userid int64 = -1
 568	if u != nil {
 569		userid = u.UserID
 570	}
 571	honks := gethonksbyontology(userid, "#"+name)
 572	templinfo := getInfo(r)
 573	templinfo["ServerMessage"] = "honks by ontology: " + name
 574	templinfo["HonkCSRF"] = login.GetCSRF("honkhonk", r)
 575	honkpage(w, u, honks, templinfo)
 576}
 577
 578func thelistingoftheontologies(w http.ResponseWriter, r *http.Request) {
 579	u := login.GetUserInfo(r)
 580	var userid int64 = -1
 581	if u != nil {
 582		userid = u.UserID
 583	}
 584	rows, err := stmtSelectOnts.Query(userid)
 585	if err != nil {
 586		log.Printf("selection error: %s", err)
 587		return
 588	}
 589	var onts [][]string
 590	for rows.Next() {
 591		var o string
 592		err := rows.Scan(&o)
 593		if err != nil {
 594			log.Printf("error scanning ont: %s", err)
 595			continue
 596		}
 597		onts = append(onts, []string{o, o[1:]})
 598	}
 599	if u == nil {
 600		w.Header().Set("Cache-Control", "max-age=300")
 601	}
 602	templinfo := getInfo(r)
 603	templinfo["Onts"] = onts
 604	err = readviews.Execute(w, "onts.html", templinfo)
 605	if err != nil {
 606		log.Print(err)
 607	}
 608}
 609
 610func showhonk(w http.ResponseWriter, r *http.Request) {
 611	name := mux.Vars(r)["name"]
 612	user, err := butwhatabout(name)
 613	if err != nil {
 614		http.NotFound(w, r)
 615		return
 616	}
 617	if stealthed(r) {
 618		http.NotFound(w, r)
 619		return
 620	}
 621
 622	xid := fmt.Sprintf("https://%s%s", serverName, r.URL.Path)
 623	honk := getxonk(user.ID, xid)
 624	if honk == nil {
 625		http.NotFound(w, r)
 626		return
 627	}
 628	u := login.GetUserInfo(r)
 629	if u != nil && u.UserID != user.ID {
 630		u = nil
 631	}
 632	if !honk.Public {
 633		if u == nil {
 634			http.NotFound(w, r)
 635			return
 636
 637		}
 638		templinfo := getInfo(r)
 639		templinfo["ServerMessage"] = "one honk maybe more"
 640		templinfo["HonkCSRF"] = login.GetCSRF("honkhonk", r)
 641		honkpage(w, u, []*Honk{honk}, templinfo)
 642		return
 643	}
 644	rawhonks := gethonksbyconvoy(honk.UserID, honk.Convoy)
 645	if friendorfoe(r.Header.Get("Accept")) {
 646		for _, h := range rawhonks {
 647			if h.RID == honk.XID && h.Public && (h.Whofore == 2 || h.IsAcked()) {
 648				honk.Replies = append(honk.Replies, h)
 649			}
 650		}
 651		donksforhonks([]*Honk{honk})
 652		_, j := jonkjonk(user, honk)
 653		j["@context"] = itiswhatitis
 654		w.Header().Set("Content-Type", theonetruename)
 655		j.Write(w)
 656		return
 657	}
 658	var honks []*Honk
 659	for _, h := range rawhonks {
 660		if h.Public && (h.Whofore == 2 || h.IsAcked()) {
 661			honks = append(honks, h)
 662		}
 663	}
 664
 665	templinfo := getInfo(r)
 666	templinfo["ServerMessage"] = "one honk maybe more"
 667	templinfo["HonkCSRF"] = login.GetCSRF("honkhonk", r)
 668	honkpage(w, u, honks, templinfo)
 669}
 670
 671func honkpage(w http.ResponseWriter, u *login.UserInfo, honks []*Honk, templinfo map[string]interface{}) {
 672	var userid int64 = -1
 673	if u != nil {
 674		userid = u.UserID
 675	}
 676	if u == nil {
 677		w.Header().Set("Cache-Control", "max-age=60")
 678	}
 679	reverbolate(userid, honks)
 680	templinfo["Honks"] = honks
 681	err := readviews.Execute(w, "honkpage.html", templinfo)
 682	if err != nil {
 683		log.Print(err)
 684	}
 685}
 686
 687func saveuser(w http.ResponseWriter, r *http.Request) {
 688	whatabout := r.FormValue("whatabout")
 689	u := login.GetUserInfo(r)
 690	db := opendatabase()
 691	options := ""
 692	if r.FormValue("skinny") == "skinny" {
 693		options += " skinny "
 694	}
 695	_, err := db.Exec("update users set about = ?, options = ? where username = ?", whatabout, options, u.Username)
 696	if err != nil {
 697		log.Printf("error bouting what: %s", err)
 698	}
 699
 700	http.Redirect(w, r, "/account", http.StatusSeeOther)
 701}
 702
 703func submitbonk(w http.ResponseWriter, r *http.Request) {
 704	xid := r.FormValue("xid")
 705	userinfo := login.GetUserInfo(r)
 706	user, _ := butwhatabout(userinfo.Username)
 707
 708	log.Printf("bonking %s", xid)
 709
 710	xonk := getxonk(userinfo.UserID, xid)
 711	if xonk == nil {
 712		return
 713	}
 714	if !xonk.Public {
 715		return
 716	}
 717	donksforhonks([]*Honk{xonk})
 718
 719	_, err := stmtUpdateFlags.Exec(flagIsBonked, xonk.ID)
 720	if err != nil {
 721		log.Printf("error acking bonk: %s", err)
 722	}
 723
 724	oonker := xonk.Oonker
 725	if oonker == "" {
 726		oonker = xonk.Honker
 727	}
 728	dt := time.Now().UTC()
 729	bonk := Honk{
 730		UserID:   userinfo.UserID,
 731		Username: userinfo.Username,
 732		What:     "bonk",
 733		Honker:   user.URL,
 734		Oonker:   oonker,
 735		XID:      xonk.XID,
 736		RID:      xonk.RID,
 737		Noise:    xonk.Noise,
 738		Precis:   xonk.Precis,
 739		URL:      xonk.URL,
 740		Date:     dt,
 741		Donks:    xonk.Donks,
 742		Whofore:  2,
 743		Convoy:   xonk.Convoy,
 744		Audience: []string{thewholeworld, oonker},
 745		Public:   true,
 746	}
 747
 748	bonk.Format = "html"
 749
 750	err = savehonk(&bonk)
 751	if err != nil {
 752		log.Printf("uh oh")
 753		return
 754	}
 755
 756	go honkworldwide(user, &bonk)
 757}
 758
 759func sendzonkofsorts(xonk *Honk, user *WhatAbout, what string) {
 760	zonk := Honk{
 761		What:     what,
 762		XID:      xonk.XID,
 763		Date:     time.Now().UTC(),
 764		Audience: oneofakind(xonk.Audience),
 765	}
 766	zonk.Public = !keepitquiet(zonk.Audience)
 767
 768	log.Printf("announcing %sed honk: %s", what, xonk.XID)
 769	go honkworldwide(user, &zonk)
 770}
 771
 772func zonkit(w http.ResponseWriter, r *http.Request) {
 773	wherefore := r.FormValue("wherefore")
 774	what := r.FormValue("what")
 775	userinfo := login.GetUserInfo(r)
 776	user, _ := butwhatabout(userinfo.Username)
 777
 778	if wherefore == "ack" {
 779		xonk := getxonk(userinfo.UserID, what)
 780		if xonk != nil {
 781			_, err := stmtUpdateFlags.Exec(flagIsAcked, xonk.ID)
 782			if err != nil {
 783				log.Printf("error acking: %s", err)
 784			}
 785			sendzonkofsorts(xonk, user, "ack")
 786		}
 787		return
 788	}
 789
 790	if wherefore == "deack" {
 791		xonk := getxonk(userinfo.UserID, what)
 792		if xonk != nil {
 793			_, err := stmtClearFlags.Exec(flagIsAcked, xonk.ID)
 794			if err != nil {
 795				log.Printf("error deacking: %s", err)
 796			}
 797			sendzonkofsorts(xonk, user, "deack")
 798		}
 799		return
 800	}
 801
 802	if wherefore == "unbonk" {
 803		xonk := getbonk(userinfo.UserID, what)
 804		if xonk != nil {
 805			deletehonk(xonk.ID)
 806			xonk = getxonk(userinfo.UserID, what)
 807			_, err := stmtClearFlags.Exec(flagIsBonked, xonk.ID)
 808			if err != nil {
 809				log.Printf("error unbonking: %s", err)
 810			}
 811			sendzonkofsorts(xonk, user, "unbonk")
 812		}
 813		return
 814	}
 815
 816	log.Printf("zonking %s %s", wherefore, what)
 817	if wherefore == "zonk" {
 818		xonk := getxonk(userinfo.UserID, what)
 819		if xonk != nil {
 820			deletehonk(xonk.ID)
 821			if xonk.Whofore == 2 || xonk.Whofore == 3 {
 822				sendzonkofsorts(xonk, user, "zonk")
 823			}
 824		}
 825	}
 826	_, err := stmtSaveZonker.Exec(userinfo.UserID, what, wherefore)
 827	if err != nil {
 828		log.Printf("error saving zonker: %s", err)
 829		return
 830	}
 831}
 832
 833func edithonkpage(w http.ResponseWriter, r *http.Request) {
 834	u := login.GetUserInfo(r)
 835	user, _ := butwhatabout(u.Username)
 836	xid := r.FormValue("xid")
 837	honk := getxonk(u.UserID, xid)
 838	if honk == nil || honk.Honker != user.URL || honk.What != "honk" {
 839		log.Printf("no edit")
 840		return
 841	}
 842
 843	honks := []*Honk{honk}
 844	reverbolate(u.UserID, honks)
 845	templinfo := getInfo(r)
 846	templinfo["HonkCSRF"] = login.GetCSRF("honkhonk", r)
 847	templinfo["Honks"] = honks
 848	templinfo["Noise"] = honk.Noise
 849	templinfo["ServerMessage"] = "honk edit"
 850	templinfo["UpdateXID"] = honk.XID
 851	err := readviews.Execute(w, "honkpage.html", templinfo)
 852	if err != nil {
 853		log.Print(err)
 854	}
 855}
 856
 857func submithonk(w http.ResponseWriter, r *http.Request) {
 858	rid := r.FormValue("rid")
 859	noise := r.FormValue("noise")
 860
 861	userinfo := login.GetUserInfo(r)
 862	user, _ := butwhatabout(userinfo.Username)
 863
 864	dt := time.Now().UTC()
 865	updatexid := r.FormValue("updatexid")
 866	var honk *Honk
 867	if updatexid != "" {
 868		honk = getxonk(userinfo.UserID, updatexid)
 869		if honk == nil || honk.Honker != user.URL || honk.What != "honk" {
 870			log.Printf("not saving edit")
 871			return
 872		}
 873		honk.Date = dt
 874		honk.What = "update"
 875	} else {
 876		xid := fmt.Sprintf("%s/%s/%s", user.URL, honkSep, xfiltrate())
 877		what := "honk"
 878		if rid != "" {
 879			what = "tonk"
 880		}
 881		honk = &Honk{
 882			UserID:   userinfo.UserID,
 883			Username: userinfo.Username,
 884			What:     what,
 885			Honker:   user.URL,
 886			XID:      xid,
 887			Date:     dt,
 888		}
 889	}
 890	if strings.HasPrefix(noise, "DZ:") {
 891		idx := strings.Index(noise, "\n")
 892		if idx == -1 {
 893			honk.Precis = noise
 894			noise = ""
 895		} else {
 896			honk.Precis = noise[:idx]
 897			noise = noise[idx+1:]
 898		}
 899	}
 900	noise = quickrename(noise, userinfo.UserID)
 901	noise = hooterize(noise)
 902	noise = strings.TrimSpace(noise)
 903	honk.Precis = strings.TrimSpace(honk.Precis)
 904
 905	var convoy string
 906	if rid != "" {
 907		xonk := getxonk(userinfo.UserID, rid)
 908		if xonk != nil {
 909			if xonk.Public {
 910				honk.Audience = append(honk.Audience, xonk.Audience...)
 911			}
 912			convoy = xonk.Convoy
 913		} else {
 914			xonkaud, c := whosthere(rid)
 915			honk.Audience = append(honk.Audience, xonkaud...)
 916			convoy = c
 917		}
 918		for i, a := range honk.Audience {
 919			if a == thewholeworld {
 920				honk.Audience[0], honk.Audience[i] = honk.Audience[i], honk.Audience[0]
 921				break
 922			}
 923		}
 924		honk.RID = rid
 925	} else {
 926		honk.Audience = []string{thewholeworld}
 927	}
 928	if noise != "" && noise[0] == '@' {
 929		honk.Audience = append(grapevine(noise), honk.Audience...)
 930	} else {
 931		honk.Audience = append(honk.Audience, grapevine(noise)...)
 932	}
 933	if convoy == "" {
 934		convoy = "data:,electrichonkytonk-" + xfiltrate()
 935	}
 936	butnottooloud(honk.Audience)
 937	honk.Audience = oneofakind(honk.Audience)
 938	if len(honk.Audience) == 0 {
 939		log.Printf("honk to nowhere")
 940		http.Error(w, "honk to nowhere...", http.StatusNotFound)
 941		return
 942	}
 943	honk.Public = !keepitquiet(honk.Audience)
 944	noise = obfusbreak(noise)
 945	honk.Noise = noise
 946	honk.Convoy = convoy
 947
 948	donkxid := r.FormValue("donkxid")
 949	if donkxid == "" {
 950		file, filehdr, err := r.FormFile("donk")
 951		if err == nil {
 952			var buf bytes.Buffer
 953			io.Copy(&buf, file)
 954			file.Close()
 955			data := buf.Bytes()
 956			xid := xfiltrate()
 957			var media, name string
 958			img, err := image.Vacuum(&buf, image.Params{MaxWidth: 2048, MaxHeight: 2048})
 959			if err == nil {
 960				data = img.Data
 961				format := img.Format
 962				media = "image/" + format
 963				if format == "jpeg" {
 964					format = "jpg"
 965				}
 966				name = xid + "." + format
 967				xid = name
 968			} else {
 969				maxsize := 100000
 970				if len(data) > maxsize {
 971					log.Printf("bad image: %s too much text: %d", err, len(data))
 972					http.Error(w, "didn't like your attachment", http.StatusUnsupportedMediaType)
 973					return
 974				}
 975				for i := 0; i < len(data); i++ {
 976					if data[i] < 32 && data[i] != '\t' && data[i] != '\r' && data[i] != '\n' {
 977						log.Printf("bad image: %s not text: %d", err, data[i])
 978						http.Error(w, "didn't like your attachment", http.StatusUnsupportedMediaType)
 979						return
 980					}
 981				}
 982				media = "text/plain"
 983				name = filehdr.Filename
 984				if name == "" {
 985					name = xid + ".txt"
 986				}
 987				xid += ".txt"
 988			}
 989			desc := r.FormValue("donkdesc")
 990			if desc == "" {
 991				desc = name
 992			}
 993			url := fmt.Sprintf("https://%s/d/%s", serverName, xid)
 994			res, err := stmtSaveFile.Exec(xid, name, desc, url, media, 1, data)
 995			if err != nil {
 996				log.Printf("unable to save image: %s", err)
 997				return
 998			}
 999			var d Donk
1000			d.FileID, _ = res.LastInsertId()
1001			honk.Donks = append(honk.Donks, &d)
1002			donkxid = d.XID
1003		}
1004	} else {
1005		xid := donkxid
1006		url := fmt.Sprintf("https://%s/d/%s", serverName, xid)
1007		var donk Donk
1008		row := stmtFindFile.QueryRow(url)
1009		err := row.Scan(&donk.FileID)
1010		if err == nil {
1011			honk.Donks = append(honk.Donks, &donk)
1012		} else {
1013			log.Printf("can't find file: %s", xid)
1014		}
1015	}
1016	herd := herdofemus(honk.Noise)
1017	for _, e := range herd {
1018		donk := savedonk(e.ID, e.Name, e.Name, "image/png", true)
1019		if donk != nil {
1020			donk.Name = e.Name
1021			honk.Donks = append(honk.Donks, donk)
1022		}
1023	}
1024	memetize(honk)
1025
1026	if honk.Public {
1027		honk.Whofore = 2
1028	} else {
1029		honk.Whofore = 3
1030	}
1031	if r.FormValue("preview") == "preview" {
1032		honks := []*Honk{honk}
1033		reverbolate(userinfo.UserID, honks)
1034		templinfo := getInfo(r)
1035		templinfo["HonkCSRF"] = login.GetCSRF("honkhonk", r)
1036		templinfo["Honks"] = honks
1037		templinfo["InReplyTo"] = r.FormValue("rid")
1038		templinfo["Noise"] = r.FormValue("noise")
1039		templinfo["SavedFile"] = donkxid
1040		templinfo["ServerMessage"] = "honk preview"
1041		err := readviews.Execute(w, "honkpage.html", templinfo)
1042		if err != nil {
1043			log.Print(err)
1044		}
1045		return
1046	}
1047	honk.Onts = oneofakind(ontologies(honk.Noise))
1048	honk.UserID = userinfo.UserID
1049	honk.RID = rid
1050	honk.Date = dt
1051	honk.Convoy = convoy
1052	honk.Format = "html"
1053
1054	if updatexid != "" {
1055		updatehonk(honk)
1056
1057	} else {
1058
1059		err := savehonk(honk)
1060		if err != nil {
1061			log.Printf("uh oh")
1062			return
1063		}
1064	}
1065
1066	// reload for consistency
1067	honk.Donks = nil
1068	donksforhonks([]*Honk{honk})
1069
1070	go honkworldwide(user, honk)
1071
1072	http.Redirect(w, r, honk.XID, http.StatusSeeOther)
1073}
1074
1075func showhonkers(w http.ResponseWriter, r *http.Request) {
1076	userinfo := login.GetUserInfo(r)
1077	templinfo := getInfo(r)
1078	templinfo["Honkers"] = gethonkers(userinfo.UserID)
1079	templinfo["HonkerCSRF"] = login.GetCSRF("submithonker", r)
1080	err := readviews.Execute(w, "honkers.html", templinfo)
1081	if err != nil {
1082		log.Print(err)
1083	}
1084}
1085
1086func showcombos(w http.ResponseWriter, r *http.Request) {
1087	userinfo := login.GetUserInfo(r)
1088	templinfo := getInfo(r)
1089	honkers := gethonkers(userinfo.UserID)
1090	var combos []string
1091	for _, h := range honkers {
1092		combos = append(combos, h.Combos...)
1093	}
1094	for i, c := range combos {
1095		if c == "-" {
1096			combos[i] = ""
1097		}
1098	}
1099	combos = oneofakind(combos)
1100	sort.Strings(combos)
1101	templinfo["Combos"] = combos
1102	err := readviews.Execute(w, "combos.html", templinfo)
1103	if err != nil {
1104		log.Print(err)
1105	}
1106}
1107
1108func submithonker(w http.ResponseWriter, r *http.Request) {
1109	u := login.GetUserInfo(r)
1110	name := r.FormValue("name")
1111	url := r.FormValue("url")
1112	peep := r.FormValue("peep")
1113	combos := r.FormValue("combos")
1114	honkerid, _ := strconv.ParseInt(r.FormValue("honkerid"), 10, 0)
1115
1116	if honkerid > 0 {
1117		goodbye := r.FormValue("goodbye")
1118		if goodbye == "F" {
1119			db := opendatabase()
1120			row := db.QueryRow("select xid from honkers where honkerid = ? and userid = ?",
1121				honkerid, u.UserID)
1122			var xid string
1123			err := row.Scan(&xid)
1124			if err != nil {
1125				log.Printf("can't get honker xid: %s", err)
1126				return
1127			}
1128			log.Printf("unsubscribing from %s", xid)
1129			user, _ := butwhatabout(u.Username)
1130			go itakeitallback(user, xid)
1131			_, err = stmtUpdateFlavor.Exec("unsub", u.UserID, xid, "sub")
1132			if err != nil {
1133				log.Printf("error updating honker: %s", err)
1134				return
1135			}
1136
1137			http.Redirect(w, r, "/honkers", http.StatusSeeOther)
1138			return
1139		}
1140		combos = " " + strings.TrimSpace(combos) + " "
1141		_, err := stmtUpdateCombos.Exec(combos, honkerid, u.UserID)
1142		if err != nil {
1143			log.Printf("update honker err: %s", err)
1144			return
1145		}
1146		http.Redirect(w, r, "/honkers", http.StatusSeeOther)
1147	}
1148
1149	flavor := "presub"
1150	if peep == "peep" {
1151		flavor = "peep"
1152	}
1153	p, err := investigate(url)
1154	if err != nil {
1155		http.Error(w, "error investigating: "+err.Error(), http.StatusInternalServerError)
1156		log.Printf("failed to investigate honker")
1157		return
1158	}
1159	url = p.XID
1160	if name == "" {
1161		name = p.Handle
1162	}
1163	_, err = stmtSaveHonker.Exec(u.UserID, name, url, flavor, combos)
1164	if err != nil {
1165		log.Print(err)
1166		return
1167	}
1168	if flavor == "presub" {
1169		user, _ := butwhatabout(u.Username)
1170		go subsub(user, url)
1171	}
1172	http.Redirect(w, r, "/honkers", http.StatusSeeOther)
1173}
1174
1175func zonkzone(w http.ResponseWriter, r *http.Request) {
1176	userinfo := login.GetUserInfo(r)
1177	rows, err := stmtGetZonkers.Query(userinfo.UserID)
1178	if err != nil {
1179		log.Printf("err: %s", err)
1180		return
1181	}
1182	defer rows.Close()
1183	var zonkers []Zonker
1184	for rows.Next() {
1185		var z Zonker
1186		rows.Scan(&z.ID, &z.Name, &z.Wherefore)
1187		zonkers = append(zonkers, z)
1188	}
1189	sort.Slice(zonkers, func(i, j int) bool {
1190		w1 := zonkers[i].Wherefore
1191		w2 := zonkers[j].Wherefore
1192		if w1 == w2 {
1193			return zonkers[i].Name < zonkers[j].Name
1194		}
1195		if w1 == "zonvoy" {
1196			w1 = "zzzzzzz"
1197		}
1198		if w2 == "zonvoy" {
1199			w2 = "zzzzzzz"
1200		}
1201		return w1 < w2
1202	})
1203
1204	templinfo := getInfo(r)
1205	templinfo["Zonkers"] = zonkers
1206	templinfo["ZonkCSRF"] = login.GetCSRF("zonkzonk", r)
1207	err = readviews.Execute(w, "zonkers.html", templinfo)
1208	if err != nil {
1209		log.Print(err)
1210	}
1211}
1212
1213func zonkzonk(w http.ResponseWriter, r *http.Request) {
1214	userinfo := login.GetUserInfo(r)
1215	itsok := r.FormValue("itsok")
1216	if itsok == "iforgiveyou" {
1217		zonkerid, _ := strconv.ParseInt(r.FormValue("zonkerid"), 10, 0)
1218		db := opendatabase()
1219		db.Exec("delete from zonkers where userid = ? and zonkerid = ?",
1220			userinfo.UserID, zonkerid)
1221		bitethethumbs()
1222		http.Redirect(w, r, "/zonkzone", http.StatusSeeOther)
1223		return
1224	}
1225	wherefore := r.FormValue("wherefore")
1226	name := r.FormValue("name")
1227	if name == "" {
1228		return
1229	}
1230	switch wherefore {
1231	case "zonker":
1232	case "zomain":
1233	case "zonvoy":
1234	case "zord":
1235	case "zilence":
1236	default:
1237		return
1238	}
1239	db := opendatabase()
1240	db.Exec("insert into zonkers (userid, name, wherefore) values (?, ?, ?)",
1241		userinfo.UserID, name, wherefore)
1242	if wherefore == "zonker" || wherefore == "zomain" || wherefore == "zord" || wherefore == "zilence" {
1243		bitethethumbs()
1244	}
1245
1246	http.Redirect(w, r, "/zonkzone", http.StatusSeeOther)
1247}
1248
1249func accountpage(w http.ResponseWriter, r *http.Request) {
1250	u := login.GetUserInfo(r)
1251	user, _ := butwhatabout(u.Username)
1252	templinfo := getInfo(r)
1253	templinfo["UserCSRF"] = login.GetCSRF("saveuser", r)
1254	templinfo["LogoutCSRF"] = login.GetCSRF("logout", r)
1255	templinfo["User"] = user
1256	err := readviews.Execute(w, "account.html", templinfo)
1257	if err != nil {
1258		log.Print(err)
1259	}
1260}
1261
1262func dochpass(w http.ResponseWriter, r *http.Request) {
1263	err := login.ChangePassword(w, r)
1264	if err != nil {
1265		log.Printf("error changing password: %s", err)
1266	}
1267	http.Redirect(w, r, "/account", http.StatusSeeOther)
1268}
1269
1270func fingerlicker(w http.ResponseWriter, r *http.Request) {
1271	orig := r.FormValue("resource")
1272
1273	log.Printf("finger lick: %s", orig)
1274
1275	if strings.HasPrefix(orig, "acct:") {
1276		orig = orig[5:]
1277	}
1278
1279	name := orig
1280	idx := strings.LastIndexByte(name, '/')
1281	if idx != -1 {
1282		name = name[idx+1:]
1283		if fmt.Sprintf("https://%s/%s/%s", serverName, userSep, name) != orig {
1284			log.Printf("foreign request rejected")
1285			name = ""
1286		}
1287	} else {
1288		idx = strings.IndexByte(name, '@')
1289		if idx != -1 {
1290			name = name[:idx]
1291			if name+"@"+serverName != orig {
1292				log.Printf("foreign request rejected")
1293				name = ""
1294			}
1295		}
1296	}
1297	user, err := butwhatabout(name)
1298	if err != nil {
1299		http.NotFound(w, r)
1300		return
1301	}
1302
1303	j := junk.New()
1304	j["subject"] = fmt.Sprintf("acct:%s@%s", user.Name, serverName)
1305	j["aliases"] = []string{user.URL}
1306	var links []junk.Junk
1307	l := junk.New()
1308	l["rel"] = "self"
1309	l["type"] = `application/activity+json`
1310	l["href"] = user.URL
1311	links = append(links, l)
1312	j["links"] = links
1313
1314	w.Header().Set("Cache-Control", "max-age=3600")
1315	w.Header().Set("Content-Type", "application/jrd+json")
1316	j.Write(w)
1317}
1318
1319func somedays() string {
1320	secs := 432000 + notrand.Int63n(432000)
1321	return fmt.Sprintf("%d", secs)
1322}
1323
1324func avatate(w http.ResponseWriter, r *http.Request) {
1325	n := r.FormValue("a")
1326	a := avatar(n)
1327	w.Header().Set("Cache-Control", "max-age="+somedays())
1328	w.Write(a)
1329}
1330
1331func servecss(w http.ResponseWriter, r *http.Request) {
1332	data, _ := ioutil.ReadFile("views" + r.URL.Path)
1333	s := css.Process(string(data))
1334	w.Header().Set("Cache-Control", "max-age=7776000")
1335	w.Header().Set("Content-Type", "text/css; charset=utf-8")
1336	w.Write([]byte(s))
1337}
1338func servehtml(w http.ResponseWriter, r *http.Request) {
1339	templinfo := getInfo(r)
1340	err := readviews.Execute(w, r.URL.Path[1:]+".html", templinfo)
1341	if err != nil {
1342		log.Print(err)
1343	}
1344}
1345func serveemu(w http.ResponseWriter, r *http.Request) {
1346	xid := mux.Vars(r)["xid"]
1347	w.Header().Set("Cache-Control", "max-age="+somedays())
1348	http.ServeFile(w, r, "emus/"+xid)
1349}
1350func servememe(w http.ResponseWriter, r *http.Request) {
1351	xid := mux.Vars(r)["xid"]
1352	w.Header().Set("Cache-Control", "max-age="+somedays())
1353	http.ServeFile(w, r, "memes/"+xid)
1354}
1355
1356func servefile(w http.ResponseWriter, r *http.Request) {
1357	xid := mux.Vars(r)["xid"]
1358	row := stmtFileData.QueryRow(xid)
1359	var media string
1360	var data []byte
1361	err := row.Scan(&media, &data)
1362	if err != nil {
1363		log.Printf("error loading file: %s", err)
1364		http.NotFound(w, r)
1365		return
1366	}
1367	w.Header().Set("Content-Type", media)
1368	w.Header().Set("X-Content-Type-Options", "nosniff")
1369	w.Header().Set("Cache-Control", "max-age="+somedays())
1370	w.Write(data)
1371}
1372
1373func nomoroboto(w http.ResponseWriter, r *http.Request) {
1374	io.WriteString(w, "User-agent: *\n")
1375	io.WriteString(w, "Disallow: /a\n")
1376	io.WriteString(w, "Disallow: /d\n")
1377	io.WriteString(w, "Disallow: /meme\n")
1378	for _, u := range allusers() {
1379		fmt.Fprintf(w, "Disallow: /%s/%s/%s/\n", userSep, u.Username, honkSep)
1380	}
1381}
1382
1383func webhydra(w http.ResponseWriter, r *http.Request) {
1384	u := login.GetUserInfo(r)
1385	userid := u.UserID
1386	templinfo := getInfo(r)
1387	templinfo["HonkCSRF"] = login.GetCSRF("honkhonk", r)
1388	page := r.FormValue("page")
1389	var honks []*Honk
1390	switch page {
1391	case "atme":
1392		honks = gethonksforme(userid)
1393	case "home":
1394		honks = gethonksforuser(userid)
1395		honks = osmosis(honks, userid)
1396	case "convoy":
1397		c := r.FormValue("c")
1398		honks = gethonksbyconvoy(userid, c)
1399	default:
1400		http.NotFound(w, r)
1401	}
1402	if len(honks) > 0 {
1403		templinfo["TopXID"] = honks[0].XID
1404	}
1405	if topxid := r.FormValue("topxid"); topxid != "" {
1406		for i, h := range honks {
1407			if h.XID == topxid {
1408				honks = honks[0:i]
1409				break
1410			}
1411		}
1412		log.Printf("topxid %d frags", len(honks))
1413	}
1414	reverbolate(userid, honks)
1415	templinfo["Honks"] = honks
1416	w.Header().Set("Content-Type", "text/html; charset=utf-8")
1417	err := readviews.Execute(w, "honkfrags.html", templinfo)
1418	if err != nil {
1419		log.Printf("frag error: %s", err)
1420	}
1421}
1422
1423func serve() {
1424	db := opendatabase()
1425	login.Init(db)
1426
1427	listener, err := openListener()
1428	if err != nil {
1429		log.Fatal(err)
1430	}
1431	go redeliverator()
1432
1433	debug := false
1434	getconfig("debug", &debug)
1435	readviews = templates.Load(debug,
1436		"views/honkpage.html",
1437		"views/honkfrags.html",
1438		"views/honkers.html",
1439		"views/zonkers.html",
1440		"views/combos.html",
1441		"views/honkform.html",
1442		"views/honk.html",
1443		"views/account.html",
1444		"views/about.html",
1445		"views/funzone.html",
1446		"views/login.html",
1447		"views/xzone.html",
1448		"views/header.html",
1449		"views/onts.html",
1450		"views/honkpage.js",
1451	)
1452	if !debug {
1453		s := "views/style.css"
1454		savedstyleparams[s] = getstyleparam(s)
1455		s = "views/local.css"
1456		savedstyleparams[s] = getstyleparam(s)
1457	}
1458
1459	bitethethumbs()
1460
1461	mux := mux.NewRouter()
1462	mux.Use(login.Checker)
1463
1464	posters := mux.Methods("POST").Subrouter()
1465	getters := mux.Methods("GET").Subrouter()
1466
1467	getters.HandleFunc("/", homepage)
1468	getters.HandleFunc("/home", homepage)
1469	getters.HandleFunc("/front", homepage)
1470	getters.HandleFunc("/robots.txt", nomoroboto)
1471	getters.HandleFunc("/rss", showrss)
1472	getters.HandleFunc("/"+userSep+"/{name:[[:alnum:]]+}", showuser)
1473	getters.HandleFunc("/"+userSep+"/{name:[[:alnum:]]+}/"+honkSep+"/{xid:[[:alnum:]]+}", showhonk)
1474	getters.HandleFunc("/"+userSep+"/{name:[[:alnum:]]+}/rss", showrss)
1475	posters.HandleFunc("/"+userSep+"/{name:[[:alnum:]]+}/inbox", inbox)
1476	getters.HandleFunc("/"+userSep+"/{name:[[:alnum:]]+}/outbox", outbox)
1477	getters.HandleFunc("/"+userSep+"/{name:[[:alnum:]]+}/followers", emptiness)
1478	getters.HandleFunc("/"+userSep+"/{name:[[:alnum:]]+}/following", emptiness)
1479	getters.HandleFunc("/a", avatate)
1480	getters.HandleFunc("/o", thelistingoftheontologies)
1481	getters.HandleFunc("/o/{name:[a-z0-9-]+}", showontology)
1482	getters.HandleFunc("/d/{xid:[[:alnum:].]+}", servefile)
1483	getters.HandleFunc("/emu/{xid:[[:alnum:]_.-]+}", serveemu)
1484	getters.HandleFunc("/meme/{xid:[[:alnum:]_.-]+}", servememe)
1485	getters.HandleFunc("/.well-known/webfinger", fingerlicker)
1486
1487	getters.HandleFunc("/style.css", servecss)
1488	getters.HandleFunc("/local.css", servecss)
1489	getters.HandleFunc("/about", servehtml)
1490	getters.HandleFunc("/login", servehtml)
1491	posters.HandleFunc("/dologin", login.LoginFunc)
1492	getters.HandleFunc("/logout", login.LogoutFunc)
1493
1494	loggedin := mux.NewRoute().Subrouter()
1495	loggedin.Use(login.Required)
1496	loggedin.HandleFunc("/account", accountpage)
1497	loggedin.HandleFunc("/funzone", showfunzone)
1498	loggedin.HandleFunc("/chpass", dochpass)
1499	loggedin.HandleFunc("/atme", homepage)
1500	loggedin.HandleFunc("/zonkzone", zonkzone)
1501	loggedin.HandleFunc("/xzone", xzone)
1502	loggedin.HandleFunc("/edit", edithonkpage)
1503	loggedin.Handle("/honk", login.CSRFWrap("honkhonk", http.HandlerFunc(submithonk)))
1504	loggedin.Handle("/bonk", login.CSRFWrap("honkhonk", http.HandlerFunc(submitbonk)))
1505	loggedin.Handle("/zonkit", login.CSRFWrap("honkhonk", http.HandlerFunc(zonkit)))
1506	loggedin.Handle("/zonkzonk", login.CSRFWrap("zonkzonk", http.HandlerFunc(zonkzonk)))
1507	loggedin.Handle("/saveuser", login.CSRFWrap("saveuser", http.HandlerFunc(saveuser)))
1508	loggedin.Handle("/ximport", login.CSRFWrap("ximport", http.HandlerFunc(ximport)))
1509	loggedin.HandleFunc("/honkers", showhonkers)
1510	loggedin.HandleFunc("/h/{name:[[:alnum:]]+}", showhonker)
1511	loggedin.HandleFunc("/h", showhonker)
1512	loggedin.HandleFunc("/c/{name:[[:alnum:]]+}", showcombo)
1513	loggedin.HandleFunc("/c", showcombos)
1514	loggedin.HandleFunc("/t", showconvoy)
1515	loggedin.HandleFunc("/q", showsearch)
1516	loggedin.HandleFunc("/hydra", webhydra)
1517	loggedin.Handle("/submithonker", login.CSRFWrap("submithonker", http.HandlerFunc(submithonker)))
1518
1519	err = http.Serve(listener, mux)
1520	if err != nil {
1521		log.Fatal(err)
1522	}
1523}