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