all repos — honk @ 237b4896e7e0bca992b799c29bbd1c52204e4d1d

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