ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/zoe/trunk/Zoe.cpp
Revision: 58
Committed: 2004-09-12T15:18:25-07:00 (20 years, 9 months ago) by douglas
File size: 5519 byte(s)
Log Message:
I can finally commit again!

File Contents

# Content
1 // Zoe AIM Away Message RSS Feed Generator
2 //
3 // Seth King and Douglas Thrift
4 //
5 // $Id$
6
7 #include "Matcher.hpp"
8 #include "Collector.hpp"
9 #include "Publisher.hpp"
10
11 #include <menes-api/exename.hpp>
12 #include <menes-app/application.hpp>
13
14 #include <algorithm>
15 #include <cstring>
16 #include <iterator>
17
18 extern "C"
19 {
20 #include <pwd.h>
21 #include <sys/utsname.h>
22 #include <unistd.h>
23 }
24
25 struct ZoeCommand : public app::Application
26 {
27 virtual int Run(const app::ArgumentList& args)
28 {
29 Zoe::program = api::GetExecutableName();
30
31 for (size_t index(0); index < args.GetSize(); ++index)
32 {
33 ext::String arg(args[index]);
34 Matcher matcher;
35
36 if (arg == matcher("^-config=(.*)$"))
37 {
38 Zoe::config = matcher[1];
39 }
40 else if (arg == "-collector")
41 {
42 if (!Zoe::collector) Zoe::collector = true;
43 }
44 else if (arg == "-publisher")
45 {
46 if (!Zoe::publisher) Zoe::publisher = true;
47 }
48 else if (arg == "-color")
49 {
50 if (!Zoe::color) Zoe::color = true;
51 }
52 else if (arg == "-D")
53 {
54 if (!Zoe::debug) Zoe::debug = true;
55 }
56 else Zoe::usage();
57 }
58
59 Zoe zoe;
60
61 return 0;
62 }
63 } zoe;
64
65 Zoe::Zoe()
66 {
67 if (!(collector || publisher)) usage();
68
69 configure();
70 initialize();
71
72 Collector collector(login, password, buddies, database, Zoe::collector);
73 Publisher publisher(buddies, database, Zoe::publisher);
74 }
75
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 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 switch (generator)
93 {
94 case all:
95 generator_.InsertAllLast("/0.9");
96 case agent:
97 {
98 utsname system;
99
100 uname(&system);
101
102 generator_.InsertAllLast(_S(" (") + system.sysname);
103 }
104
105 if (generator == agent)
106 {
107 generator_.InsertAllLast(")");
108
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 return generator_;
123 }
124
125 void Zoe::configure()
126 {
127 if (debug) 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) 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 if (debug)
175 {
176 cerr << "buddies = {\n";
177
178 for (std::set<Buddy>::const_iterator buddy(this->buddies.begin());
179 buddy != this->buddies.end(); ++buddy)
180 {
181 cerr << " " << *buddy << " = {\n rss = " << buddy->getRss()
182 << "\n atom = " << buddy->getAtom() << "\n link = "
183 << buddy->getLink() << "\n }\n";
184 }
185
186 cerr << "}\n";
187 }
188
189 if (debug) cerr << "collector = " << lexical_cast<ext::String>(collector)
190 << "\npublisher = " << lexical_cast<ext::String>(publisher)
191 << "\ncolor = " << lexical_cast<ext::String>(color) << '\n';
192 }
193
194 void Zoe::initialize()
195 {
196 ext::Handle<dbi::Connection> db(dbi::Connect(database.driver, database.host,
197 database.user, database.password, database.db));
198 ext::Handle<dbi::ResultSet> buddies(db->Execute("SELECT * FROM buddies"));
199 std::set<Buddy> buddies_;
200
201 while (buddies->MoveNext())
202 {
203 if (this->buddies.find(buddies->GetString("buddy")) != this->buddies.end())
204 {
205 Buddy buddy(*this->buddies.find(buddies->GetString("buddy")));
206
207 buddy.setId(lexical_cast<ext::Uuid>(buddies->GetString("id")));
208 buddies_.insert(buddy);
209 }
210 }
211
212 std::vector<Buddy> difference;
213
214 set_difference(this->buddies.begin(), this->buddies.end(), buddies_.begin(),
215 buddies_.end(), std::insert_iterator<std::vector<Buddy> >(difference,
216 difference.begin()));
217
218 for (std::vector<Buddy>::iterator buddy(difference.begin());
219 buddy != difference.end(); ++buddy)
220 {
221 ext::Uuid id;
222
223 api::Uuid::CreateSequential(id);
224
225 buddy->setId(id);
226 db->Execute("INSERT INTO buddies (id, buddy) VALUES ('"
227 + lexical_cast<ext::String>(id) + "', '" + ext::String(*buddy)
228 + "')");
229
230 buddies_.insert(*buddy);
231 }
232
233 this->buddies = buddies_;
234 }

Properties

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