-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
137 lines (121 loc) · 2.65 KB
/
main.go
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
package main
import (
"flag"
"io/ioutil"
"log"
"math/rand"
"net"
"path/filepath"
"regexp"
"strings"
)
var (
listenAddr = flag.String("listen", ":2000", "local listen address")
dataDir = flag.String("datadir", "/usr/local/share/ponyserver", "path to data directory")
)
type Pony struct {
Name string
Image string
Quotes []string
}
var ponies = make(map[string]*Pony)
var ponynamesWithQuotes []string
func main() {
flag.Parse()
log.SetPrefix("ponyserver: ")
preloadPonies()
if len(ponies) == 0 {
log.Fatal("No ponies found in datadir ", *dataDir)
}
l, err := net.Listen("tcp", *listenAddr)
if err != nil {
log.Fatal(err)
}
log.Println("listen", *listenAddr)
for {
conn, err := l.Accept()
if err != nil {
log.Fatal(err)
}
log.Println("accept conn", "localAddr.", conn.LocalAddr(), "remoteAddr.", conn.RemoteAddr())
go handler(conn)
}
}
func handler(c net.Conn) {
defer c.Close()
rb := make([]byte, 512)
_, err := c.Read(rb)
if err != nil {
log.Fatal(err)
}
//request := strings.Trim(string(rb), "\r\n"+string(0))
p := ponies[ponynamesWithQuotes[rand.Intn(len(ponynamesWithQuotes))]]
regex, err := regexp.Compile(`\$balloon\d+\$`)
if err != nil {
log.Fatal(err)
}
p.Image = regex.ReplaceAllString(p.Image, drawBalloon(p.Quotes[rand.Intn(len(p.Quotes))]))
r := strings.NewReplacer("$\\$", "\\")
_, err = c.Write([]byte(r.Replace(p.Image)))
if err != nil {
log.Fatal(err)
}
}
func drawBalloon(m string) string {
mess := strings.Split(m, "\n")
max := 0
for _, line := range mess {
if max <= len(line) {
max = len(line)
}
}
l := max + 2
balloon := "┌" + strings.Repeat("─", l) + "┐\n"
for _, line := range mess {
space := max - len(line)
if space == max { // empty line
break
}
balloon += "│ " + line + strings.Repeat(" ", space) + " │\n"
}
balloon += "└" + strings.Repeat("─", l) + "┘"
return balloon
}
func preloadPonies() {
pf, err := filepath.Glob(*dataDir + "/ponies/*.pony")
if err != nil {
log.Fatal(err)
}
for _, f := range pf {
b, err := ioutil.ReadFile(f)
if err != nil {
log.Fatal(err)
}
n := strings.TrimRight(filepath.Base(f), ".pony")
ponies[n] = &Pony{
Name: n,
Image: string(b),
Quotes: preloadQuotes(n),
}
}
for _, p := range ponies {
if len(p.Quotes) > 0 {
ponynamesWithQuotes = append(ponynamesWithQuotes, p.Name)
}
}
}
func preloadQuotes(n string) []string {
pf, err := filepath.Glob(*dataDir + "/ponyquotes/" + n + ".*")
if err != nil {
log.Fatal(err)
}
var quotes []string
for _, f := range pf {
b, err := ioutil.ReadFile(f)
if err != nil {
log.Fatal(err)
}
quotes = append(quotes, string(b))
}
return quotes
}