From 452320b5ec31447a526735016fa07589cb848032 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 7 Jun 2009 22:32:28 +0200 Subject: CommandParser in mehrere kleinere Klassen aufgeteilt --- src/Client/UserCommands.cpp | 230 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 230 insertions(+) create mode 100644 src/Client/UserCommands.cpp (limited to 'src/Client/UserCommands.cpp') diff --git a/src/Client/UserCommands.cpp b/src/Client/UserCommands.cpp new file mode 100644 index 0000000..6defbcf --- /dev/null +++ b/src/Client/UserCommands.cpp @@ -0,0 +1,230 @@ +/* + * 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) { + Common::Logger::logf(Common::Logger::ERROR, "%s: No user id given.", args[0].c_str()); + CommandParser::printUsage("user_info"); + return; + } + if(args.size() > 2) { + Common::Logger::logf(Common::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') { + Common::Logger::logf(Common::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, Net::Exception> result = request->getResult(); + + if(!result.first || result.second) { + Common::Logger::logf(Common::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, Net::Exception> result = request->getResult(); + + if(!result.first || result.second) { + Common::Logger::logf(Common::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) { + Common::Logger::logf(Common::Logger::ERROR, "%s: No user id given.", args[0].c_str()); + CommandParser::printUsage("list_user_groups"); + return; + } + if(args.size() > 2) { + Common::Logger::logf(Common::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') { + Common::Logger::logf(Common::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, Net::Exception> result = request->getResult(); + + if(!result.first || result.second) { + Common::Logger::logf(Common::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, Net::Exception> result = request->getResult(); + + if(!result.first || result.second) { + Common::Logger::logf(Common::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) { + Common::Logger::logf(Common::Logger::ERROR, "%s: No group id given.", args[0].c_str()); + CommandParser::printUsage("list_group_users"); + return; + } + if(args.size() > 2) { + Common::Logger::logf(Common::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') { + Common::Logger::logf(Common::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, Net::Exception> result = request->getResult(); + + if(!result.first || result.second) { + Common::Logger::logf(Common::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; + } +} + +} +} -- cgit v1.2.3