// Zoe AIM Away Message RSS Feed Generator // // Seth King and Douglas Thrift // // $Id$ #include "Matcher.hpp" #include "Collector.hpp" #include "Publisher.hpp" #include #include #include extern "C" { #include #include #include } int main(int argc, char* argv[]) { Zoe::program = argv[0]; for (int index(1); index < argc; ++index) { ext::String arg(argv[index]); Matcher matcher; if (arg == matcher("^-config=(.*)$")) { Zoe::config = matcher[1]; } else if (arg == "-collector") { if (!Zoe::collector) Zoe::collector = true; } else if (arg == "-publisher") { if (!Zoe::publisher) Zoe::publisher = true; } else if (arg == "-D") { if (!Zoe::debug) Zoe::debug = true; } else { cerr << bright << "Usage: " << cyan << Zoe::program << reset << " [" << bright << blue << "-config=" << yellow << "config" << reset << "] [" << bright << blue << "-collector" << reset << "] [" << bright << blue << "-publisher" << reset << "] [" << bright << blue << "-D" << reset << "]\n"; return 1; } } Zoe zoe; return 0; } Zoe::Zoe() { configure(); initialize(); Collector collector(login, password, buddies, database, Zoe::collector); Publisher publisher(buddies, database, Zoe::publisher); } bool Zoe::debug(false), Zoe::collector(false), Zoe::publisher(false); ext::String Zoe::program, Zoe::config("zoe.xml"); ext::String Zoe::generator() { utsname system; uname(&system); return ext::String("Zoe/0.9 (") + system.sysname + "; http://computers.doug" + "lasthrift.net/zoe.xml)"; } void Zoe::configure() { if (debug) cerr << "config = " << config << '\n'; ext::Handle document(xml::Parse(config)); ext::Handle zoe(*document/"zoe"); login = std::string(*zoe/"login"); password = std::string(*zoe/"password"); database.driver = std::string(*zoe/"database"/"driver"); database.host = std::string(*zoe/"database"/"host"); database.user = std::string(*zoe/"database"/"user"); if ((*zoe/"database"/"password").IsEmpty()) { ext::String prompt(database.user + (!database.host.IsEmpty() ? "@" : "") + database.host + (!database.user.IsEmpty() ? "'s " : "") + "Databa" + "se Password: "); char* password(getpass(prompt.NullTerminate())); database.password = password; for (size_t index(std::strlen(password)); index > 0; --index) password[index - 1] = '\0'; } else database.password = std::string(*zoe/"database"/"password"); database.db = std::string(*zoe/"database"/"db"); if (debug) cerr << "login = " << login << "\npassword = " << std::string(password.GetSize(), '*') << "\ndatabase = {\n driver =" << ' ' << database.driver << "\n host=" << database.host << "\n use" << "r = " << database.user << "\n password = " << std::string(database.password.GetSize(), '*') << "\n db = " << database.db << "\n}\n"; xml::NodeSet buddies(*zoe/"buddy"); for (xml::NodeSet::Iterator buddy(buddies.Begin()); buddy != buddies.End(); ++buddy) this->buddies.insert(Buddy(**buddy/"login", **buddy/"rss")); if (debug) { cerr << "buddies = {\n"; for (std::set::const_iterator buddy(this->buddies.begin()); buddy != this->buddies.end(); ++buddy) cerr << " " << *buddy << '\n'; cerr << "}\n"; } if (!collector && !publisher) { collector = true; publisher = true; } if (debug) cerr << "collector = " << lexical_cast(collector) << "\npublisher = " << lexical_cast(publisher) << '\n'; } void Zoe::initialize() { ext::Handle db(dbi::Connect(database.driver, database.host, database.user, database.password, database.db)); ext::Handle buddies(db->Execute("SELECT * FROM buddies")); std::set buddies_; while (buddies->MoveNext()) { Buddy buddy(*this->buddies.find(buddies->GetString("buddy"))); buddy.setId(lexical_cast(buddies->GetString("id"))); buddies_.insert(buddy); } std::vector difference; set_difference(this->buddies.begin(), this->buddies.end(), buddies_.begin(), buddies_.end(), std::insert_iterator >(difference, difference.begin())); for (std::vector::iterator buddy(difference.begin()); buddy != difference.end(); ++buddy) { ext::Uuid id; api::Uuid::CreateSequential(id); buddy->setId(id); db->Execute("INSERT INTO buddies (id, buddy) VALUES ('" + lexical_cast(id) + "', '" + ext::String(*buddy) + "')"); buddies_.insert(*buddy); } this->buddies = buddies_; }