ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/zoe/trunk/Zoe.cpp
Revision: 21
Committed: 2004-07-16T23:37:41-07:00 (20 years, 11 months ago) by douglas
File size: 4727 byte(s)
Log Message:
Database!

File Contents

# Content
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 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 };
24
25 inline std::ostream& operator<<(std::ostream& sout, Color color)
26 {
27 sout << "\033[" << unsigned(color) << 'm';
28
29 return sout;
30 }
31
32 int main(int argc, char* argv[])
33 {
34 Zoe::program = argv[0];
35
36 for (int index(1); index < argc; ++index)
37 {
38 ext::String arg(argv[index]);
39 Matcher matcher;
40
41 if (arg == matcher("^-config=(.*)$"))
42 {
43 Zoe::config = matcher[1];
44 }
45 else if (arg == "-collector")
46 {
47 if (!Zoe::collector) Zoe::collector = true;
48 }
49 else if (arg == "-publisher")
50 {
51 if (!Zoe::publisher) Zoe::publisher = true;
52 }
53 else if (arg == "-D")
54 {
55 if (!Zoe::debug) Zoe::debug = true;
56 }
57 else
58 {
59 cerr << bright << "Usage: " << cyan << Zoe::program << reset << " ["
60 << bright << blue << "-config=" << yellow << "config" << reset
61 << "] [" << bright << blue << "-collector" << reset << "] ["
62 << bright << blue << "-publisher" << reset << "] [" << bright
63 << blue << "-D" << reset << "]\n";
64
65 return 1;
66 }
67 }
68
69 Zoe zoe;
70
71 return 0;
72 }
73
74 Zoe::Zoe()
75 {
76 configure();
77 initialize();
78
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);
84 ext::String Zoe::program, Zoe::config("zoe.xml");
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 << '\n';
133
134 cerr << "}\n";
135 }
136
137 if (!collector && !publisher)
138 {
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 }

Properties

Name Value
svn:eol-style native
svn:keywords Id