ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/zoe/trunk/Zoe.cpp
Revision: 31
Committed: 2004-07-20T00:11:56-07:00 (20 years, 11 months ago) by douglas
File size: 4579 byte(s)
Log Message:
Ooh, pretty colors, and hourly updates, maybe.

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 5 int main(int argc, char* argv[])
23     {
24 douglas 7 Zoe::program = argv[0];
25 douglas 5
26 douglas 7 for (int index(1); index < argc; ++index)
27     {
28     ext::String arg(argv[index]);
29     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 7 else if (arg == "-D")
44     {
45     if (!Zoe::debug) Zoe::debug = true;
46     }
47     else
48     {
49 douglas 14 cerr << bright << "Usage: " << cyan << Zoe::program << reset << " ["
50     << bright << blue << "-config=" << yellow << "config" << reset
51     << "] [" << bright << blue << "-collector" << reset << "] ["
52     << bright << blue << "-publisher" << reset << "] [" << bright
53     << blue << "-D" << reset << "]\n";
54 douglas 9
55     return 1;
56 douglas 7 }
57     }
58    
59     Zoe zoe;
60    
61 douglas 5 return 0;
62     }
63 douglas 7
64 douglas 12 Zoe::Zoe()
65 douglas 7 {
66     configure();
67 douglas 21 initialize();
68 douglas 8
69 douglas 21 Collector collector(login, password, buddies, database, Zoe::collector);
70     Publisher publisher(buddies, database, Zoe::publisher);
71 douglas 7 }
72    
73 douglas 13 bool Zoe::debug(false), Zoe::collector(false), Zoe::publisher(false);
74 douglas 7 ext::String Zoe::program, Zoe::config("zoe.xml");
75    
76 douglas 25 ext::String Zoe::generator()
77     {
78     utsname system;
79    
80     uname(&system);
81    
82     return ext::String("Zoe/0.9 (") + system.sysname + "; http://computers.doug"
83     + "lasthrift.net/zoe.xml)";
84     }
85    
86 douglas 7 void Zoe::configure()
87     {
88 douglas 21 if (debug) cerr << "config = " << config << '\n';
89    
90 douglas 7 ext::Handle<xml::Document> document(xml::Parse(config));
91     ext::Handle<xml::Node> zoe(*document/"zoe");
92    
93 douglas 12 login = std::string(*zoe/"login");
94     password = std::string(*zoe/"password");
95 douglas 21 database.driver = std::string(*zoe/"database"/"driver");
96     database.host = std::string(*zoe/"database"/"host");
97     database.user = std::string(*zoe/"database"/"user");
98 douglas 7
99 douglas 21 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 douglas 12 xml::NodeSet buddies(*zoe/"buddy");
123    
124     for (xml::NodeSet::Iterator buddy(buddies.Begin()); buddy != buddies.End();
125 douglas 21 ++buddy) this->buddies.insert(Buddy(**buddy/"login", **buddy/"rss"));
126    
127     if (debug)
128 douglas 12 {
129 douglas 21 cerr << "buddies = {\n";
130    
131     for (std::set<Buddy>::const_iterator buddy(this->buddies.begin()); buddy
132 douglas 31 != this->buddies.end(); ++buddy) cerr << " " << *buddy << " = "
133     << buddy->getRss() << '\n';
134 douglas 21
135     cerr << "}\n";
136 douglas 12 }
137 douglas 13
138     if (!collector && !publisher)
139     {
140     collector = true;
141     publisher = true;
142     }
143 douglas 21
144     if (debug) cerr << "collector = " << lexical_cast<ext::String>(collector)
145     << "\npublisher = " << lexical_cast<ext::String>(publisher) << '\n';
146 douglas 7 }
147 douglas 21
148     void Zoe::initialize()
149     {
150     ext::Handle<dbi::Connection> db(dbi::Connect(database.driver, database.host,
151     database.user, database.password, database.db));
152     ext::Handle<dbi::ResultSet> buddies(db->Execute("SELECT * FROM buddies"));
153     std::set<Buddy> buddies_;
154    
155     while (buddies->MoveNext())
156     {
157     Buddy buddy(*this->buddies.find(buddies->GetString("buddy")));
158    
159     buddy.setId(lexical_cast<ext::Uuid>(buddies->GetString("id")));
160     buddies_.insert(buddy);
161     }
162    
163     std::vector<Buddy> difference;
164    
165     set_difference(this->buddies.begin(), this->buddies.end(), buddies_.begin(),
166     buddies_.end(), std::insert_iterator<std::vector<Buddy> >(difference,
167     difference.begin()));
168    
169     for (std::vector<Buddy>::iterator buddy(difference.begin()); buddy !=
170     difference.end(); ++buddy)
171     {
172     ext::Uuid id;
173    
174     api::Uuid::CreateSequential(id);
175    
176     buddy->setId(id);
177 douglas 30 db->Execute("INSERT INTO buddies (id, buddy) VALUES ('" + id + "', '" +
178     *buddy + "')");
179 douglas 21
180     buddies_.insert(*buddy);
181     }
182    
183     this->buddies = buddies_;
184     }

Properties

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