all repos — honk @ master

my fork of honk

avatar.go (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
//
// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

package main

import (
	"bufio"
	"bytes"
	"crypto/sha512"
	"fmt"
	"image"
	"image/png"
	"net/http"
	"regexp"
	"strconv"
	"strings"

	"github.com/gorilla/mux"
)

var avatarcolors = [4][4]byte{
	{80, 156, 147, 255},
	{80, 156, 147, 255},
	{57, 106, 101, 255},
	{96, 169, 161, 255},
}

func loadAvatarColors() {
	var colors string
	getconfig("avatarcolors", &colors)
	if colors == "" {
		return
	}
	r := bufio.NewReader(strings.NewReader(colors))
	for i := 0; i < 4; i++ {
		l, _ := r.ReadString(' ')
		for l == " " {
			l, _ = r.ReadString(' ')
		}
		l = strings.Trim(l, "# \n")
		if len(l) == 6 {
			l = l + "ff"
		}
		c, err := strconv.ParseUint(l, 16, 32)
		if err != nil {
			elog.Printf("error reading avatar color %d: %s", i, err)
			continue
		}
		avatarcolors[i][0] = byte(c >> 24 & 0xff)
		avatarcolors[i][1] = byte(c >> 16 & 0xff)
		avatarcolors[i][2] = byte(c >> 8 & 0xff)
		avatarcolors[i][3] = byte(c >> 0 & 0xff)
	}
}

func genAvatar(name string, hex bool) []byte {
	h := sha512.New()
	h.Write([]byte(name))
	s := h.Sum(nil)
	img := image.NewNRGBA(image.Rect(0, 0, 64, 64))
	for i := 0; i < 64; i++ {
		for j := 0; j < 64; j++ {
			p := i*img.Stride + j*4
			if hex {
				tan := 0.577
				if i < 32 {
					if j < 17-int(float64(i)*tan) || j > 46+int(float64(i)*tan) {
						img.Pix[p+0] = 0
						img.Pix[p+1] = 0
						img.Pix[p+2] = 0
						img.Pix[p+3] = 255
						continue
					}
				} else {
					if j < 17-int(float64(64-i)*tan) || j > 46+int(float64(64-i)*tan) {
						img.Pix[p+0] = 0
						img.Pix[p+1] = 0
						img.Pix[p+2] = 0
						img.Pix[p+3] = 255
						continue

					}
				}
			}
			xx := i/16*16 + j/16
			x := s[xx]
			if x < 64 {
				img.Pix[p+0] = avatarcolors[0][0]
				img.Pix[p+1] = avatarcolors[0][1]
				img.Pix[p+2] = avatarcolors[0][2]
				img.Pix[p+3] = avatarcolors[0][3]
			} else if x < 128 {
				img.Pix[p+0] = avatarcolors[1][0]
				img.Pix[p+1] = avatarcolors[1][1]
				img.Pix[p+2] = avatarcolors[1][2]
				img.Pix[p+3] = avatarcolors[1][3]
			} else if x < 192 {
				img.Pix[p+0] = avatarcolors[2][0]
				img.Pix[p+1] = avatarcolors[2][1]
				img.Pix[p+2] = avatarcolors[2][2]
				img.Pix[p+3] = avatarcolors[2][3]
			} else {
				img.Pix[p+0] = avatarcolors[3][0]
				img.Pix[p+1] = avatarcolors[3][1]
				img.Pix[p+2] = avatarcolors[3][2]
				img.Pix[p+3] = avatarcolors[3][3]
			}
		}
	}
	var buf bytes.Buffer
	png.Encode(&buf, img)
	return buf.Bytes()
}

func showflag(writer http.ResponseWriter, req *http.Request) {
	code := mux.Vars(req)["code"]
	colors := strings.Split(code, ",")
	numcolors := len(colors)
	vert := false
	if colors[0] == "vert" {
		vert = true
		colors = colors[1:]
		numcolors--
		if numcolors == 0 {
			http.Error(writer, "bad flag", 400)
			return
		}
	}
	pixels := make([][4]byte, numcolors)
	for i := 0; i < numcolors; i++ {
		hex := colors[i]
		if len(hex) == 3 {
			hex = fmt.Sprintf("%c%c%c%c%c%c",
				hex[0], hex[0], hex[1], hex[1], hex[2], hex[2])
		}
		c, _ := strconv.ParseUint(hex, 16, 32)
		r := byte(c >> 16 & 0xff)
		g := byte(c >> 8 & 0xff)
		b := byte(c >> 0 & 0xff)
		pixels[i][0] = r
		pixels[i][1] = g
		pixels[i][2] = b
		pixels[i][3] = 255
	}

	h := 128
	w := h * 3 / 2
	img := image.NewRGBA(image.Rect(0, 0, w, h))
	if vert {
		for j := 0; j < w; j++ {
			pix := pixels[j*numcolors/w][:]
			for i := 0; i < h; i++ {
				p := i*img.Stride + j*4
				copy(img.Pix[p:], pix)
			}
		}
	} else {
		for i := 0; i < h; i++ {
			pix := pixels[i*numcolors/h][:]
			for j := 0; j < w; j++ {
				p := i*img.Stride + j*4
				copy(img.Pix[p:], pix)
			}
		}
	}

	writer.Header().Set("Cache-Control", "max-age="+somedays())
	png.Encode(writer, img)
}

var re_flags = regexp.MustCompile("flag:[[:alnum:],]+")

func fixupflags(h *Honk) []Emu {
	var emus []Emu
	count := 0
	h.Noise = re_flags.ReplaceAllStringFunc(h.Noise, func(m string) string {
		count++
		var e Emu
		e.Name = fmt.Sprintf(":flag%d:", count)
		e.ID = fmt.Sprintf("https://%s/flag/%s", serverName, m[5:])
		emus = append(emus, e)
		return e.Name
	})
	return emus
}

func renderflags(h *Honk) {
	h.Noise = re_flags.ReplaceAllStringFunc(h.Noise, func(m string) string {
		code := m[5:]
		src := fmt.Sprintf("https://%s/flag/%s", serverName, code)
		return fmt.Sprintf(`<img class="emu" title="%s" src="%s">`, "flag", src)
	})
}