/* * 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 "Application.h" #include "CommandParser.h" #include #include #include #include #include #include #include namespace Mad { namespace Client { void UserCommands::userInfoCommand(CommandParser *commandParser, const std::vector &args) { if(args.size() == 1) { std::cerr << args[0] << ": No user id given." << std::endl; commandParser->printUsage("user_info"); return; } if(args.size() > 2) { std::cerr << args[0] << ": Too many arguments." << std::endl; commandParser->printUsage("user_info"); return; } char *endptr; unsigned long uid = std::strtoul(args[1].c_str(), &endptr, 10); if(args[1].empty() || *endptr != '\0') { std::cerr << args[0] << ": Unable to parse user id." << std::endl; commandParser->printUsage("user_info"); return; } boost::shared_ptr request(new Common::Requests::UserInfoRequest(commandParser->application, uid)); commandParser->application->getRequestManager()->sendRequest(commandParser->connection, request); request->wait(); std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) { std::cerr << "An error occurred during your request: " << result.second.strerror() << "." << std::endl; } 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(CommandParser *commandParser, const std::vector &args _UNUSED_PARAMETER_) { boost::shared_ptr request(new Common::Requests::UserListRequest(commandParser->application)); commandParser->application->getRequestManager()->sendRequest(commandParser->connection, request); request->wait(); std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) { std::cerr << "An error occurred during your request: " << result.second.strerror() << "." << std::endl; } 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(CommandParser *commandParser, const std::vector &args) { if(args.size() == 1) { std::cerr << args[0] << ": No user id given." << std::endl; commandParser->printUsage("list_user_groups"); return; } if(args.size() > 2) { std::cerr << args[0] << ": Too many arguments." << std::endl; 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') { std::cerr << args[0] << ": Unable to parse user id." << std::endl; commandParser->printUsage("list_user_groups"); return; } boost::shared_ptr request(new Common::Requests::UserGroupListRequest(commandParser->application, uid)); commandParser->application->getRequestManager()->sendRequest(commandParser->connection, request); request->wait(); std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) { std::cerr << "An error occurred during your request: " << result.second.strerror() << "." << std::endl; } 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(CommandParser *commandParser, const std::vector &args _UNUSED_PARAMETER_) { boost::shared_ptr request(new Common::Requests::GroupListRequest(commandParser->application)); commandParser->application->getRequestManager()->sendRequest(commandParser->connection, request); request->wait(); std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) { std::cerr << "An error occurred during your request: " << result.second.strerror() << "." << std::endl; } 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(CommandParser *commandParser, const std::vector &args) { if(args.size() == 1) { std::cerr << args[0] << ": No group id given." << std::endl; commandParser->printUsage("list_group_users"); return; } if(args.size() > 2) { std::cerr << args[0] << ": Too many arguments." << std::endl; 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') { std::cerr << args[0] << ": Unable to parse group id." << std::endl; commandParser->printUsage("list_group_users"); return; } boost::shared_ptr request(new Common::Requests::GroupUserListRequest(commandParser->application, gid)); commandParser->application->getRequestManager()->sendRequest(commandParser->connection, request); request->wait(); std::pair, Core::Exception> result = request->getResult(); if(!result.first || result.second) { std::cerr << "An error occurred during your request: " << result.second.strerror() << "." << std::endl; } 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; } } } }