// Host Update // // Douglas Thrift // // $Id$ #include "HostUpdate.hpp" #include "Host.hpp" int main(int argc, char* argv[]) { HostUpdate update; return 0; } HostUpdate::HostUpdate() { struct stat* hosts = new struct stat; if (stat("hosts", hosts) != 0) { mkdir("hosts"); } delete [] hosts; chdir("hosts"); CgiEnvironment env = cgi.getEnvironment(); string agent = env.getUserAgent(); string method = env.getRequestMethod(); if (agent.find("Host Update/") == 0 && method == "POST" && agent.find(" (") != string::npos && agent.find(") libwww-perl/") != string::npos) { update(env, agent); } else { display(env); } } HostUpdate::~HostUpdate() { // } void HostUpdate::update(CgiEnvironment& env, const string& agent) { cout << "Content-Type: text/plain\n\n"; vector entries; if (!cgi.getElement("host", entries)) return; if (entries[0].isEmpty()) return; string host = entries[0].getStrippedValue(), name = env.getRemoteHost(), address = env.getRemoteAddr(); if (name == address) name = ""; string::size_type begin = agent.find('(') + 1, end = agent.find(')', begin); string platform = agent.substr(begin, end - begin); cout << "host=" << host << '\n' << "name=" << name << '\n' << "address=" << address << '\n' << "platform=" << platform << '\n'; Host client(host, name, address, platform), saved(host); if (client != ++saved) client--; } void HostUpdate::display(CgiEnvironment& env) { cout << "Content-Type: text/plain\n\n"; set hosts; DIR* dir = opendir("."); struct dirent* ent; while ((ent = readdir(dir)) != NULL) { string file = ent->d_name; cerr << file << '\n'; if (file == "." || file == "..") continue; Host host(file); hosts.insert(++host); } closedir(dir); for (set::iterator itor = hosts.begin(); itor != hosts.end(); itor++) { Host host = *itor; cout << "host=" << host.getHost() << '\n' << "name=" << host.getName() << '\n' << "address=" << host.getAddress() << '\n' << "platform=" << host.getPlatform() << "\n\n"; } }