cmd.c 5.5 KB

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