|
|
|
@ -93,47 +93,93 @@ 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;
|
|
|
|
|
} else {
|
|
|
|
|
printf("Message intended for %s\n",to_user[i]->username);
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
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 );
|
|
|
|
|
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);
|
|
|
|
|
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 {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;i<num_of_messages;i++) {
|
|
|
|
|
Message* message = stack_pop(message_stack[user_to_index(from_user[i])]);
|
|
|
|
|
printf("You have a message from %s"
|
|
|
|
|
", sent at %s"
|
|
|
|
|
": \"%s\"\n",
|
|
|
|
|
message->sender->username,
|
|
|
|
|
asctime(message->timeinfo),
|
|
|
|
|
message->text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
printf("You are not sending or receiving.\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
close(conn_sockets[i]);
|
|
|
|
|
|
|
|
|
|