/* 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
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int do_cmd_help(char *);
int do_cmd_quit(char *);
int do_cmd_ls(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 do_cmd_ban(char *);
int do_cmd_unban(char *);
int do_cmd_encry(char *);
int do_cmd_pls(char *);
int do_cmd_pins(char *);
int do_cmd_prem(char *);
int do_cmd_ml(char *);
int handle_command(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 send_msg_encrypted(char *msg, char *s);
int encrypt(char *s);
extern struct klist_node banned_people;
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);
int did_warn = 0;
struct cmdtable 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. " },
{ "-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. " },
{ "-ban", do_cmd_ban, "Don't receive more messages from somebody. " },
{ "-unban", do_cmd_unban, "Receive more messages from somebody. " },
{ "-encry", do_cmd_encry, "Send an encrypted message. " },
{ "-pls", do_cmd_pls, "List plugins installed (not inserted). " },
{ "-pins", do_cmd_pins, "Insert a plugin. " },
{ "-prem", do_cmd_prem, "Remove a plugin. " },
{ "-ml", do_cmd_ml, "Multi-line. " },
{ NULL, NULL, NULL }
};
#define clear_line() fprintf(stderr, "\033[2K\r")
#define __unused __attribute__((unused))
typedef int (* fptr_t) (char *);
fptr_t execute_in(struct cmdtable *tab, char *s, int len) {
struct cmdtable *t;
for (t = tab; t->cmd; t++)
if (!strncmp(t->cmd, s, len))
return t->do_cmd;
return NULL;
}
int do_cmd(char *s) {
struct plugin *plg;
char *p;
int len, i;
fptr_t f;
p = s;
for (len = 0; *p && !isspace(*p); len++)
p++;
if ((f = execute_in(cmdtab, s, len)))
return f(s);
for (i = 0; (plg = plugins[i]); i++)
if ((f = execute_in(plg->cmdtab, s, len)))
return f(s);
pretty_unknown_cmd(s);
return 0;
}
static int __help(struct cmdtable *cmdtab) {
struct cmdtable *t;
for (t = cmdtab; t->cmd; t++)
pretty_cmd_help(t->cmd, t->help);
return 0;
}
int do_cmd_help(char *s) {
struct plugin *p;
int i;
(void) s;
__help(cmdtab);
for (i = 0; (p = plugins[i]); i++)
__help(p->cmdtab);
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_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");
pretty_prompt(usrname);
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;
}
int do_cmd_ban(char *s) {
char *people_name, people_arr[USRNAME_SIZE];
struct klist_node *i;
(void) s;
fprintf(stderr, "Enter the user to ban: ");
scanf("%s", people_arr);
people_name = strdup(people_arr);
for (i = banned_people.next; i != &banned_people; i = i->next) {
if (!strcmp(people_name, i->usrname)) {
fprintf(stderr, "User %s is already banned", people_name);
free(people_name);
return 0;
}
}
i = malloc(sizeof(struct klist_node));
klist_init(i);
i->usrname = people_name;
klist_add(&banned_people, i);
return 0;
}
int do_cmd_unban(char *s) {
char *people_name;
struct klist_node *i;
(void) s;
people_name = malloc(USRNAME_SIZE + 1);
fprintf(stderr, "Enter the user to unban: ");
scanf("%s", people_name);
for (i = banned_people.next; i != &banned_people; i = i->next) {
if (!strcmp(people_name, i->usrname)) {
klist_del(&banned_people, i);
free(i->usrname);
free(i);
break;
}
}
free(people_name);
return 0;
}
static char const warn_msg[] = {
"warning: encrypting messages are not always usable. in usual, that man which \n"
"talks with you gets something f**king, in the starting, or even all the message \n"
"is some f**king guys. \n"
"do you want to continue? (y/N) "
};
static int show_warn_msg(void) {
char c;
fprintf(stderr, "%s", warn_msg);
for (;;) {
read(0, &c, 1);
if (c == 'y' || c == 'Y')
return 0;
else if (c == 'n' || c == 'N')
return 1;
printf("Please answer y / n\n");
}
return 1;
}
int do_cmd_encry(char *s) {
char msg[MSG_SIZE];
(void) s;
memset(msg, 0, MSG_SIZE);
if (!did_warn) {
did_warn = 1;
if (show_warn_msg())
return 0;
}
fprintf(stderr, "Message: ");
scanf("%s", msg);
encrypt(msg);
send_msg_encrypted(msg, usrname);
return 0;
}
int do_cmd_ml(char *s) {
char *p;
int i, size;
(void) s;
if (unlikely(!(p = malloc(MSG_SIZE)))) {
fprintf(stderr, "*** malloc() failed\n");
return 1;
}
printf("Type 'finish' at the starting of a line to finish. \n");
for (i = 0; i < (int) MSG_SIZE; i += size) {
size = read(0, p + i, 128);
if (!strncmp(p + i, "finish", 6))
break;
}
p[i] = 0;
handle_command(p);
free(p);
return 0;
}