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 |
|
|
FeepingCreaturism creaturism; |
21 |
|
|
|
22 |
|
|
return 0; |
23 |
|
|
} |
24 |
Douglas Thrift |
194 |
|
25 |
|
|
FeepingCreaturism::FeepingCreaturism() |
26 |
|
|
{ |
27 |
|
|
initialize(); |
28 |
|
|
|
29 |
|
|
std::string path(env.get("PATH_INFO")); |
30 |
|
|
Matcher matcher; |
31 |
|
|
|
32 |
Douglas Thrift |
198 |
if (path == "/daily/") daily(); else if (path == "/random/") |
33 |
Douglas Thrift |
194 |
{ |
34 |
|
|
random(); |
35 |
|
|
} |
36 |
|
|
else if (path == matcher("^/(" + this->matcher + ")$")) |
37 |
|
|
{ |
38 |
|
|
select(matcher[1], true); |
39 |
|
|
} |
40 |
Douglas Thrift |
198 |
else |
41 |
|
|
{ |
42 |
|
|
cout << "Status: 301\r\n"; |
43 |
|
|
} |
44 |
Douglas Thrift |
194 |
} |
45 |
|
|
|
46 |
|
|
void FeepingCreaturism::initialize() |
47 |
|
|
{ |
48 |
|
|
ext::Handle<xml::Document> document(xml::Parse("FeepingCreaturism.xml")); |
49 |
|
|
ext::Handle<xml::Node> node(*document/"FeepingCreaturism"); |
50 |
|
|
|
51 |
|
|
this->path = *node/"Jargon"; |
52 |
|
|
this->matcher = *node/"Matcher"; |
53 |
|
|
|
54 |
|
|
char* path[] = { new char[this->path.size()] }; |
55 |
|
|
|
56 |
|
|
std::strcpy(path[0], this->path.c_str()); |
57 |
|
|
|
58 |
|
|
::FTS* traversal(::fts_open(path, FTS_LOGICAL, NULL)); |
59 |
|
|
Matcher matcher('^' + this->path + "/(" + this->matcher + ")$"); |
60 |
|
|
|
61 |
Douglas Thrift |
196 |
if (traversal == NULL) |
62 |
|
|
{ |
63 |
|
|
cerr << "FeepingCreaturism: Horrible Failure!\n"; |
64 |
|
|
|
65 |
|
|
std::exit(1); |
66 |
|
|
} |
67 |
|
|
|
68 |
Douglas Thrift |
194 |
for (::FTSENT* entity(::fts_read(traversal)); entity != NULL; |
69 |
|
|
entity = ::fts_read(traversal)) |
70 |
|
|
{ |
71 |
|
|
if (entity->fts_info == FTS_F && entity->fts_path == matcher) |
72 |
|
|
{ |
73 |
|
|
jargon.insert(matcher[1]); |
74 |
|
|
} |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
::fts_close(traversal); |
78 |
|
|
|
79 |
|
|
delete [] path[0]; |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
void FeepingCreaturism::daily() |
83 |
|
|
{ |
84 |
|
|
std::time_t when(std::time(NULL)); |
85 |
|
|
std::tm* now(std::localtime(&when)); |
86 |
|
|
|
87 |
|
|
now->tm_sec = 0; |
88 |
|
|
now->tm_min = 0; |
89 |
|
|
now->tm_hour = 0; |
90 |
|
|
|
91 |
|
|
std::time_t difference(mktime(now) / 86400); |
92 |
|
|
std::vector<std::string> jargon(this->jargon.begin(), this->jargon.end()); |
93 |
|
|
std::string entry(jargon.size() ? jargon[difference % jargon.size()] : ""); |
94 |
|
|
|
95 |
|
|
select(entry); |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
void FeepingCreaturism::random() |
99 |
|
|
{ |
100 |
|
|
::srandomdev(); |
101 |
|
|
|
102 |
|
|
std::vector<std::string> jargon(this->jargon.begin(), this->jargon.end()); |
103 |
|
|
std::string entry(jargon.size() ? jargon[::random() % jargon.size()] : ""); |
104 |
|
|
|
105 |
|
|
select(entry); |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
void FeepingCreaturism::select(const std::string& selection, bool validate) |
109 |
|
|
{ |
110 |
|
|
if (!validate || jargon.find(selection) != jargon.end()) |
111 |
|
|
{ |
112 |
Douglas Thrift |
196 |
cout << "Content-Type: text/html; charset=UTF-8\r\n\r\n"; |
113 |
|
|
|
114 |
|
|
Jargon jargon(path + '/' + selection); |
115 |
|
|
|
116 |
|
|
cout << jargon; |
117 |
Douglas Thrift |
194 |
} |
118 |
Douglas Thrift |
195 |
else |
119 |
|
|
{ |
120 |
|
|
cout << "Status: 404\r\nContent-Type: text/html; charset=ISO-8859-1\r\n" |
121 |
|
|
<< "\r\n<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" |
122 |
|
|
<< "<html><head>\n<title>404 Not Found</title>\n</head><body>\n" |
123 |
|
|
<< "<h1>Not Found</h1>\n<p>The requested URL " |
124 |
|
|
<< env.get("PATH_INFO") << " was not found on this server.</p>\n" |
125 |
|
|
<< "<hr />\n" << env.get("SERVER_SIGNATURE") << "</body></html>\n"; |
126 |
|
|
} |
127 |
Douglas Thrift |
194 |
} |