// // Copyright (c) 2019 Ted Unangst // // Permission to use, copy, modify, and distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. // // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. package main import ( "bytes" "context" "crypto/tls" "database/sql" "fmt" "html" "io" notrand "math/rand" "net/http" "os" "regexp" "strings" "time" "humungus.tedunangst.com/r/webs/cache" "humungus.tedunangst.com/r/webs/gate" "humungus.tedunangst.com/r/webs/httpsig" "humungus.tedunangst.com/r/webs/junk" "humungus.tedunangst.com/r/webs/templates" ) var theonetruename = `application/ld+json; profile="https://www.w3.org/ns/activitystreams"` var thefakename = `application/activity+json` var falsenames = []string{ `application/ld+json`, `application/activity+json`, } var itiswhatitis = "https://www.w3.org/ns/activitystreams" var thewholeworld = "https://www.w3.org/ns/activitystreams#Public" var fastTimeout time.Duration = 5 var slowTimeout time.Duration = 30 func friendorfoe(ct string) bool { ct = strings.ToLower(ct) for _, at := range falsenames { if strings.HasPrefix(ct, at) { return true } } return false } var develClient = &http.Client{ Transport: &http.Transport{ TLSClientConfig: &tls.Config{ InsecureSkipVerify: true, }, }, } func PostJunk(keyname string, key httpsig.PrivateKey, url string, j junk.Junk) error { return PostMsg(keyname, key, url, j.ToBytes()) } func PostMsg(keyname string, key httpsig.PrivateKey, url string, msg []byte) error { client := http.DefaultClient if develMode { client = develClient } req, err := http.NewRequest("POST", url, bytes.NewReader(msg)) if err != nil { return err } req.Header.Set("User-Agent", "honksnonk/5.0; "+serverName) req.Header.Set("Content-Type", theonetruename) httpsig.SignRequest(keyname, key, req, msg) ctx, cancel := context.WithTimeout(context.Background(), 2*slowTimeout*time.Second) defer cancel() req = req.WithContext(ctx) resp, err := client.Do(req) if err != nil { return err } resp.Body.Close() switch resp.StatusCode { case 200: case 201: case 202: default: return fmt.Errorf("http post status: %d", resp.StatusCode) } ilog.Printf("successful post: %s %d", url, resp.StatusCode) return nil } func GetJunk(userid int64, url string) (junk.Junk, error) { return GetJunkTimeout(userid, url, slowTimeout*time.Second) } func GetJunkFast(userid int64, url string) (junk.Junk, error) { return GetJunkTimeout(userid, url, fastTimeout*time.Second) } func GetJunkHardMode(userid int64, url string) (junk.Junk, error) { j, err := GetJunk(userid, url) if err != nil { emsg := err.Error() if emsg == "http get status: 502" || strings.Contains(emsg, "timeout") { ilog.Printf("trying again after error: %s", emsg) time.Sleep(time.Duration(60+notrand.Int63n(60)) * time.Second) j, err = GetJunk(userid, url) if err != nil { ilog.Printf("still couldn't get it") } else { ilog.Printf("retry success!") } } } return j, err } var flightdeck = gate.NewSerializer() var signGets = true func junkGet(userid int64, url string, args junk.GetArgs) (junk.Junk, error) { client := http.DefaultClient if args.Client != nil { client = args.Client } req, err := http.NewRequest("GET", url, nil) if err != nil { return nil, err } if args.Accept != "" { req.Header.Set("Accept", args.Accept) } if args.Agent != "" { req.Header.Set("User-Agent", args.Agent) } if signGets { var ki *KeyInfo ok := ziggies.Get(userid, &ki) if ok { httpsig.SignRequest(ki.keyname, ki.seckey, req, nil) } } if args.Timeout != 0 { ctx, cancel := context.WithTimeout(context.Background(), args.Timeout) defer cancel() req = req.WithContext(ctx) } resp, err := client.Do(req) if err != nil { return nil, err } defer resp.Body.Close() switch resp.StatusCode { case 200: case 201: case 202: default: return nil, fmt.Errorf("http get status: %d", resp.StatusCode) } return junk.Read(resp.Body) } func GetJunkTimeout(userid int64, url string, timeout time.Duration) (junk.Junk, error) { client := http.DefaultClient if develMode { client = develClient } fn := func() (interface{}, error) { at := thefakename if strings.Contains(url, ".well-known/webfinger?resource") { at = "application/jrd+json" } j, err := junkGet(userid, url, junk.GetArgs{ Accept: at, Agent: "honksnonk/5.0; " + serverName, Timeout: timeout, Client: client, }) return j, err } ji, err := flightdeck.Call(url, fn) if err != nil { return nil, err } j := ji.(junk.Junk) return j, nil } func fetchsome(url string) ([]byte, error) { client := http.DefaultClient if develMode { client = develClient } req, err := http.NewRequest("GET", url, nil) if err != nil { ilog.Printf("error fetching %s: %s", url, err) return nil, err } req.Header.Set("User-Agent", "honksnonk/5.0; "+serverName) ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute) defer cancel() req = req.WithContext(ctx) resp, err := client.Do(req) if err != nil { ilog.Printf("error fetching %s: %s", url, err) return nil, err } defer resp.Body.Close() switch resp.StatusCode { case 200: case 201: case 202: default: return nil, fmt.Errorf("http get not 200: %d %s", resp.StatusCode, url) } var buf bytes.Buffer limiter := io.LimitReader(resp.Body, 10*1024*1024) io.Copy(&buf, limiter) return buf.Bytes(), nil } func savedonk(url string, name, desc, media string, localize bool) *Donk { if url == "" { return nil } if donk := finddonk(url); donk != nil { return donk } ilog.Printf("saving donk: %s", url) data := []byte{} if localize { fn := func() (interface{}, error) { return fetchsome(url) } ii, err := flightdeck.Call(url, fn) if err != nil { ilog.Printf("error fetching donk: %s", err) localize = false goto saveit } data = ii.([]byte) if len(data) == 10*1024*1024 { ilog.Printf("truncation likely") } if strings.HasPrefix(media, "image") { img, err := shrinkit(data) if err != nil { ilog.Printf("unable to decode image: %s", err) localize = false data = []byte{} goto saveit } data = img.Data media = "image/" + img.Format } else if media == "application/pdf" { if len(data) > 1000000 { ilog.Printf("not saving large pdf") localize = false data = []byte{} } } else if len(data) > 100000 { ilog.Printf("not saving large attachment") localize = false data = []byte{} } } saveit: fileid, err := savefile(name, desc, url, media, localize, data) if err != nil { elog.Printf("error saving file %s: %s", url, err) return nil } donk := new(Donk) donk.FileID = fileid return donk } func iszonked(userid int64, xid string) bool { var id int64 row := stmtFindZonk.QueryRow(userid, xid) err := row.Scan(&id) if err == nil { return true } if err != sql.ErrNoRows { ilog.Printf("error querying zonk: %s", err) } return false } func needxonk(user *WhatAbout, x *Honk) bool { if rejectxonk(x) { return false } return needxonkid(user, x.XID) } func needbonkid(user *WhatAbout, xid string) bool { return needxonkidX(user, xid, true) } func needxonkid(user *WhatAbout, xid string) bool { return needxonkidX(user, xid, false) } func needxonkidX(user *WhatAbout, xid string, isannounce bool) bool { if !strings.HasPrefix(xid, "https://") { return false } if strings.HasPrefix(xid, user.URL+"/") { return false } if rejectorigin(user.ID, xid, isannounce) { ilog.Printf("rejecting origin: %s", xid) return false } if iszonked(user.ID, xid) { ilog.Printf("already zonked: %s", xid) return false } var id int64 row := stmtFindXonk.QueryRow(user.ID, xid) err := row.Scan(&id) if err == nil { return false } if err != sql.ErrNoRows { ilog.Printf("error querying xonk: %s", err) } return true } func eradicatexonk(userid int64, xid string) { xonk := getxonk(userid, xid) if xonk != nil { deletehonk(xonk.ID) } _, err := stmtSaveZonker.Exec(userid, xid, "zonk") if err != nil { elog.Printf("error eradicating: %s", err) } } func savexonk(x *Honk) { ilog.Printf("saving xonk: %s", x.XID) go handles(x.Honker) go handles(x.Oonker) savehonk(x) } type Box struct { In string Out string Shared string } var boxofboxes = cache.New(cache.Options{Filler: func(ident string) (*Box, bool) { var info string row := stmtGetXonker.QueryRow(ident, "boxes") err := row.Scan(&info) if err != nil { dlog.Printf("need to get boxes for %s", ident) var j junk.Junk j, err = GetJunk(readyLuserOne, ident) if err != nil { dlog.Printf("error getting boxes: %s", err) return nil, false } allinjest(originate(ident), j) row = stmtGetXonker.QueryRow(ident, "boxes") err = row.Scan(&info) } if err == nil { m := strings.Split(info, " ") b := &Box{In: m[0], Out: m[1], Shared: m[2]} return b, true } return nil, false }}) func gimmexonks(user *WhatAbout, outbox string) { dlog.Printf("getting outbox: %s", outbox) j, err := GetJunk(user.ID, outbox) if err != nil { ilog.Printf("error getting outbox: %s", err) return } t, _ := j.GetString("type") origin := originate(outbox) if t == "OrderedCollection" { items, _ := j.GetArray("orderedItems") if items == nil { items, _ = j.GetArray("items") } if items == nil { obj, ok := j.GetMap("first") if ok { items, _ = obj.GetArray("orderedItems") } else { page1, ok := j.GetString("first") if ok { j, err = GetJunk(user.ID, page1) if err != nil { ilog.Printf("error getting page1: %s", err) return } items, _ = j.GetArray("orderedItems") } } } if len(items) > 20 { items = items[0:20] } for i, j := 0, len(items)-1; i < j; i, j = i+1, j-1 { items[i], items[j] = items[j], items[i] } for _, item := range items { obj, ok := item.(junk.Junk) if ok { xonksaver(user, obj, origin) continue } xid, ok := item.(string) if ok { if !needxonkid(user, xid) { continue } obj, err = GetJunk(user.ID, xid) if err != nil { ilog.Printf("error getting item: %s", err) continue } xonksaver(user, obj, originate(xid)) } } } } func newphone(a []string, obj junk.Junk) []string { for _, addr := range []string{"to", "cc", "attributedTo"} { who, _ := obj.GetString(addr) if who != "" { a = append(a, who) } whos, _ := obj.GetArray(addr) for _, w := range whos { who, _ := w.(string) if who != "" { a = append(a, who) } } } return a } func extractattrto(obj junk.Junk) string { who, _ := obj.GetString("attributedTo") if who != "" { return who } o, ok := obj.GetMap("attributedTo") if ok { id, ok := o.GetString("id") if ok { return id } } arr, _ := obj.GetArray("attributedTo") for _, a := range arr { o, ok := a.(junk.Junk) if ok { t, _ := o.GetString("type") id, _ := o.GetString("id") if t == "Person" || t == "" { return id } } s, ok := a.(string) if ok { return s } } return "" } func firstofmany(obj junk.Junk, key string) string { if val, _ := obj.GetString(key); val != "" { return val } if arr, _ := obj.GetArray(key); len(arr) > 0 { val, ok := arr[0].(string) if ok { return val } } return "" } var re_mast0link = regexp.MustCompile(`https://[[:alnum:].]+/users/[[:alnum:]]+/statuses/[[:digit:]]+`) var re_masto1ink = regexp.MustCompile(`https://([[:alnum:].]+)/@([[:alnum:]]+)/([[:digit:]]+)`) var re_misslink = regexp.MustCompile(`https://[[:alnum:].]+/notes/[[:alnum:]]+`) var re_honklink = regexp.MustCompile(`https://[[:alnum:].]+/u/[[:alnum:]]+/h/[[:alnum:]]+`) var re_r0malink = regexp.MustCompile(`https://[[:alnum:].]+/objects/[[:alnum:]-]+`) var re_roma1ink = regexp.MustCompile(`https://[[:alnum:].]+/notice/[[:alnum:]]+`) var re_qtlinks = regexp.MustCompile(`>https://[^\s<]+<`) func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk { depth := 0 maxdepth := 10 currenttid := "" goingup := 0 var xonkxonkfn func(item junk.Junk, origin string, isUpdate bool) *Honk qutify := func(user *WhatAbout, content string) string { if depth >= maxdepth { ilog.Printf("in too deep") return content } // well this is gross malcontent := strings.ReplaceAll(content, ``, "") malcontent = strings.ReplaceAll(malcontent, `