all repos — honk @ 33044eb1a719c8f7f7cceef3625b50165df36d3a

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