intirain 4 лет назад
Родитель
Сommit
e1fc7cb1c6
13 измененных файлов с 149 добавлено и 38 удалено
  1. 1 1
      Makefile
  2. 0 16
      cmd.c
  3. 20 3
      hooks.c
  4. 6 0
      include/vcc/hooks.h
  5. 1 1
      include/vcc/pretty.h
  6. 2 2
      include/vcc/version.h
  7. 2 8
      main.c
  8. 2 2
      plugins.c
  9. 1 1
      plugins/Makefile
  10. 14 0
      plugins/cqd/Makefile
  11. 95 0
      plugins/cqd/cqd.c
  12. 3 2
      plugins/hist/hist.c
  13. 2 2
      pretty.c

+ 1 - 1
Makefile

@@ -1,5 +1,5 @@
 VCC_MAJOR 		= 1
 VCC_MAJOR 		= 1
-VCC_MINOR 		= 30
+VCC_MINOR 		= 31
 VCC_PATCH 		= 0
 VCC_PATCH 		= 0
 VCC_STR 		= -rc 
 VCC_STR 		= -rc 
 
 

+ 0 - 16
cmd.c

@@ -533,22 +533,6 @@ int do_cmd_incr_bh(struct vcc_request *req) {
 	return 0;
 	return 0;
 }
 }
 
 
-
-int do_cmd_cqd(int argc, char **argv) {
-	(void) argv;
-
-	if (argc != 1) {
-		fprintf(stderr, _("usage: -cqd\n"));
-
-		return 1;
-	}
-
-	send_msg("CQD", usrname);
-
-	return 0;
-}
-
-
 int do_cmd_ban(int argc, char **argv) {
 int do_cmd_ban(int argc, char **argv) {
 	char 			*people_name, people_arr[USRNAME_SIZE];
 	char 			*people_name, people_arr[USRNAME_SIZE];
 	struct banlist 		*i;
 	struct banlist 		*i;

+ 20 - 3
hooks.c

@@ -84,7 +84,8 @@ int do_hook(int type, char *s, char *usrname, int sid) {
 	struct klist_node 	*n;
 	struct klist_node 	*n;
 	struct hook 		*hook;
 	struct hook 		*hook;
 	char 			*p;
 	char 			*p;
-	int 			i;
+	/* times of malloc(). we won't free() when it's 0 */
+	int 			i = 0;
 
 
 	hi.type = type;
 	hi.type = type;
 	hi.msg = s;
 	hi.msg = s;
@@ -95,9 +96,23 @@ int do_hook(int type, char *s, char *usrname, int sid) {
 		hook = hook_of(n);
 		hook = hook_of(n);
 
 
 		if (hook->type == type) {
 		if (hook->type == type) {
-			/* so the order is quiet important */
-			if (unlikely(!(p = hook->hook(&hi)))) 
+			p = hook->hook(&hi);
+
+			if (p == HOOK_FAILED) {
 				fprintf(stderr, "*** do_hook: one of hooks returned NULL. abandon... \n");
 				fprintf(stderr, "*** do_hook: one of hooks returned NULL. abandon... \n");
+				return 1;
+			}
+
+			else if (p == HOOK_HANDLED) {
+				*s = 0;
+
+				return 0;
+			}
+
+			else if (p == HOOK_IGNORE) 
+				continue;
+
+			/* so the order is quiet important */
 
 
 			if (i) 
 			if (i) 
 				free(hi.msg);
 				free(hi.msg);
@@ -107,6 +122,8 @@ int do_hook(int type, char *s, char *usrname, int sid) {
 		}
 		}
 	}
 	}
 
 
+	strcpy(s, hi.msg);
+
 	return 0;
 	return 0;
 }
 }
 
 

+ 6 - 0
include/vcc/hooks.h

@@ -48,6 +48,12 @@ struct hook {
 };
 };
 
 
 #define hook_of(n) 		container_of(n, struct hook, node)
 #define hook_of(n) 		container_of(n, struct hook, node)
+/* failed, abore */
+#define HOOK_FAILED 		NULL
+/* ignored, continue */
+#define HOOK_IGNORE 		((char *) 1)
+/* has handled, so break hook calling */
+#define HOOK_HANDLED 		((char *) 2)
 
 
 struct hook *register_hook(int type, hookfn_t hook);
 struct hook *register_hook(int type, hookfn_t hook);
 int do_hook_recv(char *s, char *usrname, int sid);
 int do_hook_recv(char *s, char *usrname, int sid);

+ 1 - 1
include/vcc/pretty.h

@@ -91,7 +91,7 @@ int pretty_new_msg(char *usrname, char *msg, int sid, int flags);
 int pretty_prompt(char *usrname);
 int pretty_prompt(char *usrname);
 int pretty_unknown_cmd(char *cmd);
 int pretty_unknown_cmd(char *cmd);
 int pretty_cmd_help(char *cmd, char *msg);
 int pretty_cmd_help(char *cmd, char *msg);
-int pretty_cqd(char *usrname);
+int pretty_cqd(char *usrname, char *msg);
 
 
 
 
 #endif
 #endif

+ 2 - 2
include/vcc/version.h

@@ -1,2 +1,2 @@
-#define VCC_VERSION "VCC 1.30.0 Sun Jun 12 09:10:00 PM CST 2022 root"
-#define VCC_SEMVER "vcc 1.30.0-rc "
+#define VCC_VERSION "VCC 1.31.0 Mon Jun 13 01:23:55 PM CST 2022 root"
+#define VCC_SEMVER "vcc 1.31.0-rc "

+ 2 - 8
main.c

@@ -257,19 +257,13 @@ void sigint_handler(int signr) {
 
 
 
 
 int new_msg(char *msg, char *name, int session, int flags) {
 int new_msg(char *msg, char *name, int session, int flags) {
-	if (!strcmp(msg, "CQD")) {
-		pretty_cqd(name);
-		pretty_prompt(usrname);
-
-		return 1;
-	}
-
 	if (unlikely(is_user_banned(name)))
 	if (unlikely(is_user_banned(name)))
 		return 1;
 		return 1;
 
 
 	do_hook_recv(msg, name, session);
 	do_hook_recv(msg, name, session);
 
 
-	pretty_new_msg(name, msg, session, flags);
+	if (*msg) 
+		pretty_new_msg(name, msg, session, flags);
 	pretty_prompt(usrname);
 	pretty_prompt(usrname);
 
 
 	return 0;
 	return 0;

+ 2 - 2
plugins.c

@@ -43,10 +43,10 @@
 #include <vcc/interfaces.h>
 #include <vcc/interfaces.h>
 
 
 
 
-extern struct plugin plg_hello, plg_robot, plg_m4, plg_script, plg_hist;
+extern struct plugin plg_hello, plg_robot, plg_m4, plg_script, plg_hist, plg_cqd;
 
 
 struct plugin *plugins[] = {
 struct plugin *plugins[] = {
-	&plg_hello, &plg_robot, &plg_m4, &plg_script, &plg_hist, 
+	&plg_hello, &plg_robot, &plg_m4, &plg_script, &plg_hist, &plg_cqd, 
 	(struct plugin *) NULL
 	(struct plugin *) NULL
 };
 };
 
 

+ 1 - 1
plugins/Makefile

@@ -1,4 +1,4 @@
-plugins 	= hello robot m4 script hist 
+plugins 	= hello robot m4 script hist cqd 
 target 		= libplugins.a 
 target 		= libplugins.a 
 
 
 all: 
 all: 

+ 14 - 0
plugins/cqd/Makefile

@@ -0,0 +1,14 @@
+objs 	= cqd.o 
+target 	= ../plg_cqd.o 
+CFLAGS 	= -std=gnu11 -Wall -Wextra -Werror -I../../include -g 
+
+$(target): $(objs)
+	@cp $(objs) $(target)
+
+%.o: %.c
+	@$(CC) -c $*.c $(CFLAGS)
+	@echo -e "CC\t\t\t$*.c"
+
+.PHONY clean: 
+	rm -f $(target) $(objs)
+

+ 95 - 0
plugins/cqd/cqd.c

@@ -0,0 +1,95 @@
+/* vcc/plugins/cqd/cqd.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 <string.h>
+
+#include <vcc/vcc.h>
+#include <vcc/pretty.h>
+#include <vcc/plugin.h>
+#include <vcc/version.h>
+#include <vcc/interfaces.h>
+
+
+int do_cmd_cqd(int argc, char **argv);
+int cqd_init(void);
+
+struct cmdtable cqd_cmdtab[] = {
+	{ "-cqd", do_cmd_cqd, "Raise CQD. " }, 
+
+	{ NULL, NULL, NULL }
+};
+
+struct plugin plg_cqd = {
+	.state = 0, 
+	.name = "cqd", 
+	.descr = "Raise CQD. ", 
+	.init = cqd_init, 
+	.cmdtab = cqd_cmdtab
+};
+
+#define this_plg (&plg_cqd)
+
+
+int do_cmd_cqd(int argc, char **argv) {
+	char buf[MSG_SIZE];
+	char *p;
+
+	(void) argv;
+
+	plg_check_enabled();
+
+	switch (argc) {
+	case 1:
+		p = "CQD\n";
+		break;
+
+	case 2:
+		p = argv[1];
+		break;
+
+	default:
+		fprintf(stderr, _("usage: -cqd\n"));
+		return 1;
+	}
+
+	sprintf(buf, "-cqd#%s\n", p);
+	send_msg(buf, usrname);
+
+	return 0;
+}
+
+
+char *cqd_hook(struct hook_info *hi) {
+	if (strncmp(hi->msg, "-cqd#", 5)) 
+		return HOOK_IGNORE;
+
+	pretty_cqd(hi->usr, hi->msg + 5);
+	return HOOK_HANDLED;
+}
+
+
+int cqd_init(void) {
+	register_hook(HOOK_RECV, cqd_hook);
+	return 0;
+}
+
+

+ 3 - 2
plugins/hist/hist.c

@@ -44,7 +44,8 @@ int do_cmd_hist(int argc, char **argv);
 int hist_fd;
 int hist_fd;
 
 
 struct cmdtable hist_cmdtab[] = {
 struct cmdtable hist_cmdtab[] = {
-	{ "-hist", do_cmd_hist, "Dump history (from login to now). \n" }, 
+	{ "-hist", do_cmd_hist, "Dump history (from login to now). " }, 
+
 	{ NULL, NULL, NULL }
 	{ NULL, NULL, NULL }
 };
 };
 
 
@@ -78,7 +79,7 @@ char *hist_hook(struct hook_info *hi) {
 
 
 	dprintf(hist_fd, "[%ld] %s: %s", times(NULL), hi->usr, hi->msg);
 	dprintf(hist_fd, "[%ld] %s: %s", times(NULL), hi->usr, hi->msg);
 
 
-	return strdup(hi->msg);
+	return HOOK_IGNORE;
 }
 }
 
 
 int open_hist_file(void) {
 int open_hist_file(void) {

+ 2 - 2
pretty.c

@@ -295,7 +295,7 @@ int pretty_cmd_help(char *cmdname, char *descr) {
 }
 }
 
 
 
 
-int pretty_cqd(char *username) {
+int pretty_cqd(char *username, char *msg) {
 	printf("\n");
 	printf("\n");
 
 
 	setcolor(stdout, curr_cqd);
 	setcolor(stdout, curr_cqd);
@@ -303,7 +303,7 @@ int pretty_cqd(char *username) {
 
 
 	ansi_csi_ed(stdout);
 	ansi_csi_ed(stdout);
 
 
-	printf(_(" %s sent CQD. \n"), username);
+	printf(_(" %s sent %s"), username, msg);
 
 
 	return 0;
 	return 0;
 }
 }