cmd.c 9.1 KB

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