all repos — honk @ f3e53adeeffca9d736b1a52fa057bd2b897eae21

my fork of honk

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	"image"
21	"image/png"
22	"net/http"
23	"regexp"
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	writer.Header().Set("Cache-Control", "max-age="+somedays())
56	png.Encode(writer, img)
57}
58
59var re_flags = regexp.MustCompile("flag:[[:alnum:],]+")
60
61func bloat_fixupflags(h *Honk) []Emu {
62	var emus []Emu
63	count := 0
64	h.Noise = re_flags.ReplaceAllStringFunc(h.Noise, func(m string) string {
65		count++
66		var e Emu
67		e.Name = fmt.Sprintf(":flag%d:", count)
68		e.ID = fmt.Sprintf("https://%s/flag/%s", serverName, m[5:])
69		emus = append(emus, e)
70		return e.Name
71	})
72	return emus
73}
74
75func bloat_renderflags(h *Honk) {
76	h.Noise = re_flags.ReplaceAllStringFunc(h.Noise, func(m string) string {
77		code := m[5:]
78		src := fmt.Sprintf("https://%s/flag/%s", serverName, code)
79		return fmt.Sprintf(`<img class="emu" title="%s" src="%s">`, "flag", src)
80	})
81}