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.Panicf("err picking xid: %s", err)
92 }
93 r = db.QueryRow("select fileid from files where xid = ?", s)
94 err = r.Scan(&x)
95 if err == nil {
96 continue
97 }
98 if err != sql.ErrNoRows {
99 log.Panicf("err picking xid: %s", err)
100 }
101 return s
102 }
103}
104
105type Mention struct {
106 who string
107 where string
108}
109
110var re_mentions = regexp.MustCompile(`@[[:alnum:]]+@[[:alnum:].]+`)
111
112func grapevine(s string) []string {
113 m := re_mentions.FindAllString(s, -1)
114 var mentions []string
115 for i := range m {
116 where := gofish(m[i])
117 if where != "" {
118 mentions = append(mentions, where)
119 }
120 }
121 return mentions
122}
123
124func bunchofgrapes(s string) []Mention {
125 m := re_mentions.FindAllString(s, -1)
126 var mentions []Mention
127 for i := range m {
128 where := gofish(m[i])
129 if where != "" {
130 mentions = append(mentions, Mention{who: m[i], where: where})
131 }
132 }
133 return mentions
134}
135
136type Emu struct {
137 ID string
138 Name string
139}
140
141var re_link = regexp.MustCompile(`https?://[^\s"]+[\w/)]`)
142var re_emus = regexp.MustCompile(`:[[:alnum:]_]+:`)
143
144func herdofemus(noise string) []Emu {
145 m := re_emus.FindAllString(noise, -1)
146 m = oneofakind(m)
147 var emus []Emu
148 for _, e := range m {
149 fname := e[1 : len(e)-1]
150 url := fmt.Sprintf("https://%s/emu/%s.png", serverName, fname)
151 emus = append(emus, Emu{ID: url, Name: e})
152 }
153 return emus
154}
155
156func obfusbreak(s string) string {
157 s = strings.TrimSpace(s)
158 s = strings.Replace(s, "\r", "", -1)
159 s = html.EscapeString(s)
160 linkfn := func(url string) string {
161 addparen := false
162 adddot := false
163 if strings.HasSuffix(url, ")") && strings.IndexByte(url, '(') == -1 {
164 url = url[:len(url)-1]
165 addparen = true
166 }
167 if strings.HasSuffix(url, ".") {
168 url = url[:len(url)-1]
169 adddot = true
170 }
171 url = fmt.Sprintf(`<a href="%s">%s</a>`, url, url)
172 if adddot {
173 url += "."
174 }
175 if addparen {
176 url += ")"
177 }
178 return url
179 }
180 s = re_link.ReplaceAllStringFunc(s, linkfn)
181
182 s = strings.Replace(s, "\n", "<br>", -1)
183 s = re_mentions.ReplaceAllStringFunc(s, func(m string) string {
184 where := gofish(m)
185 if where == "" {
186 return m
187 }
188 who := m[0:1 + strings.IndexByte(m[1:], '@')]
189 return fmt.Sprintf(`<span class="h-card"><a class="u-url mention" href="%s">%s</a></span>`,
190 html.EscapeString(where), html.EscapeString(who))
191 })
192 return s
193}
194
195var re_unurl = regexp.MustCompile("https://([^/]+).*/([^/]+)")
196
197func honkerhandle(h string) string {
198 m := re_unurl.FindStringSubmatch(h)
199 if len(m) > 2 {
200 return fmt.Sprintf("%s@%s", m[2], m[1])
201 }
202 return h
203}
204
205func prepend(s string, x []string) []string {
206 return append([]string{s}, x...)
207}
208
209func oneofakind(a []string) []string {
210 var x []string
211 for n, s := range a {
212 if s != "" {
213 x = append(x, s)
214 for i := n + 1; i < len(a); i++ {
215 if a[i] == s {
216 a[i] = ""
217 }
218 }
219 }
220 }
221 return x
222}
223
224var ziggies = make(map[string]*rsa.PrivateKey)
225var zaggies = make(map[string]*rsa.PublicKey)
226var ziggylock sync.Mutex
227
228func ziggy(username string) (keyname string, key *rsa.PrivateKey) {
229 ziggylock.Lock()
230 key = ziggies[username]
231 ziggylock.Unlock()
232 if key == nil {
233 db := opendatabase()
234 row := db.QueryRow("select seckey from users where username = ?", username)
235 var data string
236 row.Scan(&data)
237 var err error
238 key, _, err = pez(data)
239 if err != nil {
240 log.Printf("error decoding %s seckey: %s", username, err)
241 return
242 }
243 ziggylock.Lock()
244 ziggies[username] = key
245 ziggylock.Unlock()
246 }
247 keyname = fmt.Sprintf("https://%s/u/%s#key", serverName, username)
248 return
249}
250
251func zaggy(keyname string) (key *rsa.PublicKey) {
252 ziggylock.Lock()
253 key = zaggies[keyname]
254 ziggylock.Unlock()
255 if key != nil {
256 return
257 }
258 db := opendatabase()
259 row := db.QueryRow("select pubkey from honkers where flavor = 'key' and xid = ?", keyname)
260 var data string
261 err := row.Scan(&data)
262 savekey := false
263 if err != nil {
264 log.Printf("hitting the webs for missing pubkey: %s", keyname)
265 j, err := GetJunk(keyname)
266 if err != nil {
267 log.Printf("error getting %s pubkey: %s", keyname, err)
268 return
269 }
270 var ok bool
271 data, ok = jsonfindstring(j, []string{"publicKey", "publicKeyPem"})
272 if !ok {
273 log.Printf("error getting %s pubkey", keyname)
274 return
275 }
276 _, ok = jsonfindstring(j, []string{"publicKey", "owner"})
277 if !ok {
278 log.Printf("error getting %s pubkey owner", keyname)
279 return
280 }
281 savekey = true
282 }
283 _, key, err = pez(data)
284 if err != nil {
285 log.Printf("error decoding %s pubkey: %s", keyname, err)
286 return
287 }
288 ziggylock.Lock()
289 zaggies[keyname] = key
290 ziggylock.Unlock()
291 if savekey {
292 db.Exec("insert into honkers (name, xid, flavor, pubkey) values (?, ?, ?, ?)",
293 "", keyname, "key", data)
294 }
295 return
296}
297
298func keymatch(keyname string, actor string) bool {
299 return strings.HasPrefix(keyname, actor)
300}