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