all repos — honk @ a544d31fcf76a4c129010c412f9716645245daa1

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