Jelajahi Sumber

vcc: add robot and a 'lslcn', which isn't a command, but it doesn't work well

intirain 4 tahun lalu
induk
melakukan
0888fd6cb6
12 mengubah file dengan 352 tambahan dan 10 penghapusan
  1. 1 1
      Makefile
  2. 1 1
      cmd.c
  3. 34 0
      include/vcc/robot.h
  4. 2 2
      include/vcc/version.h
  5. 2 2
      plugins.c
  6. 1 1
      plugins/Makefile
  7. 14 0
      plugins/lcn/Makefile
  8. 75 0
      plugins/lcn/lcn.c
  9. 14 0
      plugins/robot/Makefile
  10. 94 0
      plugins/robot/robot.c
  11. 111 0
      robot.c
  12. 3 3
      vcc.c

+ 1 - 1
Makefile

@@ -1,6 +1,6 @@
 VCC_MAJOR 		= 1
 VCC_MINOR 		= 14
-VCC_PATCH 		= 0
+VCC_PATCH 		= 1
 VCC_STR 		= -rc 
 
 VCC_COMPILE_TIME 	= $(shell date)

+ 1 - 1
cmd.c

@@ -106,7 +106,7 @@ struct cmdtable cmdtab[] = {
 
 typedef int (* fptr_t) (char *);
 
-static fptr_t execute_in(struct cmdtable *tab, char *s, int len) {
+fptr_t execute_in(struct cmdtable *tab, char *s, int len) {
 	struct cmdtable *t;
 
 	for (t = tab; t->cmd; t++) 

+ 34 - 0
include/vcc/robot.h

@@ -0,0 +1,34 @@
+/* vcc/include/vcc/robot.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_ROBOT_H
+#define __VCC_ROBOT_H
+
+#include <sys/types.h>
+#include <vcc/vcc.h>
+
+extern int 	mode;
+struct plugin 	*plgctl;
+
+int robot(void);
+
+#endif
+

+ 2 - 2
include/vcc/version.h

@@ -1,2 +1,2 @@
-#define VCC_VERSION "VCC 1.14.0 Sun May  1 13:34:23 CST 2022 root"
-#define VCC_SEMVER "vcc 1.14.0-rc "
+#define VCC_VERSION "VCC 1.14.1 Sun May  1 14:39:46 CST 2022 root"
+#define VCC_SEMVER "vcc 1.14.1-rc "

+ 2 - 2
plugins.c

@@ -42,10 +42,10 @@
 #include <vcc/pretty.h>
 
 
-extern struct plugin plg_hello, plg_robot;
+extern struct plugin plg_hello, plg_robot, plg_lcn;
 
 struct plugin *plugins[] = {
-	&plg_hello, &plg_robot, 
+	&plg_hello, &plg_robot, &plg_lcn, 
 	(struct plugin *) NULL
 };
 

+ 1 - 1
plugins/Makefile

@@ -1,4 +1,4 @@
-plugins 	= hello robot
+plugins 	= hello robot lcn 
 target 		= libplugins.a 
 
 all: 

+ 14 - 0
plugins/lcn/Makefile

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

+ 75 - 0
plugins/lcn/lcn.c

@@ -0,0 +1,75 @@
+/* vcc/plugins/robot/lcn.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 <dirent.h>
+#include <arpa/inet.h>
+
+#include <vcc/vcc.h>
+#include <vcc/pretty.h>
+#include <vcc/plugin.h>
+#include <vcc/version.h>
+
+
+#define LCN_PATH 	"/root/lcn-articles/"
+
+int send_msg(char *, char *);
+
+struct cmdtable lcn_cmdtab[] = {
+	{ NULL, 0, 0 }
+};
+
+struct plugin plg_lcn = {
+	.state = 0, 
+	.name = "lcn", 
+	.descr = "The lcn plugin: read lcn articles online. ", 
+	.cmdtab = lcn_cmdtab
+};
+
+#define this_plg (&plg_lcn)
+
+int robot_lslcn(char *s) {
+	DIR *dir;
+	struct dirent *dirent;
+	char p[MSG_SIZE];
+
+	(void) s;
+
+	if (!(dir = opendir(LCN_PATH))) {
+		send_msg("Oops! There are something wrong when I tried to finish my work...\n", "robot");
+
+		return 1;
+	}
+
+	while ((dirent = readdir(dir))) {
+		if (dirent->d_type == DT_REG) {
+			memset(p, 0, MSG_SIZE);
+			sprintf(p, "%s\n", dirent->d_name);
+			send_msg(p, "robot");
+		}
+	}
+
+	closedir(dir);
+	
+	return 0;
+}
+

+ 14 - 0
plugins/robot/Makefile

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

+ 94 - 0
plugins/robot/robot.c

@@ -0,0 +1,94 @@
+/* vcc/plugins/robot/robot.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 <arpa/inet.h>
+
+#include <vcc/vcc.h>
+#include <vcc/pretty.h>
+#include <vcc/plugin.h>
+#include <vcc/version.h>
+
+
+int do_bh(struct vcc_request *);
+int send_msg(char *, char *);
+int robot_lslcn(char *s);
+int do_robot_cmd(char *s);
+
+struct cmdtable robot_cmdtab[] = {
+	{ NULL, NULL, NULL }
+};
+
+struct plugin plg_robot = {
+	.state = 0, 
+	.name = "robot", 
+	.descr = "The robot plugin. ", 
+	.cmdtab = robot_cmdtab
+};
+
+static struct cmdtable commands[] = {
+	{ "lslcn", robot_lslcn, "List articles of LCTT. " }, 
+	{ NULL, NULL, NULL }
+};
+
+#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)) 
+		return 0;
+
+	do_robot_cmd(msg);
+
+	return 0;
+}
+
+typedef int (* fptr_t) (char *);
+fptr_t execute_in(struct cmdtable *, char *, int);
+
+int do_robot_cmd(char *msg) {
+	fptr_t f;
+	int len;
+	char *p;
+	
+	p = msg += 7;
+
+	for (len = 0; *p && *p != '\n'; p++, len++) 
+		;
+
+	if (!(f = execute_in(commands, msg, len))) {
+		send_msg("Are you calling me? I don't know what's your meaning. \n", "robot");
+
+		return 0;
+	}
+
+	return f(msg);
+}
+
+

+ 111 - 0
robot.c

@@ -0,0 +1,111 @@
+/* vcc/robot.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 int fd;
+int vcc_login(char *, char *);
+int robot_new_msg(struct vcc_request *);
+
+
+int becomes_daemon(void) {
+	int pid, fd;
+
+	if ((pid = fork())) 
+		/* then prompt was there again */
+		exit(0);
+
+	umask(0);
+	chdir("/");
+
+	close(0);
+
+	if (unlikely(open("/var/log/vcc.log", O_WRONLY | O_CREAT, 0644) < 0)) {
+		perror("open(/var/run/vcc.log)");
+
+		exit(1);
+	}
+
+	close(1);
+	close(2);
+
+	(void) dup(0);
+	(void) dup(0);
+
+	setsid();
+
+	if (unlikely((fd = open("/var/run/vcc.pid", O_WRONLY | O_CREAT, 0644))) < 0) {
+		perror("open(/var/run/vcc.pid)");
+
+		exit(1);
+	}
+
+	dprintf(fd, "%d\n", getpid());
+	close(fd);
+
+	/* fd 3 is now /var/run/vcc.pid */
+	printf(VCC_VERSION " started. \n");
+
+	return 0;
+}
+
+
+int robot(void) {
+	struct vcc_request req;
+
+	plg_insert(find_plg("robot"));
+	plg_insert(find_plg("lcn"));
+//	becomes_daemon();
+
+	vcc_login("robot", "robot");
+
+	for (;;) {
+		memset(&req, 0, REQ_SIZE);
+		read(fd, &req, REQ_SIZE);
+
+		robot_new_msg(&req);
+	}
+
+	return 0;
+}
+
+

+ 3 - 3
vcc.c

@@ -101,14 +101,14 @@ int __send_msg(const char *msg, const char *usrname, int flags) {
 
 	clear_req(&req);
 
-	req.magic = htonl(VCC_MAGIC_CR);
+	req.magic = htonl(VCC_MAGIC);
 	req.reqtype = htonl(REQ_MSG_SEND);
 	req.uid = 0;
 	req.session = htonl(current_sid);
 	req.flags = htonl(flags);
        
 	strncpy(req.usrname, usrname, USRNAME_SIZE);
-	memcpy(req.msg, msg, MSG_SIZE);
+	strncpy(req.msg, msg, MSG_SIZE);
 	write(fd, &req, REQ_SIZE);
 
 	return 0;
@@ -195,7 +195,7 @@ int do_bh(struct vcc_request *req) {
 		break;
 
 	default:
-		fprintf(stderr, "*** unexpected packet type %d. \n", ntohl(req->reqtype));
+//		fprintf(stderr, "*** unexpected packet type %d. \n", ntohl(req->reqtype));
 
 		break;
 	}