浏览代码

vcc: change the implementation of ban

3swordman 4 年之前
父节点
当前提交
a7cd2e17f7
共有 8 个文件被更改,包括 172 次插入 和 42 次删除
  1. 3 3
      Makefile
  2. 22 22
      cmd.c
  3. 54 0
      include/klist.h
  4. 2 2
      include/vcc/version.h
  5. 59 0
      klist.c
  6. 21 4
      main.c
  7. 0 8
      pretty.c
  8. 11 3
      vcc.c

+ 3 - 3
Makefile

@@ -1,14 +1,14 @@
 VCC_MAJOR 		= 1
 VCC_MINOR 		= 9
-VCC_PATCH 		= 2
+VCC_PATCH 		= 3
 VCC_STR 		= -rc 
 
 VCC_COMPILE_TIME 	= $(shell date)
 VCC_COMPILE_BY 		= $(shell whoami)
 VCC_SEMVER 		= vcc $(VCC_MAJOR).$(VCC_MINOR).$(VCC_PATCH)$(VCC_STR)
 
-srcs 			= main.c vcc.c pretty.c cmd.c crypt.c 
-objs 			= main.o vcc.o pretty.o cmd.o crypt.o 
+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)

+ 22 - 22
cmd.c

@@ -35,6 +35,7 @@
 #include <poll.h>
 #include <signal.h>
 
+#include <klist.h>
 #include <vcc/vcc.h>
 #include <vcc/version.h>
 #include <vcc/pretty.h>
@@ -63,7 +64,7 @@ int incr_score(char *username, int incr);
 int send_msg(char *msg, char *s);
 int encrypt(char *s);
 
-extern str_t banned_people[BAN_SIZE];
+extern struct klist_node *banned_people;
 extern char usrname[];
 extern int rate;
 extern int current_sid;
@@ -388,45 +389,44 @@ int do_cmd_cqd(char *s) {
 
 
 int do_cmd_ban(char *s) {
-	char 	*people_name;
-	int 	i;
+	char 			*people_name, people_arr[USRNAME_SIZE];
+	struct klist_node	*i;
 
 	(void) s;
 
-	people_name = malloc(USRNAME_SIZE + 1);
+	fprintf(stderr, "Enter the user to ban: ");
+	scanf("%s", people_arr);
 
-	printf("Enter the user to ban: ");
-	scanf("%s", people_name);
+	people_name = strdup(people_arr);
 
-	for (i = 0; i < BAN_SIZE; ++i) {
-		if (!banned_people[i]) {
-			banned_people[i] = people_name;	
-			return 0;
-		}
-		else if (!strcmp(people_name, banned_people[i])) {
-			printf("User %s is already banned", people_name);
+	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;
 		}
 	}
-	printf("Cannot ban the user");
+	i = malloc(sizeof(struct klist_node));
+	i->usrname = people_name;
+	klist_add(banned_people, i);
 	return 0;
 }
-
 int do_cmd_unban(char *s) {
-	char 	*people_name;
-	int 	i;
+	char 			*people_name;
+	struct klist_node 	*i;
 
 	(void) s;
 
 	people_name = malloc(USRNAME_SIZE + 1);
 
-	printf("Enter the user to unban: ");
+	fprintf(stderr, "Enter the user to unban: ");
 	scanf("%s", people_name);
 	
-	for (i = 0; i < BAN_SIZE; ++i) {
-		if (banned_people[i] && !strcmp(people_name, banned_people[i])) {
-			free(banned_people[i]);
-			banned_people[i] = NULL;
+	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;
 		}
 	}

+ 54 - 0
include/klist.h

@@ -0,0 +1,54 @@
+/* vcc/include/klist.h 
+ * 
+ * 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/>
+ */ 
+
+
+
+
+#ifndef __KLIST_H
+#define __KLIST_H
+
+#include <vcc/vcc.h>
+
+
+/* before, i think using a 'container_of' in things we did in kernel 
+ * is silly, but now i realize that, i'm the stupid one, there are 
+ * too many things in the structure. */
+
+struct klist_node {
+	char				*usrname;
+	struct klist_node 		*next, *prev;
+};
+
+
+
+void klist_init(struct klist_node *head);
+
+void klist_add(struct klist_node *head, struct klist_node *new);
+void klist_del(struct klist_node *head, struct klist_node *will_del);
+
+#define klist_empty(list) ((list)->next == (list))
+
+#define KLIST_NODE_INIT(__x) { \
+	.usrname = NULL, \
+	.next = __x, \
+	.prev = __x, \
+}
+
+
+#endif
+

+ 2 - 2
include/vcc/version.h

@@ -1,2 +1,2 @@
-#define VCC_VERSION "VCC 1.9.2 Thu 07 Apr 2022 10:43:28 AM CST ubuntu"
-#define VCC_SEMVER "vcc 1.9.2-rc "
+#define VCC_VERSION "VCC 1.9.3 Fri 08 Apr 2022 12:52:57 PM CST ubuntu"
+#define VCC_SEMVER "vcc 1.9.3-rc "

+ 59 - 0
klist.c

@@ -0,0 +1,59 @@
+/* vcc/klist.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 <sys/types.h>
+#include <stddef.h>
+#include <klist.h>
+
+
+
+void klist_init(struct klist_node *head) {
+	head->next = head;
+	head->prev = head;
+	head->usrname = NULL;
+
+	return;
+}
+
+
+void klist_add(struct klist_node *head, struct klist_node *new) {
+	/* head->prev: the last. */
+	new->prev = head->prev;
+	new->next = head;
+
+	/* head->prev->next: (was) the head */
+	head->prev->next = new;
+	head->prev = new;
+
+	return;
+}
+
+
+void klist_del(struct klist_node *head, struct klist_node *will_del) {
+	(void) head;
+
+	will_del->prev->next = will_del->next;
+	will_del->next->prev = will_del->prev;
+
+	return;
+}
+
+

+ 21 - 4
main.c

@@ -35,6 +35,7 @@
 #include <poll.h>
 #include <signal.h>
 
+#include <klist.h>
 #include <vcc/vcc.h>
 #include <vcc/version.h>
 #include <vcc/pretty.h>
@@ -46,6 +47,7 @@ int send_msg(const char *msg, const char *usrname);
 int read_msg(struct vcc_request *buf);
 int get_msgs(void);
 int vcc_login(char *, char *);
+int is_user_banned(char *);
 
 int show_algo_info(void);
 int crypt_init(void *key, void *iv);
@@ -63,13 +65,24 @@ int fd;
 int rate;
 
 char usrname[USRNAME_SIZE];
-str_t banned_people[BAN_SIZE];
+struct klist_node *banned_people;
 
 extern int current_sid;
 
+static char const *help_msg = {
+	"Usage: vcc [-up] [server]\n"
+	"Options: \n"
+	"  -u    --user    Set the username on the server. "
+	"If unspecified, you must input it interatively. \n"
+	"  -p    --port    Specified the port on the server. "
+	"Default port is 46. You must keep this as same "
+	"as the port on the server. \n"
+	"  [server]        Address of the server. "
+	"As default, server is 124.223.105.230. "
+};
 
 static int usage(void) {
-
+	fputs(help_msg, stderr);
 	return 1;
 }
 
@@ -169,8 +182,9 @@ int main(int argc, char **argv) {
 
 	crypt_init(key, iv);
 	
-	memset(banned_people, 0, BAN_SIZE * sizeof(void *));
-
+	banned_people = malloc(sizeof(struct klist_node));
+	klist_init(banned_people);
+	
 	fds[0].fd = 0;
 	fds[0].events = POLLIN;
 	fds[0].revents = 0;
@@ -269,6 +283,9 @@ int main(int argc, char **argv) {
 			if (strcmp(req->usrname, "system")) 
 				decrypt(msg);
 
+			if (is_user_banned(req->usrname))
+				continue;
+
 			pretty_new_msg(req->usrname, msg, ntohl(req->session));
 			pretty_prompt(usrname);
 		}

+ 0 - 8
pretty.c

@@ -28,7 +28,6 @@
 #include <vcc/version.h>
 #include <vcc/pretty.h>
 
-extern str_t banned_people[BAN_SIZE];
 extern int current_sid;
 extern char *usrname;
 
@@ -119,13 +118,6 @@ int self_level;
 int pretty_new_msg(char *usrname, char *buf, int sid) {
 	time_t time_now;
 	struct tm *tm_now;
-	int i;
-
-	for (i = 0;i < BAN_SIZE;++i) {
-		if (banned_people[i] && !strcmp(banned_people[i], usrname)) {
-			return 0;
-		}
-	}
 
 	ansi_csi_st(stdout, MODE_HIGHLIGHT, FG_BLACK, BG_BLACK);
 

+ 11 - 3
vcc.c

@@ -34,12 +34,13 @@
 #include <poll.h>
 #include <signal.h>
 
+#include <klist.h>
 #include <vcc/vcc.h>
 #include <vcc/version.h>
 
 #define clear_req(req) 	memset(req, 0, REQ_SIZE)
 
-
+extern struct klist_node *banned_people;
 extern int fd;
 
 int current_sid = 0;
@@ -50,6 +51,15 @@ 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;
@@ -300,5 +310,3 @@ int incr_score(char *name, int incr) {
 
 	return 0;
 }
-
-