cmd.c 9.0 KB

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