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 5 by douglas, 2004-07-09T15:04:18-07:00 vs.
Revision 67 by douglas, 2004-10-01T10:27:27-07:00

# Line 4 | Line 4
4   //
5   // $Id$
6  
7 #include "Zoe.hpp"
7   #include "Matcher.hpp"
8 + #include "Collector.hpp"
9 + #include "Publisher.hpp"
10  
11 < int main(int argc, char* argv[])
11 > #include <menes-api/exename.hpp>
12 > #include <menes-app/application.hpp>
13 > #include <menes-ios/stdadapters.hpp>
14 >
15 > #include <algorithm>
16 > #include <cstring>
17 > #include <iterator>
18 >
19 > extern "C"
20 > {
21 > #include <pwd.h>
22 > #include <sys/utsname.h>
23 > #include <unistd.h>
24 > }
25 >
26 > struct ZoeCommand : public app::Application
27 > {
28 >        virtual int Run(const app::ArgumentList& args)
29 >        {
30 >                Zoe::program = api::GetExecutablePath().GetName();
31 >
32 >                for (size_t index(0); index < args.GetSize(); ++index)
33 >                {
34 >                        ext::String arg(args[index]);
35 >                        Matcher matcher;
36 >
37 >                        if (arg == matcher("^-config=(.*)$"))
38 >                        {
39 >                                Zoe::config = matcher[1];
40 >                        }
41 >                        else if (arg == "-collector")
42 >                        {
43 >                                if (!Zoe::collector) Zoe::collector = true;
44 >                        }
45 >                        else if (arg == "-publisher")
46 >                        {
47 >                                if (!Zoe::publisher) Zoe::publisher = true;
48 >                        }
49 >                        else if (arg == "-color")
50 >                        {
51 >                                if (!Zoe::color) Zoe::color = true;
52 >                        }
53 >                        else if (arg == "-D")
54 >                        {
55 >                                if (!Zoe::debug) Zoe::debug = true;
56 >                        }
57 >                        else Zoe::usage();
58 >                }
59 >
60 >                Zoe zoe;
61 >
62 >                return 0;
63 >        }
64 > } zoe;
65 >
66 > Zoe::Zoe()
67 > {
68 >        if (!(collector || publisher)) usage();
69 >        
70 >        configure();
71 >
72 >        Collector collector(login, password, buddies, database, Zoe::collector);
73 >        Publisher publisher(buddies, database, Zoe::publisher);
74 > }
75 >
76 > bool Zoe::debug(false), Zoe::collector(false), Zoe::publisher(false),
77 >        Zoe::color(false);
78 > ext::String Zoe::program, Zoe::config("zoe.xml");
79 >
80 > void Zoe::usage()
81 > {
82 >        api::Cerr << "Usage: " << Zoe::program << " [-config=config] [-collector] "
83 >                "[-publisher] [-color] [-D]\n";
84 >
85 >        exit(1);
86 > }
87 >
88 > ext::String Zoe::generator(Generator generator)
89 > {
90 >        ext::String generator_(generator == all || generator == agent ? "Zoe" : "");
91 >
92 >        switch (generator)
93 >        {
94 >        case all:
95 >                generator_.InsertAllLast("/0.9");
96 >        case agent:
97 >                {
98 >                        utsname system;
99 >
100 >                        uname(&system);
101 >
102 >                        generator_.InsertAllLast(_R(" (") + system.sysname);
103 >                }
104 >
105 >                if (generator == agent)
106 >                {
107 >                        generator_.InsertAllLast(")");
108 >
109 >                        break;
110 >                }
111 >                else generator_.InsertAllLast("; ");
112 >        case url:
113 >                generator_.InsertAllLast("http://computers.douglasthrift.net/zoe.xml");
114 >
115 >                if (generator == all) generator_.InsertAllLast(")");
116 >
117 >                break;
118 >        case version:
119 >                generator_ = "0.9";
120 >        }
121 >
122 >        return generator_;
123 > }
124 >
125 > void Zoe::configure()
126 > {
127 >        if (debug) api::Cerr << "config = " << config << "\n";
128 >
129 >        ext::Handle<xml::Document> document(xml::Parse(config));
130 >        ext::Handle<xml::Node> zoe(*document/"zoe");
131 >
132 >        login = *zoe/"login";
133 >        password = *zoe/"password";
134 >        database.driver = *zoe/"database"/"driver";
135 >        database.host = *zoe/"database"/"host";
136 >        database.user = *zoe/"database"/"user";
137 >
138 >        if ((*zoe/"database"/"password").IsEmpty())
139 >        {
140 >                ext::String prompt(database.user + (!database.host.IsEmpty() ? "@" : "")
141 >                        + database.host + (!database.user.IsEmpty() ? "'s " : "")
142 >                        + "Database Password: ");
143 >                char* password(getpass(prompt.NullTerminate()));
144 >
145 >                database.password = password;
146 >
147 >                for (size_t index(std::strlen(password)); index > 0; --index)
148 >                        password[index - 1] = '\0';
149 >        }
150 >        else database.password = *zoe/"database"/"password";
151 >
152 >        database.db = *zoe/"database"/"db";
153 >
154 >        if (debug) api::Cerr << "login = " << login << "\npassword = "
155 >                << std::string(password.GetSize(), '*') << "\ndatabase = {\n"
156 >                << "   driver = " << database.driver << "\n   host = " << database.host
157 >                << "\n   user = " << database.user << "\n"
158 >                << "   password = " << std::string(database.password.GetSize(), '*')
159 >                << "\n   db = " << database.db << "\n}\n";
160 >
161 >        ext::String link(*zoe/"link");
162 >        xml::NodeSet buddies(*zoe/"buddy");
163 >
164 >        for (xml::NodeSet::Iterator buddy(buddies.Begin()); buddy != buddies.End();
165 >                ++buddy)
166 >        {
167 >                Buddy buddy_(**buddy/"login", **buddy/"rss", **buddy/"atom",
168 >                        !(**buddy/"link").IsEmpty() ? **buddy/"link" : (link
169 >                        + ext::String(**buddy/"login") + ".html"));
170 >
171 >                this->buddies.insert(buddy_);
172 >        }
173 >
174 >        initialize();
175 >
176 >        if (debug)
177 >        {
178 >                api::Cerr << "buddies = {\n";
179 >
180 >                for (std::set<Buddy>::const_iterator buddy(this->buddies.begin());
181 >                        buddy != this->buddies.end(); ++buddy)
182 >                {
183 >                        api::Cerr << "   " << *buddy << " = {\n      rss = "
184 >                                << buddy->getRss() << "\n      atom = " << buddy->getAtom()
185 >                                << "\n      link = " << buddy->getLink() << "\n      id = "
186 >                                << buddy->getId() << "\n   }\n";
187 >                }
188 >
189 >                api::Cerr << "}\n";
190 >        }
191 >
192 >        if (debug) api::Cerr << "collector = " << collector << "\npublisher = "
193 >                << publisher << "\ncolor = " << color << "\n";
194 > }
195 >
196 > void Zoe::initialize()
197   {
198 <        //
198 >        ext::Handle<dbi::Driver> driver(dbi::GetDriver(database.driver));
199 >        ext::Handle<dbi::Connection> db(driver->Connect(database.host,
200 >                database.user, database.password, database.db));
201 >        ext::Handle<dbi::ResultSet> buddies(db->Execute("SELECT * FROM buddies"));
202 >        std::set<Buddy> buddies_;
203 >
204 >        while (buddies->MoveNext())
205 >        {
206 >                if (this->buddies.find(buddies->GetString("buddy")) != this->buddies.end())
207 >                {
208 >                        Buddy buddy(*this->buddies.find(buddies->GetString("buddy")));
209 >
210 >                        buddy.setId(lexical_cast<ext::Uuid>(buddies->GetString("id")));
211 >                        buddies_.insert(buddy);
212 >                }
213 >        }
214 >
215 >        std::vector<Buddy> difference;
216 >
217 >        set_difference(this->buddies.begin(), this->buddies.end(), buddies_.begin(),
218 >                buddies_.end(), std::insert_iterator<std::vector<Buddy> >(difference,
219 >                difference.begin()));
220 >
221 >        for (std::vector<Buddy>::iterator buddy(difference.begin());
222 >                buddy != difference.end(); ++buddy)
223 >        {
224 >                ext::Uuid id(api::Uuid::CreateSequential());
225 >
226 > //              api::Uuid::CreateSequential(id);
227 >
228 >                buddy->setId(id);
229 >                db->Execute("INSERT INTO buddies (id, buddy) VALUES ('"
230 >                        + lexical_cast<ext::String>(id) + "', '" + ext::String(*buddy)
231 >                        + "')");
232 >                
233 >                buddies_.insert(*buddy);
234 >        }
235  
236 <        return 0;
236 >        this->buddies = buddies_;
237   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines