소스 검색

vcc: add vcr and configuration

intirain 4 년 전
부모
커밋
552581edf5
13개의 변경된 파일과 327개의 추가작업 그리고 56개의 파일을 삭제
  1. 2 2
      Makefile
  2. 8 0
      include/vcc/interfaces.h
  3. 8 0
      include/vcc/plugin.h
  4. 2 2
      include/vcc/version.h
  5. 0 6
      libs/readconf/include/readconf/readconf.h
  6. 70 12
      main.c
  7. 13 2
      plugins.c
  8. 1 1
      plugins/Makefile
  9. 14 17
      plugins/robot/robot.c
  10. 14 0
      plugins/vcr/Makefile
  11. 185 0
      plugins/vcr/vcr.c
  12. 3 9
      pretty.c
  13. 7 5
      robot.c

+ 2 - 2
Makefile

@@ -1,6 +1,6 @@
 VCC_MAJOR 		= 1
-VCC_MINOR 		= 38
-VCC_PATCH 		= 2
+VCC_MINOR 		= 39
+VCC_PATCH 		= 0
 VCC_STR 		= -rc
 
 # change this to '/usr/share/' or '/opt/vcc/' etc

+ 8 - 0
include/vcc/interfaces.h

@@ -49,8 +49,16 @@ struct sname_cache {
 
 #define sname_cache_of(n) 	container_of(n, struct sname_cache, node)
 
+struct delayed_pins {
+	char 			*name;
+	struct klist_node 	node;
+};
+
+#define dpins_of(n) 		container_of(n, struct delayed_pins, node)
+
 int vcc_login(char *, char *);
 int do_intiauth(void);
+int insert_delayed_plugins(void);
 
 int send_msg(const char *msg, const char *usrname);
 int send_msg_encrypted(const char *msg, const char *s);

+ 8 - 0
include/vcc/plugin.h

@@ -66,8 +66,16 @@ extern struct plugin 		*plugins[];
 	} \
 })
 
+#define MODE_NORMAL 		1
+#define MODE_ROBOT 		2
+extern int 			mode;
+
 int plg_insert(struct plugin *);
 struct plugin *find_plg(char *name);
+int insert_plugin(char *name);
+
+int 				openconf(void);
+extern int 			conf_fd;
 
 #endif
 

+ 2 - 2
include/vcc/version.h

@@ -1,2 +1,2 @@
-#define VCC_VERSION "vcc 1.38.1 Wed Jul  6 03:39:42 PM CST 2022 root"
-#define VCC_SEMVER "vcc 1.38.1-rc"
+#define VCC_VERSION "vcc 1.39.0 Fri Jul  8 09:42:34 AM CST 2022 root"
+#define VCC_SEMVER "vcc 1.39.0-rc"

+ 0 - 6
libs/readconf/include/readconf/readconf.h

@@ -34,11 +34,8 @@
 #define READCONF_COMMENT 	'#'
 #define READCONF_EQUAL 		'='
 
-
 #define READCONF_WHITESPACE 	" \t\0"
 
-
-
 struct readconf_op {
 	char *op;
 	int oplen;
@@ -46,14 +43,12 @@ struct readconf_op {
 	int (* do_op) (void);
 };
 
-
 /* in that readconf.c */
 
 extern struct readconf_op ops[];
 extern char *line_buffer;
 extern int line_no;
 
-
 /* and here are readconf apis */
 
 int readconf_init(void);
@@ -62,7 +57,6 @@ void *readconf_find_name_any(char *name, int *out_type);
 struct readconf_strconf *readconf_find_strname(char *name);
 struct readconf_uintconf *readconf_find_uintname(char *name);
 
-
 int do_op_set(void);
 
 char *skip_space(char *);

+ 70 - 12
main.c

@@ -50,6 +50,8 @@
 #include <vcc/config.h>
 #include <vcc/interfaces.h>
 
+#include <readconf/readconf.h>
+
 
 /* we tried to use libreadline, but after using that, we feel strange 
  * because... because of what? i don't know. */
@@ -57,15 +59,12 @@
 
 int 		fd;
 
-#define _(s) gettext(s)
-
 /* normally. it belongs to the foreground pgroup, all messages write to terminal. */
-#define MODE_NORMAL 	1
 /* robot mode. we run in the background pgroup, and give all the messages to the plugin specified. */
-#define MODE_ROBOT 	2
 int 		mode = MODE_NORMAL;
 
 char 		usrname[USRNAME_SIZE];
+struct klist_node delayed_pins = KLIST_NODE_INIT(&delayed_pins);
 
 static char const *help_msg = {
 	"Usage: vcc [-auprh] [server]\n"
@@ -78,6 +77,7 @@ static char const *help_msg = {
 	"  -r    --robot   Enter the robot mode. \n"
 	"  -a    --address Address of the server. "
 	"As default, server is %s. \n"
+	"  -l    --plugin  Load plugin automatically. \n"
 	"  -h    --help    Show this help information. \n"
 };
 
@@ -110,6 +110,13 @@ static struct option longopts[] = {
 		.val = 'r'
 	}, 
 
+	{
+		.name = "plugin", 
+		.has_arg = 1, 
+		.flag = NULL, 
+		.val = 'l'
+	}, 
+
 	{
 		.name = "help", 
 		.has_arg = 0, 
@@ -184,10 +191,43 @@ int vos_login(void) {
 	return 1;
 }
 
+
+int join_delayed_pins(char *name) {
+	struct delayed_pins *dp;
+
+	if (unlikely(!(dp = malloc(sizeof(struct delayed_pins))))) {
+		fprintf(stderr, "*** malloc() failed\n");
+
+		return 1;
+	}
+
+	klist_init(&dp->node);
+	klist_add(&delayed_pins, &dp->node);
+
+	dp->name = strdup(name);
+	return 0;
+}
+
+int insert_delayed_plugins(void) {
+	struct klist_node *n, *next;
+	struct delayed_pins *dp;
+
+	for (n = delayed_pins.next; n != &delayed_pins; n = next) {
+		next = n->next;
+		dp = dpins_of(n);
+
+		insert_plugin(dp->name);
+		free(dp->name);
+		free(dp);
+	}
+
+	return 0;
+}
+
 int parse_args(int argc, char **argv, int *port, char **ip) {
 	int i, c, got_usrname = 0;
 
-	while ((c = getopt_long(argc, argv, "a:u:p:rh", longopts, &i)) != -1) {
+	while ((c = getopt_long(argc, argv, "a:u:p:rl:h", longopts, &i)) != -1) {
 		switch (c) {
 		case 'a':
 			*ip = strdup(optarg);
@@ -212,6 +252,11 @@ int parse_args(int argc, char **argv, int *port, char **ip) {
 			mode = MODE_ROBOT;
 			break;
 
+		case 'l':
+			join_delayed_pins(optarg);
+			
+			break;
+
 		case 'h':
 		default:
 			exit(usage());
@@ -260,9 +305,11 @@ void sigint_handler(int signr) {
 int new_msg(char *msg, char *name, int session, int flags) {
 	do_hook_recv(msg, name, session);
 
-	if (*msg) 
-		pretty_new_msg(name, msg, ntohl(session), flags);
-	pretty_prompt(usrname);
+	if (mode == MODE_NORMAL) {
+		if (*msg) 
+			pretty_new_msg(name, msg, session, flags);
+		pretty_prompt(usrname);
+	}
 
 	return 0;
 }
@@ -284,7 +331,7 @@ int do_new_request(struct vcc_relay_header *hdr) {
 			if (ntohl(req.flags) & FLAG_ENCRYPTED) 
 				decrypt(req.msg);
 
-			new_msg(req.msg, req.usrname, req.session, 0);
+			new_msg(req.msg, req.usrname, ntohl(req.session), 0);
 		}
 
 		else 
@@ -299,7 +346,7 @@ int do_new_request(struct vcc_relay_header *hdr) {
 				flags |= MSG_NEW_ONLY_VISIBLE;
 
 			msg = relay_get_msg(fd, hdr);
-			new_msg(msg, hdr->usrname, hdr->session, flags);
+			new_msg(msg, hdr->usrname, ntohl(hdr->session), flags);
 
 			free(msg);
 		}
@@ -340,6 +387,14 @@ int init_signal(void) {
 	return 0;
 }
 
+static int vcc_init_config(void) {
+	if (openconf()) 
+		return 1;
+
+	readconf_init();
+	return 0;
+}
+
 
 int main(int argc, char **argv) {
 	struct pollfd 		fds[2];
@@ -356,15 +411,16 @@ int main(int argc, char **argv) {
 	bindtextdomain("vcc", "lang");
 	textdomain("vcc");
 
-	vcc_show_version();
+	vcc_init_config();
 
 	n = parse_args(argc, argv, &port, &ip);
+	vcc_show_version();
 	init_socket(ip, port);
 
 	printf(_("Connected to server. \n"));
 
 	if (mode == MODE_ROBOT) 
-		return robot();
+		return robot();	
 
 	if (!vos_login() && !do_intiauth()) {
 		/* normally startup */
@@ -394,6 +450,8 @@ int main(int argc, char **argv) {
 	pretty_init();
 	init_signal();
 
+	insert_delayed_plugins();
+
 	if (unlikely(!(buf = malloc(PATH_MAX)))) {
 		fprintf(stderr, "*** malloc() failed\n");
 

+ 13 - 2
plugins.c

@@ -44,11 +44,11 @@
 
 
 extern struct plugin plg_hello, plg_robot, plg_m4, plg_script, plg_hist, 
-       plg_cqd, plg_beep, plg_ban;
+       plg_cqd, plg_beep, plg_ban, plg_vcr;
 
 struct plugin *plugins[] = {
 	&plg_hello, &plg_robot, &plg_m4, &plg_script, &plg_hist, 
-	&plg_cqd, &plg_beep, &plg_ban, 
+	&plg_cqd, &plg_beep, &plg_ban, &plg_vcr, 
 	(struct plugin *) NULL
 };
 
@@ -115,6 +115,17 @@ int plg_remove(struct plugin *plg) {
 }
 
 
+int insert_plugin(char *s) {
+	struct plugin *plg;
+
+	if (!(plg = find_plg(s))) 
+		return 1;
+
+	plg_insert(plg);
+	return plg->init ? plg->init() : 0;
+}
+
+
 int plg_prem(int argc, char **argv) {
 	struct plugin *p;
 	char name[MSG_SIZE];

+ 1 - 1
plugins/Makefile

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

+ 14 - 17
plugins/robot/robot.c

@@ -30,8 +30,8 @@
 #include <vcc/interfaces.h>
 
 
-int robot_lslcn(char *s);
-int do_robot_cmd(char *s);
+static int robot_plg_init(void);
+static char *do_robot_cmd(char *s);
 
 struct cmdtable robot_cmdtab[] = {
 	{ NULL, NULL, NULL }
@@ -41,7 +41,8 @@ struct plugin plg_robot = {
 	.state = 0, 
 	.name = "robot", 
 	.descr = "The robot plugin. ", 
-	.cmdtab = robot_cmdtab
+	.cmdtab = robot_cmdtab, 
+	.init = robot_plg_init, 
 };
 
 static struct cmdtable commands[] = {
@@ -51,26 +52,17 @@ static struct cmdtable commands[] = {
 #define this_plg (&plg_robot)
 
 
-int robot_new_msg(struct vcc_request *req) {
-	char *msg;
-
-	if (ntohl(req->reqtype) != REQ_MSG_NEW) 
-		return do_bh(req);
-
-	msg = req->msg;
-
-	if (strncmp(msg, "robot, ", 7)) 
+char *robot_hook(struct hook_info *hi) {
+	if (strncmp(hi->msg, "robot, ", 7)) 
 		return 0;
 
-	do_robot_cmd(msg);
-
-	return 0;
+	return do_robot_cmd(hi->msg);
 }
 
 typedef int (* fptr_t) (char *);
 fptr_t execute_in(struct cmdtable *, char *, int);
 
-int do_robot_cmd(char *msg) {
+static char *do_robot_cmd(char *msg) {
 	fptr_t f;
 	int len;
 	char *p;
@@ -86,7 +78,12 @@ int do_robot_cmd(char *msg) {
 		return 0;
 	}
 
-	return f(msg);
+	return f(msg) ? HOOK_IGNORE : HOOK_FAILED;
+}
+
+static int robot_plg_init(void) {
+	register_hook(HOOK_RECV, robot_hook);
+	return 0;
 }
 
 

+ 14 - 0
plugins/vcr/Makefile

@@ -0,0 +1,14 @@
+objs 	= vcr.o 
+target 	= ../plg_vcr.o 
+CFLAGS 	= -std=gnu11 -Wall -Wextra -Werror -I../../include -I../../libs/readconf/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)
+

+ 185 - 0
plugins/vcr/vcr.c

@@ -0,0 +1,185 @@
+/* vcc/plugins/vcr/vcr.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 <stdlib.h>
+#include <sys/wait.h>
+
+#include <vcc/vcc.h>
+#include <vcc/pretty.h>
+#include <vcc/plugin.h>
+#include <vcc/hooks.h>
+#include <vcc/interfaces.h>
+#include <vcc/version.h>
+
+#include <readconf/readconf.h>
+#include <readconf/do_op_set.h>
+
+
+extern char 			**environ;
+static int 			vcr_listen_sid;
+static char 			*vcr_executable;
+extern char 			usrname[];
+
+static int vcr_init(void);
+
+struct cmdtable vcr_cmdtab[] = {
+	{ NULL, NULL, NULL }
+};
+
+
+struct plugin plg_vcr = {
+	.state = 0, 
+	.name = "vcr", 
+	.descr = "vcc bridge. ", 
+	.cmdtab = vcr_cmdtab, 
+	.init = vcr_init
+};
+
+#define this_plg (&plg_vcr)
+
+static char *set_environments(struct hook_info *hi) {
+	char *t;
+
+	if (unlikely(!(t = malloc(64)))) {
+		fprintf(stderr, "*** malloc() failed\n");
+
+		return NULL;
+	}
+
+	sprintf(t, "%d", hi->sid);
+
+	setenv("VCR_TYPE", hi->type == HOOK_RECV ? "recv" : "send", 1);
+	setenv("VCR_USER", hi->usr, 1);
+	setenv("VCR_MSG", hi->msg, 1);
+	setenv("VCR_SID", t, 1);
+
+	return t;
+}
+
+static int unset_environments(void) {
+	unsetenv("VCR_TYPE");
+	unsetenv("VCR_USER");
+	unsetenv("VCR_MSG");
+	unsetenv("VCR_SID");
+
+	return 0;
+}
+
+static int send_result(int fd) {
+	char p[MSG_SIZE];
+
+	read(fd, &p, MSG_SIZE);
+	p[MSG_SIZE - 1] = 0;
+
+	send_msg(p, usrname);
+	return 0;
+}
+
+static int vcr_execute(struct hook_info *hi) {
+	int pid, status;
+	int pipefd[2];
+	char *t;
+
+	t = set_environments(hi);
+	if (pipe(pipefd) < 0) {
+		perror("pipe");
+		return 1;
+	}
+
+	switch ((pid = fork())) {
+	case 0:
+		close(pipefd[0]);
+		dup2(pipefd[1], 1);
+
+		execlp(vcr_executable, vcr_executable, NULL);
+		printf("%s\n", vcr_executable);
+		perror("execlp");
+
+		abort();
+
+	case -1:
+		perror("fork");
+		return 1;
+
+	default:
+		wait4(pid, &status, 0, NULL);
+		printf("%d exited with status %d. \n", pid, status);
+
+		send_result(pipefd[0]);
+
+		unset_environments();
+		free(t);
+	}
+
+	close(pipefd[0]);
+	close(pipefd[1]);
+
+	return status;
+}
+
+static char *vcr_hook(struct hook_info *hi) {
+	if (hi->sid != vcr_listen_sid) 
+		return HOOK_IGNORE;
+
+	vcr_execute(hi);
+
+	return HOOK_IGNORE;
+}
+
+static int load_vcr_config(void) {
+	struct readconf_uintconf *conf_sid;
+	struct readconf_strconf *conf_exec;
+
+	if (!(conf_sid = readconf_find_uintname("vcr_listen_sid")) || 
+			!(conf_exec = readconf_find_strname("vcr_executable"))) {
+		fprintf(stderr, "*** vcr: cannot load configure\n");
+		return 1;
+	}
+
+	vcr_listen_sid = (int) conf_sid->conf_value;
+	vcr_executable = conf_exec->conf_value;
+
+	printf("vcr is listening on sid %d. \n", vcr_listen_sid);
+	printf("vcr's executable is %s\n", vcr_executable);
+
+	return 0;
+}
+
+static int vcr_init(void) {
+	if (load_vcr_config()) 
+		return 1;
+
+	if (mode != MODE_ROBOT) {
+		fprintf(stderr, "*** vcr should be loaded in the robot mode. \n");
+		return 1;
+	}
+
+	register_hook(HOOK_SEND, vcr_hook);
+	register_hook(HOOK_RECV, vcr_hook);
+
+	join_session((current_sid = vcr_listen_sid));
+
+	return 0;
+}
+
+

+ 3 - 9
pretty.c

@@ -118,23 +118,22 @@ int conf_fd;
 #include "gnr_theme_macros.s"
 
 
-int open_pretty_conf(void) {
+int openconf(void) {
 	char 	*home;
 	char 	path[PATH_MAX];
 
 	if (unlikely(!(home = getenv("HOME")))) 
 		goto abandon;
 	
-	sprintf(path, "%s/.vcc-theme", home);
+	sprintf(path, "%s/.vcc-config", home);
 
 	if ((conf_fd = open(path, O_RDONLY)) < 0) 
 		goto abandon;
 
-	printf("reading theme ...");
 	return 0;
 
 abandon:
-	fprintf(stderr, "using default theme. \n");
+	fprintf(stderr, "using default config. \n");
 	return 1;
 }
 
@@ -178,12 +177,7 @@ int load_themes(void) {
 
 
 int pretty_init(void) {
-	if (open_pretty_conf()) 
-		return 0;
-
-	readconf_init();
 	load_themes();
-
 	close(conf_fd);
 
 	return 0;

+ 7 - 5
robot.c

@@ -85,18 +85,20 @@ int becomes_daemon(void) {
 
 
 int robot(void) {
-	struct vcc_request req;
+	struct vcc_relay_header hdr;
 
-	plg_insert(find_plg("robot"));
 //	becomes_daemon();
 
 	vcc_login("robot", "robot");
+	strcpy(usrname, "robot");
+
+	insert_delayed_plugins();
 
 	for (;;) {
-		memset(&req, 0, REQ_SIZE);
-		read(fd, &req, REQ_SIZE);
+		memset(&hdr, 0, sizeof(struct vcc_relay_header));
+		read(fd, &hdr, sizeof(struct vcc_relay_header));
 
-		robot_new_msg(&req);
+		do_new_request(&hdr);
 	}
 
 	return 0;