/* */ #include "Netrc.h" #include "Util.h" #include "RecoverableException.h" #include string Netrc::getRequiredNextToken(ifstream& f) const { string token; if(f >> token) { return token; } else { throw new RecoverableException("Netrc:parse error. EOF reached where a token expected."); } } void Netrc::skipMacdef(ifstream& f) const { string line; getline(f, line); while(getline(f, line)) { if(line == "\r" || line == "") { break; } } } void Netrc::parse(const string& path) { authenticators.clear(); ifstream f(path.c_str()); if(!f) { throw new RecoverableException("File not found: %s", path.c_str()); } AuthenticatorHandle authenticator = 0; string token; while(f >> token) { if(token == "machine") { storeAuthenticator(authenticator); authenticator = new Authenticator(); authenticator->setMachine(getRequiredNextToken(f)); } else if(token == "default") { storeAuthenticator(authenticator); authenticator = new DefaultAuthenticator(); } else { if(authenticator.isNull()) { throw new RecoverableException("Netrc:parse error. %s encounterd where 'machine' or 'default' expected."); } if(token == "login") { authenticator->setLogin(getRequiredNextToken(f)); } else if(token == "password") { authenticator->setPassword(getRequiredNextToken(f)); } else if(token == "account") { authenticator->setAccount(getRequiredNextToken(f)); } else if(token == "macdef") { getRequiredNextToken(f); skipMacdef(f); } } } storeAuthenticator(authenticator); } void Netrc::storeAuthenticator(const AuthenticatorHandle& authenticator) { if(!authenticator.isNull()) { authenticators.push_back(authenticator); } } class AuthHostMatch { private: string hostname; public: AuthHostMatch(const string& hostname):hostname(hostname) {} bool operator()(const AuthenticatorHandle& authenticator) { return authenticator->match(hostname); } }; AuthenticatorHandle Netrc::findAuthenticator(const string& hostname) const { Authenticators::const_iterator itr = find_if(authenticators.begin(), authenticators.end(), AuthHostMatch(hostname)); if(itr == authenticators.end()) { return 0; } else { return *itr; } }