all repos — honk @ b2c9ce05ad226ffbe2ee77850c383e9934aa4830

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