all repos — honk @ 8020df1d08dffa2385f614b9f81f4eaffbb92077

my fork of honk

activity.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	"context"
  21	"crypto/tls"
  22	"database/sql"
  23	"fmt"
  24	"html"
  25	"io"
  26	notrand "math/rand"
  27	"net/http"
  28	"net/url"
  29	"os"
  30	"strings"
  31	"time"
  32
  33	"humungus.tedunangst.com/r/webs/cache"
  34	"humungus.tedunangst.com/r/webs/gate"
  35	"humungus.tedunangst.com/r/webs/httpsig"
  36	"humungus.tedunangst.com/r/webs/junk"
  37	"humungus.tedunangst.com/r/webs/templates"
  38)
  39
  40var theonetruename = `application/ld+json; profile="https://www.w3.org/ns/activitystreams"`
  41var thefakename = `application/activity+json`
  42var falsenames = []string{
  43	`application/ld+json`,
  44	`application/activity+json`,
  45}
  46var itiswhatitis = "https://www.w3.org/ns/activitystreams"
  47var thewholeworld = "https://www.w3.org/ns/activitystreams#Public"
  48
  49func friendorfoe(ct string) bool {
  50	ct = strings.ToLower(ct)
  51	for _, at := range falsenames {
  52		if strings.HasPrefix(ct, at) {
  53			return true
  54		}
  55	}
  56	return false
  57}
  58
  59var develClient = &http.Client{
  60	Transport: &http.Transport{
  61		TLSClientConfig: &tls.Config{
  62			InsecureSkipVerify: true,
  63		},
  64	},
  65}
  66
  67func PostJunk(keyname string, key httpsig.PrivateKey, url string, j junk.Junk) error {
  68	return PostMsg(keyname, key, url, j.ToBytes())
  69}
  70
  71func PostMsg(keyname string, key httpsig.PrivateKey, url string, msg []byte) error {
  72	client := http.DefaultClient
  73	if develMode {
  74		client = develClient
  75	}
  76	req, err := http.NewRequest("POST", url, bytes.NewReader(msg))
  77	if err != nil {
  78		return err
  79	}
  80	req.Header.Set("User-Agent", "honksnonk/5.0; "+serverName)
  81	req.Header.Set("Content-Type", theonetruename)
  82	httpsig.SignRequest(keyname, key, req, msg)
  83	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
  84	defer cancel()
  85	req = req.WithContext(ctx)
  86	resp, err := client.Do(req)
  87	if err != nil {
  88		return err
  89	}
  90	resp.Body.Close()
  91	switch resp.StatusCode {
  92	case 200:
  93	case 201:
  94	case 202:
  95	default:
  96		return fmt.Errorf("http post status: %d", resp.StatusCode)
  97	}
  98	ilog.Printf("successful post: %s %d", url, resp.StatusCode)
  99	return nil
 100}
 101
 102func GetJunk(url string) (junk.Junk, error) {
 103	return GetJunkTimeout(url, 30*time.Second)
 104}
 105
 106func GetJunkFast(url string) (junk.Junk, error) {
 107	return GetJunkTimeout(url, 5*time.Second)
 108}
 109
 110func GetJunkHardMode(url string) (junk.Junk, error) {
 111	j, err := GetJunk(url)
 112	if err != nil {
 113		emsg := err.Error()
 114		if emsg == "http get status: 502" || strings.Contains(emsg, "timeout") {
 115			ilog.Printf("trying again after error: %s", emsg)
 116			time.Sleep(time.Duration(60+notrand.Int63n(60)) * time.Second)
 117			j, err = GetJunk(url)
 118			if err != nil {
 119				ilog.Printf("still couldn't get it")
 120			} else {
 121				ilog.Printf("retry success!")
 122			}
 123		}
 124	}
 125	return j, err
 126}
 127
 128var flightdeck = gate.NewSerializer()
 129
 130func GetJunkTimeout(url string, timeout time.Duration) (junk.Junk, error) {
 131	client := http.DefaultClient
 132	if develMode {
 133		client = develClient
 134	}
 135	fn := func() (interface{}, error) {
 136		at := thefakename
 137		if strings.Contains(url, ".well-known/webfinger?resource") {
 138			at = "application/jrd+json"
 139		}
 140		j, err := junk.Get(url, junk.GetArgs{
 141			Accept:  at,
 142			Agent:   "honksnonk/5.0; " + serverName,
 143			Timeout: timeout,
 144			Client:  client,
 145		})
 146		return j, err
 147	}
 148
 149	ji, err := flightdeck.Call(url, fn)
 150	if err != nil {
 151		return nil, err
 152	}
 153	j := ji.(junk.Junk)
 154	return j, nil
 155}
 156
 157func fetchsome(url string) ([]byte, error) {
 158	client := http.DefaultClient
 159	if develMode {
 160		client = develClient
 161	}
 162	req, err := http.NewRequest("GET", url, nil)
 163	if err != nil {
 164		ilog.Printf("error fetching %s: %s", url, err)
 165		return nil, err
 166	}
 167	req.Header.Set("User-Agent", "honksnonk/5.0; "+serverName)
 168	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
 169	defer cancel()
 170	req = req.WithContext(ctx)
 171	resp, err := client.Do(req)
 172	if err != nil {
 173		ilog.Printf("error fetching %s: %s", url, err)
 174		return nil, err
 175	}
 176	defer resp.Body.Close()
 177	switch resp.StatusCode {
 178	case 200:
 179	case 201:
 180	case 202:
 181	default:
 182		return nil, fmt.Errorf("http get not 200: %d %s", resp.StatusCode, url)
 183	}
 184	var buf bytes.Buffer
 185	limiter := io.LimitReader(resp.Body, 10*1024*1024)
 186	io.Copy(&buf, limiter)
 187	return buf.Bytes(), nil
 188}
 189
 190func savedonk(url string, name, desc, media string, localize bool) *Donk {
 191	if url == "" {
 192		return nil
 193	}
 194	if donk := finddonk(url); donk != nil {
 195		return donk
 196	}
 197	ilog.Printf("saving donk: %s", url)
 198	data := []byte{}
 199	if localize {
 200		fn := func() (interface{}, error) {
 201			return fetchsome(url)
 202		}
 203		ii, err := flightdeck.Call(url, fn)
 204		if err != nil {
 205			ilog.Printf("error fetching donk: %s", err)
 206			localize = false
 207			goto saveit
 208		}
 209		data = ii.([]byte)
 210
 211		if len(data) == 10*1024*1024 {
 212			ilog.Printf("truncation likely")
 213		}
 214		if strings.HasPrefix(media, "image") {
 215			img, err := shrinkit(data)
 216			if err != nil {
 217				ilog.Printf("unable to decode image: %s", err)
 218				localize = false
 219				data = []byte{}
 220				goto saveit
 221			}
 222			data = img.Data
 223			media = "image/" + img.Format
 224		} else if media == "application/pdf" {
 225			if len(data) > 1000000 {
 226				ilog.Printf("not saving large pdf")
 227				localize = false
 228				data = []byte{}
 229			}
 230		} else if len(data) > 100000 {
 231			ilog.Printf("not saving large attachment")
 232			localize = false
 233			data = []byte{}
 234		}
 235	}
 236saveit:
 237	fileid, err := savefile(name, desc, url, media, localize, data)
 238	if err != nil {
 239		elog.Printf("error saving file %s: %s", url, err)
 240		return nil
 241	}
 242	donk := new(Donk)
 243	donk.FileID = fileid
 244	return donk
 245}
 246
 247func iszonked(userid int64, xid string) bool {
 248	var id int64
 249	row := stmtFindZonk.QueryRow(userid, xid)
 250	err := row.Scan(&id)
 251	if err == nil {
 252		return true
 253	}
 254	if err != sql.ErrNoRows {
 255		ilog.Printf("error querying zonk: %s", err)
 256	}
 257	return false
 258}
 259
 260func needxonk(user *WhatAbout, x *Honk) bool {
 261	if rejectxonk(x) {
 262		return false
 263	}
 264	return needxonkid(user, x.XID)
 265}
 266func needbonkid(user *WhatAbout, xid string) bool {
 267	return needxonkidX(user, xid, true)
 268}
 269func needxonkid(user *WhatAbout, xid string) bool {
 270	return needxonkidX(user, xid, false)
 271}
 272func needxonkidX(user *WhatAbout, xid string, isannounce bool) bool {
 273	if !strings.HasPrefix(xid, "https://") {
 274		return false
 275	}
 276	if strings.HasPrefix(xid, user.URL+"/") {
 277		return false
 278	}
 279	if rejectorigin(user.ID, xid, isannounce) {
 280		ilog.Printf("rejecting origin: %s", xid)
 281		return false
 282	}
 283	if iszonked(user.ID, xid) {
 284		ilog.Printf("already zonked: %s", xid)
 285		return false
 286	}
 287	var id int64
 288	row := stmtFindXonk.QueryRow(user.ID, xid)
 289	err := row.Scan(&id)
 290	if err == nil {
 291		return false
 292	}
 293	if err != sql.ErrNoRows {
 294		ilog.Printf("error querying xonk: %s", err)
 295	}
 296	return true
 297}
 298
 299func eradicatexonk(userid int64, xid string) {
 300	xonk := getxonk(userid, xid)
 301	if xonk != nil {
 302		deletehonk(xonk.ID)
 303	}
 304	_, err := stmtSaveZonker.Exec(userid, xid, "zonk")
 305	if err != nil {
 306		elog.Printf("error eradicating: %s", err)
 307	}
 308}
 309
 310func savexonk(x *Honk) {
 311	ilog.Printf("saving xonk: %s", x.XID)
 312	go handles(x.Honker)
 313	go handles(x.Oonker)
 314	savehonk(x)
 315}
 316
 317type Box struct {
 318	In     string
 319	Out    string
 320	Shared string
 321}
 322
 323var boxofboxes = cache.New(cache.Options{Filler: func(ident string) (*Box, bool) {
 324	var info string
 325	row := stmtGetXonker.QueryRow(ident, "boxes")
 326	err := row.Scan(&info)
 327	if err != nil {
 328		dlog.Printf("need to get boxes for %s", ident)
 329		var j junk.Junk
 330		j, err = GetJunk(ident)
 331		if err != nil {
 332			dlog.Printf("error getting boxes: %s", err)
 333			return nil, false
 334		}
 335		allinjest(originate(ident), j)
 336		row = stmtGetXonker.QueryRow(ident, "boxes")
 337		err = row.Scan(&info)
 338	}
 339	if err == nil {
 340		m := strings.Split(info, " ")
 341		b := &Box{In: m[0], Out: m[1], Shared: m[2]}
 342		return b, true
 343	}
 344	return nil, false
 345}})
 346
 347func gimmexonks(user *WhatAbout, outbox string) {
 348	dlog.Printf("getting outbox: %s", outbox)
 349	j, err := GetJunk(outbox)
 350	if err != nil {
 351		ilog.Printf("error getting outbox: %s", err)
 352		return
 353	}
 354	t, _ := j.GetString("type")
 355	origin := originate(outbox)
 356	if t == "OrderedCollection" {
 357		items, _ := j.GetArray("orderedItems")
 358		if items == nil {
 359			items, _ = j.GetArray("items")
 360		}
 361		if items == nil {
 362			obj, ok := j.GetMap("first")
 363			if ok {
 364				items, _ = obj.GetArray("orderedItems")
 365			} else {
 366				page1, ok := j.GetString("first")
 367				if ok {
 368					j, err = GetJunk(page1)
 369					if err != nil {
 370						ilog.Printf("error gettings page1: %s", err)
 371						return
 372					}
 373					items, _ = j.GetArray("orderedItems")
 374				}
 375			}
 376		}
 377		if len(items) > 20 {
 378			items = items[0:20]
 379		}
 380		for i, j := 0, len(items)-1; i < j; i, j = i+1, j-1 {
 381			items[i], items[j] = items[j], items[i]
 382		}
 383		for _, item := range items {
 384			obj, ok := item.(junk.Junk)
 385			if ok {
 386				xonksaver(user, obj, origin)
 387				continue
 388			}
 389			xid, ok := item.(string)
 390			if ok {
 391				if !needxonkid(user, xid) {
 392					continue
 393				}
 394				obj, err = GetJunk(xid)
 395				if err != nil {
 396					ilog.Printf("error getting item: %s", err)
 397					continue
 398				}
 399				xonksaver(user, obj, originate(xid))
 400			}
 401		}
 402	}
 403}
 404
 405func newphone(a []string, obj junk.Junk) []string {
 406	for _, addr := range []string{"to", "cc", "attributedTo"} {
 407		who, _ := obj.GetString(addr)
 408		if who != "" {
 409			a = append(a, who)
 410		}
 411		whos, _ := obj.GetArray(addr)
 412		for _, w := range whos {
 413			who, _ := w.(string)
 414			if who != "" {
 415				a = append(a, who)
 416			}
 417		}
 418	}
 419	return a
 420}
 421
 422func extractattrto(obj junk.Junk) string {
 423	who, _ := obj.GetString("attributedTo")
 424	if who != "" {
 425		return who
 426	}
 427	o, ok := obj.GetMap("attributedTo")
 428	if ok {
 429		id, ok := o.GetString("id")
 430		if ok {
 431			return id
 432		}
 433	}
 434	arr, _ := obj.GetArray("attributedTo")
 435	for _, a := range arr {
 436		o, ok := a.(junk.Junk)
 437		if ok {
 438			t, _ := o.GetString("type")
 439			id, _ := o.GetString("id")
 440			if t == "Person" || t == "" {
 441				return id
 442			}
 443		}
 444		s, ok := a.(string)
 445		if ok {
 446			return s
 447		}
 448	}
 449	return ""
 450}
 451
 452func firstofmany(obj junk.Junk, key string) string {
 453	if val, _ := obj.GetString(key); val != "" {
 454		return val
 455	}
 456	if arr, _ := obj.GetArray(key); len(arr) > 0 {
 457		val, ok := arr[0].(string)
 458		if ok {
 459			return val
 460		}
 461	}
 462	return ""
 463}
 464
 465func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk {
 466	depth := 0
 467	maxdepth := 10
 468	currenttid := ""
 469	goingup := 0
 470	var xonkxonkfn func(item junk.Junk, origin string, isUpdate bool) *Honk
 471
 472	saveonemore := func(xid string) {
 473		dlog.Printf("getting onemore: %s", xid)
 474		if depth >= maxdepth {
 475			ilog.Printf("in too deep")
 476			return
 477		}
 478		obj, err := GetJunkHardMode(xid)
 479		if err != nil {
 480			ilog.Printf("error getting onemore: %s: %s", xid, err)
 481			return
 482		}
 483		depth++
 484		xonkxonkfn(obj, originate(xid), false)
 485		depth--
 486	}
 487
 488	xonkxonkfn = func(item junk.Junk, origin string, isUpdate bool) *Honk {
 489		id, _ := item.GetString("id")
 490		what := firstofmany(item, "type")
 491		dt, ok := item.GetString("published")
 492		if !ok {
 493			dt = time.Now().Format(time.RFC3339)
 494		}
 495
 496		var err error
 497		var xid, rid, url, convoy string
 498		var replies []string
 499		var obj junk.Junk
 500		switch what {
 501		case "Delete":
 502			obj, ok = item.GetMap("object")
 503			if ok {
 504				xid, _ = obj.GetString("id")
 505			} else {
 506				xid, _ = item.GetString("object")
 507			}
 508			if xid == "" {
 509				return nil
 510			}
 511			if originate(xid) != origin {
 512				ilog.Printf("forged delete: %s", xid)
 513				return nil
 514			}
 515			ilog.Printf("eradicating %s", xid)
 516			eradicatexonk(user.ID, xid)
 517			return nil
 518		case "Remove":
 519			xid, _ = item.GetString("object")
 520			targ, _ := obj.GetString("target")
 521			ilog.Printf("remove %s from %s", obj, targ)
 522			return nil
 523		case "Tombstone":
 524			xid, _ = item.GetString("id")
 525			if xid == "" {
 526				return nil
 527			}
 528			if originate(xid) != origin {
 529				ilog.Printf("forged delete: %s", xid)
 530				return nil
 531			}
 532			ilog.Printf("eradicating %s", xid)
 533			eradicatexonk(user.ID, xid)
 534			return nil
 535		case "Announce":
 536			obj, ok = item.GetMap("object")
 537			if ok {
 538				xid, _ = obj.GetString("id")
 539			} else {
 540				xid, _ = item.GetString("object")
 541			}
 542			if !needbonkid(user, xid) {
 543				return nil
 544			}
 545			dlog.Printf("getting bonk: %s", xid)
 546			obj, err = GetJunkHardMode(xid)
 547			if err != nil {
 548				ilog.Printf("error getting bonk: %s: %s", xid, err)
 549			}
 550			origin = originate(xid)
 551			what = "bonk"
 552		case "Update":
 553			isUpdate = true
 554			fallthrough
 555		case "Create":
 556			obj, ok = item.GetMap("object")
 557			if !ok {
 558				xid, _ = item.GetString("object")
 559				dlog.Printf("getting created honk: %s", xid)
 560				if originate(xid) != origin {
 561					ilog.Printf("out of bounds %s not from %s", xid, origin)
 562					return nil
 563				}
 564				obj, err = GetJunkHardMode(xid)
 565				if err != nil {
 566					ilog.Printf("error getting creation: %s", err)
 567				}
 568			}
 569			if obj == nil {
 570				ilog.Printf("no object for creation %s", id)
 571				return nil
 572			}
 573			return xonkxonkfn(obj, origin, isUpdate)
 574		case "Read":
 575			xid, ok = item.GetString("object")
 576			if ok {
 577				if !needxonkid(user, xid) {
 578					dlog.Printf("don't need read obj: %s", xid)
 579					return nil
 580				}
 581				obj, err = GetJunkHardMode(xid)
 582				if err != nil {
 583					ilog.Printf("error getting read: %s", err)
 584					return nil
 585				}
 586				return xonkxonkfn(obj, originate(xid), false)
 587			}
 588			return nil
 589		case "Add":
 590			xid, ok = item.GetString("object")
 591			if ok {
 592				// check target...
 593				if !needxonkid(user, xid) {
 594					dlog.Printf("don't need added obj: %s", xid)
 595					return nil
 596				}
 597				obj, err = GetJunkHardMode(xid)
 598				if err != nil {
 599					ilog.Printf("error getting add: %s", err)
 600					return nil
 601				}
 602				return xonkxonkfn(obj, originate(xid), false)
 603			}
 604			return nil
 605		case "Move":
 606			obj = item
 607			what = "move"
 608		case "GuessWord": // dealt with below
 609			fallthrough
 610		case "Audio":
 611			fallthrough
 612		case "Image":
 613			fallthrough
 614		case "Video":
 615			fallthrough
 616		case "Question":
 617			fallthrough
 618		case "Note":
 619			fallthrough
 620		case "Article":
 621			fallthrough
 622		case "Page":
 623			obj = item
 624			what = "honk"
 625		case "Event":
 626			obj = item
 627			what = "event"
 628		case "ChatMessage":
 629			obj = item
 630			what = "chonk"
 631		default:
 632			ilog.Printf("unknown activity: %s", what)
 633			dumpactivity(item)
 634			return nil
 635		}
 636
 637		if obj != nil {
 638			xid, _ = obj.GetString("id")
 639		}
 640
 641		if xid == "" {
 642			ilog.Printf("don't know what xid is")
 643			item.Write(ilog.Writer())
 644			return nil
 645		}
 646		if originate(xid) != origin {
 647			ilog.Printf("original sin: %s not from %s", xid, origin)
 648			item.Write(ilog.Writer())
 649			return nil
 650		}
 651
 652		var xonk Honk
 653		// early init
 654		xonk.XID = xid
 655		xonk.UserID = user.ID
 656		xonk.Honker, _ = item.GetString("actor")
 657		if xonk.Honker == "" {
 658			xonk.Honker, _ = item.GetString("attributedTo")
 659		}
 660		if obj != nil {
 661			if xonk.Honker == "" {
 662				xonk.Honker = extractattrto(obj)
 663			}
 664			xonk.Oonker = extractattrto(obj)
 665			if xonk.Oonker == xonk.Honker {
 666				xonk.Oonker = ""
 667			}
 668			xonk.Audience = newphone(nil, obj)
 669		}
 670		xonk.Audience = append(xonk.Audience, xonk.Honker)
 671		xonk.Audience = oneofakind(xonk.Audience)
 672
 673		var mentions []Mention
 674		if obj != nil {
 675			ot, _ := obj.GetString("type")
 676			url, _ = obj.GetString("url")
 677			if dt2, ok := obj.GetString("published"); ok {
 678				dt = dt2
 679			}
 680			content, _ := obj.GetString("content")
 681			if !strings.HasPrefix(content, "<p>") {
 682				content = "<p>" + content
 683			}
 684			precis, _ := obj.GetString("summary")
 685			if name, ok := obj.GetString("name"); ok {
 686				if precis != "" {
 687					content = precis + "<p>" + content
 688				}
 689				precis = html.EscapeString(name)
 690			}
 691			if sens, _ := obj["sensitive"].(bool); sens && precis == "" {
 692				precis = "unspecified horror"
 693			}
 694			rid, ok = obj.GetString("inReplyTo")
 695			if !ok {
 696				if robj, ok := obj.GetMap("inReplyTo"); ok {
 697					rid, _ = robj.GetString("id")
 698				}
 699			}
 700			convoy, _ = obj.GetString("context")
 701			if convoy == "" {
 702				convoy, _ = obj.GetString("conversation")
 703			}
 704			if ot == "Question" {
 705				if what == "honk" {
 706					what = "qonk"
 707				}
 708				content += "<ul>"
 709				ans, _ := obj.GetArray("oneOf")
 710				for _, ai := range ans {
 711					a, ok := ai.(junk.Junk)
 712					if !ok {
 713						continue
 714					}
 715					as, _ := a.GetString("name")
 716					content += "<li>" + as
 717				}
 718				ans, _ = obj.GetArray("anyOf")
 719				for _, ai := range ans {
 720					a, ok := ai.(junk.Junk)
 721					if !ok {
 722						continue
 723					}
 724					as, _ := a.GetString("name")
 725					content += "<li>" + as
 726				}
 727				content += "</ul>"
 728			}
 729			if ot == "Move" {
 730				targ, _ := obj.GetString("target")
 731				content += string(templates.Sprintf(`<p>Moved to <a href="%s">%s</a>`, targ, targ))
 732			}
 733			if ot == "GuessWord" {
 734				what = "wonk"
 735				content, _ = obj.GetString("content")
 736				xonk.Wonkles, _ = obj.GetString("wordlist")
 737				go savewonkles(xonk.Wonkles)
 738			}
 739			if what == "honk" && rid != "" {
 740				what = "tonk"
 741			}
 742			if len(content) > 90001 {
 743				ilog.Printf("content too long. truncating")
 744				content = content[:90001]
 745			}
 746
 747			xonk.Noise = content
 748			xonk.Precis = precis
 749			if rejectxonk(&xonk) {
 750				dlog.Printf("fast reject: %s", xid)
 751				return nil
 752			}
 753
 754			numatts := 0
 755			procatt := func(att junk.Junk) {
 756				at, _ := att.GetString("type")
 757				mt, _ := att.GetString("mediaType")
 758				u, ok := att.GetString("url")
 759				if !ok {
 760					if ua, ok := att.GetArray("url"); ok && len(ua) > 0 {
 761						u, ok = ua[0].(string)
 762						if !ok {
 763							if uu, ok := ua[0].(junk.Junk); ok {
 764								u, _ = uu.GetString("href")
 765								if mt == "" {
 766									mt, _ = uu.GetString("mediaType")
 767								}
 768							}
 769						}
 770					} else if uu, ok := att.GetMap("url"); ok {
 771						u, _ = uu.GetString("href")
 772						if mt == "" {
 773							mt, _ = uu.GetString("mediaType")
 774						}
 775					}
 776				}
 777				name, _ := att.GetString("name")
 778				desc, _ := att.GetString("summary")
 779				desc = html.UnescapeString(desc)
 780				if desc == "" {
 781					desc = name
 782				}
 783				localize := false
 784				if numatts > 4 {
 785					ilog.Printf("excessive attachment: %s", at)
 786				} else if at == "Document" || at == "Image" {
 787					mt = strings.ToLower(mt)
 788					dlog.Printf("attachment: %s %s", mt, u)
 789					if mt == "text/plain" || mt == "application/pdf" ||
 790						strings.HasPrefix(mt, "image") {
 791						localize = true
 792					}
 793				} else {
 794					ilog.Printf("unknown attachment: %s", at)
 795				}
 796				if skipMedia(&xonk) {
 797					localize = false
 798				}
 799				donk := savedonk(u, name, desc, mt, localize)
 800				if donk != nil {
 801					xonk.Donks = append(xonk.Donks, donk)
 802				}
 803				numatts++
 804			}
 805			atts, _ := obj.GetArray("attachment")
 806			for _, atti := range atts {
 807				att, ok := atti.(junk.Junk)
 808				if !ok {
 809					ilog.Printf("attachment that wasn't map?")
 810					continue
 811				}
 812				procatt(att)
 813			}
 814			if att, ok := obj.GetMap("attachment"); ok {
 815				procatt(att)
 816			}
 817			tags, _ := obj.GetArray("tag")
 818			for _, tagi := range tags {
 819				tag, ok := tagi.(junk.Junk)
 820				if !ok {
 821					continue
 822				}
 823				tt, _ := tag.GetString("type")
 824				name, _ := tag.GetString("name")
 825				desc, _ := tag.GetString("summary")
 826				desc = html.UnescapeString(desc)
 827				if desc == "" {
 828					desc = name
 829				}
 830				if tt == "Emoji" {
 831					icon, _ := tag.GetMap("icon")
 832					mt, _ := icon.GetString("mediaType")
 833					if mt == "" {
 834						mt = "image/png"
 835					}
 836					u, _ := icon.GetString("url")
 837					donk := savedonk(u, name, desc, mt, true)
 838					if donk != nil {
 839						xonk.Donks = append(xonk.Donks, donk)
 840					}
 841				}
 842				if tt == "Hashtag" {
 843					if name == "" || name == "#" {
 844						// skip it
 845					} else {
 846						if name[0] != '#' {
 847							name = "#" + name
 848						}
 849						xonk.Onts = append(xonk.Onts, name)
 850					}
 851				}
 852				if tt == "Place" {
 853					p := new(Place)
 854					p.Name = name
 855					p.Latitude, _ = tag.GetNumber("latitude")
 856					p.Longitude, _ = tag.GetNumber("longitude")
 857					p.Url, _ = tag.GetString("url")
 858					xonk.Place = p
 859				}
 860				if tt == "Mention" {
 861					var m Mention
 862					m.Who, _ = tag.GetString("name")
 863					m.Where, _ = tag.GetString("href")
 864					mentions = append(mentions, m)
 865				}
 866			}
 867			if starttime, ok := obj.GetString("startTime"); ok {
 868				if start, err := time.Parse(time.RFC3339, starttime); err == nil {
 869					t := new(Time)
 870					t.StartTime = start
 871					endtime, _ := obj.GetString("endTime")
 872					t.EndTime, _ = time.Parse(time.RFC3339, endtime)
 873					dura, _ := obj.GetString("duration")
 874					if strings.HasPrefix(dura, "PT") {
 875						dura = strings.ToLower(dura[2:])
 876						d, _ := time.ParseDuration(dura)
 877						t.Duration = Duration(d)
 878					}
 879					xonk.Time = t
 880				}
 881			}
 882			if loca, ok := obj.GetMap("location"); ok {
 883				if tt, _ := loca.GetString("type"); tt == "Place" {
 884					p := new(Place)
 885					p.Name, _ = loca.GetString("name")
 886					p.Latitude, _ = loca.GetNumber("latitude")
 887					p.Longitude, _ = loca.GetNumber("longitude")
 888					p.Url, _ = loca.GetString("url")
 889					xonk.Place = p
 890				}
 891			}
 892
 893			xonk.Onts = oneofakind(xonk.Onts)
 894			replyobj, ok := obj.GetMap("replies")
 895			if ok {
 896				items, ok := replyobj.GetArray("items")
 897				if !ok {
 898					first, ok := replyobj.GetMap("first")
 899					if ok {
 900						items, _ = first.GetArray("items")
 901					}
 902				}
 903				for _, repl := range items {
 904					s, ok := repl.(string)
 905					if ok {
 906						replies = append(replies, s)
 907					}
 908				}
 909			}
 910
 911		}
 912
 913		if currenttid == "" {
 914			currenttid = convoy
 915		}
 916
 917		// init xonk
 918		xonk.What = what
 919		xonk.RID = rid
 920		xonk.Date, _ = time.Parse(time.RFC3339, dt)
 921		xonk.URL = url
 922		xonk.Format = "html"
 923		xonk.Convoy = convoy
 924		xonk.Mentions = mentions
 925		for _, m := range mentions {
 926			if m.Where == user.URL {
 927				xonk.Whofore = 1
 928			}
 929		}
 930		imaginate(&xonk)
 931
 932		if what == "chonk" {
 933			ch := Chonk{
 934				UserID: xonk.UserID,
 935				XID:    xid,
 936				Who:    xonk.Honker,
 937				Target: xonk.Honker,
 938				Date:   xonk.Date,
 939				Noise:  xonk.Noise,
 940				Format: xonk.Format,
 941				Donks:  xonk.Donks,
 942			}
 943			savechonk(&ch)
 944			return nil
 945		}
 946
 947		if isUpdate {
 948			dlog.Printf("something has changed! %s", xonk.XID)
 949			prev := getxonk(user.ID, xonk.XID)
 950			if prev == nil {
 951				ilog.Printf("didn't find old version for update: %s", xonk.XID)
 952				isUpdate = false
 953			} else {
 954				xonk.ID = prev.ID
 955				updatehonk(&xonk)
 956			}
 957		}
 958		if !isUpdate && needxonk(user, &xonk) {
 959			if rid != "" {
 960				if needxonkid(user, rid) {
 961					goingup++
 962					saveonemore(rid)
 963					goingup--
 964				}
 965				if convoy == "" {
 966					xx := getxonk(user.ID, rid)
 967					if xx != nil {
 968						convoy = xx.Convoy
 969					}
 970				}
 971			}
 972			if convoy == "" {
 973				convoy = currenttid
 974			}
 975			if convoy == "" {
 976				convoy = "data:,missing-" + xfiltrate()
 977				currenttid = convoy
 978			}
 979			xonk.Convoy = convoy
 980			savexonk(&xonk)
 981		}
 982		if goingup == 0 {
 983			for _, replid := range replies {
 984				if needxonkid(user, replid) {
 985					dlog.Printf("missing a reply: %s", replid)
 986					saveonemore(replid)
 987				}
 988			}
 989		}
 990		return &xonk
 991	}
 992
 993	return xonkxonkfn(item, origin, false)
 994}
 995
 996func dumpactivity(item junk.Junk) {
 997	fd, err := os.OpenFile("savedinbox.json", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
 998	if err != nil {
 999		elog.Printf("error opening inbox! %s", err)
1000		return
1001	}
1002	defer fd.Close()
1003	item.Write(fd)
1004	io.WriteString(fd, "\n")
1005}
1006
1007func rubadubdub(user *WhatAbout, req junk.Junk) {
1008	actor, _ := req.GetString("actor")
1009	j := junk.New()
1010	j["@context"] = itiswhatitis
1011	j["id"] = user.URL + "/dub/" + xfiltrate()
1012	j["type"] = "Accept"
1013	j["actor"] = user.URL
1014	j["to"] = actor
1015	j["published"] = time.Now().UTC().Format(time.RFC3339)
1016	j["object"] = req
1017
1018	deliverate(0, user.ID, actor, j.ToBytes(), true)
1019}
1020
1021func itakeitallback(user *WhatAbout, xid string, owner string, folxid string) {
1022	j := junk.New()
1023	j["@context"] = itiswhatitis
1024	j["id"] = user.URL + "/unsub/" + folxid
1025	j["type"] = "Undo"
1026	j["actor"] = user.URL
1027	j["to"] = owner
1028	f := junk.New()
1029	f["id"] = user.URL + "/sub/" + folxid
1030	f["type"] = "Follow"
1031	f["actor"] = user.URL
1032	f["to"] = owner
1033	f["object"] = xid
1034	j["object"] = f
1035	j["published"] = time.Now().UTC().Format(time.RFC3339)
1036
1037	deliverate(0, user.ID, owner, j.ToBytes(), true)
1038}
1039
1040func subsub(user *WhatAbout, xid string, owner string, folxid string) {
1041	if xid == "" {
1042		ilog.Printf("can't subscribe to empty")
1043		return
1044	}
1045	j := junk.New()
1046	j["@context"] = itiswhatitis
1047	j["id"] = user.URL + "/sub/" + folxid
1048	j["type"] = "Follow"
1049	j["actor"] = user.URL
1050	j["to"] = owner
1051	j["object"] = xid
1052	j["published"] = time.Now().UTC().Format(time.RFC3339)
1053
1054	deliverate(0, user.ID, owner, j.ToBytes(), true)
1055}
1056
1057func activatedonks(donks []*Donk) []junk.Junk {
1058	var atts []junk.Junk
1059	for _, d := range donks {
1060		if re_emus.MatchString(d.Name) {
1061			continue
1062		}
1063		jd := junk.New()
1064		jd["mediaType"] = d.Media
1065		jd["name"] = d.Name
1066		jd["summary"] = html.EscapeString(d.Desc)
1067		jd["type"] = "Document"
1068		jd["url"] = d.URL
1069		atts = append(atts, jd)
1070	}
1071	return atts
1072}
1073
1074// returns activity, object
1075func jonkjonk(user *WhatAbout, h *Honk) (junk.Junk, junk.Junk) {
1076	dt := h.Date.Format(time.RFC3339)
1077	var jo junk.Junk
1078	j := junk.New()
1079	j["id"] = user.URL + "/" + h.What + "/" + shortxid(h.XID)
1080	j["actor"] = user.URL
1081	j["published"] = dt
1082	j["to"] = h.Audience[0]
1083	if len(h.Audience) > 1 {
1084		j["cc"] = h.Audience[1:]
1085	}
1086
1087	switch h.What {
1088	case "update":
1089		fallthrough
1090	case "tonk":
1091		fallthrough
1092	case "event":
1093		fallthrough
1094	case "wonk":
1095		fallthrough
1096	case "honk":
1097		j["type"] = "Create"
1098		if h.What == "update" {
1099			j["type"] = "Update"
1100		}
1101
1102		jo = junk.New()
1103		jo["id"] = h.XID
1104		jo["type"] = "Note"
1105		if h.What == "event" {
1106			jo["type"] = "Event"
1107		} else if h.What == "wonk" {
1108			jo["type"] = "GuessWord"
1109		}
1110		jo["published"] = dt
1111		jo["url"] = h.XID
1112		jo["attributedTo"] = user.URL
1113		if h.RID != "" {
1114			jo["inReplyTo"] = h.RID
1115		}
1116		if h.Convoy != "" {
1117			jo["context"] = h.Convoy
1118			jo["conversation"] = h.Convoy
1119		}
1120		jo["to"] = h.Audience[0]
1121		if len(h.Audience) > 1 {
1122			jo["cc"] = h.Audience[1:]
1123		}
1124		if !h.Public {
1125			jo["directMessage"] = true
1126		}
1127		translate(h)
1128		redoimages(h)
1129		if h.Precis != "" {
1130			jo["sensitive"] = true
1131		}
1132
1133		var replies []string
1134		for _, reply := range h.Replies {
1135			replies = append(replies, reply.XID)
1136		}
1137		if len(replies) > 0 {
1138			jr := junk.New()
1139			jr["type"] = "Collection"
1140			jr["totalItems"] = len(replies)
1141			jr["items"] = replies
1142			jo["replies"] = jr
1143		}
1144
1145		var tags []junk.Junk
1146		for _, m := range h.Mentions {
1147			t := junk.New()
1148			t["type"] = "Mention"
1149			t["name"] = m.Who
1150			t["href"] = m.Where
1151			tags = append(tags, t)
1152		}
1153		for _, o := range h.Onts {
1154			t := junk.New()
1155			t["type"] = "Hashtag"
1156			o = strings.ToLower(o)
1157			t["href"] = fmt.Sprintf("https://%s/o/%s", serverName, o[1:])
1158			t["name"] = o
1159			tags = append(tags, t)
1160		}
1161		for _, e := range herdofemus(h.Noise) {
1162			t := junk.New()
1163			t["id"] = e.ID
1164			t["type"] = "Emoji"
1165			t["name"] = e.Name
1166			i := junk.New()
1167			i["type"] = "Image"
1168			i["mediaType"] = "image/png"
1169			i["url"] = e.ID
1170			t["icon"] = i
1171			tags = append(tags, t)
1172		}
1173		for _, e := range fixupflags(h) {
1174			t := junk.New()
1175			t["id"] = e.ID
1176			t["type"] = "Emoji"
1177			t["name"] = e.Name
1178			i := junk.New()
1179			i["type"] = "Image"
1180			i["mediaType"] = "image/png"
1181			i["url"] = e.ID
1182			t["icon"] = i
1183			tags = append(tags, t)
1184		}
1185		if len(tags) > 0 {
1186			jo["tag"] = tags
1187		}
1188		if p := h.Place; p != nil {
1189			t := junk.New()
1190			t["type"] = "Place"
1191			if p.Name != "" {
1192				t["name"] = p.Name
1193			}
1194			if p.Latitude != 0 {
1195				t["latitude"] = p.Latitude
1196			}
1197			if p.Longitude != 0 {
1198				t["longitude"] = p.Longitude
1199			}
1200			if p.Url != "" {
1201				t["url"] = p.Url
1202			}
1203			jo["location"] = t
1204		}
1205		if t := h.Time; t != nil {
1206			jo["startTime"] = t.StartTime.Format(time.RFC3339)
1207			if t.Duration != 0 {
1208				jo["duration"] = "PT" + strings.ToUpper(t.Duration.String())
1209			}
1210		}
1211		if w := h.Wonkles; w != "" {
1212			jo["wordlist"] = w
1213		}
1214		atts := activatedonks(h.Donks)
1215		if len(atts) > 0 {
1216			jo["attachment"] = atts
1217		}
1218		jo["summary"] = html.EscapeString(h.Precis)
1219		jo["content"] = h.Noise
1220		j["object"] = jo
1221	case "bonk":
1222		j["type"] = "Announce"
1223		if h.Convoy != "" {
1224			j["context"] = h.Convoy
1225		}
1226		j["object"] = h.XID
1227	case "unbonk":
1228		b := junk.New()
1229		b["id"] = user.URL + "/" + "bonk" + "/" + shortxid(h.XID)
1230		b["type"] = "Announce"
1231		b["actor"] = user.URL
1232		if h.Convoy != "" {
1233			b["context"] = h.Convoy
1234		}
1235		b["object"] = h.XID
1236		j["type"] = "Undo"
1237		j["object"] = b
1238	case "zonk":
1239		j["type"] = "Delete"
1240		j["object"] = h.XID
1241	case "ack":
1242		j["type"] = "Read"
1243		j["object"] = h.XID
1244		if h.Convoy != "" {
1245			j["context"] = h.Convoy
1246		}
1247	case "react":
1248		j["type"] = "EmojiReact"
1249		j["object"] = h.XID
1250		if h.Convoy != "" {
1251			j["context"] = h.Convoy
1252		}
1253		j["content"] = h.Noise
1254	case "deack":
1255		b := junk.New()
1256		b["id"] = user.URL + "/" + "ack" + "/" + shortxid(h.XID)
1257		b["type"] = "Read"
1258		b["actor"] = user.URL
1259		b["object"] = h.XID
1260		if h.Convoy != "" {
1261			b["context"] = h.Convoy
1262		}
1263		j["type"] = "Undo"
1264		j["object"] = b
1265	}
1266
1267	return j, jo
1268}
1269
1270var oldjonks = cache.New(cache.Options{Filler: func(xid string) ([]byte, bool) {
1271	row := stmtAnyXonk.QueryRow(xid)
1272	honk := scanhonk(row)
1273	if honk == nil || !honk.Public {
1274		return nil, true
1275	}
1276	user, _ := butwhatabout(honk.Username)
1277	rawhonks := gethonksbyconvoy(honk.UserID, honk.Convoy, 0)
1278	reversehonks(rawhonks)
1279	for _, h := range rawhonks {
1280		if h.RID == honk.XID && h.Public && (h.Whofore == 2 || h.IsAcked()) {
1281			honk.Replies = append(honk.Replies, h)
1282		}
1283	}
1284	donksforhonks([]*Honk{honk})
1285	_, j := jonkjonk(user, honk)
1286	j["@context"] = itiswhatitis
1287
1288	return j.ToBytes(), true
1289}, Limit: 128})
1290
1291func gimmejonk(xid string) ([]byte, bool) {
1292	var j []byte
1293	ok := oldjonks.Get(xid, &j)
1294	return j, ok
1295}
1296
1297func boxuprcpts(user *WhatAbout, addresses []string, useshared bool) map[string]bool {
1298	rcpts := make(map[string]bool)
1299	for _, a := range addresses {
1300		if a == "" || a == thewholeworld || a == user.URL || strings.HasSuffix(a, "/followers") {
1301			continue
1302		}
1303		if a[0] == '%' {
1304			rcpts[a] = true
1305			continue
1306		}
1307		var box *Box
1308		ok := boxofboxes.Get(a, &box)
1309		if ok && useshared && box.Shared != "" {
1310			rcpts["%"+box.Shared] = true
1311		} else {
1312			rcpts[a] = true
1313		}
1314	}
1315	return rcpts
1316}
1317
1318func chonkifymsg(user *WhatAbout, ch *Chonk) []byte {
1319	dt := ch.Date.Format(time.RFC3339)
1320	aud := []string{ch.Target}
1321
1322	jo := junk.New()
1323	jo["id"] = ch.XID
1324	jo["type"] = "ChatMessage"
1325	jo["published"] = dt
1326	jo["attributedTo"] = user.URL
1327	jo["to"] = aud
1328	jo["content"] = ch.HTML
1329	atts := activatedonks(ch.Donks)
1330	if len(atts) > 0 {
1331		jo["attachment"] = atts
1332	}
1333	var tags []junk.Junk
1334	for _, e := range herdofemus(ch.Noise) {
1335		t := junk.New()
1336		t["id"] = e.ID
1337		t["type"] = "Emoji"
1338		t["name"] = e.Name
1339		i := junk.New()
1340		i["type"] = "Image"
1341		i["mediaType"] = "image/png"
1342		i["url"] = e.ID
1343		t["icon"] = i
1344		tags = append(tags, t)
1345	}
1346	if len(tags) > 0 {
1347		jo["tag"] = tags
1348	}
1349
1350	j := junk.New()
1351	j["@context"] = itiswhatitis
1352	j["id"] = user.URL + "/" + "honk" + "/" + shortxid(ch.XID)
1353	j["type"] = "Create"
1354	j["actor"] = user.URL
1355	j["published"] = dt
1356	j["to"] = aud
1357	j["object"] = jo
1358
1359	return j.ToBytes()
1360}
1361
1362func sendchonk(user *WhatAbout, ch *Chonk) {
1363	msg := chonkifymsg(user, ch)
1364
1365	rcpts := make(map[string]bool)
1366	rcpts[ch.Target] = true
1367	for a := range rcpts {
1368		go deliverate(0, user.ID, a, msg, true)
1369	}
1370}
1371
1372func honkworldwide(user *WhatAbout, honk *Honk) {
1373	jonk, _ := jonkjonk(user, honk)
1374	jonk["@context"] = itiswhatitis
1375	msg := jonk.ToBytes()
1376
1377	rcpts := boxuprcpts(user, honk.Audience, honk.Public)
1378
1379	if honk.Public {
1380		for _, h := range getdubs(user.ID) {
1381			if h.XID == user.URL {
1382				continue
1383			}
1384			var box *Box
1385			ok := boxofboxes.Get(h.XID, &box)
1386			if ok && box.Shared != "" {
1387				rcpts["%"+box.Shared] = true
1388			} else {
1389				rcpts[h.XID] = true
1390			}
1391		}
1392		for _, f := range getbacktracks(honk.XID) {
1393			if f[0] == '%' {
1394				rcpts[f] = true
1395			} else {
1396				var box *Box
1397				ok := boxofboxes.Get(f, &box)
1398				if ok && box.Shared != "" {
1399					rcpts["%"+box.Shared] = true
1400				} else {
1401					rcpts[f] = true
1402				}
1403			}
1404		}
1405	}
1406	for a := range rcpts {
1407		go deliverate(0, user.ID, a, msg, doesitmatter(honk.What))
1408	}
1409	if honk.Public && len(honk.Onts) > 0 {
1410		collectiveaction(honk)
1411	}
1412}
1413
1414func doesitmatter(what string) bool {
1415	switch what {
1416	case "ack":
1417		return false
1418	case "react":
1419		return false
1420	case "deack":
1421		return false
1422	}
1423	return true
1424}
1425
1426func collectiveaction(honk *Honk) {
1427	user := getserveruser()
1428	for _, ont := range honk.Onts {
1429		dubs := getnameddubs(serverUID, ont)
1430		if len(dubs) == 0 {
1431			continue
1432		}
1433		j := junk.New()
1434		j["@context"] = itiswhatitis
1435		j["type"] = "Add"
1436		j["id"] = user.URL + "/add/" + shortxid(ont+honk.XID)
1437		j["actor"] = user.URL
1438		j["object"] = honk.XID
1439		j["target"] = fmt.Sprintf("https://%s/o/%s", serverName, ont[1:])
1440		rcpts := make(map[string]bool)
1441		for _, dub := range dubs {
1442			var box *Box
1443			ok := boxofboxes.Get(dub.XID, &box)
1444			if ok && box.Shared != "" {
1445				rcpts["%"+box.Shared] = true
1446			} else {
1447				rcpts[dub.XID] = true
1448			}
1449		}
1450		msg := j.ToBytes()
1451		for a := range rcpts {
1452			go deliverate(0, user.ID, a, msg, false)
1453		}
1454	}
1455}
1456
1457func junkuser(user *WhatAbout) junk.Junk {
1458	j := junk.New()
1459	j["@context"] = itiswhatitis
1460	j["id"] = user.URL
1461	j["inbox"] = user.URL + "/inbox"
1462	j["outbox"] = user.URL + "/outbox"
1463	j["name"] = user.Display
1464	j["preferredUsername"] = user.Name
1465	j["summary"] = user.HTAbout
1466	var tags []junk.Junk
1467	for _, o := range user.Onts {
1468		t := junk.New()
1469		t["type"] = "Hashtag"
1470		o = strings.ToLower(o)
1471		t["href"] = fmt.Sprintf("https://%s/o/%s", serverName, o[1:])
1472		t["name"] = o
1473		tags = append(tags, t)
1474	}
1475	if len(tags) > 0 {
1476		j["tag"] = tags
1477	}
1478
1479	if user.ID > 0 {
1480		j["type"] = "Person"
1481		j["url"] = user.URL
1482		j["followers"] = user.URL + "/followers"
1483		j["following"] = user.URL + "/following"
1484		a := junk.New()
1485		a["type"] = "Image"
1486		a["mediaType"] = "image/png"
1487		if ava := user.Options.Avatar; ava != "" {
1488			a["url"] = ava
1489		} else {
1490			u := fmt.Sprintf("https://%s/a?a=%s", serverName, url.QueryEscape(user.URL))
1491			if user.Options.Avahex {
1492				u += "&hex=1"
1493			}
1494			a["url"] = u
1495		}
1496		j["icon"] = a
1497	} else {
1498		j["type"] = "Service"
1499	}
1500	k := junk.New()
1501	k["id"] = user.URL + "#key"
1502	k["owner"] = user.URL
1503	k["publicKeyPem"] = user.Key
1504	j["publicKey"] = k
1505
1506	return j
1507}
1508
1509var oldjonkers = cache.New(cache.Options{Filler: func(name string) ([]byte, bool) {
1510	user, err := butwhatabout(name)
1511	if err != nil {
1512		return nil, false
1513	}
1514	var buf bytes.Buffer
1515	j := junkuser(user)
1516	j.Write(&buf)
1517	return buf.Bytes(), true
1518}, Duration: 1 * time.Minute})
1519
1520func asjonker(name string) ([]byte, bool) {
1521	var j []byte
1522	ok := oldjonkers.Get(name, &j)
1523	return j, ok
1524}
1525
1526var handfull = cache.New(cache.Options{Filler: func(name string) (string, bool) {
1527	m := strings.Split(name, "@")
1528	if len(m) != 2 {
1529		dlog.Printf("bad fish name: %s", name)
1530		return "", true
1531	}
1532	var href string
1533	row := stmtGetXonker.QueryRow(name, "fishname")
1534	err := row.Scan(&href)
1535	if err == nil {
1536		return href, true
1537	}
1538	dlog.Printf("fishing for %s", name)
1539	j, err := GetJunkFast(fmt.Sprintf("https://%s/.well-known/webfinger?resource=acct:%s", m[1], name))
1540	if err != nil {
1541		ilog.Printf("failed to go fish %s: %s", name, err)
1542		return "", true
1543	}
1544	links, _ := j.GetArray("links")
1545	for _, li := range links {
1546		l, ok := li.(junk.Junk)
1547		if !ok {
1548			continue
1549		}
1550		href, _ := l.GetString("href")
1551		rel, _ := l.GetString("rel")
1552		t, _ := l.GetString("type")
1553		if rel == "self" && friendorfoe(t) {
1554			when := time.Now().UTC().Format(dbtimeformat)
1555			_, err := stmtSaveXonker.Exec(name, href, "fishname", when)
1556			if err != nil {
1557				elog.Printf("error saving fishname: %s", err)
1558			}
1559			return href, true
1560		}
1561	}
1562	return href, true
1563}, Duration: 1 * time.Minute})
1564
1565func gofish(name string) string {
1566	if name[0] == '@' {
1567		name = name[1:]
1568	}
1569	var href string
1570	handfull.Get(name, &href)
1571	return href
1572}
1573
1574func investigate(name string) (*SomeThing, error) {
1575	if name == "" {
1576		return nil, fmt.Errorf("no name")
1577	}
1578	if name[0] == '@' {
1579		name = gofish(name)
1580	}
1581	if name == "" {
1582		return nil, fmt.Errorf("no name")
1583	}
1584	obj, err := GetJunkFast(name)
1585	if err != nil {
1586		return nil, err
1587	}
1588	allinjest(originate(name), obj)
1589	return somethingabout(obj)
1590}
1591
1592func somethingabout(obj junk.Junk) (*SomeThing, error) {
1593	info := new(SomeThing)
1594	t, _ := obj.GetString("type")
1595	switch t {
1596	case "Person":
1597		fallthrough
1598	case "Organization":
1599		fallthrough
1600	case "Application":
1601		fallthrough
1602	case "Service":
1603		info.What = SomeActor
1604	case "OrderedCollection":
1605		fallthrough
1606	case "Collection":
1607		info.What = SomeCollection
1608	default:
1609		return nil, fmt.Errorf("unknown object type")
1610	}
1611	info.XID, _ = obj.GetString("id")
1612	info.Name, _ = obj.GetString("preferredUsername")
1613	if info.Name == "" {
1614		info.Name, _ = obj.GetString("name")
1615	}
1616	info.Owner, _ = obj.GetString("attributedTo")
1617	if info.Owner == "" {
1618		info.Owner = info.XID
1619	}
1620	return info, nil
1621}
1622
1623func allinjest(origin string, obj junk.Junk) {
1624	keyobj, ok := obj.GetMap("publicKey")
1625	if ok {
1626		ingestpubkey(origin, keyobj)
1627	}
1628	ingestboxes(origin, obj)
1629	ingesthandle(origin, obj)
1630}
1631
1632func ingestpubkey(origin string, obj junk.Junk) {
1633	keyobj, ok := obj.GetMap("publicKey")
1634	if ok {
1635		obj = keyobj
1636	}
1637	keyname, ok := obj.GetString("id")
1638	var data string
1639	row := stmtGetXonker.QueryRow(keyname, "pubkey")
1640	err := row.Scan(&data)
1641	if err == nil {
1642		return
1643	}
1644	if !ok || origin != originate(keyname) {
1645		ilog.Printf("bad key origin %s <> %s", origin, keyname)
1646		return
1647	}
1648	dlog.Printf("ingesting a needed pubkey: %s", keyname)
1649	owner, ok := obj.GetString("owner")
1650	if !ok {
1651		ilog.Printf("error finding %s pubkey owner", keyname)
1652		return
1653	}
1654	data, ok = obj.GetString("publicKeyPem")
1655	if !ok {
1656		ilog.Printf("error finding %s pubkey", keyname)
1657		return
1658	}
1659	if originate(owner) != origin {
1660		ilog.Printf("bad key owner: %s <> %s", owner, origin)
1661		return
1662	}
1663	_, _, err = httpsig.DecodeKey(data)
1664	if err != nil {
1665		ilog.Printf("error decoding %s pubkey: %s", keyname, err)
1666		return
1667	}
1668	when := time.Now().UTC().Format(dbtimeformat)
1669	_, err = stmtSaveXonker.Exec(keyname, data, "pubkey", when)
1670	if err != nil {
1671		elog.Printf("error saving key: %s", err)
1672	}
1673}
1674
1675func ingestboxes(origin string, obj junk.Junk) {
1676	ident, _ := obj.GetString("id")
1677	if ident == "" {
1678		return
1679	}
1680	if originate(ident) != origin {
1681		return
1682	}
1683	var info string
1684	row := stmtGetXonker.QueryRow(ident, "boxes")
1685	err := row.Scan(&info)
1686	if err == nil {
1687		return
1688	}
1689	dlog.Printf("ingesting boxes: %s", ident)
1690	inbox, _ := obj.GetString("inbox")
1691	outbox, _ := obj.GetString("outbox")
1692	sbox, _ := obj.GetString("endpoints", "sharedInbox")
1693	if inbox != "" {
1694		when := time.Now().UTC().Format(dbtimeformat)
1695		m := strings.Join([]string{inbox, outbox, sbox}, " ")
1696		_, err = stmtSaveXonker.Exec(ident, m, "boxes", when)
1697		if err != nil {
1698			elog.Printf("error saving boxes: %s", err)
1699		}
1700	}
1701}
1702
1703func ingesthandle(origin string, obj junk.Junk) {
1704	xid, _ := obj.GetString("id")
1705	if xid == "" {
1706		return
1707	}
1708	if originate(xid) != origin {
1709		return
1710	}
1711	var handle string
1712	row := stmtGetXonker.QueryRow(xid, "handle")
1713	err := row.Scan(&handle)
1714	if err == nil {
1715		return
1716	}
1717	handle, _ = obj.GetString("preferredUsername")
1718	if handle != "" {
1719		when := time.Now().UTC().Format(dbtimeformat)
1720		_, err = stmtSaveXonker.Exec(xid, handle, "handle", when)
1721		if err != nil {
1722			elog.Printf("error saving handle: %s", err)
1723		}
1724	}
1725}
1726
1727func updateMe(username string) {
1728	var user *WhatAbout
1729	somenamedusers.Get(username, &user)
1730	dt := time.Now().UTC().Format(time.RFC3339)
1731	j := junk.New()
1732	j["@context"] = itiswhatitis
1733	j["id"] = fmt.Sprintf("%s/upme/%s/%d", user.URL, user.Name, time.Now().Unix())
1734	j["actor"] = user.URL
1735	j["published"] = dt
1736	j["to"] = thewholeworld
1737	j["type"] = "Update"
1738	j["object"] = junkuser(user)
1739
1740	msg := j.ToBytes()
1741
1742	rcpts := make(map[string]bool)
1743	for _, f := range getdubs(user.ID) {
1744		if f.XID == user.URL {
1745			continue
1746		}
1747		var box *Box
1748		boxofboxes.Get(f.XID, &box)
1749		if box != nil && box.Shared != "" {
1750			rcpts["%"+box.Shared] = true
1751		} else {
1752			rcpts[f.XID] = true
1753		}
1754	}
1755	for a := range rcpts {
1756		go deliverate(0, user.ID, a, msg, false)
1757	}
1758}
1759
1760func followme(user *WhatAbout, who string, name string, j junk.Junk) {
1761	folxid, _ := j.GetString("id")
1762
1763	ilog.Printf("updating honker follow: %s %s", who, folxid)
1764
1765	var x string
1766	db := opendatabase()
1767	row := db.QueryRow("select xid from honkers where name = ? and xid = ? and userid = ? and flavor in ('dub', 'undub')", name, who, user.ID)
1768	err := row.Scan(&x)
1769	if err != sql.ErrNoRows {
1770		ilog.Printf("duplicate follow request: %s", who)
1771		_, err = stmtUpdateFlavor.Exec("dub", folxid, user.ID, name, who, "undub")
1772		if err != nil {
1773			elog.Printf("error updating honker: %s", err)
1774		}
1775	} else {
1776		stmtSaveDub.Exec(user.ID, name, who, "dub", folxid)
1777	}
1778	go rubadubdub(user, j)
1779}
1780
1781func unfollowme(user *WhatAbout, who string, name string, j junk.Junk) {
1782	var folxid string
1783	if who == "" {
1784		folxid, _ = j.GetString("object")
1785
1786		db := opendatabase()
1787		row := db.QueryRow("select xid, name from honkers where userid = ? and folxid = ? and flavor in ('dub', 'undub')", user.ID, folxid)
1788		err := row.Scan(&who, &name)
1789		if err != nil {
1790			if err != sql.ErrNoRows {
1791				elog.Printf("error scanning honker: %s", err)
1792			}
1793			return
1794		}
1795	}
1796
1797	ilog.Printf("updating honker undo: %s %s", who, folxid)
1798	_, err := stmtUpdateFlavor.Exec("undub", folxid, user.ID, name, who, "dub")
1799	if err != nil {
1800		elog.Printf("error updating honker: %s", err)
1801		return
1802	}
1803}
1804
1805func followyou(user *WhatAbout, honkerid int64) {
1806	var url, owner string
1807	db := opendatabase()
1808	row := db.QueryRow("select xid, owner from honkers where honkerid = ? and userid = ? and flavor in ('unsub', 'peep', 'presub', 'sub')",
1809		honkerid, user.ID)
1810	err := row.Scan(&url, &owner)
1811	if err != nil {
1812		elog.Printf("can't get honker xid: %s", err)
1813		return
1814	}
1815	folxid := xfiltrate()
1816	ilog.Printf("subscribing to %s", url)
1817	_, err = db.Exec("update honkers set flavor = ?, folxid = ? where honkerid = ?", "presub", folxid, honkerid)
1818	if err != nil {
1819		elog.Printf("error updating honker: %s", err)
1820		return
1821	}
1822	go subsub(user, url, owner, folxid)
1823
1824}
1825func unfollowyou(user *WhatAbout, honkerid int64) {
1826	db := opendatabase()
1827	row := db.QueryRow("select xid, owner, folxid from honkers where honkerid = ? and userid = ? and flavor in ('sub')",
1828		honkerid, user.ID)
1829	var url, owner, folxid string
1830	err := row.Scan(&url, &owner, &folxid)
1831	if err != nil {
1832		elog.Printf("can't get honker xid: %s", err)
1833		return
1834	}
1835	ilog.Printf("unsubscribing from %s", url)
1836	_, err = db.Exec("update honkers set flavor = ? where honkerid = ?", "unsub", honkerid)
1837	if err != nil {
1838		elog.Printf("error updating honker: %s", err)
1839		return
1840	}
1841	go itakeitallback(user, url, owner, folxid)
1842}
1843
1844func followyou2(user *WhatAbout, j junk.Junk) {
1845	who, _ := j.GetString("actor")
1846
1847	ilog.Printf("updating honker accept: %s", who)
1848	db := opendatabase()
1849	row := db.QueryRow("select name, folxid from honkers where userid = ? and xid = ? and flavor in ('presub')",
1850		user.ID, who)
1851	var name, folxid string
1852	err := row.Scan(&name, &folxid)
1853	if err != nil {
1854		elog.Printf("can't get honker name: %s", err)
1855		return
1856	}
1857	_, err = stmtUpdateFlavor.Exec("sub", folxid, user.ID, name, who, "presub")
1858	if err != nil {
1859		elog.Printf("error updating honker: %s", err)
1860		return
1861	}
1862}
1863
1864func nofollowyou2(user *WhatAbout, j junk.Junk) {
1865	who, _ := j.GetString("actor")
1866
1867	ilog.Printf("updating honker reject: %s", who)
1868	db := opendatabase()
1869	row := db.QueryRow("select name, folxid from honkers where userid = ? and xid = ? and flavor in ('presub', 'sub')",
1870		user.ID, who)
1871	var name, folxid string
1872	err := row.Scan(&name, &folxid)
1873	if err != nil {
1874		elog.Printf("can't get honker name: %s", err)
1875		return
1876	}
1877	_, err = stmtUpdateFlavor.Exec("unsub", folxid, user.ID, name, who, "presub")
1878	_, err = stmtUpdateFlavor.Exec("unsub", folxid, user.ID, name, who, "sub")
1879	if err != nil {
1880		elog.Printf("error updating honker: %s", err)
1881		return
1882	}
1883}