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
|
module app;
import jdtalk.core;
import vibe.core.core : runApplication;
import vibe.http.server;
import vibe.http.router;
import std.algorithm;
import std.string;
import std.process : environment;
import std.conv : to;
enum MAX_LIMIT = 100_000;
enum MAX_LIMIT_PATTERN = 100;
enum MAX_LIMIT_SALAD = 512;
enum MAX_LIMIT_FORMAT = 256;
enum MAX_LIMIT_ACRONYM = 25;
__gshared string dataRoot;
__gshared dict_t dict;
void handleRequest(scope HTTPServerRequest req, scope HTTPServerResponse res)
{
if (req.path == "/") {
long i = 0;
auto limit = req.query.get("limit", "1").to!long;
auto custom_format = req.query.get("format", null);
auto acronym = req.query.get("acronym", null);
auto pattern = req.query.get("pattern", null);
auto exactMatch = req.query.get("exact", "false").to!bool;
auto salad = req.query.get("salad", "0").to!int;
auto rCase = req.query.get("rcase", "false").to!bool;
auto hCase = req.query.get("hcase", "false").to!bool;
auto haxor = req.query.get("leet", "false").to!bool;
if (custom_format !is null && custom_format.length > MAX_LIMIT_FORMAT) {
res.bodyWriter.write(format("Requested string is too long: %d (MAX: %d)\n",
custom_format.length, MAX_LIMIT_FORMAT));
return;
}
if (acronym.length > MAX_LIMIT_ACRONYM) {
res.bodyWriter.write(format("Requested acroynm too long: %d (MAX: %d)\n",
acronym.length, MAX_LIMIT_ACRONYM));
return;
}
if (salad > MAX_LIMIT_SALAD) {
res.bodyWriter.write(format("Requested too much salad: %d (MAX: %d)\n",
salad, MAX_LIMIT_SALAD));
return;
}
if (pattern is null && limit > MAX_LIMIT) {
res.bodyWriter.write(format("Requested too many: %d (MAX: %d)\n",
limit, MAX_LIMIT));
return;
}
else if (pattern !is null && limit > MAX_LIMIT_PATTERN) {
res.bodyWriter.write(format("Requested too many for pattern: %d (MAX: %d)\n",
limit, MAX_LIMIT_PATTERN));
return;
}
if (pattern !is null && !searchDict(dict, pattern)) {
res.bodyWriter.write(format("Word not available in dictionary: %s\n", pattern));
return;
}
if (acronym !is null) {
char result = 0;
if ((result = acronymSafe(dict, acronym, pattern)) > 0) {
res.bodyWriter.write(format("No words will start with: '%c'", result));
return;
}
}
while(true) {
string output;
if (salad) {
output = talkSalad(dict, salad);
}
else if (acronym) {
output = talkAcronym(dict, acronym);
}
else if (custom_format) {
output = talkf(dict, custom_format);
}
else {
output = talkf(dict, "%a %n %d %v");
}
if (pattern !is null) {
if (exactMatch && !hasWord(pattern, output)) {
continue;
}
else if (!exactMatch && !output.canFind(pattern)) {
continue;
}
}
if (rCase) {
output = randomCase(output);
}
else if (hCase) {
output = hillCase(output);
}
else if (haxor) {
output = leetSpeak(output);
}
if (acronym) {
// Capitalize each word in the acronym
string[] parts;
foreach (word; output.split(" ")) {
parts ~= word.capitalize;
}
output = parts.join(" ");
}
res.bodyWriter.write(format("%s\n", output));
if (limit > 0) {
i++;
if (i == limit)
break;
}
}
}
}
int main(string[] args)
{
dataRoot = environment["JDTALK_DATA"];
dict = getData(dataRoot);
auto routes = new URLRouter;
routes.get("/", &handleRequest);
auto settings = new HTTPServerSettings;
settings.port = 8080;
settings.bindAddresses = ["::1", "127.0.0.1"];
auto l = listenHTTP(settings, routes);
scope (exit) l.stopListening();
runApplication();
return 0;
}
|