cmd.c 6.0 KB

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