-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmsg_list.c
More file actions
139 lines (122 loc) · 3.97 KB
/
msg_list.c
File metadata and controls
139 lines (122 loc) · 3.97 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "msg_list.h"
#include <stdio.h>
int total_msg = 0;
void msg_list_init(msg_list_t *msg_list) {
msg_list->enabled = true;
msg_list->nr = 0;
msg_list->b_max = 0;
msg_list->b_used = 0;
msg_list->buf = malloc(0);
}
// TODO: Fix usernames not being freed
void msg_list_add(msg_list_t *msg_list, char *username, char *msg_buf, char *filename) {
if (!msg_list->enabled) {
DEBUG("msg_list_t not enabled");
return;
}
if (msg_list->nr + 1 >= MSG_LIST_MAX) {
// remove the first username+message
DEBUG("Removing first message");
int off = strlen(msg_list->buf)+1;
off += strlen(msg_list->buf+off)+1;
msg_list->nr--;
msg_list->b_used -= off;
memcpy(msg_list->buf, msg_list->buf+off, msg_list->b_used);
}
int buflen = strlen(username) + strlen(msg_buf) + 2;
char *buf;
if (msg_list->b_used + buflen >= msg_list->b_max) {
// backup msgs
if (filename != 0)
msg_list_write(msg_list, filename);
// increase size
buf = msg_list->buf;
msg_list->b_max += MSG_LIST_INCREMENT;
msg_list->buf = malloc(msg_list->b_max);
memcpy(msg_list->buf, buf, msg_list->b_used);
free(buf);
}
buf = malloc(buflen);
// if (username[0] == 'R' && username[1] == 0) {
// memcpy(buf, username, strlen(username)+1);
// int o = strlen(username)+1;
// memcpy(buf+o, msg_buf, strlen(msg_buf)+1);
// o+=strlen(msg_buf+o)+1;
// memcpy(buf+o, msg_buf, strlen(msg_buf+o)+1);
//
// INFO("%s %s %s", username, msg_buf, msg_buf+strlen(msg_buf)+1);
// } else {
// }
memcpy(buf, username, strlen(username)+1);
memcpy(buf+strlen(username)+1, msg_buf, strlen(msg_buf)+1);
int a = strlen(buf)+1;
// DEBUG("%s: %s", buf, buf+a);
// DEBUG("Adding '%s' from '%s' to msg_list_t", msg_buf, username);
memcpy(msg_list->buf+msg_list->b_used, buf, buflen);
msg_list->b_used += buflen;
msg_list->nr++;
free(buf);
total_msg++;
}
void msg_list_send(msg_list_t *msg_list, client_t client) {
if (!msg_list->enabled || msg_list->nr <= 0) {
INFO("Old messages disabled");
char id = P_MSG_LIST;
int status, len = 0;
status = write(client.clifd, &id, 1);
status = write(client.clifd, &len, sizeof(len));
}
INFO("Sending past messages to user '%s' with fd %d", client.username,
client.clifd);
char id = P_MSG_LIST;
int status;
status = write(client.clifd, &id, 1);
status = write(client.clifd, &msg_list->b_used, sizeof(msg_list->b_used));
status = write(client.clifd, msg_list->buf, msg_list->b_used);
}
void msg_list_read(msg_list_t *msg_list, char *filename) {
if (!msg_list->enabled) {
ERROR("Can't read log file, because msg_list diabled");
return;
}
FILE *f = fopen(filename, "r");
if (f <= 0) {
ERROR("Couldn't open file");
return;
}
char buf[32];
fscanf(f, "max: %s\n", buf);
if (atoi(buf) >= 0)
msg_list->b_max = atoi(buf);
fscanf(f, "used: %s\n", buf);
if (atoi(buf) >= 0)
msg_list->b_used = atoi(buf);
char c;
int read = 0;
while (!feof(f) && read < msg_list->b_used) {
c = getc(f);
if (c == '\n')
msg_list->buf[read++] = 0;
else
msg_list->buf[read++] = c;
}
DEBUG("%d/%d bytes read", read, msg_list->b_used);
fclose(f);
}
void msg_list_write(msg_list_t *msg_list, char *filename) {
if (!msg_list->enabled) {
ERROR("Can't write log file, because msg_list diabled");
return;
}
FILE *f = fopen(filename, "w");
fprintf(f, "max: %d\n", msg_list->b_max);
fprintf(f, "used: %d\n", msg_list->b_used);
for (int i = 0; i < msg_list->b_used; i++) {
if (msg_list->buf[i] == 0)
fputc('\n', f);
else
fputc(msg_list->buf[i], f);
}
fclose(f);
INFO("Finished writing msg_list");
}