Mercurial > hg > nnchat
comparison nnchat.c @ 9:a02659cc5bc8
Cleanup.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 20 Mar 2008 05:13:00 +0000 |
parents | 526ba3b578d7 |
children | 53e127854dca |
comparison
equal
deleted
inserted
replaced
8:355d908d9d00 | 9:a02659cc5bc8 |
---|---|
491 } | 491 } |
492 | 492 |
493 | 493 |
494 int handleAddUser(int sock, char *str) | 494 int handleAddUser(int sock, char *str) |
495 { | 495 { |
496 char *s = strstr(str, "</ADD_USER>"); | 496 char *p, *s = strstr(str, "</ADD_USER>"); |
497 | 497 |
498 (void) sock; | 498 (void) sock; |
499 | 499 |
500 if (!s) return 1; | 500 if (!s) return 1; |
501 *s = 0; | 501 *s = 0; |
502 printMsg("! %s ADDED.\n", str); | 502 |
503 p = decodeStr1(str); | |
504 if (!p) return -1; | |
505 | |
506 printMsg("! %s ADDED.\n", p); | |
507 th_free(p); | |
503 return 0; | 508 return 0; |
504 } | 509 } |
505 | 510 |
506 | 511 |
507 int handleDeleteUser(int sock, char *str) | 512 int handleDeleteUser(int sock, char *str) |
508 { | 513 { |
509 char *s = strstr(str, "</DELETE_USER>"); | 514 char *p, *s = strstr(str, "</DELETE_USER>"); |
510 | 515 |
511 (void) sock; | 516 (void) sock; |
512 | 517 |
513 if (!s) return 1; | 518 if (!s) return 1; |
514 *s = 0; | 519 *s = 0; |
515 printMsg("! %s DELETED.\n", str); | 520 |
521 p = decodeStr1(str); | |
522 if (!p) return -1; | |
523 | |
524 printMsg("! %s DELETED.\n", p); | |
525 th_free(p); | |
516 return 0; | 526 return 0; |
517 } | 527 } |
518 | 528 |
519 | 529 |
520 int handleFoo(int sock, char *str) | 530 int handleFoo(int sock, char *str) |
698 /* Check for incoming data from the server */ | 708 /* Check for incoming data from the server */ |
699 tv.tv_sec = 0; | 709 tv.tv_sec = 0; |
700 tv.tv_usec = SET_SELECT_USEC; | 710 tv.tv_usec = SET_SELECT_USEC; |
701 tmpfds = sockfds; | 711 tmpfds = sockfds; |
702 if ((result = select(tmpSocket+1, &tmpfds, NULL, NULL, &tv)) == -1) { | 712 if ((result = select(tmpSocket+1, &tmpfds, NULL, NULL, &tv)) == -1) { |
703 THERR("Error occured in select(sockfds): %s\n", strerror(errno)); | 713 printMsg("Error occured in select(sockfds): %s\n", strerror(errno)); |
704 exitProg = TRUE; | 714 exitProg = TRUE; |
705 } else if (FD_ISSET(tmpSocket, &tmpfds)) { | 715 } else if (FD_ISSET(tmpSocket, &tmpfds)) { |
706 gotBuf = recv(tmpSocket, tmpBuf, sizeof(tmpBuf), 0); | 716 gotBuf = recv(tmpSocket, tmpBuf, sizeof(tmpBuf), 0); |
707 | 717 |
708 if (gotBuf < 0) { | 718 if (gotBuf < 0) { |
709 THERR("Error in recv: %s\n", strerror(errno)); | 719 printMsg("Error in recv: %s\n", strerror(errno)); |
710 exitProg = TRUE; | 720 exitProg = TRUE; |
711 } else if (gotBuf == 0) { | 721 } else if (gotBuf == 0) { |
712 THERR("Server closed connection.\n"); | 722 printMsg("Server closed connection.\n"); |
713 exitProg = TRUE; | 723 exitProg = TRUE; |
714 } else { | 724 } else { |
715 /* Handle protocol data */ | 725 /* Handle protocol data */ |
716 tmpBuf[gotBuf] = 0; | 726 tmpBuf[gotBuf] = 0; |
717 result = handleProtocol(tmpSocket, tmpBuf, gotBuf); | 727 result = handleProtocol(tmpSocket, tmpBuf, gotBuf); |
718 | 728 |
719 if (result > 0) { | 729 if (result > 0) { |
720 /* Couldn't handle the message for some reason */ | 730 /* Couldn't handle the message for some reason */ |
721 THERR("Could not handle: %s\n", tmpBuf); | 731 printMsg("Could not handle: %s\n", tmpBuf); |
722 } else if (result < 0) { | 732 } else if (result < 0) { |
723 /* Fatal error, quit */ | 733 /* Fatal error, quit */ |
724 THERR("Fatal error with message: %s\n", tmpBuf); | 734 printMsg("Fatal error with message: %s\n", tmpBuf); |
725 exitProg = TRUE; | 735 exitProg = TRUE; |
726 } | 736 } |
727 } | 737 } |
728 } | 738 } |
729 | 739 |
730 /* Check for user input */ | 740 /* Check for user input */ |
731 tv.tv_sec = 0; | 741 tv.tv_sec = 0; |
732 tv.tv_usec = SET_SELECT_USEC; | 742 tv.tv_usec = SET_SELECT_USEC; |
733 tmpfds = inputfds; | 743 tmpfds = inputfds; |
734 if ((result = select(1, &tmpfds, NULL, NULL, &tv)) == -1) { | 744 if ((result = select(1, &tmpfds, NULL, NULL, &tv)) == -1) { |
735 THERR("Error occured in select(inputfds): %s\n", strerror(errno)); | 745 printMsg("Error occured in select(inputfds): %s\n", strerror(errno)); |
736 exitProg = TRUE; | 746 exitProg = TRUE; |
737 } else if (FD_ISSET(0, &tmpfds)) { | 747 } else if (FD_ISSET(0, &tmpfds)) { |
738 gotBuf = read(0, tmpBuf, sizeof(tmpBuf)); | 748 gotBuf = read(0, tmpBuf, sizeof(tmpBuf)); |
739 | 749 |
740 if (gotBuf < 0) { | 750 if (gotBuf < 0) { |
741 THERR("Error in reading stdio.\n"); | 751 printMsg("Error in reading stdio.\n"); |
742 exitProg = TRUE; | 752 exitProg = TRUE; |
743 } else { | 753 } else { |
744 /* Call the user input handler */ | 754 /* Call the user input handler */ |
745 result = handleInput(tmpSocket, tmpBuf, gotBuf); | 755 result = handleInput(tmpSocket, tmpBuf, gotBuf); |
746 if (result < 0) { | 756 if (result < 0) { |
747 THERR("Fatal error handling user input: %s\n", | 757 printMsg("Fatal error handling user input: %s\n", |
748 tmpBuf); | 758 tmpBuf); |
749 exitProg = TRUE; | 759 exitProg = TRUE; |
750 } | 760 } |
751 } | 761 } |
752 } | 762 } |