all repos — honk @ d451bb63e456ae28258e4d2d7226502593b4651e

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