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