all repos — honk @ 07adb29f734f3f610b06d2732d9991956292c692

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