main.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* vcc/main.c
  2. *
  3. * This file is part of vcc.
  4. *
  5. * vcc is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * vcc is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with vcc. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22. #include <errno.h>
  23. #include <string.h>
  24. #include <locale.h>
  25. #include <sys/stat.h>
  26. #include <sys/types.h>
  27. #include <sys/socket.h>
  28. #include <netinet/in.h>
  29. #include <arpa/inet.h>
  30. #include <poll.h>
  31. #include <signal.h>
  32. #include <vcc/vcc.h>
  33. #include <vcc/version.h>
  34. #include <vcc/pretty.h>
  35. #define DEFAULT_RATE 1000
  36. int send_msg(const char *msg, const char *usrname);
  37. int read_msg(struct vcc_request *buf);
  38. int get_msgs(void);
  39. int vcc_login(char *, char *);
  40. int do_cmd(char *s);
  41. int do_bh(struct vcc_request *);
  42. int init_socket(const char *server_addr);
  43. int fd;
  44. int rate;
  45. char usrname[USRNAME_SIZE];
  46. static int usage(void) {
  47. fprintf(stderr, "Usage: vcc [server]\n");
  48. fprintf(stderr, "As default, server is 124.223.105.230. \n");
  49. return 1;
  50. }
  51. static int login(void) {
  52. char *passwd;
  53. fprintf(stderr, "login as: ");
  54. scanf("%s", usrname);
  55. passwd = getpass("password: ");
  56. vcc_login(usrname, passwd);
  57. return 0;
  58. }
  59. int main(int argc, const char **argv) {
  60. struct pollfd fds[2];
  61. struct vcc_request *req;
  62. char *buf, *msg;
  63. int n, size;
  64. #ifdef _WIN32
  65. setlocale(LC_ALL, ".UTF-8");
  66. #else
  67. setlocale(LC_ALL, "C.UTF-8");
  68. #endif
  69. if (unlikely(argc != 1 && argc != 2))
  70. return usage();
  71. printf(VCC_VERSION "\n");
  72. init_socket(argv[1] ? argv[1] : "124.223.105.230");
  73. printf("Connected to server. \n");
  74. login();
  75. fds[0].fd = 0;
  76. fds[0].events = POLLIN;
  77. fds[0].revents = 0;
  78. fds[1].fd = fd;
  79. fds[1].events = POLLIN;
  80. fds[1].revents = 0;
  81. rate = DEFAULT_RATE;
  82. if (unlikely(!(buf = malloc(MSG_SIZE)))) {
  83. fprintf(stderr, "*** malloc() failed\n");
  84. return 1;
  85. }
  86. if (unlikely(!(req = malloc(REQ_SIZE)))) {
  87. fprintf(stderr, "*** malloc() failed\n");
  88. return 1;
  89. }
  90. for (;;) {
  91. n = poll(fds, 2, rate);
  92. if (n == -1) {
  93. if (likely(errno == EINTR))
  94. continue;
  95. perror("poll()");
  96. break;
  97. }
  98. if (!n) {
  99. /* 1 sec timed out */
  100. get_msgs();
  101. continue;
  102. }
  103. if (fds[0].revents & POLLIN) {
  104. /* read keyboard */
  105. memset(buf, 0, MSG_SIZE);
  106. if (read(0, buf, MSG_SIZE) < 0) {
  107. perror("read()");
  108. continue;
  109. }
  110. if (*buf == '-')
  111. do_cmd(buf);
  112. else if (*buf != '\n') {
  113. send_msg(buf, usrname);
  114. pretty_new_msg(usrname, buf);
  115. }
  116. pretty_prompt(usrname);
  117. }
  118. if (fds[1].revents & POLLIN) {
  119. size = read(fd, req, REQ_SIZE);
  120. if (unlikely(!size)) {
  121. fprintf(stderr, "Server closed. \n");
  122. break;
  123. }
  124. if (ntohl(req->reqtype) != REQ_MSG_NEW) {
  125. do_bh(req);
  126. continue;
  127. }
  128. msg = req->msg;
  129. if (!*msg || *(unsigned char *) msg == 0xffu)
  130. continue;
  131. pretty_new_msg(req->usrname, msg);
  132. pretty_prompt(usrname);
  133. }
  134. }
  135. printf("bye. \n");
  136. return 0;
  137. }