all repos — honk @ c822a66ae25ec3e1638b620a074d756c022f0906

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