Bläddra i källkod

vcc: PLUGINS!

intirain 4 år sedan
förälder
incheckning
fb9d8afaee
7 ändrade filer med 238 tillägg och 21 borttagningar
  1. 8 5
      Makefile
  2. 45 14
      cmd.c
  3. 2 2
      include/vcc/version.h
  4. 94 0
      plugins.c
  5. 16 0
      plugins/Makefile
  6. 14 0
      plugins/hello/Makefile
  7. 59 0
      plugins/hello/hello.c

+ 8 - 5
Makefile

@@ -1,17 +1,17 @@
 VCC_MAJOR 		= 1
-VCC_MINOR 		= 11
-VCC_PATCH 		= 3
+VCC_MINOR 		= 12
+VCC_PATCH 		= 0
 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 klist.c 
-objs 			= main.o vcc.o pretty.o cmd.o crypt.o klist.o
+srcs 			= main.c vcc.c pretty.c cmd.c crypt.c klist.c plugins.c 
+objs 			= main.o vcc.o pretty.o cmd.o crypt.o klist.o plugins.o 
 target 			= vcc 
 libgcrypt_cflags 	= $(shell libgcrypt-config --libs)
-CFLAGS 			= -std=gnu11 -Wall -Wextra -Werror -Iinclude -g $(libgcrypt_cflags) 
+CFLAGS 			= -std=gnu11 -Wall -Wextra -Werror -Iinclude -g $(libgcrypt_cflags) -Lplugins -lplugins 
 MAKEFLAGS 		= --no-print-directory
 VCC_DEFSERV 		= 39.104.27.216 
 VCC_VERSION 		= VCC $(VCC_MAJOR).$(VCC_MINOR).$(VCC_PATCH) \
@@ -23,6 +23,7 @@ all :
 	@echo "#define VCC_VERSION \"$(VCC_VERSION)\"" > include/vcc/version.h 
 	@echo "#define VCC_SEMVER \"$(VCC_SEMVER)\"" >> include/vcc/version.h 
 	@echo "#define VCC_DEFSERV \"$(VCC_DEFSERV)\"" > include/vcc/defserv.h 
+	@$(MAKE) -C plugins/ 
 	@$(MAKE) $(target) $(MAKEFLAGS)
 
 $(target) : 	$(objs) $(srcs)
@@ -35,5 +36,7 @@ $(target) : 	$(objs) $(srcs)
 
 .PHONY clean : 
 	$(RM) $(objs) $(target) -f  
+	@$(MAKE) -C plugins/ clean
+
 w :
 	@echo 1

+ 45 - 14
cmd.c

@@ -37,6 +37,7 @@
 
 #include <klist.h>
 #include <vcc/vcc.h>
+#include <vcc/plugin.h>
 #include <vcc/version.h>
 #include <vcc/pretty.h>
 
@@ -57,6 +58,8 @@ 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 read_msg(struct vcc_request *);
 int vcc_create_session(char *name);
@@ -78,11 +81,7 @@ int join_session(int sid);
 
 int did_warn = 0;
 
-struct cmdtable {
-	char 		*cmd;
-	int 		(* do_cmd) (char *);
-	char 		*help;
-} cmdtab[] = {
+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. " }, 
@@ -98,6 +97,8 @@ struct cmdtable {
 	{ "-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. " }, 
 
 	{ NULL, NULL, NULL }
 };
@@ -105,33 +106,63 @@ struct cmdtable {
 #define clear_line() 	fprintf(stderr, "\033[2K\r")
 #define __unused 	__attribute__((unused))
 
+typedef int (* fptr_t) (char *);
+
+static 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 cmdtable 	*t;
+	struct plugin 		*plg;
 	char 			*p;
-	int 			len;
+	int 			len, i;
+	fptr_t 			f;
 
 	p = s;
 
 	for (len = 0; *p && !isspace(*p); len++) 
 		p++;
-	for (t = cmdtab; t->cmd; t++) 
-		if (!strncmp(t->cmd, s, len)) 
-			return t->do_cmd(s);
-	
+
+	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;
 }
 
 
-int do_cmd_help(char *s) {
+static int __help(struct cmdtable *cmdtab) {
 	struct cmdtable *t;
 
-	(void) s;
-
 	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;
 }

+ 2 - 2
include/vcc/version.h

@@ -1,2 +1,2 @@
-#define VCC_VERSION "VCC 1.11.3 Sat Apr 30 15:01:06 CST 2022 root"
-#define VCC_SEMVER "vcc 1.11.3-rc "
+#define VCC_VERSION "VCC 1.12.0 Sat Apr 30 16:24:19 CST 2022 root"
+#define VCC_SEMVER "vcc 1.12.0-rc "

+ 94 - 0
plugins.c

@@ -0,0 +1,94 @@
+/* vcc/plugins.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 <ctype.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 <klist.h>
+#include <vcc/vcc.h>
+#include <vcc/plugin.h>
+#include <vcc/version.h>
+#include <vcc/pretty.h>
+
+
+extern struct plugin plg_hello;
+
+struct plugin *plugins[] = {
+	&plg_hello, 
+	(struct plugin *) NULL
+};
+
+int do_cmd_pls(char *s) {
+	int i;
+	struct plugin *p;
+
+	(void) s;
+
+	for (i = 0; (p = plugins[i]); i++) 
+		pretty_cmd_help(p->name, p->descr);
+	
+	return 0;
+}
+
+struct plugin *find_plg(char *s) {
+	int i;
+	struct plugin *p;
+
+	for (i = 0; (p = plugins[i]); i++) 
+		if (!strcmp(s, p->name)) 
+			return p;
+
+	return NULL;
+}
+
+int do_cmd_pins(char *s) {
+	struct plugin *p;
+	char name[MSG_SIZE];
+
+	(void) s;
+
+	fprintf(stderr, "plugin: ");
+	scanf("%s", name);
+
+	if (!(p = find_plg(name))) {
+		fprintf(stderr, "*** plugin not found. \n");
+
+		return 1;
+	}
+	
+	p->state = PLG_INSERTED;
+	return 0;
+}
+
+

+ 16 - 0
plugins/Makefile

@@ -0,0 +1,16 @@
+plugins 	= hello
+target 		= libplugins.a 
+
+all: 
+	@for i in $(plugins); do \
+		$(MAKE) -C $$i; \
+	done;
+	@$(AR) -cr $(target) *.o 
+	@echo -e "AR\t\t\t$(target)"
+
+.PHONY clean: 
+	for i in $(plugins); do \
+		$(MAKE) -C $$i clean; \
+	done;
+	rm $(target) -f 
+

+ 14 - 0
plugins/hello/Makefile

@@ -0,0 +1,14 @@
+objs 	= hello.o 
+target 	= ../plg_hello.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)
+

+ 59 - 0
plugins/hello/hello.c

@@ -0,0 +1,59 @@
+/* vcc/plugins/hello/hello.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>
+
+
+int do_cmd_hello(char *s);
+
+struct cmdtable hello_cmdtab[] = {
+	{ "-hello", do_cmd_hello, "Greet you. " }, 
+
+	{ NULL, NULL, NULL }
+};
+
+struct plugin plg_hello = {
+	.state = 0, 
+	.name = "hello", 
+	.descr = "Greet you. ", 
+	.cmdtab = hello_cmdtab
+};
+
+#define this_plg (&plg_hello)
+
+
+int do_cmd_hello(char *s) {
+	plg_check_enabled();
+
+	(void) s;
+
+	printf("hello, world\n");
+
+	return 0;
+}
+
+