Started working on receiving messages
This commit is contained in:
118
main.c
118
main.c
@@ -93,48 +93,94 @@ int main() {
|
|||||||
strcat(data, buffer);
|
strcat(data, buffer);
|
||||||
}
|
}
|
||||||
strcat(data,"\0");
|
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) {
|
if (from_user[i] == NULL) {
|
||||||
printf("Please identify yourself.\n");
|
from_user[i] = fetch_user(fetch_from_string(data,"IAM"));
|
||||||
close(conn_sockets[i]);
|
if (from_user[i] == NULL) {
|
||||||
continue;
|
printf("Please identify yourself.\n");
|
||||||
} else {
|
close(conn_sockets[i]);
|
||||||
printf("You are %s\n",from_user[i]->username);
|
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) {
|
if (to_user[i] == NULL) {
|
||||||
printf("Invalid user or message format.\n");
|
to_user[i] = fetch_user(fetch_from_string(data,"TO"));
|
||||||
close(conn_sockets[i]);
|
if (to_user[i] == NULL) {
|
||||||
continue;
|
printf("Invalid user or message format.\n");
|
||||||
} else {
|
close(conn_sockets[i]);
|
||||||
printf("Message intended for %s\n",to_user[i]->username);
|
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 {
|
||||||
|
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");
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
close(conn_sockets[i]);
|
close(conn_sockets[i]);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user