all repos — honk @ 4d654c63d25a3f224b7f947ce07c632dcea43f25

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