cmd.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* vcc/cmd.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 <ctype.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. int do_cmd_help(char *);
  36. int do_cmd_quit(char *);
  37. int do_cmd_ls(char *);
  38. int do_cmd_rate(char *);
  39. int do_cmd_newse(char *);
  40. int do_cmd_currs(char *);
  41. int do_cmd_swtch(char *);
  42. int do_cmd_lsse(char *);
  43. int do_cmd_uinfo(char *);
  44. int read_msg(struct vcc_request *);
  45. int vcc_create_session(char *name);
  46. int get_user_info(char *usrname);
  47. int do_cmd_ls_bh(struct vcc_request *);
  48. extern char usrname[];
  49. extern int rate;
  50. extern int current_sid;
  51. int get_users(void);
  52. int get_sessions(void);
  53. int join_session(int sid);
  54. struct cmdtable {
  55. char *cmd;
  56. int (* do_cmd) (char *);
  57. char *help;
  58. } cmdtab[] = {
  59. { "-help", do_cmd_help, "Show information about every message. " },
  60. { "-quit", do_cmd_quit, "Disconnect to server and exit vcc. " },
  61. { "-ls", do_cmd_ls, "List all users online now. " },
  62. { "-rate", do_cmd_rate, "Change message refresh rate. " },
  63. { "-newse", do_cmd_newse, "Create a new session. " },
  64. { "-currs", do_cmd_currs, "Get current session id. " },
  65. { "-swtch", do_cmd_swtch, "Switch to another session. " },
  66. { "-lsse", do_cmd_lsse, "List all sessions created. " },
  67. { "-uinfo", do_cmd_uinfo, "Get one's information. " },
  68. { NULL, NULL, NULL }
  69. };
  70. #define clear_line() fprintf(stderr, "\033[2K\r")
  71. #define __unused __attribute__((unused))
  72. int do_cmd(char *s) {
  73. struct cmdtable *t;
  74. char *p;
  75. int len;
  76. p = s;
  77. for (len = 0; !isspace(*p); len++)
  78. p++;
  79. for (t = cmdtab; t->cmd; t++)
  80. if (!strncmp(t->cmd, s, len))
  81. return t->do_cmd(s);
  82. pretty_unknown_cmd(s);
  83. return 0;
  84. }
  85. int do_cmd_help(char *s) {
  86. struct cmdtable *t;
  87. (void) s;
  88. for (t = cmdtab; t->cmd; t++)
  89. pretty_cmd_help(t->cmd, t->help);
  90. return 0;
  91. }
  92. int do_cmd_quit(char *s) {
  93. (void) s;
  94. printf("bye. \n");
  95. exit(0);
  96. return 0;
  97. }
  98. int do_cmd_ls(char *s) {
  99. (void) s;
  100. get_users();
  101. return 0;
  102. }
  103. int do_cmd_rate(char *s) {
  104. int n;
  105. (void) s;
  106. printf("Current rate: %d Hz\n", rate / 1000);
  107. fprintf(stderr, "New rate (0 < rate <= 100): ");
  108. scanf("%d", &n);
  109. if (n > 100) {
  110. fprintf(stderr, "*** too fast. \n"
  111. "0 < rate <= 100\n");
  112. return 1;
  113. }
  114. if (n <= 0) {
  115. fprintf(stderr, "*** too slow. \n"
  116. "0 < rate <= 100\n");
  117. return 1;
  118. }
  119. rate = 1000 / n;
  120. printf("New rate: %d Hz\n", n);
  121. return 0;
  122. }
  123. int do_cmd_ls_bh(struct vcc_request *req) {
  124. int n, i;
  125. n = ntohl(req->uid);
  126. clear_line();
  127. if (unlikely(ntohl(req->reqtype) != REQ_CTL_USRS)) {
  128. fprintf(stderr, "--- do_cmd_ls_bh: bad package. \n");
  129. return 1;
  130. }
  131. for (i = 0; i < n; i++)
  132. printf("%s\n", req->msg + i * USRNAME_SIZE);
  133. pretty_prompt(usrname);
  134. return 0;
  135. }
  136. int do_cmd_lsse(char *s) {
  137. (void) s;
  138. get_sessions();
  139. return 0;
  140. }
  141. int do_cmd_lsse_bh(struct vcc_request *req) {
  142. int n, i;
  143. n = ntohl(req->uid);
  144. clear_line();
  145. if (unlikely(ntohl(req->reqtype) != REQ_CTL_SESS)) {
  146. fprintf(stderr, "--- do_cmd_ls_bh: bad package. \n");
  147. return 1;
  148. }
  149. for (i = 0; i < n; i++)
  150. printf("%s\n", req->msg + i * USRNAME_SIZE);
  151. pretty_prompt(usrname);
  152. return 0;
  153. }
  154. int do_cmd_newse(char *s) {
  155. char sname[USRNAME_SIZE];
  156. (void) s;
  157. fprintf(stderr, "Name of new session: ");
  158. scanf("%s", sname);
  159. vcc_create_session(sname);
  160. printf("Session create request sent. \n");
  161. return 0;
  162. }
  163. int do_cmd_currs(char *s) {
  164. (void) s;
  165. printf("Current session id: %d\n", current_sid);
  166. return 0;
  167. }
  168. int do_cmd_swtch(char *s) {
  169. int sid;
  170. (void) s;
  171. printf("Old session id: %d\n", current_sid);
  172. fprintf(stderr, "New session id: ");
  173. scanf("%d", &sid);
  174. current_sid = sid;
  175. join_session(current_sid);
  176. return 0;
  177. }
  178. int do_cmd_uinfo(char *s) {
  179. char usrname[USRNAME_SIZE];
  180. (void) s;
  181. fprintf(stderr, "username: ");
  182. scanf("%s", usrname);
  183. get_user_info(usrname);
  184. return 0;
  185. }
  186. struct user {
  187. int score;
  188. int level;
  189. char usrname[USRNAME_SIZE];
  190. /* it's blank */
  191. char passwd[PASSWD_SIZE];
  192. };
  193. int do_cmd_uinfo_bh(struct vcc_request *req) {
  194. struct user *u = (struct user *) req->msg;
  195. clear_line();
  196. if (ntohl(req->uid) == -1u) {
  197. printf("user not found. \n");
  198. return 1;
  199. }
  200. printf("%s's info: \n", u->usrname);
  201. printf("\tscore\tlevel\n");
  202. printf("\t%d\t%d\n", ntohl(u->score), ntohl(u->level));
  203. pretty_prompt(usrname);
  204. return 0;
  205. }