Compare commits

...

2 Commits

Author SHA1 Message Date
5bc6c575fc Started working on receiving messages 2023-04-14 08:11:09 -05:00
c8c9a34aab Used 'tm*' instead of 'tm' for storing time 2023-04-14 08:10:52 -05:00
3 changed files with 85 additions and 39 deletions

46
main.c
View File

@@ -93,6 +93,8 @@ int main() {
strcat(data, buffer);
}
strcat(data,"\0");
if (strcmp(strtok(data," \r\n"), "SENDING") == 0) {
if (from_user[i] == NULL) {
from_user[i] = fetch_user(fetch_from_string(data,"IAM"));
if (from_user[i] == NULL) {
@@ -133,7 +135,51 @@ int main() {
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]);

View File

@@ -22,10 +22,10 @@ Message* new_message(char* string, User* from, User* to) {
// *(new_message->recipient) = *to;
time_t rawtime;
struct tm timeinfo;
struct tm* timeinfo;
time(&rawtime);
timeinfo = *localtime(&rawtime);
timeinfo = localtime(&rawtime);
new_message->timeinfo = timeinfo;

View File

@@ -10,7 +10,7 @@ typedef struct Message_s Message;
struct Message_s {
char* text;
struct tm timeinfo;
struct tm* timeinfo;
User* sender;
User* recipient;
};