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 return fmt.Sprintf(`<a href="%s">%s</a>`, html.EscapeString(gofish(m)),
185 html.EscapeString(m))
186 })
187 return s
188}
189
190var re_unurl = regexp.MustCompile("https://([^/]+).*/([^/]+)")
191
192func honkerhandle(h string) string {
193 m := re_unurl.FindStringSubmatch(h)
194 if len(m) > 2 {
195 return fmt.Sprintf("%s@%s", m[2], m[1])
196 }
197 return h
198}
199
200func prepend(s string, x []string) []string {
201 return append([]string{s}, x...)
202}
203
204func oneofakind(a []string) []string {
205 var x []string
206 for n, s := range a {
207 if s != "" {
208 x = append(x, s)
209 for i := n + 1; i < len(a); i++ {
210 if a[i] == s {
211 a[i] = ""
212 }
213 }
214 }
215 }
216 return x
217}
218
219var ziggies = make(map[string]*rsa.PrivateKey)
220var zaggies = make(map[string]*rsa.PublicKey)
221var ziggylock sync.Mutex
222
223func ziggy(username string) (keyname string, key *rsa.PrivateKey) {
224 ziggylock.Lock()
225 key = ziggies[username]
226 ziggylock.Unlock()
227 if key == nil {
228 db := opendatabase()
229 row := db.QueryRow("select seckey from users where username = ?", username)
230 var data string
231 row.Scan(&data)
232 var err error
233 key, _, err = pez(data)
234 if err != nil {
235 log.Printf("error decoding %s seckey: %s", username, err)
236 return
237 }
238 ziggylock.Lock()
239 ziggies[username] = key
240 ziggylock.Unlock()
241 }
242 keyname = fmt.Sprintf("https://%s/u/%s#key", serverName, username)
243 return
244}
245
246func zaggy(keyname string) (key *rsa.PublicKey) {
247 ziggylock.Lock()
248 key = zaggies[keyname]
249 ziggylock.Unlock()
250 if key != nil {
251 return
252 }
253 db := opendatabase()
254 row := db.QueryRow("select pubkey from honkers where flavor = 'key' and xid = ?", keyname)
255 var data string
256 err := row.Scan(&data)
257 savekey := false
258 if err != nil {
259 log.Printf("hitting the webs for missing pubkey: %s", keyname)
260 j, err := GetJunk(keyname)
261 if err != nil {
262 log.Printf("error getting %s pubkey: %s", keyname, err)
263 return
264 }
265 var ok bool
266 data, ok = jsonfindstring(j, []string{"publicKey", "publicKeyPem"})
267 if !ok {
268 log.Printf("error getting %s pubkey", keyname)
269 return
270 }
271 _, ok = jsonfindstring(j, []string{"publicKey", "owner"})
272 if !ok {
273 log.Printf("error getting %s pubkey owner", keyname)
274 return
275 }
276 savekey = true
277 }
278 _, key, err = pez(data)
279 if err != nil {
280 log.Printf("error decoding %s pubkey: %s", keyname, err)
281 return
282 }
283 ziggylock.Lock()
284 zaggies[keyname] = key
285 ziggylock.Unlock()
286 if savekey {
287 db.Exec("insert into honkers (name, xid, flavor, pubkey) values (?, ?, ?, ?)",
288 "", keyname, "key", data)
289 }
290 return
291}
292
293func keymatch(keyname string, actor string) bool {
294 return strings.HasPrefix(keyname, actor)
295}