/* * UserCommands.cpp * * Copyright (C) 2009 Matthias Schiffer * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program. If not, see . */ #include "UserCommands.h" #include "CommandParser.h" #include #include #include #include #include #include #include #include namespace Mad { namespace Client { void UserCommands::userInfoCommand(const std::vector &args, Common::Connection *connection) { if(args.size() == 1) { Core::Logger::logf(Core::Logger::ERROR, "%s: No user id given.", args[0].c_str()); CommandParser::printUsage("user_info"); return; } if(args.size() > 2) { Core::Logger::logf(Core::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); CommandParser::printUsage("user_info"); return; } char *endptr; unsigned long uid = std::strtoul(args[1].c_str(), &endptr, 10); if(args[1].empty() || *endptr != '\0') { Core::Logger::logf(Core::Logger::ERROR, "%s: Unable to parse user id.", args[0].c_str()); CommandParser::printUsage("user_info"); return; } boost::shared_ptr request(new Common::Requests::UserInfoRequest(uid)); Common::RequestManager::get()->sendRequest(connection, request); request->wait(); std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) { Core::Logger::logf(Core::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); } else { const Common::XmlPacket &packet = *result.first; std::cout << " " << (unsigned long)packet["uid"] << ", " << (unsigned long)packet["gid"] << ", " << (const std::string&)packet["username"] << ", " << (const std::string&)packet["fullName"] << std::endl << std::endl; } } void UserCommands::listUsersCommand(const std::vector &args _UNUSED_PARAMETER_, Common::Connection *connection) { boost::shared_ptr request(new Common::Requests::UserListRequest); Common::RequestManager::get()->sendRequest(connection, request); request->wait(); std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) { Core::Logger::logf(Core::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); } else { const Common::XmlPacket::Element &users = (*result.first)["users"]; if(users.isEmpty()) { std::cout << "User list is empty." << std::endl; } else { std::cout << "Found " << users.getSize() << " user" << (users.getSize()==1 ? "" : "s") << ":" << std::endl; for(size_t i = 0; i < users.getSize(); ++i) std::cout << " " << (unsigned long)users[i]["uid"] << ", " << (unsigned long)users[i]["gid"] << ", " << (const std::string&)users[i]["username"] << ", " << (const std::string&)users[i]["fullName"] << std::endl; } std::cout << std::endl; } } void UserCommands::listUserGroupsCommand(const std::vector &args, Common::Connection *connection) { if(args.size() == 1) { Core::Logger::logf(Core::Logger::ERROR, "%s: No user id given.", args[0].c_str()); CommandParser::printUsage("list_user_groups"); return; } if(args.size() > 2) { Core::Logger::logf(Core::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); CommandParser::printUsage("list_user_groups"); return; } char *endptr; unsigned long uid = std::strtoul(args[1].c_str(), &endptr, 10); if(args[1].empty() || *endptr != '\0') { Core::Logger::logf(Core::Logger::ERROR, "%s: Unable to parse user id.", args[0].c_str()); CommandParser::printUsage("list_user_groups"); return; } boost::shared_ptr request(new Common::Requests::UserGroupListRequest(uid)); Common::RequestManager::get()->sendRequest(connection, request); request->wait(); std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) { Core::Logger::logf(Core::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); } else { const Common::XmlPacket::Element &groups = (*result.first)["groups"]; if(groups.isEmpty()) { std::cout << "The user isn't member of any group." << std::endl; } else { std::cout << "User is member of " << groups.getSize() << " group" << (groups.getSize()==1 ? "" : "s") << ":" << std::endl; for(size_t i = 0; i < groups.getSize(); ++i) std::cout << " " << (unsigned long)groups[i]["gid"] << std::endl; } std::cout << std::endl; } } void UserCommands::listGroupsCommand(const std::vector &args _UNUSED_PARAMETER_, Common::Connection *connection) { boost::shared_ptr request(new Common::Requests::GroupListRequest); Common::RequestManager::get()->sendRequest(connection, request); request->wait(); std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) { Core::Logger::logf(Core::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); } else { const Common::XmlPacket::Element &groups = (*result.first)["groups"]; if(groups.isEmpty()) { std::cout << "Group list is empty." << std::endl; } else { std::cout << "Found " << groups.getSize() << " group" << (groups.getSize()==1 ? "" : "s") << ":" << std::endl; for(size_t i = 0; i < groups.getSize(); ++i) std::cout << " " << (unsigned long)groups[i]["gid"] << ", " << (const std::string&)groups[i]["name"] << std::endl; } std::cout << std::endl; } } void UserCommands::listGroupUsersCommand(const std::vector &args, Common::Connection *connection) { if(args.size() == 1) { Core::Logger::logf(Core::Logger::ERROR, "%s: No group id given.", args[0].c_str()); CommandParser::printUsage("list_group_users"); return; } if(args.size() > 2) { Core::Logger::logf(Core::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); CommandParser::printUsage("list_group_users"); return; } char *endptr; unsigned long gid = std::strtoul(args[1].c_str(), &endptr, 10); if(args[1].empty() || *endptr != '\0') { Core::Logger::logf(Core::Logger::ERROR, "%s: Unable to parse group id.", args[0].c_str()); CommandParser::printUsage("list_group_users"); return; } boost::shared_ptr request(new Common::Requests::GroupUserListRequest(gid)); Common::RequestManager::get()->sendRequest(connection, request); request->wait(); std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) { Core::Logger::logf(Core::Logger::ERROR, "An error occurred during your request: %s.", result.second.strerror().c_str()); } else { const Common::XmlPacket::Element &users = (*result.first)["users"]; if(users.isEmpty()) { std::cout << "The group doesn't have any members." << std::endl; } else { std::cout << "The group has " << users.getSize() << " member" << (users.getSize()==1 ? "" : "s") << ":" << std::endl; for(size_t i = 0; i < users.getSize(); ++i) std::cout << " " << (unsigned long)users[i]["uid"] << std::endl; } std::cout << std::endl; } } } }