Browse Source

vcc: send magic

intirain 4 years ago
parent
commit
0d0d8d3914
6 changed files with 259 additions and 5 deletions
  1. 1 1
      Makefile
  2. 77 0
      include/vcc/pretty.h
  3. 1 1
      include/vcc/version.h
  4. 10 3
      main.c
  5. 107 0
      pretty.c
  6. 63 0
      vcc.c

+ 1 - 1
Makefile

@@ -1,5 +1,5 @@
 VCCD_MAJOR 		= 0
-VCCD_MINOR 		= 3
+VCCD_MINOR 		= 4
 VCCD_PATCH 		= 0
 VCCD_COMPILE_TIME 	= $(shell date)
 VCCD_COMPILE_BY 	= $(shell whoami)

+ 77 - 0
include/vcc/pretty.h

@@ -0,0 +1,77 @@
+/* vccd/include/vcc/pretty.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_PRETTY_H
+#define __VCC_PRETTY_H
+
+#include <stdio.h>
+
+#define FG_BLACK 		30
+#define FG_RED 			31
+#define FG_GREEN 		32
+#define FG_YELLOW 		33
+#define FG_BLUE 		34
+#define FG_WHITE 		35
+#define FG_LIGHTBLUE 		36
+
+#define BG_BLACK 		40
+#define BG_RED 			41
+#define BG_GREEN 		42
+#define BG_YELLOW 		43
+#define BG_BLUE 		44
+#define BG_WHITE 		47
+
+#define MODE_DEFAULT 		0
+#define MODE_HIGHLIGHT 		1
+#define MODE_LINE 		4
+
+#define ansi_csi_st(strm, m, fg, bg) 	\
+	fprintf(strm, "\033[%d;%d;%dm", m, fg, bg)
+
+#define ansi_csi_ed(strm) 		\
+	fprintf(strm, "\033[0m")
+
+#define setcolor(strm, c) 		\
+	ansi_csi_st(strm, (c)->mode, (c)->fg, (c)->bg)
+
+
+struct color {
+	int 		fg;
+	int 		bg;
+	int 		mode;
+};
+
+
+struct theme {
+	struct color 	usrname, 
+			sysusername, 
+			msg, 
+			sysmsg;
+};
+
+
+int pretty_new_msg(char *usrname, char *msg);
+int pretty_prompt(char *usrname);
+
+
+
+#endif
+

+ 1 - 1
include/vcc/version.h

@@ -1 +1 @@
-#define VCCD_VERSION "vccd 0.3.0 Sun Mar  6 12:54:48 CST 2022 root"
+#define VCCD_VERSION "vccd 0.4.0 Sun Mar  6 13:04:25 CST 2022 root"

+ 10 - 3
main.c

@@ -44,7 +44,7 @@ int read_msg(struct vcc_request *buf);
 
 
 int fd;
-static unsigned char empty_package[REQ_SIZE];
+char *empty_package;
 
 static int usage(void) {
 	fprintf(stderr, "Usage: vcc [server]\n");
@@ -99,8 +99,7 @@ static int init_socket(const char *server_addr) {
 void alarm_handler(int nr) {
 	alarm(1);
 
-	memset(empty_package, 0xff, REQ_SIZE);
-	write(fd, empty_package, REQ_SIZE);
+	send_msg(empty_package, "msgcheck");
 
 	return;
 }
@@ -108,6 +107,14 @@ void alarm_handler(int nr) {
 static int setup_alarm(void) {
 	struct sigaction act;
 
+	if (unlikely(!(empty_package = malloc(MSG_SIZE)))) {
+		fprintf(stderr, "*** malloc() failed\n");
+
+		exit(1);
+	}
+
+	memset(empty_package, 0xff, MSG_SIZE);
+
 	act.sa_handler = alarm_handler;
 	act.sa_flags = SA_RESTART;
 

+ 107 - 0
pretty.c

@@ -0,0 +1,107 @@
+/* vcc/pretty.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/version.h>
+#include <vcc/pretty.h>
+
+
+struct theme theme_def = {
+	.usrname = {
+		.fg = FG_LIGHTBLUE, 
+		.bg = BG_BLACK, 
+		.mode = MODE_LINE
+	}, 
+	
+	.sysusername = {
+		.fg = FG_RED, 
+		.bg = BG_BLACK, 
+		.mode = MODE_LINE
+	}, 
+
+	.msg = {
+		.fg = FG_YELLOW, 
+		.bg = BG_BLACK, 
+		.mode = MODE_HIGHLIGHT
+	}, 
+
+	.sysmsg = {
+		.fg = FG_GREEN, 
+		.bg = BG_BLACK, 
+		.mode = MODE_HIGHLIGHT
+	}
+};
+
+struct theme *current_theme = &theme_def;
+
+#define curr_usrname 		(&current_theme->usrname)
+#define curr_sysusername 	(&current_theme->sysusername)
+#define curr_msg 		(&current_theme->msg)
+#define curr_sysmsg 		(&current_theme->sysmsg)
+
+
+int pretty_new_msg(char *usrname, char *buf) {
+	ansi_csi_st(stdout, MODE_HIGHLIGHT, FG_BLACK, BG_BLACK);
+
+	if (unlikely(!strcmp(usrname, "system"))) {
+		setcolor(stdout, curr_sysusername);
+		printf("\rsystem");
+	}
+
+	else {
+		setcolor(stdout, curr_usrname);
+		printf("\r%s", usrname);
+	}
+
+	ansi_csi_ed(stdout);
+
+	printf("@: ");
+
+	if (unlikely(!strcmp(usrname, "system"))) {
+		setcolor(stdout, curr_sysmsg);
+
+		printf("%s", buf);
+	}
+
+	else {
+		setcolor(stdout, curr_msg);
+		printf("%s", buf);
+	}
+
+	ansi_csi_ed(stdout);
+
+	return 0;
+}
+
+int pretty_prompt(char *usrname) {
+	ansi_csi_st(stderr, MODE_HIGHLIGHT, FG_BLACK, BG_BLACK);
+	setcolor(stderr, curr_usrname);
+	fprintf(stderr, "%s", usrname);
+	
+	ansi_csi_ed(stderr);
+	fprintf(stderr, "$: ");
+
+	return 0;
+}
+

+ 63 - 0
vcc.c

@@ -0,0 +1,63 @@
+/* vcc/vcc.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 <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 <vcc/vcc.h>
+#include <vcc/version.h>
+
+
+
+extern int fd;
+
+int send_msg(const char *msg, const char *usrname) {
+	struct vcc_request req;
+
+	req.magic = htonl(VCC_MAGIC);
+	req.reqtype = htonl(REQ_MSG_SEND);
+	req.uid = 0;
+       
+	strcpy(req.usrname, usrname);
+	strcpy(req.msg, msg);
+	write(fd, &req, REQ_SIZE);
+
+	return 0;
+}
+
+
+int read_msg(struct vcc_request *buf) {
+	return read(fd, buf, REQ_SIZE);
+}
+
+