cmd.c 8.5 KB

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