8 |
|
#include "Collector.hpp" |
9 |
|
#include "Publisher.hpp" |
10 |
|
|
11 |
< |
enum Color { reset, bright, dim, underscore = 4, blink, reverse = 7, hidden, |
12 |
< |
black = 30, red, green, yellow, blue, magenta, cyan, white, _black = 40, |
13 |
< |
_red, _green, _yellow, _blue, _magenta, _cyan, _white }; |
11 |
> |
#include <algorithm> |
12 |
> |
#include <cstring> |
13 |
> |
#include <iterator> |
14 |
|
|
15 |
< |
inline std::ostream& operator<<(std::ostream& sout, Color color) |
15 |
> |
extern "C" |
16 |
|
{ |
17 |
< |
sout << "\033[" << unsigned(color) << 'm'; |
18 |
< |
|
19 |
< |
return sout; |
17 |
> |
#include <pwd.h> |
18 |
> |
#include <sys/utsname.h> |
19 |
> |
#include <unistd.h> |
20 |
|
} |
21 |
|
|
22 |
|
int main(int argc, char* argv[]) |
64 |
|
Zoe::Zoe() |
65 |
|
{ |
66 |
|
configure(); |
67 |
+ |
initialize(); |
68 |
|
|
69 |
< |
Collector collector(login, password, buddies, Zoe::collector); |
70 |
< |
Publisher publisher(buddies, Zoe::publisher); |
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) |
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 << " = " |
133 |
> |
<< buddy->getRss() << '\n'; |
134 |
> |
|
135 |
> |
cerr << "}\n"; |
136 |
|
} |
137 |
|
|
138 |
|
if (!collector && !publisher) |
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 |
|
} |