5 #include "ClientSocket.h"
7 static const struct default_language_entry msgtab[] = {
8 {"TIME_MASK_2_ITEMS", "%s and %s"}, /* {ARGS: "2 days", "1 hour"} */
9 {"TIME_MASK_3_ITEMS", "%s, %s and %s"}, /* {ARGS: "2 days", "1 hour", "20 minutes"} */
10 {"TIME_YEAR", "year"},
11 {"TIME_YEARS", "years"},
12 {"TIME_MONTH", "month"},
13 {"TIME_MONTHS", "months"},
14 {"TIME_WEEK", "week"},
15 {"TIME_WEEKS", "weeks"},
17 {"TIME_DAYS", "days"},
18 {"TIME_HOUR", "hour"},
19 {"TIME_HOURS", "hours"},
20 {"TIME_MINUTE", "minute"},
21 {"TIME_MINUTES", "minutes"},
22 {"TIME_SECOND", "second"},
23 {"TIME_SECONDS", "seconds"},
27 /* copied from IRCU 2.10.12 match.c */
29 * Compare if a given string (name) matches the given
30 * mask (which can contain wild cards: '*' - match any
31 * number of chars, '?' - match any single character.
36 * Originally by Douglas A Lewis (dalewis@acsu.buffalo.edu)
37 * Rewritten by Timothy Vogelsang (netski), net@astrolink.org
39 int match(const char *mask, const char *name)
41 const char *m = mask, *n = name;
42 const char *m_tmp = mask, *n_tmp = name;
45 for (;;) switch (*m) {
59 /* allow escaping to force capitalization */
64 for (star_p = 0; ; m++) {
75 else if (*m == '\\') {
79 for (n_tmp = n; *n && *n != *m; n++) ;
82 for (n_tmp = n; *n && tolower(*n) != tolower(*m); n++) ;
85 /* and fall through */
89 if (tolower(*m) != tolower(*n))
99 struct Table *table_init(int width, int length, int flags) {
101 struct Table *table = malloc(sizeof(*table));
102 table->contents = malloc(length * sizeof(*table->contents));
103 for(row = 0; row < length; row++) {
104 table->contents[row] = calloc(width, sizeof(*table->contents[row]));
106 table->length = length;
107 table->width = width;
108 table->flags = flags;
109 table->col_flags = calloc(width, sizeof(int));
111 table->maxwidth = calloc(width, sizeof(int));
112 table->table_lines = NULL;
116 int table_add(struct Table *table, char **entry) {
118 if(table->entrys == table->length) return 0;
119 for(col = 0; col < table->width; col++) {
120 table->contents[table->entrys][col] = ((table->flags & TABLE_FLAG_USE_POINTER) || !entry[col] ? entry[col] : strdup(entry[col]));
121 if(table->contents[table->entrys][col])
122 table->col_flags[col] |= TABLE_FLAG_COL_CONTENTS;
123 if(entry[col] && strlen(entry[col]) > table->maxwidth[col])
124 table->maxwidth[col] = strlen(entry[col]);
130 int table_change(struct Table *table, int row, char **entry) {
132 if(row >= table->length) return 0;
133 for(col = 0; col < table->width; col++) {
134 if(table->contents[row][col] && !(table->flags & TABLE_FLAG_USE_POINTER))
135 free(table->contents[row][col]);
136 table->contents[row][col] = ((table->flags & TABLE_FLAG_USE_POINTER) || !entry[col] ? entry[col] : strdup(entry[col]));
137 if(table->contents[row][col])
138 table->col_flags[col] |= TABLE_FLAG_COL_CONTENTS;
139 if(entry[col] && strlen(entry[col]) > table->maxwidth[col])
140 table->maxwidth[col] = strlen(entry[col]);
145 int table_change_field(struct Table *table, int row, int col, char *entry) {
146 if(row >= table->length) return 0;
147 if(col >= table->width) return 0;
148 if(table->contents[row][col] && !(table->flags & TABLE_FLAG_USE_POINTER))
149 free(table->contents[row][col]);
150 table->contents[row][col] = (((table->flags & TABLE_FLAG_USE_POINTER) || !entry) ? entry : strdup(entry));
151 if(table->contents[row][col])
152 table->col_flags[col] |= TABLE_FLAG_COL_CONTENTS;
153 if(entry && strlen(entry) > table->maxwidth[col])
154 table->maxwidth[col] = strlen(entry);
158 int table_set_bold(struct Table *table, int collum, int bold) {
160 table->col_flags[collum] |= TABLE_FLAG_COL_BOLD;
162 table->col_flags[collum] &= ~TABLE_FLAG_COL_BOLD;
166 char **table_end(struct Table *table) {
167 int row, col, tablewidth = 0, pos,i;
168 if(!table->entrys) return NULL;
169 for(col = 0; col < table->width; col++) {
170 tablewidth += table->maxwidth[col]+1;
171 if(table->col_flags[col] & TABLE_FLAG_COL_BOLD)
174 table->table_lines = malloc(table->entrys * sizeof(table->table_lines));
175 for(row = 0; row < table->entrys; row++) {
176 table->table_lines[row] = malloc(tablewidth * sizeof(*table->table_lines[row]));
178 for(col = 0; col < table->width; col++) {
179 if(!(table->col_flags[col] & TABLE_FLAG_COL_CONTENTS)) continue;
180 if(table->col_flags[col] & TABLE_FLAG_COL_BOLD)
181 table->table_lines[row][pos++] = '\002';
183 if(table->contents[row][col]) {
184 for(; i < strlen(table->contents[row][col]); i++) {
185 table->table_lines[row][pos++] = table->contents[row][col][i];
188 if(col < table->width-1) {
189 for(;i < table->maxwidth[col]; i++) {
190 table->table_lines[row][pos++] = ' ';
192 table->table_lines[row][pos++] = ' ';
194 table->table_lines[row][pos++] = '\0';
196 if(table->col_flags[col] & TABLE_FLAG_COL_BOLD)
197 table->table_lines[row][pos++] = '\002';
200 return table->table_lines;
203 void table_free(struct Table *table) {
205 for(row = 0; row < table->length; row++) {
206 if(!(table->flags & TABLE_FLAG_USE_POINTER) && table->entrys > row) {
207 for(col = 0; col < table->width; col++) {
208 if(table->contents[row][col])
209 free(table->contents[row][col]);
212 free(table->contents[row]);
214 free(table->contents);
215 free(table->col_flags);
216 free(table->maxwidth);
217 if(table->table_lines) {
218 for(row = 0; row < table->entrys; row++) {
219 free(table->table_lines[row]);
221 free(table->table_lines);
226 char* timeToStr(struct UserNode *user, int seconds, int items, char *buf) {
227 char item[items][MAXLEN];
229 if(citem != items && seconds >= 31536000) { //60*60*24*365 = 31536000
231 tmp = seconds / 31536000;
232 sprintf(item[citem++], "%d %s", tmp, get_language_string(user, tmp == 1 ? "TIME_YEAR" : "TIME_YEARS"));
233 seconds -= tmp * 31536000;
235 if(citem != items && seconds >= 86400) { //60*60*24 = 86400
236 tmp = seconds / 86400;
237 sprintf(item[citem++], "%d %s", tmp, get_language_string(user, tmp == 1 ? "TIME_DAY" : "TIME_DAYS"));
238 seconds -= tmp * 86400;
240 if(citem != items && seconds >= 3600) { //60*60 = 3600
241 tmp = seconds / 3600;
242 sprintf(item[citem++], "%d %s", tmp, get_language_string(user, tmp == 1 ? "TIME_HOUR" : "TIME_HOURS"));
243 seconds -= tmp * 3600;
245 if(citem != items && seconds >= 60) {
247 sprintf(item[citem++], "%d %s", tmp, get_language_string(user, tmp == 1 ? "TIME_MINUTE" : "TIME_MINUTES"));
250 if(citem != items && seconds >= 1) {
251 sprintf(item[citem++], "%d %s", seconds, get_language_string(user, seconds == 1 ? "TIME_SECOND" : "TIME_SECONDS"));
254 build_language_string(user, buf, "TIME_MASK_2_ITEMS", item[0], item[1]);
255 } else if(citem == 3) {
256 build_language_string(user, buf, "TIME_MASK_3_ITEMS", item[0], item[1], item[2]);
259 for(i = 0; i < citem; i++) {
260 for(ii = 0; ii < strlen(item[i]); ii++) {
261 buf[p++] = item[i][ii];
265 buf[(p ? p-1 : 0)] = '\0';
270 int strToTime(struct UserNode *user, char *str) {
272 * y = year = 365 days
273 * M = month = 30 days
280 int total_time = 0, cvalue;
282 int unit_multiplikator;
285 while(*p && !isdigit(*p)) //skip leading chars
288 while(*p && isdigit(*p)) //get the value
292 cvalue = isdigit(*str) ? atoi(str) : 0;
294 while(*p == ' ') //skip spaces
297 while(*p && !isdigit(*p)) //get the unit
301 if(p - str > 1) { //unit has more than one char
302 if(!stricmp(str, "year") || !stricmp(str, "year") || !stricmp(str, get_language_string(user, "TIME_YEAR")) || !stricmp(str, get_language_string(user, "TIME_YEARS")))
303 unit_multiplikator = 31536000; //60*60*24*365 = 31536000
304 else if(!stricmp(str, "month") || !stricmp(str, "months") || !stricmp(str, get_language_string(user, "TIME_MONTH")) || !stricmp(str, get_language_string(user, "TIME_MONTHS")))
305 unit_multiplikator = 2592000; //60*60*24*30 = 2592000
306 else if(!stricmp(str, "week") || !stricmp(str, "weeks") || !stricmp(str, get_language_string(user, "TIME_WEEK")) || !stricmp(str, get_language_string(user, "TIME_WEEKS")))
307 unit_multiplikator = 604800; //60*60*24*7 = 604800
308 else if(!stricmp(str, "day") || !stricmp(str, "days") || !stricmp(str, get_language_string(user, "TIME_DAY")) || !stricmp(str, get_language_string(user, "TIME_DAYS")))
309 unit_multiplikator = 86400; //60*60*24 = 86400
310 else if(!stricmp(str, "hour") || !stricmp(str, "hours") || !stricmp(str, get_language_string(user, "TIME_HOUR")) || !stricmp(str, get_language_string(user, "TIME_HOURS")))
311 unit_multiplikator = 3600; //60*60 = 3600
312 else if(!stricmp(str, "minute") || !stricmp(str, "minutes") || !stricmp(str, "min") || !stricmp(str, "mins") || !stricmp(str, get_language_string(user, "TIME_MINUTE")) || !stricmp(str, get_language_string(user, "TIME_MINUTES")))
313 unit_multiplikator = 60;
315 unit_multiplikator = 1;
319 unit_multiplikator = 31536000; //60*60*24*365 = 31536000
322 unit_multiplikator = 2592000; //60*60*24*30 = 2592000
325 unit_multiplikator = 604800; //60*60*24*7 = 604800
328 unit_multiplikator = 86400; //60*60*24 = 86400
331 unit_multiplikator = 3600; //60*60 = 3600
334 unit_multiplikator = 60;
337 unit_multiplikator = 1;
341 total_time += (cvalue * unit_multiplikator);
348 struct ModeBuffer* initModeBuffer(struct ClientSocket *client, struct ChanNode *chan) {
349 struct ModeBuffer *modeBuf = malloc(sizeof(*modeBuf));
351 perror("malloc() failed");
354 modeBuf->client = client;
355 modeBuf->chan = chan;
356 modeBuf->addCount = 0;
357 modeBuf->delCount = 0;
361 void modeBufferSet(struct ModeBuffer *modeBuf, int add, char mode, char *param) {
363 modeBuf->addModes[modeBuf->addCount] = mode;
364 modeBuf->addModesParams[modeBuf->addCount] = (param ? strdup(param) : NULL);
366 modeBuf->addModes[modeBuf->addCount] = '\0';
368 modeBuf->delModes[modeBuf->delCount] = mode;
369 modeBuf->delModesParams[modeBuf->delCount] = (param ? strdup(param) : NULL);
371 modeBuf->delModes[modeBuf->delCount] = '\0';
373 if(modeBuf->addCount + modeBuf->delCount == MAXMODES)
374 flushModeBuffer(modeBuf);
377 void flushModeBuffer(struct ModeBuffer *modeBuf) {
378 char modeStr[MAXMODES+3];
380 char paramStr[MAXLEN];
383 if(modeBuf->addCount) {
384 modeStr[modePos++] = '+';
385 for(i = 0; i < modeBuf->addCount; i++) {
386 modeStr[modePos++] = modeBuf->addModes[i];
387 if(modeBuf->addModesParams[i]) {
388 paramPos += sprintf(paramStr + paramPos, " %s", modeBuf->addModesParams[i]);
391 modeBuf->addCount = 0;
393 if(modeBuf->delCount) {
394 modeStr[modePos++] = '-';
395 for(i = 0; i < modeBuf->delCount; i++) {
396 modeStr[modePos++] = modeBuf->delModes[i];
397 if(modeBuf->delModesParams[i]) {
398 paramPos += sprintf(paramStr + paramPos, " %s", modeBuf->delModesParams[i]);
401 modeBuf->delCount = 0;
403 modeStr[modePos++] = '\0';
404 putsock(modeBuf->client, "MODE %s %s%s", modeBuf->chan->name, modeStr, paramStr);
407 void freeModeBuffer(struct ModeBuffer *modeBuf) {
408 if(modeBuf->addCount + modeBuf->delCount)
409 flushModeBuffer(modeBuf);
413 int is_ircmask(const char *text) {
414 while (*text && (isalnum((char)*text) || strchr("-_[]|\\`^{}?*", *text)))
418 while (*text && *text != '@' && !isspace((char)*text))
422 while (*text && !isspace((char)*text))
427 char* generate_banmask(struct UserNode *user, char *buffer) {
428 char *userhost = user->host;
430 if(isFakeHost(user->host)) {
431 sprintf(buffer, "*!*@%s", userhost);
435 //check if the hostname has more than 4 connections (trusted host)
436 if(countUsersWithHost(userhost) > 4) {
437 sprintf(buffer, "*!%s@%s", user->ident, userhost);
440 sprintf(buffer, "*!*@%s", userhost);
445 char* make_banmask(char *input, char* buffer) {
446 char *nick = NULL, *ident = NULL, *host = NULL;
449 if((p = strstr(input, "!"))) {
453 if((p = strstr(ident, "@"))) {
457 } else if((p = strstr(input, "@"))) {
461 } else if((p = strstr(input, "."))) {
463 } else if(*input == '*' && input[1] != '\0' && !strstr(input+1, "*")) {
465 p = getAuthFakehost(input+1);
469 sprintf(tmp, "%s.*", input+1);
473 struct UserNode *user = searchUserByNick(input);
475 return generate_banmask(user, buffer);
479 if(nick && *nick == '\0') nick = NULL;
480 if(ident && *ident == '\0') ident = NULL;
481 if(host && *host == '\0') host = NULL;
482 sprintf(buffer, "%s!%s@%s", (nick ? nick : "*"), (ident ? ident : "*"), (host ? host : "*"));
486 int isFakeHost(char *host) {
487 char *p1, *p2 = host;
489 //find the last dot to identify if the hostmask is a fake host
490 while((p1 = strstr(p2, "."))) {
493 //TLD database: http://www.iana.org/domains/root/db/
494 //the longest TLD i found was 6 chars long (ignoring the stange exotic ones :D)
495 //but we even ignore '.museum' and '.travel' so we can say that the TLD of our mask needs to be less than 4 chars to be a real domain
496 return (strlen(p2+1) > 4);
500 register_default_language_table(msgtab);