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 9 by douglas, 2004-07-10T01:46:06-07:00 vs.
Revision 69 by douglas, 2004-12-15T16:14:13-08:00

# Line 4 | Line 4
4   //
5   // $Id$
6  
7 < #include "Zoe.hpp"
8 < #include "Matcher.hpp"
7 > #include "Matcher/Matcher.hpp"
8 > #include "Collector.hpp"
9 > #include "Publisher.hpp"
10 >
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 < int main(int argc, char* argv[])
19 > extern "C"
20   {
21 <        Zoe::program = argv[0];
21 > #include <pwd.h>
22 > #include <sys/utsname.h>
23 > #include <unistd.h>
24 > }
25  
26 <        for (int index(1); index < argc; ++index)
26 > struct ZoeCommand : public app::Application
27 > {
28 >        virtual int Run(const app::ArgumentList& args)
29          {
30 <                ext::String arg(argv[index]);
17 <                Matcher matcher;
30 >                Zoe::program = api::GetExecutablePath().GetName();
31  
32 <                if (arg == matcher("^-config=(.*)$"))
20 <                {
21 <                        Zoe::config = matcher[1];
22 <                }
23 <                else if (arg == "-D")
32 >                for (size_t index(0); index < args.GetSize(); ++index)
33                  {
34 <                        if (!Zoe::debug) Zoe::debug = true;
35 <                }
27 <                else
28 <                {
29 <                        cerr << "Usage: " << Zoe::program << " [-config=config] [-D]\n";
34 >                        ext::String arg(args[index]);
35 >                        Matcher matcher;
36  
37 <                        return 1;
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;
66 > Zoe::Zoe()
67 > {
68 >        if (!(collector || publisher)) usage();
69 >        
70 >        configure();
71  
72 <        return 0;
72 >        Collector collector(login, password, buddies, database, Zoe::collector);
73 >        Publisher publisher(buddies, database, Zoe::publisher);
74   }
75  
76 < Zoe::Zoe() : rss("zoe.rss")
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 <        configure();
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 <        // This is just to figure out Jay's XML output, seems to have worked!
93 <        // It should really go in Rss.cpp later
94 <        api::FileOutputStream fout;
92 >        switch (generator)
93 >        {
94 >        case all:
95 >                generator_.InsertAllLast("/0.9");
96 >        case agent:
97 >                {
98 >                        utsname system;
99  
100 <        if (this->rss != "-") fout.Open(this->rss); // need to ask Jay "Why?"
100 >                        uname(&system);
101  
102 <        xml::TextWriter rss(this->rss != "-" ? /*api::FileOutputStream(this->rss)*/
103 <                fout : api::Cout.GetStream());
52 <        xml::ScopeElement root(rss, "rss");
102 >                        generator_.InsertAllLast(ios::String() << " (" << system.sysname);
103 >                }
104  
105 <        rss.SetAttribute("version", "2.0");
105 >                if (generator == agent)
106 >                {
107 >                        generator_.InsertAllLast(")");
108  
109 <        xml::ScopeElement channel(rss, "channel");
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 <        rss.OpenElement("title");
59 <        rss.OutputText("Zoe's Away Messages");
60 <        rss.CloseElement();
61 <        rss.OpenElement("link");
62 <        rss.OutputText("http://computers.douglasthrift.net/zoe.xml");
63 <        rss.CloseElement();
122 >        return generator_;
123   }
124  
66 bool Zoe::debug(false);
67 ext::String Zoe::program, Zoe::config("zoe.xml");
68
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 +        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 <        if (!(*zoe/"rss").IsEmpty()) rss = *zoe/"rss";
236 >        this->buddies = buddies_;
237   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines