Bladeren bron

vcc: new hooks framework

intirain 4 jaren geleden
bovenliggende
commit
32df53b4d8
10 gewijzigde bestanden met toevoegingen van 241 en 94 verwijderingen
  1. 3 3
      Makefile
  2. 122 0
      hooks.c
  3. 57 0
      include/vcc/hooks.h
  4. 2 0
      include/vcc/interfaces.h
  5. 3 13
      include/vcc/plugin.h
  6. 2 2
      include/vcc/version.h
  7. 2 14
      main.c
  8. 0 45
      plugins.c
  9. 21 12
      plugins/hist/hist.c
  10. 29 5
      plugins/m4/m4.c

+ 3 - 3
Makefile

@@ -1,5 +1,5 @@
 VCC_MAJOR 		= 1
-VCC_MINOR 		= 27
+VCC_MINOR 		= 28
 VCC_PATCH 		= 0
 VCC_STR 		= -rc 
 
@@ -13,8 +13,8 @@ VCC_SEMVER 		= vcc $(VCC_MAJOR).$(VCC_MINOR).$(VCC_PATCH)$(VCC_STR)
 
 macro_srcs 		= gnr_theme_macros.m4 gnr_load_themes.m4 
 macro_objs 		= gnr_theme_macros.s gnr_load_themes.s 
-srcs 			= main.c vcc.c pretty.c cmd.c crypt.c klist.c plugins.c robot.c intiauth.c 
-objs 			= main.o vcc.o pretty.o cmd.o crypt.o klist.o plugins.o robot.o intiauth.o 
+srcs 			= main.c vcc.c pretty.c cmd.c crypt.c klist.c plugins.c robot.c intiauth.c hooks.c 
+objs 			= main.o vcc.o pretty.o cmd.o crypt.o klist.o plugins.o robot.o intiauth.o hooks.o 
 target 			= vcc 
 libgcrypt_cflags 	= $(shell libgcrypt-config --libs)
 CFLAGS 			= -std=gnu11 -Wall -Wextra -Werror -Iinclude -g $(libgcrypt_cflags) -Lplugins -lplugins \

+ 122 - 0
hooks.c

@@ -0,0 +1,122 @@
+/* vcc/hooks.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 <locale.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 <getopt.h>
+#include <libintl.h>
+#include <limits.h>
+
+#include <klist.h>
+#include <vcc/vcc.h>
+#include <vcc/version.h>
+#include <vcc/robot.h>
+#include <vcc/plugin.h>
+#include <vcc/pretty.h>
+#include <vcc/hooks.h>
+#include <vcc/defserv.h>
+#include <vcc/config.h>
+#include <vcc/interfaces.h>
+
+
+struct klist_node hooks = KLIST_NODE_INIT(&hooks);
+
+struct hook *register_hook(int type, hookfn_t hookfn) {
+	struct hook *hook;
+
+	if (unlikely(!(hook = malloc(sizeof(struct hook))))) {
+		fprintf(stderr, "*** malloc() failed\n");
+
+		return NULL;
+	}
+
+	klist_init(&hook->node);
+	hook->type = type;
+	hook->hook = hookfn;
+
+	klist_add(&hooks, &hook->node);
+
+	return hook;
+}
+
+
+int unregister_hook(struct hook *hook) {
+	klist_del(&hooks, &hook->node);
+	free(hook);
+
+	return 0;
+}
+
+
+int do_hook(int type, char *s, char *usrname, int sid) {
+	struct hook_info 	hi;
+	struct klist_node 	*n;
+	struct hook 		*hook;
+	char 			*p;
+	int 			i;
+
+	hi.type = type;
+	hi.msg = s;
+	hi.usr = usrname;
+	hi.sid = sid;
+
+	for (n = hooks.next; n != &hooks; n = n->next) {
+		hook = hook_of(n);
+
+		if (hook->type == type) {
+			/* so the order is quiet important */
+			if (unlikely(!(p = hook->hook(&hi)))) 
+				fprintf(stderr, "*** do_hook: one of hooks returned NULL. abandon... \n");
+
+			if (i) 
+				free(hi.msg);
+
+			hi.msg = p;
+			i++;
+		}
+	}
+
+	return 0;
+}
+
+
+int do_hook_recv(char *s, char *usrname, int sid) {
+	return do_hook(HOOK_RECV, s, usrname, sid);
+}
+
+int do_hook_send(char *s) {
+	return do_hook(HOOK_SEND, s, usrname, current_sid);
+}
+
+

+ 57 - 0
include/vcc/hooks.h

@@ -0,0 +1,57 @@
+/* vcc/include/vcc/hooks.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 __VCC_HOOKS_H
+#define __VCC_HOOKS_H
+
+#include <sys/types.h>
+#include <vcc/vcc.h>
+#include <vcc/hooks.h>
+#include <klist.h>
+#include <libintl.h>
+
+
+#define HOOK_RECV 	1
+#define HOOK_SEND 	2
+
+struct hook_info {
+	int 			type;
+	char 			*msg;
+	char 			*usr;
+	int 			sid;
+};
+
+typedef char *(* hookfn_t) (struct hook_info *);
+
+struct hook {
+	int 			type;
+	hookfn_t 		hook;
+	struct klist_node 	node;
+};
+
+#define hook_of(n) 		container_of(n, struct hook, node)
+
+struct hook *register_hook(int type, hookfn_t hook);
+int do_hook_recv(char *s, char *usrname, int sid);
+int do_hook_send(char *s);
+
+#endif
+

+ 2 - 0
include/vcc/interfaces.h

@@ -83,6 +83,8 @@ int do_cmd_lsse_bh(struct vcc_request *);
 int do_cmd_uinfo_bh(struct vcc_request *);
 int do_cmd_incr_bh(struct vcc_request *);
 
+struct history *hist_getmsg(int last);
+
 extern int fd;
 extern struct klist_node banned_people;
 extern char usrname[];

+ 3 - 13
include/vcc/plugin.h

@@ -24,17 +24,13 @@
 
 #include <sys/types.h>
 #include <vcc/vcc.h>
+#include <vcc/hooks.h>
 #include <libintl.h>
 
 /* a plugin can define their own commands in cmd.c, and must check 
  * it inserted, and abort command execution if it doesn't. */
 
 
-typedef char *(* hookfn_t) (char *);
-typedef char *(* recvhook_t) (char *, char *);
-extern hookfn_t msg_hook;
-extern recvhook_t recv_hook;
-
 struct cmdtable {
 	char 			*cmd;
 	int 			(* do_cmd) (int argc, char **argv);
@@ -50,14 +46,12 @@ struct plugin {
 	char 			*descr;
 	int 			(* init) (void);
 	int 			(* exit) (void);
-	
-	hookfn_t 		msg_hook;
-	recvhook_t 		recv_hook;
+
 	struct cmdtable 	*cmdtab;
 };
 
 /* plugin.c */
-extern struct plugin *plugins[];
+extern struct plugin 		*plugins[];
 
 #define _(s) 			gettext(s)
 
@@ -71,10 +65,6 @@ extern struct plugin *plugins[];
 
 int plg_insert(struct plugin *);
 struct plugin *find_plg(char *name);
-int register_msghook(hookfn_t, struct plugin *);
-int unregister_msghook(hookfn_t, struct plugin *);
-int register_recvhook(recvhook_t, struct plugin *);
-int unregister_recvhook(recvhook_t, struct plugin *);
 
 #endif
 

+ 2 - 2
include/vcc/version.h

@@ -1,2 +1,2 @@
-#define VCC_VERSION "VCC 1.27.0 Wed Jun  8 01:25:43 PM CST 2022 root"
-#define VCC_SEMVER "vcc 1.27.0-rc "
+#define VCC_VERSION "VCC 1.28.0 Fri Jun 10 09:45:04 PM CST 2022 root"
+#define VCC_SEMVER "vcc 1.28.0-rc "

+ 2 - 14
main.c

@@ -230,16 +230,11 @@ int handle_command(char *buf) {
 		return do_cmd(buf);
 	
 	default:
-		if (msg_hook) 
-			if (unlikely(!(buf = msg_hook(buf)))) 
-				return 1;
+		do_hook_send(buf);
 
 		send_msg(buf, usrname);
 		pretty_new_msg(usrname, buf, current_sid, 0);
 
-		if (msg_hook) 
-			free(buf);
-
 		break;
 	}
 
@@ -262,8 +257,6 @@ void sigint_handler(int signr) {
 
 
 int new_msg(char *msg, char *name, int session, int flags) {
-	char *p;
-
 	if (!strcmp(msg, "CQD")) {
 		pretty_cqd(name);
 		pretty_prompt(usrname);
@@ -274,16 +267,11 @@ int new_msg(char *msg, char *name, int session, int flags) {
 	if (unlikely(is_user_banned(name)))
 		return 1;
 
-	if (recv_hook) 
-		if (unlikely(!(p = recv_hook(msg, name)))) 
-			return 1;
+	do_hook_recv(msg, name, session);
 
 	pretty_new_msg(name, msg, session, flags);
 	pretty_prompt(usrname);
 
-	if (recv_hook) 
-		free(p);
-
 	return 0;
 }
 

+ 0 - 45
plugins.c

@@ -44,8 +44,6 @@
 
 
 extern struct plugin plg_hello, plg_robot, plg_m4, plg_script, plg_hist;
-hookfn_t msg_hook;
-recvhook_t recv_hook;
 
 struct plugin *plugins[] = {
 	&plg_hello, &plg_robot, &plg_m4, &plg_script, &plg_hist, 
@@ -132,9 +130,6 @@ int do_cmd_prem(int argc, char **argv) {
 	if (p->exit) 
 		p->exit();
 
-	unregister_msghook(p->msg_hook, p);
-	unregister_recvhook(p->recv_hook, p);
-	
 	return plg_remove(p);
 }
 
@@ -177,43 +172,3 @@ int do_cmd_pins(int argc, char **argv) {
 }
 
 
-int register_msghook(hookfn_t hook, struct plugin *plg) {
-	/* unlike other things, we can have only one hook at the same moment. */
-
-	if (msg_hook) 
-		fprintf(stderr, _("warning: the old hook removed. \n"));
-		
-	plg->msg_hook = msg_hook = hook;
-	return 0;
-}
-
-
-int register_recvhook(recvhook_t hook, struct plugin *plg) {	
-	if (recv_hook) 
-		fprintf(stderr, _("warning: the old hook removed. \n"));
-		
-	plg->recv_hook = recv_hook = hook;
-	return 0;
-	
-}
-
-
-int unregister_msghook(hookfn_t hook, struct plugin *plg) {
-	if (msg_hook != hook) 
-		return 0;
-
-	plg->msg_hook = msg_hook = NULL;
-
-	return 0;
-}
-
-int unregister_recvhook(recvhook_t hook, struct plugin *plg) {
-	if (recv_hook != hook) 
-		return 0;
-
-	plg->recv_hook = recv_hook = NULL;
-
-	return 0;
-}
-
-

+ 21 - 12
plugins/hist/hist.c

@@ -61,28 +61,24 @@ struct klist_node history = KLIST_NODE_INIT(&history);
 #define this_plg (&plg_hist)
 #define DEFAULT_HISTFILE ".vcc_history"
 
-char *hist_hook(char *s, char *usr) {
+char *hist_hook(struct hook_info *hi) {
 	struct history *n;
 
 	if (unlikely(!(n = malloc(sizeof(struct history))))) {
 		fprintf(stderr, "*** malloc() failed\n");
 
-		return strdup(s);
+		return strdup(hi->msg);
 	}
 
-	n->usrname = strdup(usr);
-	n->msg = strdup(s);
+	n->usrname = strdup(hi->usr);
+	n->msg = strdup(hi->msg);
 
 	klist_init(&n->node);
 	klist_add(&history, &n->node);
 
-	dprintf(hist_fd, "[%ld] %s: %s", times(NULL), usr, s);
+	dprintf(hist_fd, "[%ld] %s: %s", times(NULL), hi->usr, hi->msg);
 
-	return strdup(s);
-}
-
-char *hist_msghook(char *s) {
-	return hist_hook(s, usrname);
+	return strdup(hi->msg);
 }
 
 int open_hist_file(void) {
@@ -116,8 +112,8 @@ int hist_init(void) {
 		return 1;
 	}
 
-	register_recvhook(hist_hook, this_plg);
-	register_msghook(hist_msghook, this_plg);
+	register_hook(HOOK_RECV, hist_hook);
+	register_hook(HOOK_SEND, hist_hook);
 
 	return 0;
 }
@@ -147,3 +143,16 @@ int do_cmd_hist(int argc, char **argv) {
 }
 
 
+struct history *hist_getmsg(int last) {
+	struct klist_node *n;
+
+	for (n = history.next; n != &history && last--; n = n->next) 
+		;
+
+	if (n == &history) 
+		return NULL;
+
+	return hist_of(n);
+}
+
+

+ 29 - 5
plugins/m4/m4.c

@@ -29,6 +29,7 @@
 #include <vcc/vcc.h>
 #include <vcc/pretty.h>
 #include <vcc/plugin.h>
+#include <vcc/interfaces.h>
 #include <vcc/version.h>
 
 
@@ -95,14 +96,36 @@ int do_cmd_m4def(int argc, char **argv) {
 	return 0;
 }
 
+
+static inline int check_hist_enabled(void) {
+	if (find_plg("hist")->state == PLG_INSERTED) 
+		return 1;
+
+	return 0;
+}
+
+
 int m4_write_predef(int fd) {
-	(void) fd;
+	struct history *hist;
+
+	if (!check_hist_enabled()) {
+		fprintf(stderr, _("the history plugin (hist) doesn't insert so m4 preder can't work. \n"));
+
+		return 0;
+	}
+
+	hist = hist_getmsg(1);
+
+	if (!hist) 
+		return 0;
+
+	dprintf(fd, "divert(-1)define(`LAST_MSG', `%s'\n)divert(0)dnl\n", hist->msg);
 
 	return 0;
 }
 
 
-char *m4_msghook(char *s) {
+char *m4_hook(struct hook_info *hi) {
 	int 	fdt, size;
 	FILE 	*fp;
 	char 	*p;
@@ -116,8 +139,8 @@ char *m4_msghook(char *s) {
 	write(fdt, m4cachep, m4cache_size);
 	m4_write_predef(fdt);
 
-	size = strlen(s) + 1;
-	write(fdt, s, size);
+	size = strlen(hi->msg) + 1;
+	write(fdt, hi->msg, size);
 	close(fdt);
 
 	if (unlikely(!(fp = popen("/bin/m4 /tmp/.vcc-tmp", "r")))) {
@@ -143,7 +166,8 @@ int m4_init(void) {
 	m4cachep = NULL;
 	m4cache_size = 0;
 
-	return register_msghook(m4_msghook, this_plg);
+	register_hook(HOOK_SEND, m4_hook);
+	return 0;
 }