cmd.c 8.4 KB

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