1 |
// Feeping Creaturism |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#include "Environment.hpp" |
8 |
#include "Matcher.hpp" |
9 |
#include "Jargon.hpp" |
10 |
|
11 |
extern "C" |
12 |
{ |
13 |
#include <sys/types.h> |
14 |
#include <sys/stat.h> |
15 |
#include <fts.h> |
16 |
} |
17 |
|
18 |
#include <menes-api/exename.hpp> |
19 |
#include <menes-app/simple.hpp> |
20 |
|
21 |
int Main(const app::Options& options) |
22 |
{ |
23 |
FeepingCreaturism::program = api::GetExecutablePath().GetName(); |
24 |
|
25 |
FeepingCreaturism creaturism; |
26 |
|
27 |
return 0; |
28 |
} |
29 |
|
30 |
FeepingCreaturism::FeepingCreaturism() |
31 |
{ |
32 |
initialize(); |
33 |
parse(); |
34 |
|
35 |
ext::String path(env.get("PATH_INFO")); |
36 |
Matcher matcher; |
37 |
|
38 |
if (path == matcher("^/daily/(\\d{4}-\\d{2}-\\d{2})?$")) |
39 |
{ |
40 |
daily(matcher.size() > 1 ? matcher[1] : ""); |
41 |
} |
42 |
else if (path == matcher("^/random/(\\d+)?$")) |
43 |
{ |
44 |
random(matcher.size() > 1 ? matcher[1] : ""); |
45 |
} |
46 |
else if (path == matcher("^/(" + this->matcher + ")$")) |
47 |
{ |
48 |
select(matcher[1], true); |
49 |
} |
50 |
else |
51 |
{ |
52 |
api::Cout << "Location: http://" << env.get("HTTP_HOST") |
53 |
<< env.get("SCRIPT_NAME") << "/daily/\r\n\r\n"; |
54 |
} |
55 |
} |
56 |
|
57 |
ext::String FeepingCreaturism::program; |
58 |
|
59 |
bool FeepingCreaturism::CaseLess::operator()(const std::string& one, |
60 |
const std::string& two) |
61 |
{ |
62 |
std::string one_(one), two_(two); |
63 |
|
64 |
// XXX: should be std::tolower except g++34 doesn't believe it |
65 |
std::transform(one.begin(), one.end(), one_.begin(), ::tolower); |
66 |
std::transform(two.begin(), two.end(), two_.begin(), ::tolower); |
67 |
|
68 |
if (one_ == two_) return one < two; |
69 |
|
70 |
return one_ < two_; |
71 |
} |
72 |
|
73 |
void FeepingCreaturism::initialize() |
74 |
{ |
75 |
ext::Handle<xml::Document> document(xml::Parse("jargon.xml")); |
76 |
ext::Handle<xml::Node> node(*document/"feepingcreaturism"); |
77 |
|
78 |
this->path = *node/"jargon"; |
79 |
this->matcher = *node/"matcher"; |
80 |
|
81 |
char* path[] = { new char[this->path.GetData().GetSize()] }; |
82 |
|
83 |
std::strcpy(path[0], this->path.NullTerminate()); |
84 |
|
85 |
::FTS* traversal(::fts_open(path, FTS_LOGICAL, NULL)); |
86 |
Matcher matcher("^" + this->path + "/(" + this->matcher + ")$"); |
87 |
|
88 |
if (traversal == NULL) |
89 |
{ |
90 |
api::Cerr << program << ": Horrible Failure!\n"; |
91 |
|
92 |
std::exit(1); |
93 |
} |
94 |
|
95 |
for (::FTSENT* entity(::fts_read(traversal)); entity != NULL; |
96 |
entity = ::fts_read(traversal)) |
97 |
{ |
98 |
if (entity->fts_info == FTS_F && entity->fts_path == matcher) |
99 |
{ |
100 |
jargon.insert(matcher[1]); |
101 |
} |
102 |
} |
103 |
|
104 |
::fts_close(traversal); |
105 |
|
106 |
delete [] path[0]; |
107 |
} |
108 |
|
109 |
void FeepingCreaturism::parse() |
110 |
{ |
111 |
std::stringstream query(env.get("QUERY_STRING")); |
112 |
|
113 |
if (env.get("REQUEST_METHOD") == "POST") |
114 |
{ |
115 |
std::streamsize length(lexical_cast<std::streamsize>(env.get("CONTENT_" |
116 |
"TYPE"))); |
117 |
char* content(new char[length]); |
118 |
|
119 |
api::Cin.Read(content, length); |
120 |
query.write(content, length); |
121 |
|
122 |
delete [] content; |
123 |
} |
124 |
|
125 |
if (query.str().empty()) return; |
126 |
|
127 |
do |
128 |
{ |
129 |
std::string name, value; |
130 |
|
131 |
std::getline(query, name, '='); |
132 |
std::getline(query, value, '&'); |
133 |
|
134 |
cgi.insert(_P(name, value)); |
135 |
} |
136 |
while (query.good()); |
137 |
} |
138 |
|
139 |
void FeepingCreaturism::daily(const ext::String& date) |
140 |
{ |
141 |
std::time_t when(std::time(NULL)); |
142 |
std::tm* day(std::localtime(&when)); |
143 |
|
144 |
day->tm_sec = 0; |
145 |
day->tm_min = 0; |
146 |
day->tm_hour = 0; |
147 |
|
148 |
if (!date.IsEmpty()) ::strptime(date.NullTerminate(), "%Y-%m-%d", day); |
149 |
|
150 |
std::time_t difference(mktime(day) / 86400); |
151 |
std::vector<ext::String> jargon(this->jargon.begin(), this->jargon.end()); |
152 |
ext::String entry(jargon.size() ? jargon[difference % jargon.size()] : ""); |
153 |
|
154 |
select(entry); |
155 |
} |
156 |
|
157 |
void FeepingCreaturism::random(const ext::String& number) |
158 |
{ |
159 |
::srandomdev(); |
160 |
|
161 |
std::vector<ext::String> jargon(this->jargon.begin(), this->jargon.end()); |
162 |
std::vector<ext::String>::size_type random(number.IsEmpty() ? ::random() : |
163 |
lexical_cast<std::vector<ext::String>::size_type>(number)); |
164 |
|
165 |
assert(random >= 0); |
166 |
assert(random % jargon.size() < jargon.size()); |
167 |
|
168 |
ext::String entry(jargon.size() ? jargon[random % jargon.size()] : ""); |
169 |
|
170 |
select(entry); |
171 |
} |
172 |
|
173 |
void FeepingCreaturism::select(const ext::String& selection, bool validate) |
174 |
{ |
175 |
if (!validate || jargon.find(selection) != jargon.end()) |
176 |
{ |
177 |
api::Cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n"; |
178 |
|
179 |
Jargon jargon(path, selection, cgi.find("include") != cgi.end() |
180 |
&& lexical_cast<bool>(ext::String(cgi.find("include")->second)), |
181 |
cgi.find("relative") != cgi.end() |
182 |
? ext::String(cgi.find("relative")->second) : ext::String()); |
183 |
|
184 |
api::Cout << jargon; |
185 |
} |
186 |
else |
187 |
{ |
188 |
api::Cout << "Status: 404\r\n" |
189 |
<< "Content-Type: text/html; charset=ISO-8859-1\r\n\r\n" |
190 |
<< "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" |
191 |
<< "<html><head>\n<title>404 Not Found</title>\n</head><body>\n" |
192 |
<< "<h1>Not Found</h1>\n<p>The requested URL " |
193 |
<< env.get("PATH_INFO") << " was not found on this server.</p>\n" |
194 |
<< "<hr />\n" << env.get("SERVER_SIGNATURE") << "</body></html>\n"; |
195 |
} |
196 |
} |