all repos — honk @ eb1f2a55293816b6dedf413b87b07ff92784491d

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	vert := false
 35	if colors[0] == "vert" {
 36		vert = true
 37		colors = colors[1:]
 38		numcolors--
 39		if numcolors == 0 {
 40			http.Error(writer, "bad flag", 400)
 41			return
 42		}
 43	}
 44	pixels := make([][4]byte, numcolors)
 45	for i := 0; i < numcolors; i++ {
 46		hex := colors[i]
 47		if len(hex) == 3 {
 48			hex = fmt.Sprintf("%c%c%c%c%c%c",
 49				hex[0], hex[0], hex[1], hex[1], hex[2], hex[2])
 50		}
 51		c, _ := strconv.ParseUint(hex, 16, 32)
 52		r := byte(c >> 16 & 0xff)
 53		g := byte(c >> 8 & 0xff)
 54		b := byte(c >> 0 & 0xff)
 55		pixels[i][0] = r
 56		pixels[i][1] = g
 57		pixels[i][2] = b
 58		pixels[i][3] = 255
 59	}
 60
 61	h := 128
 62	w := h * 3 / 2
 63	img := image.NewRGBA(image.Rect(0, 0, w, h))
 64	if vert {
 65		for j := 0; j < w; j++ {
 66			pix := pixels[j*numcolors/w][:]
 67			for i := 0; i < h; i++ {
 68				p := i*img.Stride + j*4
 69				copy(img.Pix[p:], pix)
 70			}
 71		}
 72	} else {
 73		for i := 0; i < h; i++ {
 74			pix := pixels[i*numcolors/h][:]
 75			for j := 0; j < w; j++ {
 76				p := i*img.Stride + j*4
 77				copy(img.Pix[p:], pix)
 78			}
 79		}
 80	}
 81
 82	writer.Header().Set("Cache-Control", "max-age="+somedays())
 83	png.Encode(writer, img)
 84}
 85
 86var re_flags = regexp.MustCompile("flag:[[:alnum:],]+")
 87
 88func bloat_fixupflags(h *Honk) []Emu {
 89	var emus []Emu
 90	count := 0
 91	h.Noise = re_flags.ReplaceAllStringFunc(h.Noise, func(m string) string {
 92		count++
 93		var e Emu
 94		e.Name = fmt.Sprintf(":flag%d:", count)
 95		e.ID = fmt.Sprintf("https://%s/flag/%s", serverName, m[5:])
 96		emus = append(emus, e)
 97		return e.Name
 98	})
 99	return emus
100}
101
102func bloat_renderflags(h *Honk) {
103	h.Noise = re_flags.ReplaceAllStringFunc(h.Noise, func(m string) string {
104		code := m[5:]
105		src := fmt.Sprintf("https://%s/flag/%s", serverName, code)
106		return fmt.Sprintf(`<img class="emu" title="%s" src="%s">`, "flag", src)
107	})
108}