all repos — honk @ 4f19ceba2f75702dfeea5ab1604eef55b4e2ddd1

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