all repos — honk @ c8936bd6d98a5c46e4e313e9978e2f5d9c2ca462

my fork of honk

fun.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	"crypto/rand"
 20	"crypto/rsa"
 21	"database/sql"
 22	"fmt"
 23	"html"
 24	"html/template"
 25	"log"
 26	"regexp"
 27	"strings"
 28	"sync"
 29)
 30
 31func reverbolate(honks []*Honk) {
 32	for _, h := range honks {
 33		h.What += "ed"
 34		if h.Honker == "" {
 35			h.Honker = "https://" + serverName + "/u/" + h.Username
 36			if strings.IndexByte(h.XID, '/') == -1 {
 37				h.URL = h.Honker + "/h/" + h.XID
 38			} else {
 39				h.URL = h.XID
 40			}
 41		} else {
 42			idx := strings.LastIndexByte(h.Honker, '/')
 43			if idx != -1 {
 44				h.Username = honkerhandle(h.Honker)
 45			} else {
 46				h.Username = h.Honker
 47			}
 48			if h.URL == "" {
 49				h.URL = h.XID
 50			}
 51		}
 52		zap := make(map[*Donk]bool)
 53		h.HTML = cleanstring(h.Noise)
 54		emuxifier := func(e string) string {
 55			for _, d := range h.Donks {
 56				if d.Name == e {
 57					zap[d] = true
 58					return fmt.Sprintf(`<img class="emu" title="%s" src="/d/%s">`, d.Name, d.XID)
 59				}
 60			}
 61			return e
 62		}
 63		h.HTML = template.HTML(re_emus.ReplaceAllStringFunc(string(h.HTML), emuxifier))
 64		for i := 0; i < len(h.Donks); i++ {
 65			if zap[h.Donks[i]] {
 66				copy(h.Donks[i:], h.Donks[i+1:])
 67				h.Donks = h.Donks[:len(h.Donks)-1]
 68				i--
 69			}
 70		}
 71	}
 72}
 73
 74func xfiltrate() string {
 75	letters := "BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz1234567891234567891234"
 76	db := opendatabase()
 77	for {
 78		var x int64
 79		var b [16]byte
 80		rand.Read(b[:])
 81		for i, c := range b {
 82			b[i] = letters[c&63]
 83		}
 84		s := string(b[:])
 85		r := db.QueryRow("select honkid from honks where xid = ?", s)
 86		err := r.Scan(&x)
 87		if err == nil {
 88			continue
 89		}
 90		if err != sql.ErrNoRows {
 91			log.Printf("err picking xid: %s", err)
 92			return ""
 93		}
 94		r = db.QueryRow("select fileid from files where name = ?", s)
 95		err = r.Scan(&x)
 96		if err == nil {
 97			continue
 98		}
 99		if err != sql.ErrNoRows {
100			log.Printf("err picking xid: %s", err)
101			return ""
102		}
103		return s
104	}
105}
106
107type Mention struct {
108	who   string
109	where string
110}
111
112var re_mentions = regexp.MustCompile(`@[[:alnum:]]+@[[:alnum:].]+`)
113
114func grapevine(s string) []string {
115	m := re_mentions.FindAllString(s, -1)
116	var mentions []string
117	for i := range m {
118		where := gofish(m[i])
119		if where != "" {
120			mentions = append(mentions, where)
121		}
122	}
123	return mentions
124}
125
126func bunchofgrapes(s string) []Mention {
127	m := re_mentions.FindAllString(s, -1)
128	var mentions []Mention
129	for i := range m {
130		where := gofish(m[i])
131		if where != "" {
132			mentions = append(mentions, Mention{who: m[i], where: where})
133		}
134	}
135	return mentions
136}
137
138type Emu struct {
139	ID   string
140	Name string
141}
142
143var re_link = regexp.MustCompile(`https?://[^\s"]+[\w/)]`)
144var re_emus = regexp.MustCompile(`:[[:alnum:]_]+:`)
145
146func herdofemus(noise string) []Emu {
147	m := re_emus.FindAllString(noise, -1)
148	m = oneofakind(m)
149	var emus []Emu
150	for _, e := range m {
151		fname := e[1 : len(e)-1]
152		url := fmt.Sprintf("https://%s/emu/%s.png", serverName, fname)
153		emus = append(emus, Emu{ID: url, Name: e})
154	}
155	return emus
156}
157
158func obfusbreak(s string) string {
159	s = strings.TrimSpace(s)
160	s = strings.Replace(s, "\r", "", -1)
161	s = html.EscapeString(s)
162	linkfn := func(url string) string {
163		addparen := false
164		adddot := false
165		if strings.HasSuffix(url, ")") && strings.IndexByte(url, '(') == -1 {
166			url = url[:len(url)-1]
167			addparen = true
168		}
169		if strings.HasSuffix(url, ".") {
170			url = url[:len(url)-1]
171			adddot = true
172		}
173		url = fmt.Sprintf(`<a href="%s">%s</a>`, url, url)
174		if adddot {
175			url += "."
176		}
177		if addparen {
178			url += ")"
179		}
180		return url
181	}
182	s = re_link.ReplaceAllStringFunc(s, linkfn)
183
184	s = strings.Replace(s, "\n", "<br>", -1)
185	s = re_mentions.ReplaceAllStringFunc(s, func(m string) string {
186		return fmt.Sprintf(`<a href="%s">%s</a>`, html.EscapeString(gofish(m)),
187			html.EscapeString(m))
188	})
189	return s
190}
191
192var re_unurl = regexp.MustCompile("https://([^/]+).*/([^/]+)")
193
194func honkerhandle(h string) string {
195	m := re_unurl.FindStringSubmatch(h)
196	if len(m) > 2 {
197		return fmt.Sprintf("%s@%s", m[2], m[1])
198	}
199	return h
200}
201
202func prepend(s string, x []string) []string {
203	return append([]string{s}, x...)
204}
205
206func oneofakind(a []string) []string {
207	var x []string
208	for n, s := range a {
209		if s != "" {
210			x = append(x, s)
211			for i := n + 1; i < len(a); i++ {
212				if a[i] == s {
213					a[i] = ""
214				}
215			}
216		}
217	}
218	return x
219}
220
221var ziggies = make(map[string]*rsa.PrivateKey)
222var zaggies = make(map[string]*rsa.PublicKey)
223var ziggylock sync.Mutex
224
225func ziggy(username string) (keyname string, key *rsa.PrivateKey) {
226	ziggylock.Lock()
227	key = ziggies[username]
228	ziggylock.Unlock()
229	if key == nil {
230		db := opendatabase()
231		row := db.QueryRow("select seckey from users where username = ?", username)
232		var data string
233		row.Scan(&data)
234		var err error
235		key, _, err = pez(data)
236		if err != nil {
237			log.Printf("error decoding %s seckey: %s", username, err)
238			return
239		}
240		ziggylock.Lock()
241		ziggies[username] = key
242		ziggylock.Unlock()
243	}
244	keyname = fmt.Sprintf("https://%s/u/%s#key", serverName, username)
245	return
246}
247
248func zaggy(keyname string) (key *rsa.PublicKey) {
249	ziggylock.Lock()
250	key = zaggies[keyname]
251	ziggylock.Unlock()
252	if key != nil {
253		return
254	}
255	db := opendatabase()
256	row := db.QueryRow("select pubkey from honkers where flavor = 'key' and xid = ?", keyname)
257	var data string
258	err := row.Scan(&data)
259	savekey := false
260	if err != nil {
261		log.Printf("hitting the webs for missing pubkey: %s", keyname)
262		j, err := GetJunk(keyname)
263		if err != nil {
264			log.Printf("error getting %s pubkey: %s", keyname, err)
265			return
266		}
267		var ok bool
268		data, ok = jsonfindstring(j, []string{"publicKey", "publicKeyPem"})
269		if !ok {
270			log.Printf("error getting %s pubkey", keyname)
271			return
272		}
273		_, ok = jsonfindstring(j, []string{"publicKey", "owner"})
274		if !ok {
275			log.Printf("error getting %s pubkey owner", keyname)
276			return
277		}
278		savekey = true
279	}
280	_, key, err = pez(data)
281	if err != nil {
282		log.Printf("error decoding %s pubkey: %s", keyname, err)
283		return
284	}
285	ziggylock.Lock()
286	zaggies[keyname] = key
287	ziggylock.Unlock()
288	if savekey {
289		db.Exec("insert into honkers (name, xid, flavor, pubkey) values (?, ?, ?, ?)",
290			"", keyname, "key", data)
291	}
292	return
293}
294
295func keymatch(keyname string, actor string) bool {
296	return strings.HasPrefix(keyname, actor)
297}