cmd.c 6.2 KB

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