/* El formato de autentificacion es bot:username:password */ static int server_login(struct mplx_list *list) { struct mplx_socket *cur = MPLX_CUR(list); server_client *client; orix_bot *bot; char buf[256]="", *args[3]; int access; if(recv(cur->sockfd, buf, sizeof(buf), 0) <= 0) return 0; clean_buf(buf); if(*buf == 0) return 0; if(str_to_list(args, buf, ":", 3) < 3) return 0; bot = bot_find(args[0]); if(!bot) return 0; access = user_auth(bot->db, args[1], args[2]); if(access == USER_ERROR) return 0; client = malloc(sizeof(server_client)); if(!client) { perror("malloc"); exit(EXIT_FAILURE); } client->bot = bot; client->username = strdup(args[1]); client->access = access; if(!bot->clients) { bot->clients = ocore_dlist_new(); ocore_list_set_free_func(OCORE_LIST(bot->clients), kick_out); } ocore_dlist_new_node(bot->clients, (void *)cur); mplx_set(cur, MPLX_SET_DATA, (void *)client); mplx_set(cur, MPLX_RECV_CALLBACK, (void *)server_console); mplx_set(cur, MPLX_SET_TIMEOUT, (void *)300); /* 5 min */ prompt(cur->sockfd, client); return 1; }
void server_parse_input(char * message){ //if query is empty... do nothing if(strlen(message)==0)return; //if query is type 1 - login user if(message[0]=='1'){ //skip first 2 characters "1|" message+=2; //alocate memory for user user_t * user = malloc(sizeof(user_t)); //copy username char * separator = strchr(message,'|'); separator[0]='\0'; user->username = malloc((strlen(message)+1)*sizeof(char)); strcpy(user->username, message); //erase copied characters from message message = separator+1; //copy password user->password = malloc((strlen(message)+1)*sizeof(char)); strcpy(user->password, message); //authentificate used if(user_auth(user)){ //and log in if(!user_login(user)){ message_send(user,"Server is full !!!\n"); free(user->username); free(user->password); free(user); } } //wrong credentials else{ message_send(user,"Login incorrect\n"); free(user->username); free(user->password); free(user); } } //if query is of type 2 - send online users else if(message[0]=='2'){ //print to server console printf("Sending online users\n"); //skip first 2 characters - 2| message+=2; //call function with given name of named pipe print_online(message); } //query is of type 3 - send message //query format is 3|from|to|message else if(message[0]=='3'){ //skip first 2 characters - 3| message+=2; //copy "from" char * separator = strchr(message,'|'); separator[0]='\0'; char * from = malloc((strlen(message)+1)*sizeof(char)); strcpy(from, message); //erase copyied characters message = separator+1; //copy "to" separator = strchr(message,'|'); separator[0]='\0'; char * to = malloc((strlen(message)+1)*sizeof(char)); strcpy(to, message); //erase copied characters message = separator+1; //find user "to" user_t * user=NULL; for(int i=0; i<SERVER_CAPACITY; i++){ if(users_logged[i] && !strcmp(users_logged[i]->username,to)){ user=users_logged[i]; } } //if we have found user "to" if(user){ //compose message char tmp[BUFFER_SIZE]; tmp[0]='\0'; strcat(tmp,from); strcat(tmp," -> "); strcat(tmp,message); strcat(tmp,"\n"); //send message to user "to" message_send(user,tmp); //print message to server console printf("User %s sent a message to %s\n",from, to); } //free used memory free(from); free(to); } //query is of type 4 - logout user else if(message[0]=='4'){ //erase first 2 characters - 4| message+=2; //logout user user_logout(message); } }