cmd.c 7.4 KB

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