1 |
// Zoe AIM Away Message RSS Feed Generator |
2 |
// |
3 |
// Seth King and Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#include "Matcher.hpp" |
8 |
#include "Collector.hpp" |
9 |
#include "Publisher.hpp" |
10 |
|
11 |
#include <algorithm> |
12 |
#include <cstring> |
13 |
#include <iterator> |
14 |
|
15 |
extern "C" |
16 |
{ |
17 |
#include <pwd.h> |
18 |
#include <sys/utsname.h> |
19 |
#include <unistd.h> |
20 |
} |
21 |
|
22 |
int main(int argc, char* argv[]) |
23 |
{ |
24 |
Zoe::program = argv[0]; |
25 |
|
26 |
for (int index(1); index < argc; ++index) |
27 |
{ |
28 |
ext::String arg(argv[index]); |
29 |
Matcher matcher; |
30 |
|
31 |
if (arg == matcher("^-config=(.*)$")) |
32 |
{ |
33 |
Zoe::config = matcher[1]; |
34 |
} |
35 |
else if (arg == "-collector") |
36 |
{ |
37 |
if (!Zoe::collector) Zoe::collector = true; |
38 |
} |
39 |
else if (arg == "-publisher") |
40 |
{ |
41 |
if (!Zoe::publisher) Zoe::publisher = true; |
42 |
} |
43 |
else if (arg == "-D") |
44 |
{ |
45 |
if (!Zoe::debug) Zoe::debug = true; |
46 |
} |
47 |
else |
48 |
{ |
49 |
cerr << bright << "Usage: " << cyan << Zoe::program << reset << " [" |
50 |
<< bright << blue << "-config=" << yellow << "config" << reset |
51 |
<< "] [" << bright << blue << "-collector" << reset << "] [" |
52 |
<< bright << blue << "-publisher" << reset << "] [" << bright |
53 |
<< blue << "-D" << reset << "]\n"; |
54 |
|
55 |
return 1; |
56 |
} |
57 |
} |
58 |
|
59 |
Zoe zoe; |
60 |
|
61 |
return 0; |
62 |
} |
63 |
|
64 |
Zoe::Zoe() |
65 |
{ |
66 |
configure(); |
67 |
initialize(); |
68 |
|
69 |
Collector collector(login, password, buddies, database, Zoe::collector); |
70 |
Publisher publisher(buddies, database, Zoe::publisher); |
71 |
} |
72 |
|
73 |
bool Zoe::debug(false), Zoe::collector(false), Zoe::publisher(false); |
74 |
ext::String Zoe::program, Zoe::config("zoe.xml"); |
75 |
|
76 |
ext::String Zoe::generator() |
77 |
{ |
78 |
utsname system; |
79 |
|
80 |
uname(&system); |
81 |
|
82 |
return ext::String("Zoe/0.9 (") + system.sysname + "; http://computers.doug" |
83 |
+ "lasthrift.net/zoe.xml)"; |
84 |
} |
85 |
|
86 |
void Zoe::configure() |
87 |
{ |
88 |
if (debug) cerr << "config = " << config << '\n'; |
89 |
|
90 |
ext::Handle<xml::Document> document(xml::Parse(config)); |
91 |
ext::Handle<xml::Node> zoe(*document/"zoe"); |
92 |
|
93 |
login = std::string(*zoe/"login"); |
94 |
password = std::string(*zoe/"password"); |
95 |
database.driver = std::string(*zoe/"database"/"driver"); |
96 |
database.host = std::string(*zoe/"database"/"host"); |
97 |
database.user = std::string(*zoe/"database"/"user"); |
98 |
|
99 |
if ((*zoe/"database"/"password").IsEmpty()) |
100 |
{ |
101 |
ext::String prompt(database.user + (!database.host.IsEmpty() ? "@" : "") |
102 |
+ database.host + (!database.user.IsEmpty() ? "'s " : "") + "Databa" |
103 |
+ "se Password: "); |
104 |
char* password(getpass(prompt.NullTerminate())); |
105 |
|
106 |
database.password = password; |
107 |
|
108 |
for (size_t index(std::strlen(password)); index > 0; --index) |
109 |
password[index - 1] = '\0'; |
110 |
} |
111 |
else database.password = std::string(*zoe/"database"/"password"); |
112 |
|
113 |
database.db = std::string(*zoe/"database"/"db"); |
114 |
|
115 |
if (debug) cerr << "login = " << login << "\npassword = " |
116 |
<< std::string(password.GetSize(), '*') << "\ndatabase = {\n driver =" |
117 |
<< ' ' << database.driver << "\n host=" << database.host << "\n use" |
118 |
<< "r = " << database.user << "\n password = " |
119 |
<< std::string(database.password.GetSize(), '*') << "\n db = " |
120 |
<< database.db << "\n}\n"; |
121 |
|
122 |
xml::NodeSet buddies(*zoe/"buddy"); |
123 |
|
124 |
for (xml::NodeSet::Iterator buddy(buddies.Begin()); buddy != buddies.End(); |
125 |
++buddy) this->buddies.insert(Buddy(**buddy/"login", **buddy/"rss")); |
126 |
|
127 |
if (debug) |
128 |
{ |
129 |
cerr << "buddies = {\n"; |
130 |
|
131 |
for (std::set<Buddy>::const_iterator buddy(this->buddies.begin()); buddy |
132 |
!= this->buddies.end(); ++buddy) cerr << " " << *buddy << " = " |
133 |
<< buddy->getRss() << '\n'; |
134 |
|
135 |
cerr << "}\n"; |
136 |
} |
137 |
|
138 |
if (!collector && !publisher) |
139 |
{ |
140 |
collector = true; |
141 |
publisher = true; |
142 |
} |
143 |
|
144 |
if (debug) cerr << "collector = " << lexical_cast<ext::String>(collector) |
145 |
<< "\npublisher = " << lexical_cast<ext::String>(publisher) << '\n'; |
146 |
} |
147 |
|
148 |
void Zoe::initialize() |
149 |
{ |
150 |
ext::Handle<dbi::Connection> db(dbi::Connect(database.driver, database.host, |
151 |
database.user, database.password, database.db)); |
152 |
ext::Handle<dbi::ResultSet> buddies(db->Execute("SELECT * FROM buddies")); |
153 |
std::set<Buddy> buddies_; |
154 |
|
155 |
while (buddies->MoveNext()) |
156 |
{ |
157 |
Buddy buddy(*this->buddies.find(buddies->GetString("buddy"))); |
158 |
|
159 |
buddy.setId(lexical_cast<ext::Uuid>(buddies->GetString("id"))); |
160 |
buddies_.insert(buddy); |
161 |
} |
162 |
|
163 |
std::vector<Buddy> difference; |
164 |
|
165 |
set_difference(this->buddies.begin(), this->buddies.end(), buddies_.begin(), |
166 |
buddies_.end(), std::insert_iterator<std::vector<Buddy> >(difference, |
167 |
difference.begin())); |
168 |
|
169 |
for (std::vector<Buddy>::iterator buddy(difference.begin()); buddy != |
170 |
difference.end(); ++buddy) |
171 |
{ |
172 |
ext::Uuid id; |
173 |
|
174 |
api::Uuid::CreateSequential(id); |
175 |
|
176 |
buddy->setId(id); |
177 |
db->Execute("INSERT INTO buddies (id, buddy) VALUES ('" + id + "', '" + |
178 |
*buddy + "')"); |
179 |
|
180 |
buddies_.insert(*buddy); |
181 |
} |
182 |
|
183 |
this->buddies = buddies_; |
184 |
} |