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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines