Mercurial > hg > nnchat
annotate nnchat.c @ 21:29098addfa65
Added a "daemon" mode.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 12 Jun 2008 23:55:41 +0300 |
parents | e80072e26178 |
children | ccf302dae898 |
rev | line source |
---|---|
0 | 1 #ifdef __WIN32 |
2 #include <winsock2.h> | |
3 #else | |
4 #include <sys/socket.h> | |
5 #include <sys/types.h> | |
6 #include <arpa/inet.h> | |
7 #include <sys/time.h> | |
8 #include <netdb.h> | |
9 #endif | |
10 | |
11 #include <unistd.h> | |
12 #include <stdlib.h> | |
13 #include <stdio.h> | |
14 #include "th_args.h" | |
15 #include "th_string.h" | |
16 #include <string.h> | |
17 #include <errno.h> | |
18 #include <time.h> | |
19 | |
20 | |
21 #define SET_ALLOC_SIZE (128) | |
22 #define SET_SELECT_USEC (100000) | |
23 | |
24 | |
25 /* Options | |
26 */ | |
27 int optPort = 8005; | |
28 int optUserColor = 0x408060; | |
29 char *optServer = "www11.servemedata.com", | |
30 *optUserName = NULL, | |
31 *optUserName2 = NULL, | |
32 *optPassword = NULL, | |
33 *optLogFilename = NULL; | |
21 | 34 BOOL optDaemon = FALSE; |
0 | 35 FILE *optLogFile = NULL; |
36 | |
37 | |
38 /* Arguments | |
39 */ | |
40 optarg_t optList[] = { | |
41 { 0, '?', "help", "Show this help", OPT_NONE }, | |
42 { 1, 'v', "verbose", "Be more verbose", OPT_NONE }, | |
43 { 2, 'p', "port", "Connect to port", OPT_ARGREQ }, | |
44 { 3, 's', "server", "Server to connect to", OPT_ARGREQ }, | |
45 { 4, 'C', "color", "Initial color in RGB hex 000000", OPT_ARGREQ }, | |
46 { 5, 'l', "logfile", "Log filename", OPT_ARGREQ }, | |
21 | 47 { 6, 'D', "daemon", "A pseudo-daemon mode for logging", OPT_NONE }, |
0 | 48 }; |
49 | |
50 const int optListN = (sizeof(optList) / sizeof(optarg_t)); | |
51 | |
52 | |
53 void argShowHelp() | |
54 { | |
55 th_args_help(stdout, optList, optListN, th_prog_name, | |
56 "[options] <username> <password>"); | |
57 } | |
58 | |
59 | |
60 #ifdef __WIN32 | |
1 | 61 /* Just a bogus stub |
62 */ | |
0 | 63 const char *hstrerror(int err) |
64 { | |
1 | 65 (void) err; |
66 | |
0 | 67 return "???"; |
68 } | |
69 #endif | |
70 | |
71 BOOL argHandleOpt(const int optN, char *optArg, char *currArg) | |
72 { | |
73 switch (optN) { | |
74 case 0: | |
75 argShowHelp(); | |
76 exit(0); | |
77 break; | |
78 | |
79 case 1: | |
80 th_verbosityLevel++; | |
81 break; | |
82 | |
83 case 2: | |
84 optPort = atoi(optArg); | |
85 break; | |
86 | |
87 case 3: | |
88 optServer = optArg; | |
89 break; | |
90 | |
91 case 4: | |
92 if (sscanf(optArg, "%06x", &optUserColor) != 1) { | |
93 THERR("Invalid color argument '%s', should be a RGB hex triplet '000000'.\n", | |
94 optArg); | |
95 return FALSE; | |
96 } | |
97 THMSG(1, "Using color #%06x\n", optUserColor); | |
98 break; | |
99 | |
100 case 5: | |
101 optLogFilename = optArg; | |
102 break; | |
103 | |
21 | 104 case 6: |
105 optDaemon = TRUE; | |
106 THMSG(1, "Running in pseudo-daemon mode.\n"); | |
107 break; | |
108 | |
0 | 109 default: |
110 THERR("Unknown option '%s'.\n", currArg); | |
111 return FALSE; | |
112 } | |
113 | |
114 return TRUE; | |
115 } | |
116 | |
117 | |
118 BOOL argHandleFile(char *currArg) | |
119 { | |
120 if (!optUserName) | |
121 optUserName = currArg; | |
122 else if (!optPassword) | |
123 optPassword = currArg; | |
124 else { | |
125 THERR("Username '%s' already specified on commandline!\n", optUserName); | |
126 return FALSE; | |
127 } | |
128 | |
129 return TRUE; | |
130 } | |
131 | |
132 | |
15 | 133 int openConnection(struct in_addr *addr, int port) |
134 { | |
135 struct sockaddr_in tmpAddr; | |
136 int sock = -1; | |
137 | |
138 tmpAddr.sin_family = AF_INET; | |
139 tmpAddr.sin_port = htons(port); | |
140 tmpAddr.sin_addr = *addr; | |
141 | |
142 THMSG(1, "Connecting to %s:%d ...\n", | |
143 inet_ntoa(tmpAddr.sin_addr), port); | |
144 | |
145 if ((sock = socket(PF_INET, SOCK_STREAM, 0)) == -1) { | |
146 THERR("Could not open socket: %s\n", strerror(errno)); | |
147 return -2; | |
148 } | |
149 | |
150 THMSG(2, "Using socket %d.\n", sock); | |
151 | |
152 if (connect(sock, (struct sockaddr *) &tmpAddr, sizeof(tmpAddr)) == -1) { | |
153 THERR("Could not connect: %s\n", strerror(errno)); | |
154 return -5; | |
155 } | |
156 | |
157 return sock; | |
158 } | |
159 | |
160 | |
161 void closeConnection(int sock) | |
162 { | |
163 if (sock >= 0) { | |
164 #ifdef __WIN32 | |
165 closesocket(sock); | |
166 #else | |
167 close(sock); | |
168 #endif | |
169 } | |
170 } | |
171 | |
172 | |
0 | 173 BOOL sendToSocket(int sock, char *buf, const size_t bufLen) |
174 { | |
175 size_t bufLeft = bufLen; | |
176 char *bufPtr = buf; | |
177 | |
178 while (bufLeft > 0) { | |
179 ssize_t bufSent; | |
180 bufSent = send(sock, bufPtr, bufLeft, 0); | |
181 if (bufSent < 0) return FALSE; | |
182 bufLeft -= bufSent; | |
183 bufPtr += bufSent; | |
184 } | |
185 return TRUE; | |
186 } | |
187 | |
188 | |
189 void printMsg(char *fmt, ...) | |
190 { | |
191 char tmpStr[64] = ""; | |
192 va_list ap; | |
193 time_t timeStamp; | |
194 struct tm *tmpTime;; | |
195 | |
196 timeStamp = time(NULL); | |
197 if ((tmpTime = localtime(&timeStamp)) != NULL) { | |
198 strftime(tmpStr, sizeof(tmpStr), "[%H:%M:%S] ", tmpTime); | |
199 } | |
200 | |
201 if (optLogFile) { | |
202 fputs(tmpStr, optLogFile); | |
203 va_start(ap, fmt); | |
204 vfprintf(optLogFile, fmt, ap); | |
205 va_end(ap); | |
206 fflush(optLogFile); | |
207 } | |
208 | |
21 | 209 if (!optDaemon) { |
210 fputs(tmpStr, stdout); | |
211 va_start(ap, fmt); | |
212 vfprintf(stdout, fmt, ap); | |
213 va_end(ap); | |
214 fflush(stdout); | |
215 } | |
0 | 216 } |
217 | |
218 | |
219 BOOL bufRealloc(char **buf, size_t *size, size_t add) | |
220 { | |
221 return ((*buf = th_realloc(*buf, *size + add)) != NULL); | |
222 } | |
223 | |
12 | 224 #define PUSHCHAR(x) bufPushChar(&result, &resSize, &resPos, x) |
0 | 225 BOOL bufPushChar(char **buf, size_t *size, size_t *pos, char ch) |
226 { | |
227 if (*pos >= *size && !bufRealloc(buf, size, SET_ALLOC_SIZE)) | |
228 return FALSE; | |
229 | |
230 (*buf)[*pos] = ch; | |
231 (*pos)++; | |
232 return TRUE; | |
233 } | |
234 | |
12 | 235 #define PUSHSTR(x) bufPushStr(&result, &resSize, &resPos, x) |
0 | 236 BOOL bufPushStr(char **buf, size_t *size, size_t *pos, char *str) |
237 { | |
238 size_t tmpLen; | |
239 | |
240 if (!str) return FALSE; | |
241 tmpLen = strlen(str); | |
242 | |
243 if ((*pos + tmpLen) >= *size && !bufRealloc(buf, size, tmpLen + SET_ALLOC_SIZE)) | |
244 return FALSE; | |
245 | |
246 strcpy(*buf + *pos, str); | |
247 (*pos) += tmpLen; | |
248 return TRUE; | |
249 } | |
250 | |
251 | |
252 char *encodeStr1(char *str) | |
253 { | |
254 char *result, *s = str; | |
255 size_t resSize, resPos = 0; | |
256 | |
257 if (!str) return NULL; | |
258 | |
259 resSize = strlen(str) + SET_ALLOC_SIZE; | |
260 if ((result = th_malloc(resSize)) == NULL) | |
261 return NULL; | |
262 | |
263 while (*s) { | |
264 switch (*s) { | |
265 case 32: | |
12 | 266 PUSHCHAR('+'); |
0 | 267 break; |
268 | |
269 default: | |
270 if (th_isalnum(*s)) | |
12 | 271 PUSHCHAR(*s); |
0 | 272 else { |
273 char tmpStr[4]; | |
274 sprintf(tmpStr, "%2X", (unsigned char) *s); | |
12 | 275 PUSHCHAR('%'); |
276 PUSHSTR(tmpStr); | |
0 | 277 } |
278 break; | |
279 } | |
280 s++; | |
281 } | |
12 | 282 PUSHCHAR(0); |
0 | 283 |
284 return result; | |
285 } | |
286 | |
15 | 287 |
0 | 288 int getxdigit(int c, int shift) |
289 { | |
290 int i; | |
291 | |
292 if (c >= 'A' && c <= 'F') | |
293 i = c - 'A' + 10; | |
294 else if (c >= 'a' && c <= 'f') | |
295 i = c - 'a' + 10; | |
296 else if (c >= '0' && c <= '9') | |
297 i = c - '0'; | |
298 else | |
299 return -1; | |
300 | |
301 return i << shift; | |
302 } | |
303 | |
15 | 304 |
0 | 305 char *decodeStr1(char *str) |
306 { | |
307 char *result, *s = str; | |
308 size_t resSize, resPos = 0; | |
309 int c; | |
310 | |
311 if (!str) return NULL; | |
312 | |
313 resSize = strlen(str) + SET_ALLOC_SIZE; | |
314 if ((result = th_malloc(resSize)) == NULL) | |
315 return NULL; | |
316 | |
317 while (*s) { | |
318 switch (*s) { | |
319 case '+': | |
12 | 320 PUSHCHAR(' '); |
0 | 321 s++; |
322 break; | |
323 | |
324 case '%': | |
325 s++; | |
326 if (*s == '%') | |
12 | 327 PUSHCHAR('%'); |
0 | 328 else if ((c = getxdigit(*s, 4)) >= 0) { |
329 int i = getxdigit(*(++s), 0); | |
330 if (i >= 0) { | |
12 | 331 PUSHCHAR(c | i); |
0 | 332 } else { |
12 | 333 PUSHCHAR('§'); |
334 PUSHCHAR(*s); | |
0 | 335 } |
336 } else { | |
12 | 337 PUSHCHAR('§'); |
338 PUSHCHAR(*s); | |
0 | 339 } |
340 s++; | |
341 break; | |
342 | |
343 default: | |
12 | 344 PUSHCHAR(*s); |
0 | 345 s++; |
346 } | |
347 } | |
12 | 348 PUSHCHAR(0); |
0 | 349 |
350 return result; | |
351 } | |
352 | |
353 | |
354 char *stripTags(char *str) | |
355 { | |
356 char *result, *s = str; | |
357 size_t resSize, resPos = 0; | |
358 | |
359 if (!str) return NULL; | |
360 | |
361 resSize = strlen(str) + SET_ALLOC_SIZE; | |
362 if ((result = th_malloc(resSize)) == NULL) | |
363 return NULL; | |
364 | |
365 while (*s) { | |
366 if (*s == '<') { | |
367 while (*s && *s != '>') s++; | |
368 if (*s == '>') s++; | |
369 } else | |
12 | 370 PUSHCHAR(*s++); |
0 | 371 } |
12 | 372 PUSHCHAR(0); |
0 | 373 |
374 return result; | |
375 } | |
376 | |
377 | |
378 typedef struct { | |
379 char c; | |
380 char *ent; | |
381 } html_entity_t; | |
382 | |
383 | |
384 html_entity_t HTMLEntities[] = { | |
385 { '<', "<" }, | |
386 { '>', ">" }, | |
387 }; | |
388 | |
389 const int numHTMLEntities = (sizeof(HTMLEntities) / sizeof(html_entity_t)); | |
390 | |
391 | |
392 char *encodeStr2(char *str) | |
393 { | |
394 char *result, *s = str; | |
395 size_t resSize, resPos = 0; | |
396 | |
397 if (!str) return NULL; | |
398 | |
399 resSize = strlen(str) + SET_ALLOC_SIZE; | |
400 if ((result = th_malloc(resSize)) == NULL) | |
401 return NULL; | |
402 | |
403 while (*s) { | |
404 int i; | |
405 BOOL found = FALSE; | |
406 for (i = 0; i < numHTMLEntities; i++) | |
407 if (HTMLEntities[i].c == *s) { | |
12 | 408 PUSHSTR(HTMLEntities[i].ent); |
0 | 409 found = TRUE; |
410 break; | |
411 } | |
12 | 412 if (!found) PUSHCHAR(*s); |
0 | 413 |
414 s++; | |
415 } | |
12 | 416 PUSHCHAR(0); |
0 | 417 |
418 return result; | |
419 } | |
420 | |
421 | |
422 char *decodeStr2(char *str) | |
423 { | |
424 char *result, *s = str; | |
425 size_t resSize, resPos = 0; | |
426 | |
427 if (!str) return NULL; | |
428 | |
429 resSize = strlen(str); | |
430 if ((result = th_malloc(resSize)) == NULL) | |
431 return NULL; | |
432 | |
433 while (*s) { | |
434 if (*s == '&') { | |
435 int i; | |
436 BOOL found = FALSE; | |
437 for (i = 0; i < numHTMLEntities; i++) { | |
438 html_entity_t *ent = &HTMLEntities[i]; | |
439 int len = strlen(ent->ent); | |
440 if (!strncmp(s, ent->ent, len)) { | |
12 | 441 PUSHCHAR(ent->c); |
0 | 442 s += len; |
443 found = TRUE; | |
444 break; | |
445 } | |
446 } | |
12 | 447 if (!found) PUSHCHAR(*s++); |
0 | 448 } else |
12 | 449 PUSHCHAR(*s++); |
0 | 450 } |
12 | 451 PUSHCHAR(0); |
0 | 452 |
453 return result; | |
454 } | |
455 | |
456 | |
457 BOOL sendUserMsg(int sock, char *user, char *fmt, ...) | |
458 { | |
459 char tmpBuf[4096], tmpBuf2[4096+256]; | |
460 int n; | |
461 va_list ap; | |
462 | |
463 va_start(ap, fmt); | |
464 n = vsnprintf(tmpBuf, sizeof(tmpBuf), fmt, ap); | |
465 va_end(ap); | |
466 | |
467 if (n < 0) return FALSE; | |
468 | |
469 snprintf(tmpBuf2, sizeof(tmpBuf2), | |
470 "<USER>%s</USER><MESSAGE>%s</MESSAGE>", | |
471 user, tmpBuf); | |
472 | |
473 return sendToSocket(sock, tmpBuf2, strlen(tmpBuf2) + 1); | |
474 } | |
475 | |
476 | |
477 int handleUser(int sock, char *str) | |
478 { | |
479 const char *msg = "</USER><MESSAGE>"; | |
480 char *p = str, *q, *s; | |
481 | |
482 (void) sock; | |
483 | |
484 s = strstr(str, msg); | |
485 if (!s) return 1; | |
486 *s = 0; | |
487 s += strlen(msg); | |
488 | |
489 q = strstr(s, "</MESSAGE>"); | |
490 if (!q) return 3; | |
491 *q = 0; | |
492 | |
493 s = decodeStr1(s); | |
494 if (!s) return -1; | |
495 | |
496 p = decodeStr1(p); | |
497 if (!p) { | |
498 th_free(s); | |
499 return -2; | |
500 } | |
501 | |
502 /* FIXME: decodeStr2() */ | |
503 | |
504 if (*s == '/') { | |
19 | 505 char *t; |
506 if (!strncmp(s, "/BPRV", 5)) { | |
507 t = stripTags(s + 2); | |
508 printMsg("%s\n", t); | |
509 } else { | |
510 t = stripTags(s + 1); | |
511 printMsg("* %s\n", t); | |
512 } | |
0 | 513 th_free(t); |
514 } else { | |
515 char *t = stripTags(s); | |
516 printMsg("<%s> %s\n", p, t); | |
517 th_free(t); | |
518 } | |
519 | |
520 th_free(s); | |
521 th_free(p); | |
522 return 0; | |
523 } | |
524 | |
525 | |
526 int handleLogin(int sock, char *str) | |
527 { | |
19 | 528 char tmpStr[256] = ""; |
529 time_t timeStamp; | |
530 struct tm *tmpTime;; | |
531 | |
532 timeStamp = time(NULL); | |
533 if ((tmpTime = localtime(&timeStamp)) != NULL) { | |
534 strftime(tmpStr, sizeof(tmpStr), "%c", tmpTime); | |
535 } | |
536 | |
0 | 537 if (!strncmp(str, "FAILURE", 7)) { |
19 | 538 printMsg("Login failure - %s\n", tmpStr); |
0 | 539 return -2; |
540 } else if (!strncmp(str, "SUCCESS", 7)) { | |
19 | 541 printMsg("Login success - %s\n", tmpStr); |
13
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
542 sendUserMsg(sock, optUserName2, "%%2FRequestUserList"); |
0 | 543 return 0; |
544 } else | |
545 return 1; | |
546 } | |
547 | |
548 | |
549 int handleAddUser(int sock, char *str) | |
550 { | |
9 | 551 char *p, *s = strstr(str, "</ADD_USER>"); |
0 | 552 |
553 (void) sock; | |
554 | |
555 if (!s) return 1; | |
556 *s = 0; | |
9 | 557 |
558 p = decodeStr1(str); | |
559 if (!p) return -1; | |
560 | |
561 printMsg("! %s ADDED.\n", p); | |
562 th_free(p); | |
0 | 563 return 0; |
564 } | |
565 | |
566 | |
567 int handleDeleteUser(int sock, char *str) | |
568 { | |
9 | 569 char *p, *s = strstr(str, "</DELETE_USER>"); |
0 | 570 |
571 (void) sock; | |
572 | |
573 if (!s) return 1; | |
574 *s = 0; | |
9 | 575 |
576 p = decodeStr1(str); | |
577 if (!p) return -1; | |
578 | |
579 printMsg("! %s DELETED.\n", p); | |
580 th_free(p); | |
0 | 581 return 0; |
582 } | |
583 | |
584 | |
585 int handleFoo(int sock, char *str) | |
586 { | |
587 (void) sock; (void) str; | |
588 | |
589 return 0; | |
590 } | |
591 | |
592 | |
593 typedef struct { | |
594 char *cmd; | |
595 int (*handler)(int, char *); | |
596 } protocmd_t; | |
597 | |
598 | |
599 protocmd_t protoCmds[] = { | |
600 { "<USER>", handleUser }, | |
601 { "<LOGIN_", handleLogin }, | |
602 { "<DELETE_USER>", handleDeleteUser }, | |
603 { "<ADD_USER>", handleAddUser }, | |
604 { "<NUMCLIENTS>", handleFoo }, | |
605 }; | |
606 | |
607 const int nprotoCmds = (sizeof(protoCmds) / sizeof(protocmd_t)); | |
608 | |
609 | |
610 int handleProtocol(int sock, char *buf, size_t bufLen) | |
611 { | |
612 int i; | |
613 | |
614 for (i = 0; i < nprotoCmds; i++) { | |
615 size_t cmdLen = strlen(protoCmds[i].cmd); | |
616 if (cmdLen < bufLen && !strncmp(buf, protoCmds[i].cmd, cmdLen)) { | |
617 return protoCmds[i].handler(sock, buf + cmdLen); | |
618 } | |
619 } | |
620 | |
621 return 1; | |
622 } | |
623 | |
624 | |
15 | 625 int handleUserInput(int sock, char *buf, size_t bufLen) |
0 | 626 { |
627 char *tmpStr, *tmpStr2; | |
628 BOOL result; | |
629 | |
630 /* Trim right */ | |
631 buf[--bufLen] = 0; | |
632 while (bufLen > 0 && (buf[bufLen] == '\n' || buf[bufLen] == '\r' || th_isspace(buf[bufLen]))) | |
633 buf[bufLen--] = 0; | |
634 | |
635 //fprintf(stderr, "'%s'\n", buf); fflush(stderr); | |
636 | |
637 /* Check command */ | |
638 if (*buf == 0) { | |
639 return 1; | |
640 } else if (*buf == '@') { | |
641 /* Send 1-pass encoded 'RAW' */ | |
642 buf++; | |
643 printf("RAW>%s\n", buf); | |
644 fflush(stdout); | |
645 | |
646 tmpStr = encodeStr1(buf); | |
647 if (!tmpStr) return -2; | |
648 | |
649 result = sendUserMsg(sock, optUserName2, "%s", tmpStr); | |
650 th_free(tmpStr); | |
651 if (result) | |
652 return 0; | |
653 else | |
654 return -1; | |
655 } else { | |
656 /* Send double-encoded */ | |
657 printf("ENC>%s\n", buf); | |
658 fflush(stdout); | |
659 | |
660 tmpStr = encodeStr2(buf); | |
661 if (!tmpStr) return -2; | |
662 tmpStr2 = encodeStr1(tmpStr); | |
663 if (!tmpStr2) { | |
664 th_free(tmpStr); | |
665 return -3; | |
666 } | |
667 | |
668 result = sendUserMsg(sock, optUserName2, "%s", tmpStr2); | |
669 th_free(tmpStr); | |
670 th_free(tmpStr2); | |
671 if (result) | |
672 return 0; | |
673 else | |
674 return -1; | |
675 } | |
676 } | |
677 | |
678 | |
679 int main(int argc, char *argv[]) | |
680 { | |
681 int tmpSocket; | |
682 struct hostent *tmpHost; | |
13
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
683 BOOL exitProg = FALSE, colorSet = FALSE; |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
684 struct timeval tv; |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
685 fd_set sockfds; |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
686 fd_set inputfds; |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
687 char *tmpStr; |
0 | 688 |
689 /* Initialize */ | |
21 | 690 th_init("NNChat", "Newbie Nudes chat client", "0.4", |
6
526ba3b578d7
Changed copyright etc. again.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
691 "Written and designed by Anonymous Finnish Guy (C) 2008", |
526ba3b578d7
Changed copyright etc. again.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
692 "This software is freeware, use and distribute as you wish."); |
0 | 693 th_verbosityLevel = 0; |
694 | |
695 /* Parse arguments */ | |
696 th_args_process(argc, argv, optList, optListN, | |
697 argHandleOpt, argHandleFile, FALSE); | |
698 | |
699 /* Check the mode and arguments */ | |
700 if (optUserName == NULL || optPassword == NULL) { | |
701 THERR("User/pass not specified, get some --help\n"); | |
702 return -1; | |
703 } | |
704 | |
705 /* Open logfile */ | |
706 if (optLogFilename) { | |
707 THMSG(1, "Opening logfile '%s'\n", optLogFilename); | |
708 | |
709 if ((optLogFile = fopen(optLogFilename, "a")) == NULL) { | |
710 THERR("Could not open logfile for appending!\n"); | |
711 return -9; | |
712 } | |
713 } | |
714 | |
10 | 715 #ifdef __WIN32 |
716 { | |
717 WSADATA wsaData; | |
718 if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) { | |
719 THERR("WinSock API v2.0 not supported.\n"); | |
720 return -20; | |
721 } | |
722 } | |
723 #endif | |
724 | |
0 | 725 /* Okay ... */ |
726 THMSG(1, "Trying to resolve host '%s' ...\n", optServer); | |
727 tmpHost = gethostbyname(optServer); | |
728 if (tmpHost == NULL) { | |
729 THERR("Could not resolve hostname: %s.\n", | |
730 hstrerror(h_errno)); | |
731 return -3; | |
732 } | |
733 THMSG(2, "True hostname: %s\n", tmpHost->h_name); | |
734 | |
13
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
735 /* To emulate the official client, we first make a fake connection ... */ |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
736 if ((tmpSocket = openConnection((struct in_addr *) tmpHost->h_addr, optPort)) < 0) { |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
737 THERR("Fakeprobe connection setup failed!\n"); |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
738 goto err_exit; |
0 | 739 } |
740 | |
13
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
741 tmpStr = "<policy-file-request/>"; |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
742 if (sendToSocket(tmpSocket, tmpStr, strlen(tmpStr) + 1) < 0) { |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
743 THERR("Failed to send fakeprobe.\n"); |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
744 goto err_exit; |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
745 } else { |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
746 ssize_t gotBuf; |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
747 char tmpBuf[4096]; |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
748 gotBuf = recv(tmpSocket, tmpBuf, sizeof(tmpBuf), 0); |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
749 tmpBuf[gotBuf-1] = 0; |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
750 THMSG(2, "Probe got: %s\n", tmpBuf); |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
751 closeConnection(tmpSocket); |
0 | 752 } |
753 | |
13
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
754 /* Okay, now do the proper connection ... */ |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
755 if ((tmpSocket = openConnection((struct in_addr *) tmpHost->h_addr, optPort)) < 0) { |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
756 THERR("Main connection setup failed!\n"); |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
757 goto err_exit; |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
758 } |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
759 |
0 | 760 THMSG(1, "Connected, logging in as '%s'.\n", optUserName); |
761 optUserName2 = encodeStr1(optUserName); | |
762 | |
763 sendUserMsg(tmpSocket, optUserName2, "%%2Flogin%%20%%2Dsite%%20NN%%20%%2Dpassword%%20%s", optPassword); | |
764 | |
765 FD_ZERO(&inputfds); | |
766 FD_SET(0, &inputfds); | |
767 FD_ZERO(&sockfds); | |
768 FD_SET(tmpSocket, &sockfds); | |
769 | |
770 while (!exitProg) { | |
771 ssize_t gotBuf; | |
772 int result; | |
773 char tmpBuf[4096]; | |
774 fd_set tmpfds; | |
775 | |
776 /* Check for incoming data from the server */ | |
777 tv.tv_sec = 0; | |
778 tv.tv_usec = SET_SELECT_USEC; | |
779 tmpfds = sockfds; | |
780 if ((result = select(tmpSocket+1, &tmpfds, NULL, NULL, &tv)) == -1) { | |
9 | 781 printMsg("Error occured in select(sockfds): %s\n", strerror(errno)); |
0 | 782 exitProg = TRUE; |
783 } else if (FD_ISSET(tmpSocket, &tmpfds)) { | |
784 gotBuf = recv(tmpSocket, tmpBuf, sizeof(tmpBuf), 0); | |
785 | |
786 if (gotBuf < 0) { | |
9 | 787 printMsg("Error in recv: %s\n", strerror(errno)); |
0 | 788 exitProg = TRUE; |
789 } else if (gotBuf == 0) { | |
9 | 790 printMsg("Server closed connection.\n"); |
0 | 791 exitProg = TRUE; |
792 } else { | |
793 /* Handle protocol data */ | |
794 tmpBuf[gotBuf] = 0; | |
795 result = handleProtocol(tmpSocket, tmpBuf, gotBuf); | |
796 | |
797 if (result > 0) { | |
798 /* Couldn't handle the message for some reason */ | |
9 | 799 printMsg("Could not handle: %s\n", tmpBuf); |
0 | 800 } else if (result < 0) { |
801 /* Fatal error, quit */ | |
9 | 802 printMsg("Fatal error with message: %s\n", tmpBuf); |
0 | 803 exitProg = TRUE; |
804 } | |
805 } | |
806 } | |
807 | |
808 /* Check for user input */ | |
21 | 809 if (!optDaemon) { |
0 | 810 tv.tv_sec = 0; |
811 tv.tv_usec = SET_SELECT_USEC; | |
812 tmpfds = inputfds; | |
813 if ((result = select(1, &tmpfds, NULL, NULL, &tv)) == -1) { | |
9 | 814 printMsg("Error occured in select(inputfds): %s\n", strerror(errno)); |
0 | 815 exitProg = TRUE; |
816 } else if (FD_ISSET(0, &tmpfds)) { | |
817 gotBuf = read(0, tmpBuf, sizeof(tmpBuf)); | |
818 | |
819 if (gotBuf < 0) { | |
9 | 820 printMsg("Error in reading stdio.\n"); |
0 | 821 exitProg = TRUE; |
822 } else { | |
823 /* Call the user input handler */ | |
15 | 824 result = handleUserInput(tmpSocket, tmpBuf, gotBuf); |
0 | 825 if (result < 0) { |
9 | 826 printMsg("Fatal error handling user input: %s\n", |
0 | 827 tmpBuf); |
828 exitProg = TRUE; | |
829 } | |
830 } | |
831 } | |
21 | 832 } /* !optDaemon */ |
0 | 833 |
13
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
834 if (!colorSet) { |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
835 colorSet = TRUE; |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
836 sendUserMsg(tmpSocket, optUserName2, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor); |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
837 } |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
838 |
0 | 839 fflush(stdout); |
840 fflush(stderr); | |
841 } | |
842 | |
13
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
843 /* Shotdiwn */ |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
844 err_exit: |
0 | 845 th_free(optUserName2); |
10 | 846 |
13
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
847 closeConnection(tmpSocket); |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
848 |
10 | 849 #ifdef __WIN32 |
850 WSACleanup(); | |
851 #endif | |
13
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
852 |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
853 THMSG(1, "Connection terminated.\n"); |
0 | 854 |
855 if (optLogFile) { | |
856 THMSG(1, "Closing logfile.\n"); | |
857 fclose(optLogFile); | |
858 } | |
859 | |
860 | |
861 return 0; | |
862 } |