static void client_command(string line, int sockfd, Server *&server) { if (line.find(CMD_REGISTER) == 0) { int user_pos = line.find(" ") + 1, pass_pos = line.find(" ", user_pos) + 1, email_pos = line.find(" ", pass_pos) + 1; server->register_client(sockfd, line.substr(user_pos, pass_pos -1 - user_pos), line.substr(pass_pos, email_pos - 1 - pass_pos), line.substr(email_pos)); return; } ClientInfo *ci = server->get_clientInfo_by_sockfd(sockfd); if (line.find(INFO_CLIENT_PORT) == 0) { int port_pos = line.find(" ") + 1, port; char port_ch[6]; assert(line.copy(port_ch, line.length() - port_pos, port_pos) > 0); port = atoi(port_ch); dprintf("[SERVER]received port %i\n", port); assert(ci != NULL); ci->set_port(port); return; } if (line.find(CMD_AUTH) == 0) { int user_pos = line.find(" ") + 1, pass_pos = line.find(" ", user_pos) + 1, ip_pos = line.find(" ", pass_pos) + 1; server->authentication(sockfd, line.substr(user_pos, pass_pos -1 - user_pos), line.substr(pass_pos), ci->get_ip(), ci->get_port()); announce_friends_online_status(sockfd); return; } if (ci->get_username().empty()) { dprintf("[SERVER]Can not execute '%s'; user is not logged in\n", line.c_str()); assert(send(sockfd, ERR_MSG, strlen(ERR_MSG) + 1, 0) >= 0); return; } if(line.find(CMD_ADD_USER) == 0){ dprintf("sending add_user command\n"); int user_pos = line.find(" ") + 1; server->add_user(sockfd, line.substr(user_pos)); return; } if (line.find(CMD_REMOVE_USER) == 0) { int user_pos = line.find(" ") + 1; server->remove_user(sockfd, line.substr(user_pos)); return; } if (line.find(CMD_ADD_GROUP) == 0) { int group_pos = line.find(" ") + 1; server->add_group(sockfd, line.substr(group_pos), ci->get_username()); return; } if(line.find(CMD_DEL_GROUP) == 0) { int group_pos = line.find(" ") + 1; server->remove_group(sockfd,line.substr(group_pos)); return; } if(line.find(CMD_MV_USER) == 0) { int user_pos= line.find(" ") + 1, group_pos = line.find(" ",user_pos) + 1; server-> move_user_to_group( sockfd, line.substr(user_pos, group_pos-1 - user_pos), line.substr(group_pos) ); return; } if (line.find(CMD_CONN_CLIENT_TO_CLIENT_REQ) == 0) { int username_pos = line.find(" ") + 1; dprintf("[SERVER] processing %s request from %s to %s\n", CMD_CONN_CLIENT_TO_CLIENT_REQ, ci->get_username().c_str(), line.substr(username_pos).c_str()); server->send_user_ip(sockfd, line.substr(username_pos)); return; } if (line.find(CMD_GET_PROFILE) == 0) { int user_pos = line.find(" ") + 1; server->send_profile(sockfd, line.substr(user_pos)); return; } if (line.find(CMD_UPDATE_PROFILE) == 0) { int name_pos = line.find(" ") + 1, sname_pos = line.find(" ", name_pos) + 1, phone_pos = line.find(" ", sname_pos) + 1, hobb_pos = line.find(" ", phone_pos) + 1; server->update_profile( sockfd, line.substr(name_pos, sname_pos - name_pos - 1), line.substr(sname_pos, phone_pos - sname_pos - 1), line.substr(phone_pos, hobb_pos - phone_pos - 1), line.substr(hobb_pos)); return; } if (line.find(CMD_SEND_MSG) == 0) { int user_src_pos = line.find(" ") + 1, user_dst_pos = line.find(" ", user_src_pos) + 1, msg_pos = line.find(" ", user_dst_pos) + 1; server->send_msg_from_user_to_user(sockfd, line.substr(user_src_pos, user_dst_pos - user_src_pos - 1), line.substr(user_dst_pos, msg_pos - user_dst_pos - 1), line.substr(msg_pos)); return; } if (line.find(INFO_CLIENT_PORT) == 0) { int port_pos = line.find(" ") + 1, port; char port_ch[6]; assert(line.copy(port_ch, line.length() - port_pos, port_pos) > 0); port = atoi(port_ch); dprintf("received port %i\n", port); ClientInfo *ci = server->get_clientInfo_by_sockfd(sockfd); assert(ci != NULL); ci->set_port(port); return; } if (line.find(CMD_SET_STATE) == 0) { int state_pos = line.find(" ") + 1; server->set_state(sockfd, line.substr(state_pos)); announce_friends_online_status(sockfd); return; } if (line.find(CMD_SET_STATUS) == 0) { int status_pos = line.find(" ") + 1; server->set_status(sockfd, line.substr(status_pos)); announce_friends_online_status(sockfd); return; } if (line.find(CMD_SEARCH_USER) == 0) { int name_pos = line.find(" ") + 1, surname_pos = line.find(" ", name_pos) + 1, phone_pos = line.find(" ", surname_pos) + 1, email_pos = line.find(" ", phone_pos) + 1; server->search_user(sockfd, line.substr(name_pos, surname_pos -1 - name_pos), line.substr(surname_pos, phone_pos - 1 - surname_pos), line.substr(phone_pos, email_pos - 1 - phone_pos), line.substr(email_pos)); return; } }