#include #include #include #include #include #include #include #include "server_code_etudiant.h" struct message { char message[128]; struct message *prev; }; struct message *message_list; struct fortune { char fortune_text[2000]; }; client_t *clients[MAX_CLIENTS]; /* Strip CRLF */ void strip_newline(char *s) { while(*s != '\0'){ if(*s == '\r' || *s == '\n'){ *s = '\0'; } s++; } } client_t *get_client_from_fd(int fd) { int i; for(i=0; ifd == fd) return clients[i]; } return NULL; } client_t *get_client_from_name(char* name) { int i; for(i=0; iname, name) == 0) { return clients[i]; } } } return NULL; } void server_init() { memset(clients, 0, sizeof(client_t*)*MAX_CLIENTS); } void server_finalize() { } /* Add client to queue */ void queue_add(client_t *cl){ int i; for(i=0;iuid == client->uid){ clients[i] = NULL; return; } } } } /* Send a message to a client */ void send_message(char *s, client_t *client){ fwrite(s, sizeof(char), strlen(s), client->client_conn); } /* Send message to all clients */ void send_message_all(char *s){ int i; for(i=0;iname, "Anonymous_%d", cli->uid); } /* this function is called when a client connects to the server */ void say_hello(client_t *cli) { char buff_out[1024]; /* choose a default name */ assign_default_name(cli); sprintf(buff_out, "* %s joins the chatroom\n", cli->name); send_message_all(buff_out); } void process_cmd_fortune_record(client_t* client, char* param) { int n = atoi(param); } void process_cmd_forget(client_t* client, char* param) { int n = atoi(param); } void process_cmd_askGPT(client_t*client, char*param) { char* message = param; } int get_file_size(const char* filename) { struct stat stat_buf; stat(filename, &stat_buf); return stat_buf.st_size; } void process_cmd_fortune(client_t*client, char*param) { int fortune_id = atoi(param); } void process_cmd_msg(client_t*client, char*param) { char*dest = strsep(¶m, " "); if(!dest){ send_message("* to who ?\n", client); return; } char buffer[1024]; sprintf(buffer, "[PM][%s --> %s] %s\n", client->name, dest, param); client_t* to = get_client_from_name(dest); if(!to ){ send_message("* %s does not exist!\n", client); } else { send_message(buffer, to); send_message(buffer, client); } } void process_cmd_help(client_t* client) { char buff_out[1024]; sprintf(buff_out, "/help Show help\n"); strcat(buff_out, "/msg Send private message\n"); strcat(buff_out, "/ping Server test\n"); strcat(buff_out, "/quit Quit chatroom\n"); send_message(buff_out, client); } void process_cmd_ping(client_t* client, char* param) { send_message("* pong\n", client); } void handle_incoming_cmd(client_t *cli) { char buff_out[1024]; char buff_in[1024]; if(fgets(buff_in, 1024*sizeof(char), cli->client_conn) == 0) { if(!feof(cli->client_conn)) { perror("read failed"); abort(); } else { printf("Client %s disconnected\n", cli->name); queue_delete(cli); return; } } strip_newline(buff_in); /* Ignore empty buffer */ if(!strlen(buff_in)){ printf("Empty message\n"); } /* Special options */ char *cmd_line = buff_in; if(buff_in[0] == '/'){ char *command; command = strsep(&cmd_line," "); if(!strcmp(command, "/quit")){ return; } else if(!strcmp(command, "/ping")) { process_cmd_ping(cli, cmd_line); } else if(!strcmp(command, "/msg")) { process_cmd_msg(cli, cmd_line); } else if(!strcmp(command, "/askGPT")) { process_cmd_askGPT(cli, cmd_line); } else if(!strcmp(command, "/fortune")) { process_cmd_fortune(cli, cmd_line); } else if(!strcmp(command, "/fortune_record")) { process_cmd_fortune_record(cli, cmd_line); } else if(!strcmp(command, "/forget")) { process_cmd_forget(cli, cmd_line); } else { /* /help or unknown command */ process_cmd_help(cli); } }else{ /* Send message */ sprintf(buff_out, "[%s] %s\n", cli->name, cmd_line); send_message_all(buff_out); } }