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