ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/zoe/trunk/Zoe.cpp
Revision: 46
Committed: 2004-08-16T18:52:59-07:00 (20 years, 10 months ago) by douglas
File size: 5485 byte(s)
Log Message:
Made some drastic changes! Maybe not so drastic.

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

Properties

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