| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 |
- /* vcc/cmd.c
- *
- * This file is part of vcc.
- *
- * vcc is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * vcc is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with vcc. If not, see <https://www.gnu.org/licenses/>
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <string.h>
- #include <ctype.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <poll.h>
- #include <signal.h>
- #include <vcc/vcc.h>
- #include <vcc/version.h>
- #include <vcc/pretty.h>
- int do_cmd_help(char *);
- int do_cmd_quit(char *);
- int do_cmd_ls(char *);
- int do_cmd_rate(char *);
- int do_cmd_newse(char *);
- int do_cmd_currs(char *);
- int do_cmd_swtch(char *);
- int do_cmd_lsse(char *);
- int do_cmd_uinfo(char *);
- int do_cmd_lself(char *);
- int do_cmd_incr(char *);
- int do_cmd_cqd(char *);
- int read_msg(struct vcc_request *);
- int vcc_create_session(char *name);
- int get_user_info(char *usrname);
- int incr_score(char *username, int incr);
- int send_msg(char *msg, char *s);
- int encrypt(char *s);
- extern char usrname[];
- extern int rate;
- extern int current_sid;
- extern int self_level;
- int get_users(void);
- int get_sessions(void);
- int join_session(int sid);
- struct cmdtable {
- char *cmd;
- int (* do_cmd) (char *);
- char *help;
- } cmdtab[] = {
- { "-help", do_cmd_help, "Show information about every message. " },
- { "-quit", do_cmd_quit, "Disconnect to server and exit vcc. " },
- { "-ls", do_cmd_ls, "List all users online now. " },
- { "-rate", do_cmd_rate, "Change message refresh rate. " },
- { "-newse", do_cmd_newse, "Create a new session. " },
- { "-currs", do_cmd_currs, "Get current session id. " },
- { "-swtch", do_cmd_swtch, "Switch to another session. " },
- { "-lsse", do_cmd_lsse, "List all sessions created. " },
- { "-uinfo", do_cmd_uinfo, "Get one's information. " },
- { "-lself", do_cmd_lself, "Reload information of myself. " },
- { "-incr", do_cmd_incr, "Increase one's score (root required). " },
- { "-cqd", do_cmd_cqd, "Raise CQD. " },
- { NULL, NULL, NULL }
- };
- #define clear_line() fprintf(stderr, "\033[2K\r")
- #define __unused __attribute__((unused))
- int do_cmd(char *s) {
- struct cmdtable *t;
- char *p;
- int len;
- p = s;
- for (len = 0; !isspace(*p); len++)
- p++;
- for (t = cmdtab; t->cmd; t++)
- if (!strncmp(t->cmd, s, len))
- return t->do_cmd(s);
-
- pretty_unknown_cmd(s);
- return 0;
- }
- int do_cmd_help(char *s) {
- struct cmdtable *t;
- (void) s;
- for (t = cmdtab; t->cmd; t++)
- pretty_cmd_help(t->cmd, t->help);
-
- return 0;
- }
- int do_cmd_quit(char *s) {
- (void) s;
- printf("bye. \n");
- exit(0);
- return 0;
- }
- int do_cmd_ls(char *s) {
- (void) s;
- get_users();
- return 0;
- }
- int do_cmd_rate(char *s) {
- int n;
- (void) s;
- printf("Current rate: %d Hz\n", rate / 1000);
- fprintf(stderr, "New rate (0 < rate <= 100): ");
- scanf("%d", &n);
- if (n > 100) {
- fprintf(stderr, "*** too fast. \n"
- "0 < rate <= 100\n");
- return 1;
- }
- if (n <= 0) {
- fprintf(stderr, "*** too slow. \n"
- "0 < rate <= 100\n");
- return 1;
- }
- rate = 1000 / n;
- printf("New rate: %d Hz\n", n);
- return 0;
- }
- int do_cmd_ls_bh(struct vcc_request *req) {
- int n, i;
- n = ntohl(req->uid);
- clear_line();
- if (unlikely(ntohl(req->reqtype) != REQ_CTL_USRS)) {
- fprintf(stderr, "--- do_cmd_ls_bh: bad package. \n");
- return 1;
- }
- for (i = 0; i < n; i++)
- printf("%s\n", req->msg + i * USRNAME_SIZE);
- pretty_prompt(usrname);
- return 0;
- }
- int do_cmd_lsse(char *s) {
- (void) s;
- get_sessions();
- return 0;
- }
- int do_cmd_lsse_bh(struct vcc_request *req) {
- int n, i;
- n = ntohl(req->uid);
- clear_line();
- if (unlikely(ntohl(req->reqtype) != REQ_CTL_SESS)) {
- fprintf(stderr, "--- do_cmd_ls_bh: bad package. \n");
- return 1;
- }
- for (i = 0; i < n; i++)
- printf("%s\n", req->msg + i * USRNAME_SIZE);
- pretty_prompt(usrname);
- return 0;
- }
- int do_cmd_newse(char *s) {
- char sname[USRNAME_SIZE];
- (void) s;
- fprintf(stderr, "Name of new session: ");
- scanf("%s", sname);
- vcc_create_session(sname);
- printf("Session create request sent. \n");
- return 0;
- }
- int do_cmd_currs(char *s) {
- (void) s;
- printf("Current session id: %d\n", current_sid);
- return 0;
- }
- int do_cmd_swtch(char *s) {
- int sid;
- (void) s;
- printf("Old session id: %d\n", current_sid);
- fprintf(stderr, "New session id: ");
- scanf("%d", &sid);
- current_sid = sid;
- join_session(current_sid);
- return 0;
- }
- int lself = 0;
- int do_cmd_uinfo(char *s) {
- char name[USRNAME_SIZE];
- if (!lself) {
- (void) s;
- fprintf(stderr, "username: ");
- scanf("%s", name);
- get_user_info(name);
- }
- else
- /* s is the usrname of myself */
- get_user_info(s);
- return 0;
- }
- struct user {
- int score;
- int level;
- char usrname[USRNAME_SIZE];
- /* it's blank */
- char passwd[PASSWD_SIZE];
- };
- int do_cmd_uinfo_bh(struct vcc_request *req) {
- struct user *u = (struct user *) req->msg;
-
- clear_line();
- if (ntohl(req->uid) == -1u) {
- printf("user not found. \n");
- return 1;
- }
- if (lself) {
- lself = 0;
- self_level = ntohl(u->level);
- printf("Level of yourself: %d\n", self_level);
- }
- else {
- printf("%s's info: \n", u->usrname);
- printf("\tscore\tlevel\n");
- printf("\t%5d\t%d\n", ntohl(u->score), ntohl(u->level));
- }
- pretty_prompt(usrname);
-
- return 0;
- }
- int do_cmd_lself(char *s) {
- (void) s;
- lself = 1;
- do_cmd_uinfo(usrname);
-
- return 0;
- }
- int do_cmd_incr(char *s) {
- char name[USRNAME_SIZE];
- int incr;
- (void) s;
- fprintf(stderr, "username: ");
- scanf("%s", name);
- fprintf(stderr, "increment: ");
- scanf("%d", &incr);
- incr_score(name, incr);
- return 0;
- }
- int do_cmd_incr_bh(struct vcc_request *req) {
- clear_line();
- if (!ntohl(req->uid))
- printf("Sucessfully. \n");
- else
- printf("*** failed. \n");
- pretty_prompt(usrname);
- return 0;
- }
- int do_cmd_cqd(char *s) {
- (void) s;
- send_msg("CQD", usrname);
- return 0;
- }
|