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