ソースを参照

багфикс и favicon

kpmy 9 年 前
コミット
2835343e6c
3 ファイル変更101 行追加1 行削除
  1. 1 1
      bot.go
  2. 92 0
      ico.go
  3. 8 0
      neo.go

+ 1 - 1
bot.go

@@ -24,7 +24,7 @@ func bot(st stream.Stream) error {
 		st.Ring(conv(func(_e entity.Entity) {
 			switch e := _e.(type) {
 			case *entity.Message:
-				if strings.HasPrefix(e.From, ROOM+"/") {
+				if strings.HasPrefix(e.From, ROOM+"/") && e.Body != "" {
 					sender := strings.TrimPrefix(e.From, ROOM+"/")
 					um := muc.UserMapping()
 					user := sender

+ 92 - 0
ico.go

@@ -0,0 +1,92 @@
+package main
+
+import (
+	"image"
+	"image/color"
+	"image/png"
+	"io"
+	"math/rand"
+)
+
+const AvatarSize = 24
+
+func Splatter(avatar *image.RGBA, nameBytes []byte, pixelColor color.RGBA) {
+
+	// A somewhat random number based on the username.
+	var nameSum int64
+	for i := range nameBytes {
+		nameSum += int64(nameBytes[i])
+	}
+
+	// Use said number to keep random-ness deterministic for a given name
+	rand.Seed(nameSum)
+
+	// Make the "splatter"
+	for y := 0; y < AvatarSize; y++ {
+		for x := 0; x < AvatarSize; x++ {
+			if ((x + y) % 2) == 0 {
+				if rand.Intn(2) == 1 {
+					avatar.SetRGBA(x, y, pixelColor)
+				}
+			}
+		}
+	}
+
+	// Mirror left half to right half
+	for y := 0; y < AvatarSize; y++ {
+		for x := 0; x < AvatarSize; x++ {
+			if x < AvatarSize/2 {
+				avatar.Set(AvatarSize-x-1, y, avatar.At(x, y))
+			}
+		}
+	}
+
+	// Mirror top to bottom
+	for y := 0; y < AvatarSize; y++ {
+		for x := 0; x < AvatarSize; x++ {
+			if y < AvatarSize/2 {
+				avatar.Set(x, AvatarSize-y-1, avatar.At(x, y))
+			}
+		}
+	}
+}
+
+func PaintBG(avatar *image.RGBA, bgColor color.RGBA) {
+	for y := 0; y < AvatarSize; y++ {
+		for x := 0; x < AvatarSize; x++ {
+			avatar.SetRGBA(x, y, bgColor)
+		}
+	}
+}
+
+func CalcPixelColor(nameBytes []byte) (pixelColor color.RGBA) {
+	pixelColor.A = 255
+
+	var mutator = byte((len(nameBytes) * 4))
+
+	pixelColor.R = nameBytes[0] * mutator
+	pixelColor.G = nameBytes[1] * mutator
+	pixelColor.B = nameBytes[2] * mutator
+
+	return
+}
+
+func CalcBGColor(nameBytes []byte) (bgColor color.RGBA) {
+	bgColor.A = 255
+
+	var mutator = byte((len(nameBytes) * 2))
+
+	bgColor.R = nameBytes[0] * mutator
+	bgColor.G = nameBytes[1] * mutator
+	bgColor.B = nameBytes[2] * mutator
+
+	return
+}
+
+func ico(wr io.Writer) {
+	nameBytes := []byte("golang@conference.jabber.ru/xep")
+	avatar := image.NewRGBA(image.Rect(0, 0, AvatarSize, AvatarSize))
+	PaintBG(avatar, CalcBGColor(nameBytes))
+	Splatter(avatar, nameBytes, CalcPixelColor(nameBytes))
+	png.Encode(wr, avatar)
+}

+ 8 - 0
neo.go

@@ -1,11 +1,13 @@
 package main
 
 import (
+	"bytes"
 	"github.com/ivpusic/golog"
 	"github.com/ivpusic/neo"
 	"github.com/ivpusic/neo-cors"
 	"github.com/ivpusic/neo/middlewares/logger"
 	"html/template"
+	"io"
 	"os"
 	"path/filepath"
 	"sort"
@@ -53,6 +55,12 @@ func neo_server(wg *sync.WaitGroup) {
 	app.Use(cors.Init())
 	//app.Templates("tpl/*.tpl") //кэширует в этом месте и далее не загружает с диска, сука
 	app.Serve("/static", "static")
+	app.Get("/favicon.ico", func(ctx *neo.Ctx) (int, error) {
+		buf := bytes.NewBuffer(nil)
+		ico(buf)
+		io.Copy(ctx.Res, buf)
+		return 200, nil
+	})
 	app.Get("/", func(ctx *neo.Ctx) (int, error) {
 		data := struct {
 			Posts []Post