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 8 by douglas, 2004-07-09T18:13:27-07:00 vs.
Revision 59 by douglas, 2004-09-13T23:39:30-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 >
14 > #include <algorithm>
15 > #include <cstring>
16 > #include <iterator>
17 >
18 > extern "C"
19   {
20 <        Zoe::program = argv[0];
20 > #include <pwd.h>
21 > #include <sys/utsname.h>
22 > #include <unistd.h>
23 > }
24  
25 <        for (int index(1); index < argc; ++index)
25 > struct ZoeCommand : public app::Application
26 > {
27 >        virtual int Run(const app::ArgumentList& args)
28          {
29 <                ext::String arg(argv[index]);
17 <                Matcher matcher;
29 >                Zoe::program = api::GetExecutableName();
30  
31 <                if (arg == matcher("^-config=(.*)$"))
31 >                for (size_t index(0); index < args.GetSize(); ++index)
32                  {
33 <                        Zoe::config = matcher[1];
34 <                }
35 <                else if (arg == "-D")
36 <                {
37 <                        if (!Zoe::debug) Zoe::debug = true;
38 <                }
39 <                else
40 <                {
41 <                        cerr << "Usage: " << Zoe::program << " [-config=config] [-D]\n";
33 >                        ext::String arg(args[index]);
34 >                        Matcher matcher;
35 >
36 >                        if (arg == matcher("^-config=(.*)$"))
37 >                        {
38 >                                Zoe::config = matcher[1];
39 >                        }
40 >                        else if (arg == "-collector")
41 >                        {
42 >                                if (!Zoe::collector) Zoe::collector = true;
43 >                        }
44 >                        else if (arg == "-publisher")
45 >                        {
46 >                                if (!Zoe::publisher) Zoe::publisher = true;
47 >                        }
48 >                        else if (arg == "-color")
49 >                        {
50 >                                if (!Zoe::color) Zoe::color = true;
51 >                        }
52 >                        else if (arg == "-D")
53 >                        {
54 >                                if (!Zoe::debug) Zoe::debug = true;
55 >                        }
56 >                        else Zoe::usage();
57                  }
31        }
58  
59 <        Zoe zoe;
59 >                Zoe zoe;
60  
61 <        return 0;
62 < }
61 >                return 0;
62 >        }
63 > } zoe;
64  
65 < Zoe::Zoe() : rss("zoe.rss")
65 > Zoe::Zoe()
66   {
67 +        if (!(collector || publisher)) usage();
68 +        
69          configure();
70 +        initialize();
71  
72 <        // This is just to figure out Jay's XML output, seems to have worked!
73 <        xml::TextWriter rss(api::Cout.GetStream());
74 <        xml::ScopeElement root(rss, "rss");
72 >        Collector collector(login, password, buddies, database, Zoe::collector);
73 >        Publisher publisher(buddies, database, Zoe::publisher);
74 > }
75  
76 <        rss.SetAttribute("version", "2.0");
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 <        xml::ScopeElement channel(rss, "channel");
80 > void Zoe::usage()
81 > {
82 >        cerr << "Usage: " << Zoe::program << " [-config=config] [-collector] "
83 >                "[-publisher] [-color] [-D]\n";
84  
85 <        rss.OpenElement("title");
51 <        rss.OutputText("Zoe's Away Messages");
52 <        rss.CloseElement();
53 <        rss.OpenElement("link");
54 <        rss.OutputText("http://computers.douglasthrift.net/zoe.xml");
55 <        rss.CloseElement();
85 >        exit(1);
86   }
87  
88 < bool Zoe::debug(false);
89 < ext::String Zoe::program, Zoe::config("zoe.xml");
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) 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) 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 +        if (debug)
175 +        {
176 +                cerr << "buddies = {\n";
177 +
178 +                for (std::set<Buddy>::const_iterator buddy(this->buddies.begin());
179 +                        buddy != this->buddies.end(); ++buddy)
180 +                {
181 +                        cerr << "   " << *buddy << " = {\n      rss = " << buddy->getRss()
182 +                                << "\n      atom = " << buddy->getAtom() << "\n      link = "
183 +                                << buddy->getLink() << "\n   }\n";
184 +                }
185 +
186 +                cerr << "}\n";
187 +        }
188 +
189 +        if (debug) cerr << "collector = " << lexical_cast<ext::String>(collector)
190 +                << "\npublisher = " << lexical_cast<ext::String>(publisher)
191 +                << "\ncolor = " << lexical_cast<ext::String>(color) << '\n';
192 + }
193 +
194 + void Zoe::initialize()
195 + {
196 +        ext::Handle<dbi::Driver> driver(dbi::GetDriver(database.driver));
197 +        ext::Handle<dbi::Connection> db(driver->Connect(database.host,
198 +                database.user, database.password, database.db));
199 +        ext::Handle<dbi::ResultSet> buddies(db->Execute("SELECT * FROM buddies"));
200 +        std::set<Buddy> buddies_;
201 +
202 +        while (buddies->MoveNext())
203 +        {
204 +                if (this->buddies.find(buddies->GetString("buddy")) != this->buddies.end())
205 +                {
206 +                        Buddy buddy(*this->buddies.find(buddies->GetString("buddy")));
207 +
208 +                        buddy.setId(lexical_cast<ext::Uuid>(buddies->GetString("id")));
209 +                        buddies_.insert(buddy);
210 +                }
211 +        }
212 +
213 +        std::vector<Buddy> difference;
214 +
215 +        set_difference(this->buddies.begin(), this->buddies.end(), buddies_.begin(),
216 +                buddies_.end(), std::insert_iterator<std::vector<Buddy> >(difference,
217 +                difference.begin()));
218 +
219 +        for (std::vector<Buddy>::iterator buddy(difference.begin());
220 +                buddy != difference.end(); ++buddy)
221 +        {
222 +                ext::Uuid id;
223 +
224 +                api::Uuid::CreateSequential(id);
225 +
226 +                buddy->setId(id);
227 +                db->Execute("INSERT INTO buddies (id, buddy) VALUES ('"
228 +                        + lexical_cast<ext::String>(id) + "', '" + ext::String(*buddy)
229 +                        + "')");
230 +                
231 +                buddies_.insert(*buddy);
232 +        }
233  
234 <        if (!(*zoe/"rss").IsEmpty()) rss = *zoe/"rss";
234 >        this->buddies = buddies_;
235   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines