bool GetIpCommandHandler(chat_session_ptr _client, char const* args) { std::string s = "Tu IP es: " + _client->socket().remote_endpoint().address().to_string(); _client->SendMessageClient(s); return true; }
void CommandsMgr::RunCommand(chat_session_ptr _client, std::string command, bool console) { const char* command_str = _strdup(command.c_str()); char* tok = strtok((char*) command_str, " "); tok = strtok(NULL, " "); std::ostringstream args; while (tok != NULL) { args << " " << tok; tok = strtok(NULL, " "); } char* arg_f = (char*)args.str().c_str(); memcpy ( arg_f, args.str().c_str(), strlen(args.str().c_str()) +1); if (strcmp(arg_f, "") == 0) arg_f = NULL; for (int32 i = 0; i < MAX_COMMANDS; ++i) { if (boost::iequals(command_str, MyComands_[i].Name)) { if (!MyComands_[i].AllowConsole && console) { std::cout << "Este comando no se puede usar por consola." << std::endl; break; } if (!console && _client->GetSecurity() < MyComands_[i].sec) { CommandsMgr::SendMessageBoth(_client, "No tienes permisos para ejecutar este comando!"); CommandsMgr::PSendMessageBoth(_client, "Este comando requiere permisos de: %s.", GetSecurityString(MyComands_[i].sec).c_str()); break; } if (!MyComands_[i].handler(_client, arg_f)) { CommandsMgr::SendMessageBoth(_client, "Error de sintaxis."); CommandsMgr::PSendMessageBoth(_client, "%s", MyComands_[i].Help.c_str()); } break; } } // Esto manda al cliente el salto de linea e incluye TC> if (_client) _client->SendMessageClient(""); std::cout << std::endl << "TC> "; command.clear(); args.clear(); free(tok); }
void handle_accept(chat_session_ptr session,const boost::system::error_code& error) { if (!error) { session->start(); } start_accept(); }
void handle_accept(chat_session_ptr session, const asio::error_code& error) { if (!error) { session->start(); chat_session_ptr new_session(new chat_session(io_service_, room_)); acceptor_.async_accept(new_session->socket(), boost::bind(&chat_server::handle_accept, this, new_session, asio::placeholders::error)); } }
bool CommandsMgr::SendMessageBoth(chat_session_ptr _client, char const* msg) { if (!_client) { std::cout << msg << std::endl; return true; } std::string msg_ = msg; if (!_client->SendMessageClient(msg_)) return false; return true; }
bool SendAllHandler(chat_session_ptr _client, char const* args) { if (!args) return false; std::string from = std::string(); if (!_client) from = "Console: "; else from = _client->socket().remote_endpoint().address().to_string() + ": "; from += args; sServerGlobals->AssynSendMessageAllClients(from); return true; }
bool SendHandler(chat_session_ptr _client, char const* args) { if (!args) return false; char* to_ = strtok((char*)args, " "); char* msg_ = strtok(NULL, ""); if (!to_ || !msg_) return false; std::string from = std::string(); if (!_client) from = "Console: "; else from = _client->socket().remote_endpoint().address().to_string() + ": "; from += msg_; if (strcmp(to_, "Console") == 0) { std::cout << from; return true; } std::list<chat_session_ptr>::const_iterator itr = sServerGlobals->GetClientList().begin(); bool send_ = false; for (; itr != sServerGlobals->GetClientList().end(); ++itr) { if ((*itr) && (*itr)->socket().is_open()) { if ((*itr)->socket().remote_endpoint().address().to_string() == to_) { (*itr)->SendMessageClient(from); send_ = true; } } } if (!send_) CommandsMgr::SendMessageBoth(_client, "No se a encontrado un cliente con esa IP."); return true; }
bool ComandsListHandler(chat_session_ptr _client, char const* args) { ListCommands* commands_ = sCommandsMgr->GetCommandsList(); CommandsMgr::SendMessageBoth(_client, "Lista de comandos disponibles: "); for (int32 i = 0; commands_[i].Name != "NONE"; ++i) { if (!_client && !commands_[i].AllowConsole) continue; if (_client && _client->GetSecurity() < commands_[i].sec) continue; CommandsMgr::PSendMessageBoth(_client, "%s", commands_[i].Name.c_str()); } return true; }
bool LoginClientHandler(chat_session_ptr _client, char const* args) { // El !_client no deberia pasar JAMAS. if (!args || !_client) return false; char* pass_ = strtok((char*)args, " "); if (!pass_) return false; if (pass_ == sServerGlobals->AdminPass) _client->sec_level = SEC_ADMIN; else if (pass_ == sServerGlobals->ModPass) _client->sec_level = SEC_MODERATOR; else { CommandsMgr::SendMessageBoth(_client, "ContraseƱa incorrecta."); return true; } CommandsMgr::PSendMessageBoth(_client, "Se ha logeado con permisos de %s.", GetSecurityString(_client->GetSecurity()).c_str()); return true; }