summaryrefslogtreecommitdiffstats
path: root/src/Client/UserCommands.cpp
diff options
context:
space:
mode:
authorMatthias Schiffer <matthias@gamezock.de>2009-06-07 22:32:28 +0200
committerMatthias Schiffer <matthias@gamezock.de>2009-06-07 22:32:28 +0200
commit452320b5ec31447a526735016fa07589cb848032 (patch)
tree18d7a8a05cb55508302916f5daf2516fddbbe477 /src/Client/UserCommands.cpp
parentfcdd58703e3f5a0f6e77fd74e0304038e7cd4d3e (diff)
downloadmad-452320b5ec31447a526735016fa07589cb848032.tar
mad-452320b5ec31447a526735016fa07589cb848032.zip
CommandParser in mehrere kleinere Klassen aufgeteilt
Diffstat (limited to 'src/Client/UserCommands.cpp')
-rw-r--r--src/Client/UserCommands.cpp230
1 files changed, 230 insertions, 0 deletions
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 <matthias@gamezock.de>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include "UserCommands.h"
+#include "CommandParser.h"
+
+#include <Common/Logger.h>
+#include <Common/RequestManager.h>
+
+#include <Common/Requests/GroupListRequest.h>
+#include <Common/Requests/GroupUserListRequest.h>
+#include <Common/Requests/UserInfoRequest.h>
+#include <Common/Requests/UserListRequest.h>
+#include <Common/Requests/UserGroupListRequest.h>
+
+#include <iostream>
+
+
+namespace Mad {
+namespace Client {
+
+void UserCommands::userInfoCommand(const std::vector<std::string> &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<Common::Requests::UserInfoRequest> request(new Common::Requests::UserInfoRequest(uid));
+ Common::RequestManager::get()->sendRequest(connection, request);
+ request->wait();
+
+ std::pair<boost::shared_ptr<const Common::XmlPacket>, 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<std::string> &args _UNUSED_PARAMETER_, Common::Connection *connection) {
+ boost::shared_ptr<Common::Requests::UserListRequest> request(new Common::Requests::UserListRequest);
+
+ Common::RequestManager::get()->sendRequest(connection, request);
+ request->wait();
+
+ std::pair<boost::shared_ptr<const Common::XmlPacket>, 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<std::string> &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<Common::Requests::UserGroupListRequest> request(new Common::Requests::UserGroupListRequest(uid));
+ Common::RequestManager::get()->sendRequest(connection, request);
+ request->wait();
+
+ std::pair<boost::shared_ptr<const Common::XmlPacket>, 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<std::string> &args _UNUSED_PARAMETER_, Common::Connection *connection) {
+ boost::shared_ptr<Common::Requests::GroupListRequest> request(new Common::Requests::GroupListRequest);
+
+ Common::RequestManager::get()->sendRequest(connection, request);
+ request->wait();
+
+ std::pair<boost::shared_ptr<const Common::XmlPacket>, 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<std::string> &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<Common::Requests::GroupUserListRequest> request(new Common::Requests::GroupUserListRequest(gid));
+ Common::RequestManager::get()->sendRequest(connection, request);
+ request->wait();
+
+ std::pair<boost::shared_ptr<const Common::XmlPacket>, 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;
+ }
+}
+
+}
+}