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 |
|
enum Color { reset, bright, dim, underscore = 4, blink, reverse = 7, hidden, |
22 |
|
black = 30, red, green, yellow, blue, magenta, cyan, white, _black = 40, |
23 |
|
_red, _green, _yellow, _blue, _magenta, _cyan, _white }; |
74 |
|
Zoe::Zoe() |
75 |
|
{ |
76 |
|
configure(); |
77 |
+ |
initialize(); |
78 |
|
|
79 |
< |
Collector collector(login, password, buddies, Zoe::collector); |
80 |
< |
Publisher publisher(buddies, Zoe::publisher); |
79 |
> |
Collector collector(login, password, buddies, database, Zoe::collector); |
80 |
> |
Publisher publisher(buddies, database, Zoe::publisher); |
81 |
|
} |
82 |
|
|
83 |
|
bool Zoe::debug(false), Zoe::collector(false), Zoe::publisher(false); |
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) |
125 |
> |
++buddy) this->buddies.insert(Buddy(**buddy/"login", **buddy/"rss")); |
126 |
> |
|
127 |
> |
if (debug) |
128 |
|
{ |
129 |
< |
this->buddies.insert(Buddy(**buddy/"login", **buddy/"rss")); |
129 |
> |
cerr << "buddies = {\n"; |
130 |
> |
|
131 |
> |
for (std::set<Buddy>::const_iterator buddy(this->buddies.begin()); buddy |
132 |
> |
!= this->buddies.end(); ++buddy) cerr << " " << *buddy << '\n'; |
133 |
> |
|
134 |
> |
cerr << "}\n"; |
135 |
|
} |
136 |
|
|
137 |
|
if (!collector && !publisher) |
139 |
|
collector = true; |
140 |
|
publisher = true; |
141 |
|
} |
142 |
+ |
|
143 |
+ |
if (debug) cerr << "collector = " << lexical_cast<ext::String>(collector) |
144 |
+ |
<< "\npublisher = " << lexical_cast<ext::String>(publisher) << '\n'; |
145 |
+ |
} |
146 |
+ |
|
147 |
+ |
void Zoe::initialize() |
148 |
+ |
{ |
149 |
+ |
ext::Handle<dbi::Connection> db(dbi::Connect(database.driver, database.host, |
150 |
+ |
database.user, database.password, database.db)); |
151 |
+ |
ext::Handle<dbi::ResultSet> buddies(db->Execute("SELECT * FROM buddies")); |
152 |
+ |
std::set<Buddy> buddies_; |
153 |
+ |
|
154 |
+ |
while (buddies->MoveNext()) |
155 |
+ |
{ |
156 |
+ |
Buddy buddy(*this->buddies.find(buddies->GetString("buddy"))); |
157 |
+ |
|
158 |
+ |
buddy.setId(lexical_cast<ext::Uuid>(buddies->GetString("id"))); |
159 |
+ |
buddies_.insert(buddy); |
160 |
+ |
} |
161 |
+ |
|
162 |
+ |
std::vector<Buddy> difference; |
163 |
+ |
|
164 |
+ |
set_difference(this->buddies.begin(), this->buddies.end(), buddies_.begin(), |
165 |
+ |
buddies_.end(), std::insert_iterator<std::vector<Buddy> >(difference, |
166 |
+ |
difference.begin())); |
167 |
+ |
|
168 |
+ |
for (std::vector<Buddy>::iterator buddy(difference.begin()); buddy != |
169 |
+ |
difference.end(); ++buddy) |
170 |
+ |
{ |
171 |
+ |
ext::Uuid id; |
172 |
+ |
|
173 |
+ |
api::Uuid::CreateSequential(id); |
174 |
+ |
|
175 |
+ |
buddy->setId(id); |
176 |
+ |
db->Execute("INSERT INTO buddies (id, buddy) VALUES ('" + |
177 |
+ |
lexical_cast<ext::String>(id) + "', '" + ext::String(*buddy) + |
178 |
+ |
"')"); |
179 |
+ |
|
180 |
+ |
buddies_.insert(*buddy); |
181 |
+ |
} |
182 |
+ |
|
183 |
+ |
this->buddies = buddies_; |
184 |
|
} |