Преглед на файлове

vcc: add themes that can be configured

intirain преди 4 години
родител
ревизия
ee007a1f39
променени са 12 файла, в които са добавени 230 реда и са изтрити 20 реда
  1. 17 6
      Makefile
  2. 12 0
      gnr_load_themes.m4
  3. 17 0
      gnr_theme_macros.m4
  4. 1 0
      include/vcc/interfaces.h
  5. 2 0
      include/vcc/pretty.h
  6. 2 2
      include/vcc/version.h
  7. 13 0
      libs/Makefile
  8. BIN
      libs/libreadconf.a
  9. 1 0
      libs/readconf
  10. 1 0
      main.c
  11. 81 12
      pretty.c
  12. 83 0
      scripts/mktheme.py

+ 17 - 6
Makefile

@@ -1,6 +1,6 @@
 VCC_MAJOR 		= 1
-VCC_MINOR 		= 24
-VCC_PATCH 		= 1
+VCC_MINOR 		= 25
+VCC_PATCH 		= 0
 VCC_STR 		= -rc 
 
 # change this to '/usr/share/' or '/opt/vcc/' etc
@@ -11,27 +11,36 @@ VCC_COMPILE_TIME 	= $(shell date)
 VCC_COMPILE_BY 		= $(shell whoami)
 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 
 target 			= vcc 
 libgcrypt_cflags 	= $(shell libgcrypt-config --libs)
-CFLAGS 			= -std=gnu11 -Wall -Wextra -Werror -Iinclude -g $(libgcrypt_cflags) -Lplugins -lplugins 
+CFLAGS 			= -std=gnu11 -Wall -Wextra -Werror -Iinclude -g $(libgcrypt_cflags) -Lplugins -lplugins \
+			  -Llibs -lreadconf -Ilibs/readconf/include 
 MAKEFLAGS 		= --no-print-directory
 VCC_DEFSERV 		= 39.104.27.216
 VCC_VERSION 		= VCC $(VCC_MAJOR).$(VCC_MINOR).$(VCC_PATCH) \
 		  $(VCC_COMPILE_TIME) $(VCC_COMPILE_BY)
 
-all : 
+all : macros
 	@echo "$(VCC_VERSION)"
 	@echo "$(VCC_SEMVER)"
 	@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 
 	@echo "#define INSTALL_PATH \"$(INSTALL)\"" > include/vcc/config.h 
+	@$(MAKE) -C libs/ 
 	@$(MAKE) -C plugins/ 
 	@$(MAKE) $(target) $(MAKEFLAGS)
 	@$(MAKE) mo
 
+macros : $(macro_srcs) $(macro_objs)
+%.s : %.m4  
+	@m4 $*.m4 > $*.s 
+	@echo -e "M4\t\t\t$*.m4"
+
 mo : 
 	@mkdir -p $(MO_DIR)
 	@msgfmt --check --verbose --output-file $(MO_DIR)/vcc.mo po/zh_CN.po 
@@ -45,9 +54,11 @@ $(target) : 	$(objs) $(srcs)
 	@$(CC) -c $(CFLAGS) $*.c 
 	@echo -e "CC\t\t\t$*.c"
 
-.PHONY clean : 
-	$(RM) $(objs) $(target) -f  
+.PHONY : clean
+clean :
+	$(RM) $(objs) $(target) $(macro_objs) -f 
 	$(RM) -rf lang 
+	@$(MAKE) -C libs/ clean
 	@$(MAKE) -C plugins/ clean
 
 w :

+ 12 - 0
gnr_load_themes.m4

@@ -0,0 +1,12 @@
+divert(-1)
+define(load, `parse_theme("$1", curr_$1) + ')
+divert(0)dnl
+`if (('
+load(usrname)load(sysusername)load(msg)load(sysmsg)
+load(sid)load(lvl)load(time)load(relay)load(errmsg)
+load(helpmsg)load(cqd)
+`0)) {
+	fprintf(stderr, "load theme failed. \n");
+	return 0;
+}'
+

+ 17 - 0
gnr_theme_macros.m4

@@ -0,0 +1,17 @@
+dnl the C pre-processor is not so good, so let's use m4. 
+divert(-1)
+define(MKCURR, `struct color *curr_$1 = &theme_def.$1;')
+divert(0)dnl
+MKCURR(usrname)
+MKCURR(sysusername)
+MKCURR(msg)
+MKCURR(sysmsg)
+MKCURR(sid)
+MKCURR(lvl)
+MKCURR(time)
+MKCURR(relay)
+MKCURR(errmsg)
+MKCURR(helpcmd)
+MKCURR(helpmsg)
+MKCURR(cqd)
+

+ 1 - 0
include/vcc/interfaces.h

@@ -35,6 +35,7 @@ int read_msg(struct vcc_request *buf);
 char *relay_get_msg(int fd, struct vcc_relay_header *);
 
 int is_user_banned(char *);
+int pretty_init(void);
 
 int show_algo_info(void);
 int crypt_init(void *key, void *iv);

+ 2 - 0
include/vcc/pretty.h

@@ -83,6 +83,8 @@ struct theme {
 			cqd;
 };
 
+#define NR_THEMES 	12
+
 
 int pretty_new_msg(char *usrname, char *msg, int sid, int flags);
 int pretty_prompt(char *usrname);

+ 2 - 2
include/vcc/version.h

@@ -1,2 +1,2 @@
-#define VCC_VERSION "VCC 1.24.1 2022年 06月 03日 星期五 21:39:32 CST root"
-#define VCC_SEMVER "vcc 1.24.1-rc "
+#define VCC_VERSION "VCC 1.25.0 Sat Jun  4 07:05:49 PM CST 2022 root"
+#define VCC_SEMVER "vcc 1.25.0-rc "

+ 13 - 0
libs/Makefile

@@ -0,0 +1,13 @@
+libs 	= readconf
+
+all : 
+	@for i in $(libs); do \
+		$(MAKE) -C $$i; \
+	done;
+
+.PHONY : clean
+clean : 
+	@for i in $(libs); do \
+		$(MAKE) -C $$i clean; \
+	done;	
+	

BIN
libs/libreadconf.a


+ 1 - 0
libs/readconf

@@ -0,0 +1 @@
+Subproject commit d11792c2e6349083266a137dba7d579b3c983282

+ 1 - 0
main.c

@@ -351,6 +351,7 @@ int main(int argc, char **argv) {
 	fds[1].revents = 0;
 
 	init_signal();
+	pretty_init();
 
 	if (unlikely(!(buf = malloc(PATH_MAX)))) {
 		fprintf(stderr, "*** malloc() failed\n");

+ 81 - 12
pretty.c

@@ -22,6 +22,11 @@
 #include <stdio.h>
 #include <string.h>
 #include <time.h>
+#include <limits.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include <sys/types.h>
 #include <sys/times.h>
 
 #include <vcc/vcc.h>
@@ -29,6 +34,7 @@
 #include <vcc/pretty.h>
 #include <vcc/plugin.h>
 #include <vcc/interfaces.h>
+#include <readconf/readconf.h>
 
 
 struct theme theme_def = {
@@ -107,19 +113,82 @@ struct theme theme_def = {
 
 struct theme *current_theme = &theme_def;
 int self_level;
+int conf_fd;
+
+#include "gnr_theme_macros.s"
+
+
+int open_pretty_conf(void) {
+	char 	*home;
+	char 	path[PATH_MAX];
+
+	if (unlikely(!(home = getenv("HOME")))) 
+		goto abandon;
+	
+	sprintf(path, "%s/.vcc-theme", home);
+
+	if ((conf_fd = open(path, O_RDONLY)) < 0) 
+		goto abandon;
+
+	printf("reading theme ...");
+	return 0;
+
+abandon:
+	fprintf(stderr, "using default theme. \n");
+	return 1;
+}
+
+int nr_ready = 0;
+
+int parse_theme(char *name, struct color *p) {
+	struct readconf_uintconf *conf;
+	char key[32];
+
+	sprintf(key, "%s_mode", name);
+
+#define tryload \
+	if (!(conf = readconf_find_uintname(key))) { \
+		fprintf(stderr, "cannot load theme %s\n", name); \
+		return 1; \
+	}
+
+	tryload;
+	p->mode = conf->conf_value;
+
+	memset(key, 0, 32);
+	sprintf(key, "%s_fg", name);
+
+	tryload;
+	p->fg = conf->conf_value;
+
+	memset(key, 0, 32);
+	sprintf(key, "%s_bg", name);
+
+	tryload;
+	p->bg = conf->conf_value;
+
+	return 0;
+}
+
+int load_themes(void) {
+#include "gnr_load_themes.s"
+
+	return 0;
+}
+
+
+int pretty_init(void) {
+	if (open_pretty_conf()) 
+		return 0;
+
+	readconf_init();
+	load_themes();
+
+	close(conf_fd);
+
+	return 0;
+}
 
-#define curr_usrname 		(&current_theme->usrname)
-#define curr_sysusername 	(&current_theme->sysusername)
-#define curr_msg 		(&current_theme->msg)
-#define curr_sysmsg 		(&current_theme->sysmsg)
-#define curr_sid 		(&current_theme->sid)
-#define curr_lvl 		(&current_theme->lvl)
-#define curr_time 		(&current_theme->time)
-#define curr_relay 		(&current_theme->relay)
-#define curr_errmsg 		(&current_theme->errmsg)
-#define curr_helpcmd 		(&current_theme->helpcmd)
-#define curr_helpmsg 		(&current_theme->helpmsg)
-#define curr_cqd 		(&current_theme->cqd)
 
 
 int pretty_new_msg(char *usrname, char *buf, int sid, int flags) {

+ 83 - 0
scripts/mktheme.py

@@ -0,0 +1,83 @@
+#!/bin/python3
+
+# vcc/scripts/mktheme.py 
+# 
+# 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/>
+
+
+
+
+import os
+import sys
+from random import choice
+
+random = False
+
+
+modes = {'def': 0, 'high': 1, 'under': 4, 'blink': 5}
+fgs = {'black': 30, 'red': 31, 'green': 32, 'yellow': 33, 'blue': 34, \
+        'magenta': 35, 'lightblue': 36, 'white': 37, 'gray': 90}
+bgs = {'black': 40, 'red': '41', 'green': 42, 'yellow': 43, 'blue': 44, \
+        'magenta': 45, 'lightblue': 46, 'white': 47, 'gray': 100}
+
+fd = open("{}/.vcc-theme".format(os.getenv("HOME")), "w")
+
+if len(sys.argv) == 2 and sys.argv[1] == '--random':
+    random = True
+
+else:
+    print("modes: ", modes)
+    print("fgs: ", fgs)
+    print("bgs: ", bgs)
+
+def put(s, n, d):
+    fd.write("seti {s}_{n} = {d}\n".format(s=s, n=n, d=d))
+
+def get(s):
+    l = {'mode': modes, 'fg': fgs, 'bg': bgs}
+
+    for k, v in l.items():
+        if random:
+            put(s, k, choice(list(v.values())))
+            continue
+
+        while True:
+            print("[%s] %s: " % (s, k), end='')
+
+            try:
+                n = input()
+                put(s, k, v[n])
+
+                break
+
+            except KeyboardInterrupt:
+                print('')
+
+                continue
+
+            except KeyError:
+                if n == 'quit':
+                    exit(1)
+
+                print("Invalid input. ")
+                continue
+
+entries = ['usrname', 'sysusername', 'msg', 'sysmsg', 'sid', 'lvl', 'time', \
+        'relay', 'errmsg', 'helpcmd', 'helpmsg', 'cqd']
+
+for i in entries:
+    get(i)
+