ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/zoe/trunk/Zoe.cpp
(Generate patch)

Comparing trunk/Zoe.cpp (file contents):
Revision 15 by douglas, 2004-07-13T20:14:49-07:00 vs.
Revision 22 by douglas, 2004-07-17T00:54:46-07:00

# Line 8 | Line 8
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 <unistd.h>
19   }
20  
21   int main(int argc, char* argv[])
# Line 64 | Line 63 | int main(int argc, char* argv[])
63   Zoe::Zoe()
64   {
65          configure();
66 +        initialize();
67  
68 <        Collector collector(login, password, buddies, Zoe::collector);
69 <        Publisher publisher(buddies, Zoe::publisher);
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);
# Line 74 | Line 74 | ext::String Zoe::program, Zoe::config("z
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)
114 >                ++buddy) this->buddies.insert(Buddy(**buddy/"login", **buddy/"rss"));
115 >
116 >        if (debug)
117          {
118 <                this->buddies.insert(Buddy(**buddy/"login", **buddy/"rss"));
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)
# Line 93 | Line 128 | void Zoe::configure()
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   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines