Mercurial > hg > nnchat
comparison main.c @ 713:93d0e1547842
th-libs now uses stdbool.h if possible, so we need to rename all
BOOL/TRUE/FALSE to bool/true/false.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 07 Dec 2022 13:20:25 +0200 |
parents | 84d7b33c4e83 |
children | 66b9d7b861d1 |
comparison
equal
deleted
inserted
replaced
712:3a3e4fc53ac7 | 713:93d0e1547842 |
---|---|
84 *optLogPath = NULL, | 84 *optLogPath = NULL, |
85 *optLogExtension = ".log", | 85 *optLogExtension = ".log", |
86 *optSite = "NN", | 86 *optSite = "NN", |
87 *optNickSepStr = NULL; | 87 *optNickSepStr = NULL; |
88 char optNickSep; | 88 char optNickSep; |
89 BOOL optDaemon = FALSE, | 89 bool optDaemon = false, |
90 optProxyEnable = FALSE, | 90 optProxyEnable = false, |
91 setIgnoreMode = FALSE, | 91 setIgnoreMode = false, |
92 optDebug = FALSE, | 92 optDebug = false, |
93 optLogEnable = FALSE, | 93 optLogEnable = false, |
94 optLogDaily = FALSE, | 94 optLogDaily = false, |
95 optOnlyFriendPrv = FALSE, | 95 optOnlyFriendPrv = false, |
96 optShowHelp = FALSE; | 96 optShowHelp = false; |
97 | 97 |
98 char *setHomeDir = NULL, *setConfigDir = NULL, *setProxyURI = NULL; | 98 char *setHomeDir = NULL, *setConfigDir = NULL, *setProxyURI = NULL; |
99 th_llist_t *setIgnoreList = NULL, | 99 th_llist_t *setIgnoreList = NULL, |
100 *setFriendList = NULL, | 100 *setFriendList = NULL, |
101 *setIdleMessages = NULL; | 101 *setIdleMessages = NULL; |
176 | 176 |
177 printf("\n"); | 177 printf("\n"); |
178 } | 178 } |
179 | 179 |
180 | 180 |
181 BOOL argSplitStr(const char *src, const char *at, char **res1, char **res2) | 181 bool argSplitStr(const char *src, const char *at, char **res1, char **res2) |
182 { | 182 { |
183 char *pos, *tmp = th_strdup(src); | 183 char *pos, *tmp = th_strdup(src); |
184 | 184 |
185 if (tmp != NULL && (pos = strstr(tmp, at)) != NULL) | 185 if (tmp != NULL && (pos = strstr(tmp, at)) != NULL) |
186 { | 186 { |
187 *pos = 0; | 187 *pos = 0; |
188 pos += strlen(at); | 188 pos += strlen(at); |
189 *res1 = th_strdup_trim(tmp, TH_TRIM_BOTH); | 189 *res1 = th_strdup_trim(tmp, TH_TRIM_BOTH); |
190 *res2 = th_strdup_trim(pos, TH_TRIM_BOTH); | 190 *res2 = th_strdup_trim(pos, TH_TRIM_BOTH); |
191 th_free(tmp); | 191 th_free(tmp); |
192 return TRUE; | 192 return true; |
193 } | 193 } |
194 else | 194 else |
195 { | 195 { |
196 th_free(tmp); | 196 th_free(tmp); |
197 return FALSE; | 197 return false; |
198 } | 198 } |
199 } | 199 } |
200 | 200 |
201 | 201 |
202 BOOL argHandleProxyURI(const char *uri) | 202 bool argHandleProxyURI(const char *uri) |
203 { | 203 { |
204 // Attempt to parse the proxy URI | 204 // Attempt to parse the proxy URI |
205 BOOL ret = FALSE; | 205 bool ret = false; |
206 char *proto = NULL, *rest = NULL, *host = NULL, | 206 char *proto = NULL, *rest = NULL, *host = NULL, |
207 *auth = NULL, *port = NULL; | 207 *auth = NULL, *port = NULL; |
208 size_t len; | 208 size_t len; |
209 | 209 |
210 optProxyEnable = FALSE; | 210 optProxyEnable = false; |
211 | 211 |
212 // Handle disable case | 212 // Handle disable case |
213 if (th_strncasecmp(uri, "disab", 5) == 0) | 213 if (th_strncasecmp(uri, "disab", 5) == 0) |
214 return TRUE; | 214 return true; |
215 | 215 |
216 // Split the URI | 216 // Split the URI |
217 if (!argSplitStr(uri, "://", &proto, &rest)) | 217 if (!argSplitStr(uri, "://", &proto, &rest)) |
218 { | 218 { |
219 THERR("Malformed proxy URI, should be <type>://[<userid>[:passwd]@]<host>[:<port>]\n"); | 219 THERR("Malformed proxy URI, should be <type>://[<userid>[:passwd]@]<host>[:<port>]\n"); |
274 optProxyUserID != NULL && optProxyPassword != NULL) | 274 optProxyUserID != NULL && optProxyPassword != NULL) |
275 optProxyAuthType = TH_PROXY_AUTH_USER; | 275 optProxyAuthType = TH_PROXY_AUTH_USER; |
276 else | 276 else |
277 optProxyAuthType = TH_PROXY_AUTH_NONE; | 277 optProxyAuthType = TH_PROXY_AUTH_NONE; |
278 | 278 |
279 optProxyEnable = TRUE; | 279 optProxyEnable = true; |
280 ret = TRUE; | 280 ret = true; |
281 | 281 |
282 out: | 282 out: |
283 th_free(proto); | 283 th_free(proto); |
284 th_free(rest); | 284 th_free(rest); |
285 th_free(host); | 285 th_free(host); |
288 | 288 |
289 return ret; | 289 return ret; |
290 } | 290 } |
291 | 291 |
292 | 292 |
293 BOOL argHandleOpt(const int optN, char *optArg, char *currArg) | 293 bool argHandleOpt(const int optN, char *optArg, char *currArg) |
294 { | 294 { |
295 switch (optN) | 295 switch (optN) |
296 { | 296 { |
297 case 0: | 297 case 0: |
298 optShowHelp = TRUE; | 298 optShowHelp = true; |
299 break; | 299 break; |
300 | 300 |
301 case 1: | 301 case 1: |
302 th_verbosity++; | 302 th_verbosity++; |
303 break; | 303 break; |
313 case 4: | 313 case 4: |
314 if (!th_get_hex_triplet(optArg, &optUserColor)) | 314 if (!th_get_hex_triplet(optArg, &optUserColor)) |
315 { | 315 { |
316 THERR("Invalid color argument '%s', should be a RGB hex triplet '000000'.\n", | 316 THERR("Invalid color argument '%s', should be a RGB hex triplet '000000'.\n", |
317 optArg); | 317 optArg); |
318 return FALSE; | 318 return false; |
319 } | 319 } |
320 THMSG(1, "Using color #%06x\n", optUserColor); | 320 THMSG(1, "Using color #%06x\n", optUserColor); |
321 break; | 321 break; |
322 | 322 |
323 case 7: | 323 case 7: |
324 optSite = optArg; | 324 optSite = optArg; |
325 break; | 325 break; |
326 | 326 |
327 case 6: | 327 case 6: |
328 optDaemon = TRUE; | 328 optDaemon = true; |
329 THMSG(1, "Running in pseudo-daemon mode.\n"); | 329 THMSG(1, "Running in pseudo-daemon mode.\n"); |
330 break; | 330 break; |
331 | 331 |
332 case 8: | 332 case 8: |
333 optDebug = TRUE; | 333 optDebug = true; |
334 THMSG(1, "Debug mode enabled.\n"); | 334 THMSG(1, "Debug mode enabled.\n"); |
335 break; | 335 break; |
336 | 336 |
337 | 337 |
338 case 10: | 338 case 10: |
339 if (!argHandleProxyURI(optArg)) | 339 if (!argHandleProxyURI(optArg)) |
340 return FALSE; | 340 return false; |
341 break; | 341 break; |
342 | 342 |
343 case 13: | 343 case 13: |
344 { | 344 { |
345 int i; | 345 int i; |
346 for (i = 0; i < nn_room_data_n; i++) | 346 for (i = 0; i < nn_room_data_n; i++) |
347 if (!th_strcasecmp(nn_room_data[i].name, optArg)) | 347 if (!th_strcasecmp(nn_room_data[i].name, optArg)) |
348 { | 348 { |
349 optPort = nn_room_data[i].port; | 349 optPort = nn_room_data[i].port; |
350 return TRUE; | 350 return true; |
351 } | 351 } |
352 | 352 |
353 THERR("Unsupported room '%s'.\n", optArg); | 353 THERR("Unsupported room '%s'.\n", optArg); |
354 return FALSE; | 354 return false; |
355 } | 355 } |
356 break; | 356 break; |
357 | 357 |
358 default: | 358 default: |
359 THERR("Unknown option '%s'.\n", currArg); | 359 THERR("Unknown option '%s'.\n", currArg); |
360 return FALSE; | 360 return false; |
361 } | 361 } |
362 | 362 |
363 return TRUE; | 363 return true; |
364 } | 364 } |
365 | 365 |
366 | 366 |
367 BOOL argHandleFile(char *currArg) | 367 bool argHandleFile(char *currArg) |
368 { | 368 { |
369 if (!optUserNameCmd) | 369 if (!optUserNameCmd) |
370 optUserNameCmd = currArg; | 370 optUserNameCmd = currArg; |
371 else if (!optPasswordCmd) | 371 else if (!optPasswordCmd) |
372 optPasswordCmd = currArg; | 372 optPasswordCmd = currArg; |
373 else | 373 else |
374 { | 374 { |
375 THERR("Username '%s' already specified on commandline!\n", optUserNameCmd); | 375 THERR("Username '%s' already specified on commandline!\n", optUserNameCmd); |
376 return FALSE; | 376 return false; |
377 } | 377 } |
378 | 378 |
379 return TRUE; | 379 return true; |
380 } | 380 } |
381 | 381 |
382 | 382 |
383 BOOL nn_conn_send_msg(th_conn_t *conn, const char *user, const char *str) | 383 bool nn_conn_send_msg(th_conn_t *conn, const char *user, const char *str) |
384 { | 384 { |
385 char *msg; | 385 char *msg; |
386 | 386 |
387 if (str == NULL) | 387 if (str == NULL) |
388 return FALSE; | 388 return false; |
389 | 389 |
390 msg = th_strdup_printf("<USER>%s</USER><MESSAGE>%s</MESSAGE>", user, str); | 390 msg = th_strdup_printf("<USER>%s</USER><MESSAGE>%s</MESSAGE>", user, str); |
391 | 391 |
392 if (msg != NULL) | 392 if (msg != NULL) |
393 { | 393 { |
394 int ret = th_conn_send_buf(conn, msg, strlen(msg) + 1); | 394 int ret = th_conn_send_buf(conn, msg, strlen(msg) + 1); |
395 th_free(msg); | 395 th_free(msg); |
396 return ret == THERR_OK; | 396 return ret == THERR_OK; |
397 } | 397 } |
398 else | 398 else |
399 return FALSE; | 399 return false; |
400 } | 400 } |
401 | 401 |
402 | 402 |
403 BOOL nn_conn_send_msg_v(th_conn_t *conn, const char *user, const char *fmt, ...) | 403 bool nn_conn_send_msg_v(th_conn_t *conn, const char *user, const char *fmt, ...) |
404 { | 404 { |
405 BOOL res; | 405 bool res; |
406 char *tmp; | 406 char *tmp; |
407 va_list ap; | 407 va_list ap; |
408 | 408 |
409 va_start(ap, fmt); | 409 va_start(ap, fmt); |
410 tmp = th_strdup_vprintf(fmt, ap); | 410 tmp = th_strdup_vprintf(fmt, ap); |
461 } | 461 } |
462 } | 462 } |
463 | 463 |
464 | 464 |
465 void printMsgF(nn_window_t *win, int flags, const char *fmt, ...); | 465 void printMsgF(nn_window_t *win, int flags, const char *fmt, ...); |
466 BOOL nn_log_reopen(nn_window_t *win); | 466 bool nn_log_reopen(nn_window_t *win); |
467 | 467 |
468 | 468 |
469 void printMsgConst(nn_window_t *win, int flags, const char *msg) | 469 void printMsgConst(nn_window_t *win, int flags, const char *msg) |
470 { | 470 { |
471 char tmpStr[128]; | 471 char tmpStr[128]; |
626 (void) level; | 626 (void) level; |
627 printMsgConst(NULL, LOG_STAMP | LOG_WINDOW | LOG_FILE, msg); | 627 printMsgConst(NULL, LOG_STAMP | LOG_WINDOW | LOG_FILE, msg); |
628 } | 628 } |
629 | 629 |
630 | 630 |
631 BOOL nn_check_name_list(th_llist_t *list, const char *name) | 631 bool nn_check_name_list(th_llist_t *list, const char *name) |
632 { | 632 { |
633 th_llist_t *node; | 633 th_llist_t *node; |
634 | 634 |
635 for (node = list; node != NULL; node = node->next) | 635 for (node = list; node != NULL; node = node->next) |
636 { | 636 { |
637 if (th_strcasecmp(name, (char *) node->data) == 0) | 637 if (th_strcasecmp(name, (char *) node->data) == 0) |
638 return TRUE; | 638 return true; |
639 } | 639 } |
640 | 640 |
641 return FALSE; | 641 return false; |
642 } | 642 } |
643 | 643 |
644 | 644 |
645 int nnproto_parse_user(th_conn_t *conn) | 645 int nnproto_parse_user(th_conn_t *conn) |
646 { | 646 { |
647 BOOL isMine, isIgnored = FALSE; | 647 bool isMine, isIgnored = false; |
648 char *name, *msg, *t; | 648 char *name, *msg, *t; |
649 | 649 |
650 // Find start of the message | 650 // Find start of the message |
651 name = conn->base.ptr; | 651 name = conn->base.ptr; |
652 t = th_conn_buf_strstr(conn, "</USER>"); | 652 t = th_conn_buf_strstr(conn, "</USER>"); |
901 static const int nprotoCmds = sizeof(protoCmds) / sizeof(protoCmds[0]); | 901 static const int nprotoCmds = sizeof(protoCmds) / sizeof(protoCmds[0]); |
902 | 902 |
903 | 903 |
904 int nn_parse_protocol(th_conn_t *conn) | 904 int nn_parse_protocol(th_conn_t *conn) |
905 { | 905 { |
906 static BOOL protoCmdsInit = FALSE; | 906 static bool protoCmdsInit = false; |
907 int i; | 907 int i; |
908 | 908 |
909 if (!protoCmdsInit) | 909 if (!protoCmdsInit) |
910 { | 910 { |
911 for (i = 0; i < nprotoCmds; i++) | 911 for (i = 0; i < nprotoCmds; i++) |
912 protoCmds[i].len = strlen(protoCmds[i].name); | 912 protoCmds[i].len = strlen(protoCmds[i].name); |
913 | 913 |
914 protoCmdsInit = TRUE; | 914 protoCmdsInit = true; |
915 } | 915 } |
916 | 916 |
917 for (i = 0; i < nprotoCmds; i++) | 917 for (i = 0; i < nprotoCmds; i++) |
918 { | 918 { |
919 if (!th_conn_buf_strncmp(conn, protoCmds[i].name, protoCmds[i].len)) | 919 if (!th_conn_buf_strncmp(conn, protoCmds[i].name, protoCmds[i].len)) |
1102 nn_user_t *user = nn_userhash_find(nnUsers, nn_username_encode(name)); | 1102 nn_user_t *user = nn_userhash_find(nnUsers, nn_username_encode(name)); |
1103 if (user != NULL) | 1103 if (user != NULL) |
1104 { | 1104 { |
1105 name = nn_username_decode(th_strdup(user->name)); | 1105 name = nn_username_decode(th_strdup(user->name)); |
1106 printMsgQ(currWin, "Opening PRV query for '%s'.\n", name); | 1106 printMsgQ(currWin, "Opening PRV query for '%s'.\n", name); |
1107 if (nnwin_open(name, TRUE)) | 1107 if (nnwin_open(name, true)) |
1108 printMsgQ(currWin, "In PRV query with '%s'.\n", name); | 1108 printMsgQ(currWin, "In PRV query with '%s'.\n", name); |
1109 th_free(name); | 1109 th_free(name); |
1110 return 0; | 1110 return 0; |
1111 } | 1111 } |
1112 else | 1112 else |
1313 int nncmd_quit(th_conn_t *conn, char *buf) | 1313 int nncmd_quit(th_conn_t *conn, char *buf) |
1314 { | 1314 { |
1315 (void) conn; | 1315 (void) conn; |
1316 (void) buf; | 1316 (void) buf; |
1317 | 1317 |
1318 appQuitFlag = TRUE; | 1318 appQuitFlag = true; |
1319 return 0; | 1319 return 0; |
1320 } | 1320 } |
1321 | 1321 |
1322 | 1322 |
1323 enum | 1323 enum |
1429 } | 1429 } |
1430 else | 1430 else |
1431 { | 1431 { |
1432 // Server-side commands are just pass-through here | 1432 // Server-side commands are just pass-through here |
1433 char *tmp = nn_dblencode_str(buf); | 1433 char *tmp = nn_dblencode_str(buf); |
1434 BOOL result; | 1434 bool result; |
1435 if (tmp == NULL) return -2; | 1435 if (tmp == NULL) return -2; |
1436 result = nn_conn_send_msg(conn, optUserNameEnc, tmp); | 1436 result = nn_conn_send_msg(conn, optUserNameEnc, tmp); |
1437 th_free(tmp); | 1437 th_free(tmp); |
1438 return result ? 0 : -1; | 1438 return result ? 0 : -1; |
1439 } | 1439 } |
1456 } | 1456 } |
1457 return NULL; | 1457 return NULL; |
1458 } | 1458 } |
1459 | 1459 |
1460 | 1460 |
1461 nn_usercmd_t *nn_usercmd_match(th_llist_t *list, const char *pattern, const char *current, BOOL again) | 1461 nn_usercmd_t *nn_usercmd_match(th_llist_t *list, const char *pattern, const char *current, bool again) |
1462 { | 1462 { |
1463 nn_usercmd_t *curr; | 1463 nn_usercmd_t *curr; |
1464 size_t len; | 1464 size_t len; |
1465 | 1465 |
1466 if (list == NULL || pattern == NULL) return NULL; | 1466 if (list == NULL || pattern == NULL) return NULL; |
1491 } | 1491 } |
1492 | 1492 |
1493 | 1493 |
1494 int nn_handle_input(th_conn_t *conn, char *buf, size_t bufLen) | 1494 int nn_handle_input(th_conn_t *conn, char *buf, size_t bufLen) |
1495 { | 1495 { |
1496 BOOL result; | 1496 bool result; |
1497 char *tmp; | 1497 char *tmp; |
1498 | 1498 |
1499 // Trim right side | 1499 // Trim right side |
1500 while (bufLen > 0 && th_isspace(buf[bufLen - 1])) | 1500 while (bufLen > 0 && th_isspace(buf[bufLen - 1])) |
1501 buf[--bufLen] = 0; | 1501 buf[--bufLen] = 0; |
1564 th_free(*previous); | 1564 th_free(*previous); |
1565 *previous = th_strdup(name); | 1565 *previous = th_strdup(name); |
1566 } | 1566 } |
1567 | 1567 |
1568 | 1568 |
1569 BOOL nn_tabcomplete_buffer(nn_editbuf_t *buf) | 1569 bool nn_tabcomplete_buffer(nn_editbuf_t *buf) |
1570 { | 1570 { |
1571 static char *previous = NULL, *pattern = NULL; | 1571 static char *previous = NULL, *pattern = NULL; |
1572 char *str = buf->data; | 1572 char *str = buf->data; |
1573 BOOL again = FALSE, hasSeparator = FALSE, | 1573 bool again = false, hasSeparator = false, |
1574 hasSpace, newPattern = FALSE, isCommand; | 1574 hasSpace, newPattern = false, isCommand; |
1575 size_t endPos, startPos = buf->pos; | 1575 size_t endPos, startPos = buf->pos; |
1576 | 1576 |
1577 // previous word | 1577 // previous word |
1578 if (startPos >= 2 && str[startPos - 1] == ' ' && str[startPos - 2] != ' ') | 1578 if (startPos >= 2 && str[startPos - 1] == ' ' && str[startPos - 2] != ' ') |
1579 { | 1579 { |
1586 if (startPos < buf->len && str[startPos] != ' ') | 1586 if (startPos < buf->len && str[startPos] != ' ') |
1587 { | 1587 { |
1588 endPos = startPos; | 1588 endPos = startPos; |
1589 while (startPos > 0 && str[startPos - 1] != ' ') startPos--; | 1589 while (startPos > 0 && str[startPos - 1] != ' ') startPos--; |
1590 while (endPos < buf->len - 1 && str[endPos + 1] != ' ') endPos++; | 1590 while (endPos < buf->len - 1 && str[endPos + 1] != ' ') endPos++; |
1591 newPattern = TRUE; | 1591 newPattern = true; |
1592 } | 1592 } |
1593 else | 1593 else |
1594 // previous word, new pattern | 1594 // previous word, new pattern |
1595 if (startPos >= 1 && str[startPos - 1] != ' ') | 1595 if (startPos >= 1 && str[startPos - 1] != ' ') |
1596 { | 1596 { |
1597 startPos -= 1; | 1597 startPos -= 1; |
1598 endPos = startPos; | 1598 endPos = startPos; |
1599 while (startPos > 0 && str[startPos - 1] != ' ') startPos--; | 1599 while (startPos > 0 && str[startPos - 1] != ' ') startPos--; |
1600 newPattern = TRUE; | 1600 newPattern = true; |
1601 } | 1601 } |
1602 else | 1602 else |
1603 return FALSE; | 1603 return false; |
1604 | 1604 |
1605 // Check if this is a command completion | 1605 // Check if this is a command completion |
1606 isCommand = (str[0] == '/' && startPos == 0); | 1606 isCommand = (str[0] == '/' && startPos == 0); |
1607 | 1607 |
1608 if (!isCommand && str[endPos] == optNickSep) | 1608 if (!isCommand && str[endPos] == optNickSep) |
1609 { | 1609 { |
1610 endPos--; | 1610 endPos--; |
1611 if (startPos > 0) | 1611 if (startPos > 0) |
1612 return FALSE; | 1612 return false; |
1613 hasSeparator = TRUE; | 1613 hasSeparator = true; |
1614 } | 1614 } |
1615 | 1615 |
1616 hasSpace = (buf->pos > 0 && str[buf->pos - 1] == ' ') || | 1616 hasSpace = (buf->pos > 0 && str[buf->pos - 1] == ' ') || |
1617 (buf->pos <= buf->len && str[buf->pos] == ' '); | 1617 (buf->pos <= buf->len && str[buf->pos] == ' '); |
1618 | 1618 |
1619 if (newPattern) | 1619 if (newPattern) |
1620 { | 1620 { |
1621 // Get pattern, check if it matches previous pattern and set 'again' flag | 1621 // Get pattern, check if it matches previous pattern and set 'again' flag |
1622 char *npattern = nn_editbuf_get_string(buf, startPos, endPos); | 1622 char *npattern = nn_editbuf_get_string(buf, startPos, endPos); |
1623 if (pattern && npattern && th_strcasecmp(npattern, pattern) == 0) | 1623 if (pattern && npattern && th_strcasecmp(npattern, pattern) == 0) |
1624 again = TRUE; | 1624 again = true; |
1625 | 1625 |
1626 th_free(pattern); | 1626 th_free(pattern); |
1627 pattern = npattern; | 1627 pattern = npattern; |
1628 | 1628 |
1629 if (!again) | 1629 if (!again) |
1631 th_free_r(&previous); | 1631 th_free_r(&previous); |
1632 } | 1632 } |
1633 } | 1633 } |
1634 | 1634 |
1635 if (!pattern) | 1635 if (!pattern) |
1636 return FALSE; | 1636 return false; |
1637 | 1637 |
1638 if (isCommand) | 1638 if (isCommand) |
1639 { | 1639 { |
1640 nn_usercmd_t *cmd = nn_usercmd_match(userCmds, pattern, previous, again); | 1640 nn_usercmd_t *cmd = nn_usercmd_match(userCmds, pattern, previous, again); |
1641 if (cmd) | 1641 if (cmd) |
1645 | 1645 |
1646 if (!hasSpace) | 1646 if (!hasSpace) |
1647 nn_editbuf_insert(buf, i++, ' '); | 1647 nn_editbuf_insert(buf, i++, ' '); |
1648 | 1648 |
1649 nn_tabcomplete_finish(buf, &previous, startPos, cmd->name); | 1649 nn_tabcomplete_finish(buf, &previous, startPos, cmd->name); |
1650 return TRUE; | 1650 return true; |
1651 } | 1651 } |
1652 } | 1652 } |
1653 else | 1653 else |
1654 { | 1654 { |
1655 nn_user_t *user = nn_userhash_match(nnUsers, pattern, previous, again); | 1655 nn_user_t *user = nn_userhash_match(nnUsers, pattern, previous, again); |
1670 | 1670 |
1671 if (!hasSpace) | 1671 if (!hasSpace) |
1672 nn_editbuf_insert(buf, i++, ' '); | 1672 nn_editbuf_insert(buf, i++, ' '); |
1673 | 1673 |
1674 nn_tabcomplete_finish(buf, &previous, startPos, user->name); | 1674 nn_tabcomplete_finish(buf, &previous, startPos, user->name); |
1675 return TRUE; | 1675 return true; |
1676 } | 1676 } |
1677 } | 1677 } |
1678 | 1678 |
1679 return FALSE; | 1679 return false; |
1680 } | 1680 } |
1681 | 1681 |
1682 | 1682 |
1683 BOOL processUserInput(int c, nn_editbuf_t *editBuf, nn_editstate_t *editState) | 1683 bool processUserInput(int c, nn_editbuf_t *editBuf, nn_editstate_t *editState) |
1684 { | 1684 { |
1685 // Chat window switching via Meta/Esc-[1..9] | 1685 // Chat window switching via Meta/Esc-[1..9] |
1686 if (c >= 0x5001 && c <= 0x5009) | 1686 if (c >= 0x5001 && c <= 0x5009) |
1687 { | 1687 { |
1688 nn_window_t *win = nnwin_get(c - 0x5000); | 1688 nn_window_t *win = nnwin_get(c - 0x5000); |
1689 if (win != NULL) | 1689 if (win != NULL) |
1690 { | 1690 { |
1691 currWin = win; | 1691 currWin = win; |
1692 editState->update = TRUE; | 1692 editState->update = true; |
1693 } | 1693 } |
1694 } | 1694 } |
1695 else | 1695 else |
1696 switch (c) | 1696 switch (c) |
1697 { | 1697 { |
1719 nn_editbuf_clear(editBuf); | 1719 nn_editbuf_clear(editBuf); |
1720 | 1720 |
1721 if (result < 0) | 1721 if (result < 0) |
1722 { | 1722 { |
1723 errorMsg("Fatal error handling user input: %s\n", editBuf->data); | 1723 errorMsg("Fatal error handling user input: %s\n", editBuf->data); |
1724 editState->isError = TRUE; | 1724 editState->isError = true; |
1725 } | 1725 } |
1726 else | 1726 else |
1727 { | 1727 { |
1728 // Update time value of last sent message for unidle timeouts | 1728 // Update time value of last sent message for unidle timeouts |
1729 editState->prevKeepAlive = time(NULL); | 1729 editState->prevKeepAlive = time(NULL); |
1744 currWin->pos = (int) (currWin->data->n - page); | 1744 currWin->pos = (int) (currWin->data->n - page); |
1745 if (currWin->pos < 0) | 1745 if (currWin->pos < 0) |
1746 currWin->pos = 0; | 1746 currWin->pos = 0; |
1747 | 1747 |
1748 if (oldPos != currWin->pos) | 1748 if (oldPos != currWin->pos) |
1749 editState->update = TRUE; | 1749 editState->update = true; |
1750 } | 1750 } |
1751 break; | 1751 break; |
1752 | 1752 |
1753 case KEY_UP: // Backwards in input history | 1753 case KEY_UP: // Backwards in input history |
1754 if (editHistPos == 0) | 1754 if (editHistPos == 0) |
1781 printMsgQ(currWin, "Only friends allowed to PRV you = %s\n", optOnlyFriendPrv ? "ON" : "OFF"); | 1781 printMsgQ(currWin, "Only friends allowed to PRV you = %s\n", optOnlyFriendPrv ? "ON" : "OFF"); |
1782 break; | 1782 break; |
1783 | 1783 |
1784 case KEY_F(9): // F9 = Quit | 1784 case KEY_F(9): // F9 = Quit |
1785 printMsg(currWin, "Quitting per user request (%d/0x%x).\n", c, c); | 1785 printMsg(currWin, "Quitting per user request (%d/0x%x).\n", c, c); |
1786 appQuitFlag = TRUE; | 1786 appQuitFlag = true; |
1787 break; | 1787 break; |
1788 | 1788 |
1789 case 0x09: // Tab = complete username or command | 1789 case 0x09: // Tab = complete username or command |
1790 nn_tabcomplete_buffer(editBuf); | 1790 nn_tabcomplete_buffer(editBuf); |
1791 break; | 1791 break; |
1792 | 1792 |
1793 default: | 1793 default: |
1794 return FALSE; | 1794 return false; |
1795 } | 1795 } |
1796 | 1796 |
1797 return TRUE; | 1797 return true; |
1798 } | 1798 } |
1799 | 1799 |
1800 | 1800 |
1801 BOOL processUserPrompt(int c, nn_editbuf_t *editBuf, nn_editstate_t *editState) | 1801 bool processUserPrompt(int c, nn_editbuf_t *editBuf, nn_editstate_t *editState) |
1802 { | 1802 { |
1803 (void) editBuf; | 1803 (void) editBuf; |
1804 | 1804 |
1805 switch (c) | 1805 switch (c) |
1806 { | 1806 { |
1807 case KEY_ENTER: | 1807 case KEY_ENTER: |
1808 editState->done = TRUE; | 1808 editState->done = true; |
1809 break; | 1809 break; |
1810 | 1810 |
1811 case KEY_F(9): // F9 = Quit | 1811 case KEY_F(9): // F9 = Quit |
1812 printMsg(currWin, "Quitting per user request (%d/0x%x).\n", c, c); | 1812 printMsg(currWin, "Quitting per user request (%d/0x%x).\n", c, c); |
1813 appQuitFlag = TRUE; | 1813 appQuitFlag = true; |
1814 break; | 1814 break; |
1815 | 1815 |
1816 default: | 1816 default: |
1817 return FALSE; | 1817 return false; |
1818 } | 1818 } |
1819 | 1819 |
1820 return TRUE; | 1820 return true; |
1821 } | 1821 } |
1822 | 1822 |
1823 | 1823 |
1824 void updateUserPrompt(nn_editbuf_t *editBuf, nn_editstate_t *editState) | 1824 void updateUserPrompt(nn_editbuf_t *editBuf, nn_editstate_t *editState) |
1825 { | 1825 { |
1828 | 1828 |
1829 | 1829 |
1830 void clearEditState(nn_editstate_t *st) | 1830 void clearEditState(nn_editstate_t *st) |
1831 { | 1831 { |
1832 memset(st, 0, sizeof(nn_editstate_t)); | 1832 memset(st, 0, sizeof(nn_editstate_t)); |
1833 st->insertMode = TRUE; | 1833 st->insertMode = true; |
1834 st->debugMsg = debugMsg; | 1834 st->debugMsg = debugMsg; |
1835 } | 1835 } |
1836 | 1836 |
1837 | 1837 |
1838 BOOL nn_log_open(nn_window_t *win) | 1838 bool nn_log_open(nn_window_t *win) |
1839 { | 1839 { |
1840 char *path = NULL, *plt; | 1840 char *path = NULL, *plt; |
1841 #ifndef TH_PLAT_WINDOWS | 1841 #ifndef TH_PLAT_WINDOWS |
1842 int logFd = -1; | 1842 int logFd = -1; |
1843 #endif | 1843 #endif |
1844 | 1844 |
1845 if (!optLogEnable || optLogPath == NULL) | 1845 if (!optLogEnable || optLogPath == NULL) |
1846 return FALSE; | 1846 return false; |
1847 | 1847 |
1848 plt = strrchr(optLogPath, TH_DIR_SEPARATOR_CHR); | 1848 plt = strrchr(optLogPath, TH_DIR_SEPARATOR_CHR); |
1849 if (plt == NULL || plt[1] != 0) | 1849 if (plt == NULL || plt[1] != 0) |
1850 path = th_strdup_printf("%s%c", optLogPath, TH_DIR_SEPARATOR_CHR); | 1850 path = th_strdup_printf("%s%c", optLogPath, TH_DIR_SEPARATOR_CHR); |
1851 else | 1851 else |
1873 // PRV chat log | 1873 // PRV chat log |
1874 size_t pos; | 1874 size_t pos; |
1875 char *cleaned; | 1875 char *cleaned; |
1876 | 1876 |
1877 if ((cleaned = th_strdup(win->id)) == NULL) | 1877 if ((cleaned = th_strdup(win->id)) == NULL) |
1878 return FALSE; | 1878 return false; |
1879 | 1879 |
1880 for (pos = 0; cleaned[pos] != 0; pos++) | 1880 for (pos = 0; cleaned[pos] != 0; pos++) |
1881 { | 1881 { |
1882 if (!isalnum(cleaned[pos])) | 1882 if (!isalnum(cleaned[pos])) |
1883 cleaned[pos] = '_'; | 1883 cleaned[pos] = '_'; |
1907 } | 1907 } |
1908 | 1908 |
1909 printMsg(win, "Logging to '%s'.\n", win->logFilename); | 1909 printMsg(win, "Logging to '%s'.\n", win->logFilename); |
1910 | 1910 |
1911 th_free(path); | 1911 th_free(path); |
1912 return TRUE; | 1912 return true; |
1913 | 1913 |
1914 error: | 1914 error: |
1915 th_free(path); | 1915 th_free(path); |
1916 th_free_r(&win->logFilename); | 1916 th_free_r(&win->logFilename); |
1917 if (win->logFile != NULL) | 1917 if (win->logFile != NULL) |
1919 #ifndef TH_PLAT_WINDOWS | 1919 #ifndef TH_PLAT_WINDOWS |
1920 else | 1920 else |
1921 if (logFd >= 0) | 1921 if (logFd >= 0) |
1922 close(logFd); | 1922 close(logFd); |
1923 #endif | 1923 #endif |
1924 return FALSE; | 1924 return false; |
1925 } | 1925 } |
1926 | 1926 |
1927 | 1927 |
1928 void nn_log_close(nn_window_t *win) | 1928 void nn_log_close(nn_window_t *win) |
1929 { | 1929 { |
1933 | 1933 |
1934 th_free_r(&win->logFilename); | 1934 th_free_r(&win->logFilename); |
1935 } | 1935 } |
1936 | 1936 |
1937 | 1937 |
1938 BOOL nn_log_reopen(nn_window_t *win) | 1938 bool nn_log_reopen(nn_window_t *win) |
1939 { | 1939 { |
1940 nn_log_close(win); | 1940 nn_log_close(win); |
1941 return nn_log_open(win); | 1941 return nn_log_open(win); |
1942 } | 1942 } |
1943 | 1943 |
1959 | 1959 |
1960 int main(int argc, char *argv[]) | 1960 int main(int argc, char *argv[]) |
1961 { | 1961 { |
1962 char *tmpStr; | 1962 char *tmpStr; |
1963 int index, updateCount = 0, ret; | 1963 int index, updateCount = 0, ret; |
1964 BOOL argsOK, colorSet = FALSE; | 1964 bool argsOK, colorSet = false; |
1965 th_conn_t *conn = NULL; | 1965 th_conn_t *conn = NULL; |
1966 nn_editbuf_t *editBuf = nn_editbuf_new(NN_TMPBUF_SIZE); | 1966 nn_editbuf_t *editBuf = nn_editbuf_new(NN_TMPBUF_SIZE); |
1967 nn_editstate_t editState; | 1967 nn_editstate_t editState; |
1968 th_cfgitem_t *tmpcfg; | 1968 th_cfgitem_t *tmpcfg; |
1969 | 1969 |
2188 { | 2188 { |
2189 printMsg(NULL, "%s v%s - %s\n", th_prog_name, th_prog_version, th_prog_desc); | 2189 printMsg(NULL, "%s v%s - %s\n", th_prog_name, th_prog_version, th_prog_desc); |
2190 printMsg(NULL, "%s\n", th_prog_author); | 2190 printMsg(NULL, "%s\n", th_prog_author); |
2191 printMsg(NULL, "%s\n", th_prog_license); | 2191 printMsg(NULL, "%s\n", th_prog_license); |
2192 | 2192 |
2193 nnwin_update(TRUE, FALSE, NULL, optUserName, optUserColor); | 2193 nnwin_update(true, false, NULL, optUserName, optUserColor); |
2194 | 2194 |
2195 // Check if we have username and password | 2195 // Check if we have username and password |
2196 if (optUserName == NULL || optPassword == NULL) | 2196 if (optUserName == NULL || optPassword == NULL) |
2197 { | 2197 { |
2198 printMsg(NULL, "Please enter your NN login credentials.\n"); | 2198 printMsg(NULL, "Please enter your NN login credentials.\n"); |
2199 printMsg(NULL, "You can avoid doing this every time by issuing '/save' after logging in.\n"); | 2199 printMsg(NULL, "You can avoid doing this every time by issuing '/save' after logging in.\n"); |
2200 | 2200 |
2201 printMsg(NULL, "Enter your NN username ...\n"); | 2201 printMsg(NULL, "Enter your NN username ...\n"); |
2202 optUserName = nnwin_prompt_requester(FALSE, &editState, processUserPrompt, updateUserPrompt); | 2202 optUserName = nnwin_prompt_requester(false, &editState, processUserPrompt, updateUserPrompt); |
2203 if (appQuitFlag) | 2203 if (appQuitFlag) |
2204 goto err_exit; | 2204 goto err_exit; |
2205 | 2205 |
2206 editState.mask = TRUE; | 2206 editState.mask = true; |
2207 printMsg(NULL, "Enter your NN password ...\n"); | 2207 printMsg(NULL, "Enter your NN password ...\n"); |
2208 optPassword = nnwin_prompt_requester(TRUE, &editState, processUserPrompt, updateUserPrompt); | 2208 optPassword = nnwin_prompt_requester(true, &editState, processUserPrompt, updateUserPrompt); |
2209 editState.mask = FALSE; | 2209 editState.mask = false; |
2210 if (appQuitFlag) | 2210 if (appQuitFlag) |
2211 goto err_exit; | 2211 goto err_exit; |
2212 } | 2212 } |
2213 } | 2213 } |
2214 | 2214 |
2298 // Enter mainloop | 2298 // Enter mainloop |
2299 th_conn_reset(conn); | 2299 th_conn_reset(conn); |
2300 while (!editState.isError && !appQuitFlag) | 2300 while (!editState.isError && !appQuitFlag) |
2301 { | 2301 { |
2302 int retries = 3, cres; | 2302 int retries = 3, cres; |
2303 editState.update = FALSE; | 2303 editState.update = false; |
2304 | 2304 |
2305 packet_retry: | 2305 packet_retry: |
2306 cres = th_conn_pull(conn); | 2306 cres = th_conn_pull(conn); |
2307 if (cres == TH_CONN_DATA_AVAIL) | 2307 if (cres == TH_CONN_DATA_AVAIL) |
2308 { | 2308 { |
2326 // Couldn't handle the message for some reason | 2326 // Couldn't handle the message for some reason |
2327 printMsg(currWin, "Could not handle: %s\n", conn->base.ptr); | 2327 printMsg(currWin, "Could not handle: %s\n", conn->base.ptr); |
2328 th_conn_buf_skip(conn, strlen(conn->base.ptr) + 1); | 2328 th_conn_buf_skip(conn, strlen(conn->base.ptr) + 1); |
2329 } | 2329 } |
2330 else | 2330 else |
2331 editState.isError = TRUE; | 2331 editState.isError = true; |
2332 } | 2332 } |
2333 } | 2333 } |
2334 else | 2334 else |
2335 if (cres < TH_CONN_ERROR || !th_conn_check(conn)) | 2335 if (cres < TH_CONN_ERROR || !th_conn_check(conn)) |
2336 editState.isError = TRUE; | 2336 editState.isError = true; |
2337 | 2337 |
2338 // Handle user input | 2338 // Handle user input |
2339 BOOL flushed = FALSE; | 2339 bool flushed = false; |
2340 if (appCursesInit) | 2340 if (appCursesInit) |
2341 { | 2341 { |
2342 nnwin_input_process(editBuf, &editState, processUserInput); | 2342 nnwin_input_process(editBuf, &editState, processUserInput); |
2343 nnwin_update(editState.update, editState.mask, editBuf, optUserName, optUserColor); | 2343 nnwin_update(editState.update, editState.mask, editBuf, optUserName, optUserColor); |
2344 flushed = TRUE; | 2344 flushed = true; |
2345 } | 2345 } |
2346 | 2346 |
2347 if (++updateCount > 10) | 2347 if (++updateCount > 10) |
2348 { | 2348 { |
2349 time_t tmpTime = time(NULL); | 2349 time_t tmpTime = time(NULL); |
2355 editState.prevKeepAlive = tmpTime; | 2355 editState.prevKeepAlive = tmpTime; |
2356 } | 2356 } |
2357 | 2357 |
2358 if (!colorSet) | 2358 if (!colorSet) |
2359 { | 2359 { |
2360 colorSet = TRUE; | 2360 colorSet = true; |
2361 nn_conn_send_msg_v(conn, optUserNameEnc, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor); | 2361 nn_conn_send_msg_v(conn, optUserNameEnc, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor); |
2362 } | 2362 } |
2363 | 2363 |
2364 if (appCursesInit && !flushed) | 2364 if (appCursesInit && !flushed) |
2365 { | 2365 { |
2366 nnwin_update(FALSE, editState.mask, editBuf, optUserName, optUserColor); | 2366 nnwin_update(false, editState.mask, editBuf, optUserName, optUserColor); |
2367 } | 2367 } |
2368 | 2368 |
2369 updateCount = 0; | 2369 updateCount = 0; |
2370 } | 2370 } |
2371 } | 2371 } |
2375 if (errorMessages || editState.isError) | 2375 if (errorMessages || editState.isError) |
2376 { | 2376 { |
2377 char *tmp; | 2377 char *tmp; |
2378 printMsg(NULL, "Press enter to exit.\n"); | 2378 printMsg(NULL, "Press enter to exit.\n"); |
2379 clearEditState(&editState); | 2379 clearEditState(&editState); |
2380 tmp = nnwin_prompt_requester(FALSE, &editState, processUserPrompt, updateUserPrompt); | 2380 tmp = nnwin_prompt_requester(false, &editState, processUserPrompt, updateUserPrompt); |
2381 th_free(tmp); | 2381 th_free(tmp); |
2382 } | 2382 } |
2383 | 2383 |
2384 th_free(setHomeDir); | 2384 th_free(setHomeDir); |
2385 th_free(setConfigDir); | 2385 th_free(setConfigDir); |