ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/zoe/trunk/Zoe.cpp
Revision: 22
Committed: 2004-07-17T00:54:46-07:00 (20 years, 11 months ago) by douglas
File size: 4388 byte(s)
Log Message:
Moved console color so everyone can use it, other stuff.

File Contents

# User Rev Content
1 douglas 5 // Zoe AIM Away Message RSS Feed Generator
2     //
3     // Seth King and Douglas Thrift
4     //
5     // $Id$
6    
7     #include "Matcher.hpp"
8 douglas 13 #include "Collector.hpp"
9     #include "Publisher.hpp"
10 douglas 5
11 douglas 21 #include <algorithm>
12     #include <cstring>
13     #include <iterator>
14    
15     extern "C"
16     {
17     #include <pwd.h>
18     #include <unistd.h>
19     }
20    
21 douglas 5 int main(int argc, char* argv[])
22     {
23 douglas 7 Zoe::program = argv[0];
24 douglas 5
25 douglas 7 for (int index(1); index < argc; ++index)
26     {
27     ext::String arg(argv[index]);
28     Matcher matcher;
29    
30     if (arg == matcher("^-config=(.*)$"))
31     {
32     Zoe::config = matcher[1];
33     }
34 douglas 13 else if (arg == "-collector")
35     {
36     if (!Zoe::collector) Zoe::collector = true;
37     }
38     else if (arg == "-publisher")
39     {
40     if (!Zoe::publisher) Zoe::publisher = true;
41     }
42 douglas 7 else if (arg == "-D")
43     {
44     if (!Zoe::debug) Zoe::debug = true;
45     }
46     else
47     {
48 douglas 14 cerr << bright << "Usage: " << cyan << Zoe::program << reset << " ["
49     << bright << blue << "-config=" << yellow << "config" << reset
50     << "] [" << bright << blue << "-collector" << reset << "] ["
51     << bright << blue << "-publisher" << reset << "] [" << bright
52     << blue << "-D" << reset << "]\n";
53 douglas 9
54     return 1;
55 douglas 7 }
56     }
57    
58     Zoe zoe;
59    
60 douglas 5 return 0;
61     }
62 douglas 7
63 douglas 12 Zoe::Zoe()
64 douglas 7 {
65     configure();
66 douglas 21 initialize();
67 douglas 8
68 douglas 21 Collector collector(login, password, buddies, database, Zoe::collector);
69     Publisher publisher(buddies, database, Zoe::publisher);
70 douglas 7 }
71    
72 douglas 13 bool Zoe::debug(false), Zoe::collector(false), Zoe::publisher(false);
73 douglas 7 ext::String Zoe::program, Zoe::config("zoe.xml");
74    
75     void Zoe::configure()
76     {
77 douglas 21 if (debug) cerr << "config = " << config << '\n';
78    
79 douglas 7 ext::Handle<xml::Document> document(xml::Parse(config));
80     ext::Handle<xml::Node> zoe(*document/"zoe");
81    
82 douglas 12 login = std::string(*zoe/"login");
83     password = std::string(*zoe/"password");
84 douglas 21 database.driver = std::string(*zoe/"database"/"driver");
85     database.host = std::string(*zoe/"database"/"host");
86     database.user = std::string(*zoe/"database"/"user");
87 douglas 7
88 douglas 21 if ((*zoe/"database"/"password").IsEmpty())
89     {
90     ext::String prompt(database.user + (!database.host.IsEmpty() ? "@" : "")
91     + database.host + (!database.user.IsEmpty() ? "'s " : "") + "Databa"
92     + "se Password: ");
93     char* password(getpass(prompt.NullTerminate()));
94    
95     database.password = password;
96    
97     for (size_t index(std::strlen(password)); index > 0; --index)
98     password[index - 1] = '\0';
99     }
100     else database.password = std::string(*zoe/"database"/"password");
101    
102     database.db = std::string(*zoe/"database"/"db");
103    
104     if (debug) cerr << "login = " << login << "\npassword = "
105     << std::string(password.GetSize(), '*') << "\ndatabase = {\n driver ="
106     << ' ' << database.driver << "\n host=" << database.host << "\n use"
107     << "r = " << database.user << "\n password = "
108     << std::string(database.password.GetSize(), '*') << "\n db = "
109     << database.db << "\n}\n";
110    
111 douglas 12 xml::NodeSet buddies(*zoe/"buddy");
112    
113     for (xml::NodeSet::Iterator buddy(buddies.Begin()); buddy != buddies.End();
114 douglas 21 ++buddy) this->buddies.insert(Buddy(**buddy/"login", **buddy/"rss"));
115    
116     if (debug)
117 douglas 12 {
118 douglas 21 cerr << "buddies = {\n";
119    
120     for (std::set<Buddy>::const_iterator buddy(this->buddies.begin()); buddy
121     != this->buddies.end(); ++buddy) cerr << " " << *buddy << '\n';
122    
123     cerr << "}\n";
124 douglas 12 }
125 douglas 13
126     if (!collector && !publisher)
127     {
128     collector = true;
129     publisher = true;
130     }
131 douglas 21
132     if (debug) cerr << "collector = " << lexical_cast<ext::String>(collector)
133     << "\npublisher = " << lexical_cast<ext::String>(publisher) << '\n';
134 douglas 7 }
135 douglas 21
136     void Zoe::initialize()
137     {
138     ext::Handle<dbi::Connection> db(dbi::Connect(database.driver, database.host,
139     database.user, database.password, database.db));
140     ext::Handle<dbi::ResultSet> buddies(db->Execute("SELECT * FROM buddies"));
141     std::set<Buddy> buddies_;
142    
143     while (buddies->MoveNext())
144     {
145     Buddy buddy(*this->buddies.find(buddies->GetString("buddy")));
146    
147     buddy.setId(lexical_cast<ext::Uuid>(buddies->GetString("id")));
148     buddies_.insert(buddy);
149     }
150    
151     std::vector<Buddy> difference;
152    
153     set_difference(this->buddies.begin(), this->buddies.end(), buddies_.begin(),
154     buddies_.end(), std::insert_iterator<std::vector<Buddy> >(difference,
155     difference.begin()));
156    
157     for (std::vector<Buddy>::iterator buddy(difference.begin()); buddy !=
158     difference.end(); ++buddy)
159     {
160     ext::Uuid id;
161    
162     api::Uuid::CreateSequential(id);
163    
164     buddy->setId(id);
165     db->Execute("INSERT INTO buddies (id, buddy) VALUES ('" +
166     lexical_cast<ext::String>(id) + "', '" + ext::String(*buddy) +
167     "')");
168    
169     buddies_.insert(*buddy);
170     }
171    
172     this->buddies = buddies_;
173     }

Properties

Name Value
svn:eol-style native
svn:keywords Id