aboutsummaryrefslogtreecommitdiffgithub
path: root/tools/figlet.go
blob: 4616e7ede0b6c695a310eee74d064c4954cbff02 (plain)
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
// toolbag - dynamic tools for my website
// Copyright (C) 2015 Austin Adams
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package tools

import (
	"errors"
	"html/template"
	"net/http"
	"strings"

	"code.austinjadams.com/execd"
	tb "code.austinjadams.com/toolbag"
)

// by default, limit request bodies to 4KiB (2^12 bytes)
const defaultMaxReqBody int64 = 1 << 12

type Figlet struct {
	defaultFont string
	fonts       map[string][]string
	templ       *template.Template
	net, addr   string
	args        struct {
		maxReqBody          int64
		template, unix, tcp string
	}
}

func NewFiglet() *Figlet {
	return &Figlet{}
}

func (f *Figlet) Name() string { return "figlet" }
func (f *Figlet) Desc() string { return "a web frontend to figlet" }
func (f *Figlet) Path() string { return "/" + f.Name() }
func (f *Figlet) AddArgs(toolbag *tb.ToolBag) {
	toolbag.StringVar(&f.args.template, tb.Arg(f, "template"), "", "path to template")
	toolbag.StringVar(&f.args.unix, tb.Arg(f, "unix"), "", "path to unix socket to execd")
	toolbag.StringVar(&f.args.tcp, tb.Arg(f, "tcp"), "", "tcp address to execd")
	toolbag.Int64Var(&f.args.maxReqBody, tb.Arg(f, "maxReqBody"), defaultMaxReqBody, "maximum size of a request body in bytes")
}

func (f *Figlet) makeClient() (*execd.Client, error) {
	return execd.DialClient(f.net, f.addr)
}

func (f *Figlet) fontCategory(needle string) string {
	for category, fonts := range f.fonts {
		for _, font := range fonts {
			if needle == font {
				return category
			}
		}
	}
	// no match
	return ""
}

func (f *Figlet) parseArgs() error {
	if f.args.template == "" {
		return errors.New("missing template arg")
	}
	if (f.args.unix == "") == (f.args.tcp == "") {
		return errors.New("specify either unix or tcp, but not both")
	}

	templ, err := template.ParseFiles(f.args.template)
	if err != nil {
		return err
	}
	f.templ = templ

	if f.args.unix != "" {
		f.net = "unix"
		f.addr = f.args.unix
	} else {
		f.net = "tcp"
		f.addr = f.args.tcp
	}

	return nil
}

func (f *Figlet) findDefaultFont(client *execd.Client) error {
	// find default font
	defaultFont, err := client.ExecString("", "fig", "default")
	if err != nil {
		return err
	}
	f.defaultFont = strings.TrimSpace(defaultFont)

	return nil
}

func (f *Figlet) findFonts(client *execd.Client) error {
	// find categories of fonts
	output, err := client.ExecString("", "fig", "ls")
	if err != nil {
		return err
	}
	f.fonts = make(map[string][]string)
	for _, v := range splitLines(output) {
		f.fonts[v] = nil
	}

	// find the fonts in each category
	for category, _ := range f.fonts {
		output, err = client.ExecString("", "fig", "ls", category)
		if err != nil {
			return err
		}
		f.fonts[category] = splitLines(output)
	}

	return nil
}

func (f *Figlet) Init() error {
	err := f.parseArgs()
	if err != nil {
		return err
	}

	client, err := f.makeClient()
	if err != nil {
		return err
	}

	err = f.findDefaultFont(client)
	if err != nil {
		return err
	}

	err = f.findFonts(client)
	if err != nil {
		return err
	}

	return nil
}

// serve
func (f *Figlet) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	r.Body = http.MaxBytesReader(w, r.Body, f.args.maxReqBody)
	err := r.ParseForm()
	if err != nil {
		tb.Whine(f, w, err)
		return
	}

	font := r.PostFormValue("font")
	text := r.PostFormValue("text")
	result := ""

	if font == "" {
		font = f.defaultFont
	}

	if text != "" {
		category := f.fontCategory(font)
		if category == "" {
			tb.WhineString(f, w, "nice try, lad")
			return
		}

		client, err := f.makeClient()
		if err != nil {
			tb.Whine(f, w, err)
			return
		}
		// otherwise
		defer client.Close()

		result, err = client.ExecString(text, "fig", category, font)
		if err != nil {
			tb.Whine(f, w, err)
			return
		}
	}

	err = f.templ.Execute(w, &struct {
		Font         string
		Fonts        map[string][]string
		Text, Result string
	}{font, f.fonts, text, result})
	if err != nil {
		tb.LogWhine(f, err)
		return
	}
}