summaryrefslogtreecommitdiff
path: root/client.c
blob: 373a28227ba618c330acf0f43ef129789cb2d5db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "client.h"
#include "chatroom.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <stdlib.h>
extern client_t *clients[];
extern pthread_mutex_t clients_mutex;

void send_to_client(int sock, const char *msg) {
    send(sock, msg, strlen(msg), 0);
}

void broadcast_message(const char *message, int sender_sock) {
    pthread_mutex_lock(&clients_mutex);

    client_t *sender = NULL;
    for (int i = 0; i < 10; i++) {
        if (clients[i] && clients[i]->socket == sender_sock) {
            sender = clients[i];
            break;
        }
    }

    if (sender == NULL || strlen(sender->room) == 0) {
        pthread_mutex_unlock(&clients_mutex);
        return;
    }

    for (int i = 0; i < 10; i++) {
        if (clients[i] && clients[i]->socket != sender_sock &&
            strcmp(clients[i]->room, sender->room) == 0) {
            char formatted[BUFFER_SIZE + 100];
            snprintf(formatted, sizeof(formatted), "%s: %s", clients[i]->username, message);
            send_to_client(clients[i]->socket, formatted);
        }
    }

    pthread_mutex_unlock(&clients_mutex);
}

void *handle_client(void *arg) {
    client_t *cli = (client_t *)arg;
    char buffer[BUFFER_SIZE];
    int bytes;

    while ((bytes = recv(cli->socket, buffer, BUFFER_SIZE - 1, 0)) > 0) {
        buffer[bytes] = '\0';

        if (strncmp(buffer, "/join ", 6) == 0) {
            char room[50];
            sscanf(buffer + 6, "%s", room);
            if (join_chatroom(room, cli)) {
                char msg[128];
                snprintf(msg, sizeof(msg), "You joined '%s'\n", room);
                send_to_client(cli->socket, msg);
            } else {
                send_to_client(cli->socket, "Room does not exist.\n");
            }
        }
        else if (strncmp(buffer, "/list", 5) == 0) {
            print_chatroom_list(cli->socket);
        }
        else if (strncmp(buffer, "/users", 6) == 0) {
            pthread_mutex_lock(&clients_mutex);
            int count = 0;
            for (int i = 0; i < 10; i++)
                if (clients[i] && strcmp(clients[i]->room, cli->room) == 0)
                    count++;

            char header[BUFFER_SIZE];
            snprintf(header, sizeof(header), "[you are in '%s' among %d]\n\n", cli->room, count);
            send_to_client(cli->socket, header);

            for (int i = 0; i < 10; i++) {
                if (clients[i] && strcmp(clients[i]->room, cli->room) == 0) {
                    char line[BUFFER_SIZE];
                    snprintf(line, sizeof(line), "%s@%s\n", clients[i]->username,
                             inet_ntoa(clients[i]->address.sin_addr));
                    send_to_client(cli->socket, line);
                }
            }
            pthread_mutex_unlock(&clients_mutex);
        }
        else {
            broadcast_message(buffer, cli->socket);
        }
    }

    close(cli->socket);
    pthread_mutex_lock(&clients_mutex);
    for (int i = 0; i < 10; i++) {
        if (clients[i] == cli) {
            leave_chatroom(cli);
            clients[i] = NULL;
            break;
        }
    }
    pthread_mutex_unlock(&clients_mutex);
    free(cli);
    pthread_exit(NULL);
}