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 12 by douglas, 2004-07-11T20:45:38-07:00 vs.
Revision 21 by douglas, 2004-07-16T23:37:41-07:00

# Line 5 | Line 5
5   // $Id$
6  
7   #include "Matcher.hpp"
8 < #include "Buddy.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 <unistd.h>
19 > }
20 >
21 > enum Color { reset, bright, dim, underscore = 4, blink, reverse = 7, hidden,
22 >        black = 30, red, green, yellow, blue, magenta, cyan, white, _black = 40,
23 >        _red, _green, _yellow, _blue, _magenta, _cyan, _white };
24 >
25 > inline std::ostream& operator<<(std::ostream& sout, Color color)
26 > {
27 >        sout << "\033[" << unsigned(color) << 'm';
28 >
29 >        return sout;
30 > }
31  
32   int main(int argc, char* argv[])
33   {
# Line 20 | Line 42 | int main(int argc, char* argv[])
42                  {
43                          Zoe::config = matcher[1];
44                  }
45 +                else if (arg == "-collector")
46 +                {
47 +                        if (!Zoe::collector) Zoe::collector = true;
48 +                }
49 +                else if (arg == "-publisher")
50 +                {
51 +                        if (!Zoe::publisher) Zoe::publisher = true;
52 +                }
53                  else if (arg == "-D")
54                  {
55                          if (!Zoe::debug) Zoe::debug = true;
56                  }
57                  else
58                  {
59 <                        cerr << "Usage: " << Zoe::program << " [-config=config] [-D]\n";
59 >                        cerr << bright << "Usage: " << cyan << Zoe::program << reset << " ["
60 >                                << bright << blue <<  "-config=" << yellow << "config" << reset
61 >                                << "] [" << bright << blue << "-collector" << reset << "] ["
62 >                                << bright << blue << "-publisher" << reset << "] [" << bright
63 >                                << blue << "-D" << reset << "]\n";
64  
65                          return 1;
66                  }
# Line 40 | Line 74 | int main(int argc, char* argv[])
74   Zoe::Zoe()
75   {
76          configure();
77 +        initialize();
78  
79 <        // This is just to figure out Jay's XML output, seems to have worked!
80 <        // It should really go in Rss.cpp later
46 < /*      api::FileOutputStream fout(this->rss != "-" ? this->rss : "/dev/null");
47 <        xml::TextWriter rss(this->rss != "-" ? fout : api::Cout.GetStream());
48 <        xml::ScopeElement root(rss, "rss");
49 <
50 <        rss.SetAttribute("version", "2.0");
51 <
52 <        xml::ScopeElement channel(rss, "channel");
53 <
54 <        rss.OpenElement("title");
55 <        rss.OutputText("Zoe's Away Messages");
56 <        rss.CloseElement();
57 <        rss.OpenElement("link");
58 <        rss.OutputText("http://computers.douglasthrift.net/zoe.xml");
59 <        rss.CloseElement();*/
79 >        Collector collector(login, password, buddies, database, Zoe::collector);
80 >        Publisher publisher(buddies, database, Zoe::publisher);
81   }
82  
83 < bool Zoe::debug(false);
83 > bool Zoe::debug(false), Zoe::collector(false), Zoe::publisher(false);
84   ext::String Zoe::program, Zoe::config("zoe.xml");
85  
86   void Zoe::configure()
87   {
88 +        if (debug) cerr << "config = " << config << '\n';
89 +
90          ext::Handle<xml::Document> document(xml::Parse(config));
91          ext::Handle<xml::Node> zoe(*document/"zoe");
92  
93          login = std::string(*zoe/"login");
94          password = std::string(*zoe/"password");
95 +        database.driver = std::string(*zoe/"database"/"driver");
96 +        database.host = std::string(*zoe/"database"/"host");
97 +        database.user = std::string(*zoe/"database"/"user");
98 +
99 +        if ((*zoe/"database"/"password").IsEmpty())
100 +        {
101 +                ext::String prompt(database.user + (!database.host.IsEmpty() ? "@" : "")
102 +                        + database.host + (!database.user.IsEmpty() ? "'s " : "") + "Databa"
103 +                        + "se Password: ");
104 +                char* password(getpass(prompt.NullTerminate()));
105 +
106 +                database.password = password;
107 +
108 +                for (size_t index(std::strlen(password)); index > 0; --index)
109 +                        password[index - 1] = '\0';
110 +        }
111 +        else database.password = std::string(*zoe/"database"/"password");
112 +
113 +        database.db = std::string(*zoe/"database"/"db");
114 +
115 +        if (debug) cerr << "login = " << login << "\npassword = "
116 +                << std::string(password.GetSize(), '*') << "\ndatabase = {\n   driver ="
117 +                << ' ' << database.driver << "\n   host=" << database.host << "\n   use"
118 +                << "r = " << database.user << "\n   password = "
119 +                << std::string(database.password.GetSize(), '*') << "\n   db = "
120 +                << database.db << "\n}\n";
121  
122          xml::NodeSet buddies(*zoe/"buddy");
123  
124          for (xml::NodeSet::Iterator buddy(buddies.Begin()); buddy != buddies.End();
125 <                ++buddy)
125 >                ++buddy) this->buddies.insert(Buddy(**buddy/"login", **buddy/"rss"));
126 >
127 >        if (debug)
128 >        {
129 >                cerr << "buddies = {\n";
130 >
131 >                for (std::set<Buddy>::const_iterator buddy(this->buddies.begin()); buddy
132 >                        != this->buddies.end(); ++buddy) cerr << "   " << *buddy << '\n';
133 >
134 >                cerr << "}\n";
135 >        }
136 >
137 >        if (!collector && !publisher)
138          {
139 <                this->buddies.insert(Buddy(**buddy/"login", **buddy/"rss"));
139 >                collector = true;
140 >                publisher = true;
141          }
142 +
143 +        if (debug) cerr << "collector = " << lexical_cast<ext::String>(collector)
144 +                << "\npublisher = " << lexical_cast<ext::String>(publisher) << '\n';
145 + }
146 +
147 + void Zoe::initialize()
148 + {
149 +        ext::Handle<dbi::Connection> db(dbi::Connect(database.driver, database.host,
150 +                database.user, database.password, database.db));
151 +        ext::Handle<dbi::ResultSet> buddies(db->Execute("SELECT * FROM buddies"));
152 +        std::set<Buddy> buddies_;
153 +
154 +        while (buddies->MoveNext())
155 +        {
156 +                Buddy buddy(*this->buddies.find(buddies->GetString("buddy")));
157 +
158 +                buddy.setId(lexical_cast<ext::Uuid>(buddies->GetString("id")));
159 +                buddies_.insert(buddy);
160 +        }
161 +
162 +        std::vector<Buddy> difference;
163 +
164 +        set_difference(this->buddies.begin(), this->buddies.end(), buddies_.begin(),
165 +                buddies_.end(), std::insert_iterator<std::vector<Buddy> >(difference,
166 +                difference.begin()));
167 +
168 +        for (std::vector<Buddy>::iterator buddy(difference.begin()); buddy !=
169 +                difference.end(); ++buddy)
170 +        {
171 +                ext::Uuid id;
172 +
173 +                api::Uuid::CreateSequential(id);
174 +
175 +                buddy->setId(id);
176 +                db->Execute("INSERT INTO buddies (id, buddy) VALUES ('" +
177 +                        lexical_cast<ext::String>(id) + "', '" + ext::String(*buddy) +
178 +                        "')");
179 +                
180 +                buddies_.insert(*buddy);
181 +        }
182 +
183 +        this->buddies = buddies_;
184   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines