-
Notifications
You must be signed in to change notification settings - Fork 0
/
libs.h
181 lines (136 loc) · 4.08 KB
/
libs.h
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/ioctl.h>
#include <mysql.h>
#include <stdbool.h>
#define PORT 9573
#define MAX_BEFORE_REJECT 200
#define BUFFER_SIZE 1000
#define LONG_TEXT 131072
typedef struct sockaddr_in SA_IN;
typedef struct sockaddr SA;
extern int errno;
void scrambleWord(const char* initial, char* final) {
int length = strlen(initial), poz = 0;
for(int i = 0; i < length; i++)
final[i] = '_';
for(int i = 0; i < length; i++) {
poz = rand() % length;
while(final[poz] != '_') {
if(poz > 0) poz--;
else break;
}
if(final[poz] != '_') {
poz = length - 1;
while(final[poz] != '_') poz--;
}
final[poz] = initial[i];
}
}
int strlen1(const char* s) {
return strlen(s) + 1;
///
}
void debug(const char* s) {
printf("\n\nFROM DEBUGGER: %s||\n\n", s);
///
}
int errorCheck(int returnCode, const char* message) {
//printf("Cod: %d\n\n", returnCode);
if(returnCode < 0) {
perror(message); exit(errno);
}
return returnCode;
}
int errorDBCheck(const void* returnCode, const char* message) {
if(returnCode == NULL) {
perror(message); exit(errno);
}
return 0;
}
char * convertAddress (struct sockaddr_in address) {
static char str[25];
char port[7];
strcpy (str, inet_ntoa (address.sin_addr));
bzero (port, 7);
sprintf (port, ":%d", ntohs (address.sin_port));
strcat (str, port);
return (str);
}
char* trim(char* string) {
string[strlen(string) - 1] = '\0';
return string;
}
char* cap(char* string) {
string[strlen(string)] = '\0';
return string;
}
void replaceApostrophe(char* s) {
char aux[BUFFER_SIZE] = "";
int i, j, length = strlen(s);
for(i = 0, j = 0; i < length; i++, j++)
if(s[i] == '\'') {
aux[j++] = '\\'; aux[j] = '\'';
} else aux[j] = s[i];
cap(aux); strcpy(s, aux);
}
void revertApostrophe(char* s) {
char aux[BUFFER_SIZE] = "";
int i, j, length = strlen(s);
for(i = 0, j = 0; i < length; i++, j++)
if(s[i] == '\\' && s[i + 1] == '\'') {
j--;
} else aux[j] = s[i];
cap(aux); strcpy(s, aux);
}
bool goodFormat(char* p) {
int lg = strlen(p);
for(int i = 0; i < lg; i++)
if(!((p[i] >= 'a' && p[i] <= 'z') || (p[i] >= 'A' && p[i] <= 'Z') || (p[i] >= '0' && p[i] <= '9') || (p[i] == '_') || (p[i] == '-'))) return false;
return true;
}
void wrongFormat(int fd) {
char buf[BUFFER_SIZE] = "";
strcpy(buf, "FORMAT");
errorCheck(write(fd, buf, BUFFER_SIZE), "\n\n[server] Error sending format mistake message!\n\n");
}
////////////////////////////////////////////////////////////////////////////
int getRating(MYSQL* conn, const char* buff) {
char query[BUFFER_SIZE] = "";
char buf[BUFFER_SIZE] = ""; strcpy(buf, buff);
if(!strchr(buf, '\\')) replaceApostrophe(buf);
int rowsNumber = 0;
MYSQL_RES *result;
MYSQL_ROW row;
sprintf(query, "SELECT FLOOR(AVG(l.rating)), b.title FROM lists l JOIN books b ON l.isbn = b.isbn GROUP BY l.isbn HAVING LOWER(b.title) = LOWER('%s');", buf);
errorCheck(mysql_query(conn, query), "\n\n[server] Error searching for rating!\n");
errorDBCheck(result = mysql_store_result(conn), "\n\n[server] Error retrieving query result!\n");
rowsNumber = mysql_num_rows(result);
if(rowsNumber == 1) {
row = mysql_fetch_row(result);
mysql_free_result(result);
return atoi(row[0]);
}
mysql_free_result(result);
return 0;
}
int isRead(int sock, const char* username, const char* book) {
char buf[BUFFER_SIZE] = "";
sprintf(buf, "STATUS/%s/%s", username, book);
if(errorCheck(write(sock, buf, strlen1(buf)), "\n\n[client] Error sending book title to server!\n\n") == 0) return 0;
strcpy(buf, "");
if(errorCheck(read(sock, buf, BUFFER_SIZE), "\n\n[client] Error reading server response!\n\n") == 0) return 0;
if(strcmp(buf, "READ") == 0) return 1;
else if(strcmp(buf, "LATER") == 0) return 0;
return 2;
}