瀏覽代碼

vcc: use libreadline, and add a suprise message when trying to encrypt

intirain 4 年之前
父節點
當前提交
2dc28c0243
共有 8 個文件被更改,包括 138 次插入 和 61 次删除
  1. 3 3
      Makefile
  2. 59 2
      cmd.c
  3. 8 0
      crypt.c
  4. 5 3
      include/vcc/vcc.h
  5. 2 2
      include/vcc/version.h
  6. 28 38
      main.c
  7. 8 2
      pretty.c
  8. 25 11
      vcc.c

+ 3 - 3
Makefile

@@ -1,6 +1,6 @@
 VCC_MAJOR 		= 1
-VCC_MINOR 		= 9
-VCC_PATCH 		= 3
+VCC_MINOR 		= 10
+VCC_PATCH 		= 0
 VCC_STR 		= -rc 
 
 VCC_COMPILE_TIME 	= $(shell date)
@@ -11,7 +11,7 @@ srcs 			= main.c vcc.c pretty.c cmd.c crypt.c klist.c
 objs 			= main.o vcc.o pretty.o cmd.o crypt.o klist.o
 target 			= vcc 
 libgcrypt_cargs 	= $(shell libgcrypt-config --libs)
-CFLAGS 			= -std=gnu11 -Wall -Wextra -Werror -Iinclude -g $(libgcrypt_cargs)
+CFLAGS 			= -std=gnu11 -Wall -Wextra -Werror -Iinclude -g $(libgcrypt_cargs) -lreadline -lncurses 
 MAKEFLAGS 		= --no-print-directory
 
 VCC_VERSION 		= VCC $(VCC_MAJOR).$(VCC_MINOR).$(VCC_PATCH) \

+ 59 - 2
cmd.c

@@ -56,12 +56,14 @@ 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 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;
@@ -74,6 +76,8 @@ int get_users(void);
 int get_sessions(void);
 int join_session(int sid);
 
+int did_warn = 0;
+
 struct cmdtable {
 	char 		*cmd;
 	int 		(* do_cmd) (char *);
@@ -92,7 +96,9 @@ struct cmdtable {
 	{ "-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. " }, 
+	{ "-unban", do_cmd_unban, "Receive more messages from somebody. " },
+	{ "-encry", do_cmd_encry, "Send an encrypted message. " }, 
+
 	{ NULL, NULL, NULL }
 };
 
@@ -107,7 +113,7 @@ int do_cmd(char *s) {
 
 	p = s;
 
-	for (len = 0; !isspace(*p); len++) 
+	for (len = 0; *p && !isspace(*p); len++) 
 		p++;
 	for (t = cmdtab; t->cmd; t++) 
 		if (!strncmp(t->cmd, s, len)) 
@@ -412,6 +418,7 @@ int do_cmd_ban(char *s) {
 	klist_add(banned_people, i);
 	return 0;
 }
+
 int do_cmd_unban(char *s) {
 	char 			*people_name;
 	struct klist_node 	*i;
@@ -434,3 +441,53 @@ int do_cmd_unban(char *s) {
 	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) {
+	int c;
+
+	for (;;) {
+		fprintf(stderr, warn_msg);
+		scanf("%d", &c);
+
+		if (c == 'y' || c == 'Y') 
+			return 0;
+
+		else if (c == 'n' || c == 'N') 
+			return 1;
+
+		printf("Please answer y / n\n");
+	}
+
+	return 0;
+}
+
+
+int do_cmd_encry(char *s) {
+	char msg[MSG_SIZE];
+
+	(void) s;
+
+	memset(msg, 0, MSG_SIZE);
+
+	if (!did_warn) 
+		if (show_warn_msg()) 
+			return 0;
+
+	fprintf(stderr, "Message: ");
+	scanf("%s", msg);
+
+	encrypt(msg);
+	send_msg_encrypted(msg, usrname);
+
+	return 0;
+}
+

+ 8 - 0
crypt.c

@@ -30,13 +30,21 @@
 #include <vcc/pretty.h>
 
 
+/* maybe because of that i'm not good at crypting (in fact, i even don't 
+ * know eular etc. ), i think the libgcrypt is the most ****ingest library 
+ * that i've used. it's a great gnu library, but the document is **too** 
+ * ****ing ****ing, that you can't know what you're doing. */
+
+
 /* i tried aes256 but sometimes i get something garbled. then i 
  * tried des, that traditional alghrithm created by ibm and 
  * it really works. */
+
 #define CIPHER_ALGO 	GCRY_CIPHER_DES
 #define show_err(err) 	fprintf(stderr, "error: %s: %s\n", \
 		gcry_strsource(err), gcry_strerror(err))
 
+
 int 			keysize, blksize;
 int 			msgsize;
 

+ 5 - 3
include/vcc/vcc.h

@@ -63,9 +63,10 @@
 
 /* we'll not warn you anyway.  */
 #define PASSWD_SIZE 	64
-#define MSG_SIZE 	(REQ_SIZE - 4 * sizeof(int) - USRNAME_SIZE)
-#define BAN_SIZE	16
-typedef char *str_t;
+#define MSG_SIZE 	(REQ_SIZE - 5 * sizeof(int) - USRNAME_SIZE)
+
+#define FLAG_ENCRYPTED 	1
+
 struct vcc_request {
 	int 		magic;
 	int 		reqtype;
@@ -73,6 +74,7 @@ struct vcc_request {
 	/* to make server easy and fast */
 	int 		uid;
 	int 		session;
+	int 		flags;
 
 	char 		usrname[USRNAME_SIZE];
 	char 		msg[MSG_SIZE];

+ 2 - 2
include/vcc/version.h

@@ -1,2 +1,2 @@
-#define VCC_VERSION "VCC 1.9.3 Fri 08 Apr 2022 01:07:01 PM CST ubuntu"
-#define VCC_SEMVER "vcc 1.9.3-rc "
+#define VCC_VERSION "VCC 1.10.0 Fri Apr  8 20:41:59 CST 2022 root"
+#define VCC_SEMVER "vcc 1.10.0-rc "

+ 28 - 38
main.c

@@ -35,6 +35,9 @@
 #include <poll.h>
 #include <signal.h>
 
+#include <readline/readline.h>
+#include <readline/history.h>
+
 #include <klist.h>
 #include <vcc/vcc.h>
 #include <vcc/version.h>
@@ -65,7 +68,7 @@ int fd;
 int rate;
 
 char usrname[USRNAME_SIZE];
-struct klist_node *banned_people;
+struct klist_node banned_people = KLIST_NODE_INIT(&banned_people);
 
 extern int current_sid;
 
@@ -145,11 +148,27 @@ int parse_args(char **argv, int *port, char **ip) {
 	return got_usrname;
 }
 
+int do_handle_command(char *buf) {
+	add_history(buf);
+
+	if (*buf == '-') 
+		do_cmd(buf);
+
+	else if (*buf != '\n') {
+		send_msg(buf, usrname);
+		pretty_new_msg(usrname, buf, current_sid);
+	}
+
+	pretty_prompt(usrname);
+
+	return 0;
+}
+
 
 int main(int argc, char **argv) {
 	struct pollfd 		fds[2];
 	struct vcc_request 	*req;
-	char 			*buf, *msg, *ip = "124.223.105.230", *t;
+	char 			*buf, *msg, *ip = "124.223.105.230";
 	char 			key[64], iv[64];
 	int 			n, size, port = VCC_PORT, got_usrname;
 
@@ -182,9 +201,6 @@ int main(int argc, char **argv) {
 
 	crypt_init(key, iv);
 	
-	banned_people = malloc(sizeof(struct klist_node));
-	klist_init(banned_people);
-	
 	fds[0].fd = 0;
 	fds[0].events = POLLIN;
 	fds[0].revents = 0;
@@ -195,7 +211,7 @@ int main(int argc, char **argv) {
 
 	rate = DEFAULT_RATE;
 
-	if (unlikely(!(buf = malloc(MSG_SIZE))) || unlikely(!(t = malloc(MSG_SIZE)))) {
+	if (unlikely(!(buf = malloc(MSG_SIZE)))) {
 		fprintf(stderr, "*** malloc() failed\n");
 
 		return 1;
@@ -227,34 +243,8 @@ int main(int argc, char **argv) {
 			continue;
 		}
 
-		if (fds[0].revents & POLLIN) {
-			/* read keyboard */
-
-			memset(buf, 0, MSG_SIZE);
-
-			if (read(0, buf, MSG_SIZE) < 0) {
-				perror("read()");
-
-				continue;
-			}
-
-			if (*buf == '-') 
-				do_cmd(buf);
-
-			else if (*buf != '\n') {
-				memcpy(t, buf, MSG_SIZE);
-
-				if (unlikely(encrypt(buf))) 
-					continue;
-
-				send_msg(buf, usrname);
-				decrypt(buf);
-
-				pretty_new_msg(usrname, t, current_sid);
-			}
-
-			pretty_prompt(usrname);
-		}
+		if (fds[0].revents & POLLIN) 
+			rl_callback_read_char();
 
 		if (fds[1].revents & POLLIN) {
 			size = read(fd, req, REQ_SIZE);
@@ -280,12 +270,12 @@ int main(int argc, char **argv) {
 				continue;
 			}
 
-			if (strcmp(req->usrname, "system")) 
-				decrypt(msg);
-
-			if (is_user_banned(req->usrname))
+			if (unlikely(is_user_banned(req->usrname)))
 				continue;
 
+			if (ntohl(req->flags) & FLAG_ENCRYPTED) 
+				decrypt(msg);
+
 			pretty_new_msg(req->usrname, msg, ntohl(req->session));
 			pretty_prompt(usrname);
 		}

+ 8 - 2
pretty.c

@@ -23,6 +23,7 @@
 #include <string.h>
 #include <time.h>
 #include <sys/times.h>
+#include <readline/readline.h>
 
 #include <vcc/vcc.h>
 #include <vcc/version.h>
@@ -31,6 +32,9 @@
 extern int current_sid;
 extern char *usrname;
 
+/* main.c */
+void do_handle_command(char *);
+
 struct theme theme_def = {
 	.usrname = {
 		.fg = FG_LIGHTBLUE, 
@@ -146,12 +150,12 @@ int pretty_new_msg(char *usrname, char *buf, int sid) {
 	if (unlikely(!strcmp(usrname, "system"))) {
 		setcolor(stdout, curr_sysmsg);
 
-		printf("%s", buf);
+		printf("%s\n", buf);
 	}
 
 	else {
 		setcolor(stdout, curr_msg);
-		printf("%s", buf);
+		printf("%s\n", buf);
 	}
 
 	ansi_csi_ed(stdout);
@@ -174,6 +178,8 @@ int pretty_prompt(char *usrname) {
 	ansi_csi_ed(stderr);
 	fprintf(stderr, "$: ");
 
+	rl_callback_handler_install("", do_handle_command);
+
 	return 0;
 }
 

+ 25 - 11
vcc.c

@@ -40,7 +40,7 @@
 
 #define clear_req(req) 	memset(req, 0, REQ_SIZE)
 
-extern struct klist_node *banned_people;
+extern struct klist_node banned_people;
 extern int fd;
 
 int current_sid = 0;
@@ -51,15 +51,6 @@ int do_cmd_lsse_bh(struct vcc_request *);
 int do_cmd_uinfo_bh(struct vcc_request *);
 int do_cmd_incr_bh(struct vcc_request *);
 
-int is_user_banned(char *usrname) {
-	struct klist_node *item;
-	for (item = banned_people->next; item != banned_people; item = item->next) {
-		if (item->usrname && !strcmp(usrname, item->usrname)) {
-			return 1;
-		}
-	}
-	return 0;
-}
 
 int init_socket(const char *server_addr, int server_port) {
 	struct sockaddr_in addr, server;
@@ -105,7 +96,7 @@ int init_socket(const char *server_addr, int server_port) {
 
 
 
-int send_msg(const char *msg, const char *usrname) {
+int __send_msg(const char *msg, const char *usrname, int flags) {
 	struct vcc_request req;
 
 	clear_req(&req);
@@ -114,6 +105,7 @@ int send_msg(const char *msg, const char *usrname) {
 	req.reqtype = htonl(REQ_MSG_SEND);
 	req.uid = 0;
 	req.session = htonl(current_sid);
+	req.flags = htonl(flags);
        
 	strncpy(req.usrname, usrname, USRNAME_SIZE);
 	memcpy(req.msg, msg, MSG_SIZE);
@@ -122,6 +114,15 @@ int send_msg(const char *msg, const char *usrname) {
 	return 0;
 }
 
+extern inline int send_msg(const char *msg, const char *usrname) {
+	return __send_msg(msg, usrname, 0);
+}
+
+extern inline int send_msg_encrypted(const char *msg, const char *usrname) {
+	return __send_msg(msg, usrname, FLAG_ENCRYPTED);
+}
+
+
 
 int get_msgs(void) {
 	struct vcc_request req;
@@ -310,3 +311,16 @@ int incr_score(char *name, int incr) {
 
 	return 0;
 }
+
+
+int is_user_banned(char *usrname) {
+	struct klist_node *item;
+
+	for (item = banned_people.next; item != &banned_people; item = item->next) {
+		if (item->usrname && !strcmp(usrname, item->usrname)) {
+			return 1;
+		}
+	}
+	return 0;
+}
+