bloat.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 "fmt"
20 "regexp"
21 "image"
22 "image/png"
23 "net/http"
24 "strconv"
25 "strings"
26
27 "github.com/gorilla/mux"
28)
29
30func bloat_showflag(writer http.ResponseWriter, req *http.Request) {
31 code := mux.Vars(req)["code"]
32 colors := strings.Split(code, ",")
33 numcolors := len(colors)
34 h := (128 / numcolors) * numcolors
35 w := h * 3 / 2
36 img := image.NewRGBA(image.Rect(0, 0, w, h))
37 for i := 0; i < h; i++ {
38 hex := colors[i*numcolors/h]
39 if len(hex) == 3 {
40 hex = fmt.Sprintf("%c%c%c%c%c%c",
41 hex[0], hex[0], hex[1], hex[1], hex[2], hex[2])
42 }
43 c, _ := strconv.ParseUint(hex, 16, 32)
44 r := byte(c >> 16 & 0xff)
45 g := byte(c >> 8 & 0xff)
46 b := byte(c >> 0 & 0xff)
47 for j := 0; j < w; j++ {
48 p := i*img.Stride + j*4
49 img.Pix[p+0] = r
50 img.Pix[p+1] = g
51 img.Pix[p+2] = b
52 img.Pix[p+3] = 255
53 }
54 }
55 png.Encode(writer, img)
56}
57
58var re_flags = regexp.MustCompile("flag:[[:alnum:],]+")
59
60func bloat_fixupflags(h *Honk) []Emu {
61 var emus []Emu
62 count := 0
63 h.Noise = re_flags.ReplaceAllStringFunc(h.Noise, func(m string) string {
64 count++
65 var e Emu
66 e.Name = fmt.Sprintf(":flag%d:", count)
67 e.ID = fmt.Sprintf("https://%s/flag/%s", serverName, m[5:])
68 emus = append(emus, e)
69 return e.Name
70 })
71 return emus
72}
73
74func bloat_renderflags(h *Honk) {
75 h.Noise = re_flags.ReplaceAllStringFunc(h.Noise, func(m string) string {
76 code := m[5:]
77 src := fmt.Sprintf("https://%s/flag/%s", serverName, code)
78 return fmt.Sprintf(`<img class="emu" title="%s" src="%s">`, "flag", src)
79 })
80}
81