diff --git a/main.c b/main.c index a41b5fb..61de6be 100644 --- a/main.c +++ b/main.c @@ -93,48 +93,94 @@ int main() { strcat(data, buffer); } strcat(data,"\0"); - if (from_user[i] == NULL) { - from_user[i] = fetch_user(fetch_from_string(data,"IAM")); + + if (strcmp(strtok(data," \r\n"), "SENDING") == 0) { if (from_user[i] == NULL) { - printf("Please identify yourself.\n"); - close(conn_sockets[i]); - continue; - } else { - printf("You are %s\n",from_user[i]->username); + from_user[i] = fetch_user(fetch_from_string(data,"IAM")); + if (from_user[i] == NULL) { + printf("Please identify yourself.\n"); + close(conn_sockets[i]); + continue; + } else { + printf("You are %s\n",from_user[i]->username); + } } - } - - if (to_user[i] == NULL) { - to_user[i] = fetch_user(fetch_from_string(data,"TO")); + if (to_user[i] == NULL) { - printf("Invalid user or message format.\n"); - close(conn_sockets[i]); - continue; + to_user[i] = fetch_user(fetch_from_string(data,"TO")); + if (to_user[i] == NULL) { + printf("Invalid user or message format.\n"); + close(conn_sockets[i]); + continue; + } else { + printf("Message intended for %s\n",to_user[i]->username); + } + } + + char* message_string = fetch_message_string(data); + if (message_string == NULL) { + printf("Invalid message.\n"); + return -10; + } + + printf("Your message is: %s\n",message_string); + + if (user_to_index(to_user[i]) < 0) { + printf("Recipient user does not exist!\n"); + return -20; + } + + Message* message = new_message(message_string,from_user[i],to_user[i]); + stack_push( message_stack[user_to_index(to_user[i])],message ); + + printf("Message has been pushed onto stack for %s\n",to_user[i]->username); + + } else if (strcmp(strtok(buffer," \r\n"), "SENDING") == 0) { + + if (from_user[i] == NULL) { + from_user[i] = fetch_user(fetch_from_string(data,"IAM")); + if (from_user[i] == NULL) { + printf("Please identify yourself.\n"); + close(conn_sockets[i]); + continue; + } else { + printf("You are %s\n",from_user[i]->username); + } + } + + int num_of_messages; + char* num_of_messages_string = fetch_from_string(data,"GETLAST"); + + if (num_of_messages_string == NULL) { + printf("Invalid GET request.\n"); + return -30; } else { - printf("Message intended for %s\n",to_user[i]->username); + if (strstr(num_of_messages_string,"ALL") == 0) { + num_of_messages = stack_size(message_stack[user_to_index(from_user[i])]); + } else { + num_of_messages = atoi(num_of_messages_string); + } } - } - - char* message_string = fetch_message_string(data); - if (message_string == NULL) { - printf("Invalid message.\n"); - return -10; - } - printf("Your message is: %s\n",message_string); + if (num_of_messages > stack_size(message_stack[user_to_index(from_user[i])])) { + num_of_messages = stack_size(message_stack[user_to_index(from_user[i])]); + } + + for (int i=0;isender->username, + asctime(message->timeinfo), + message->text); + } + - if (user_to_index(to_user[i]) < 0) { - printf("Recipient user does not exist!\n"); - return -20; + } else { + printf("You are not sending or receiving.\n"); } - Message* message = new_message(message_string,from_user[i],to_user[i]); - stack_push( message_stack[user_to_index(to_user[i])],message ); - - printf("Message has been pushed onto stack for %s\n",to_user[i]->username); - - - close(conn_sockets[i]); }