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