initial plugin config framework, can't save multiple entries
This commit is contained in:
		
							parent
							
								
									266a86d0b5
								
							
						
					
					
						commit
						b16ca3fa64
					
				
					 8 changed files with 157 additions and 1 deletions
				
			
		|  | @ -137,6 +137,13 @@ struct _xchat_plugin | ||||||
| 	     int flags); | 	     int flags); | ||||||
| 	void (*xchat_free) (xchat_plugin *ph, | 	void (*xchat_free) (xchat_plugin *ph, | ||||||
| 	    void *ptr); | 	    void *ptr); | ||||||
|  | 	int (*xchat_set_plugin_pref) (xchat_plugin *ph, | ||||||
|  | 		char *var, | ||||||
|  | 		char *value); | ||||||
|  | 	int (*xchat_get_plugin_pref) (xchat_plugin *ph, | ||||||
|  | 		char *var, | ||||||
|  | 		char *dest, | ||||||
|  | 		int dest_len); | ||||||
| }; | }; | ||||||
| #endif | #endif | ||||||
| 
 | 
 | ||||||
|  | @ -292,6 +299,17 @@ void | ||||||
| xchat_free (xchat_plugin *ph, | xchat_free (xchat_plugin *ph, | ||||||
| 	    void *ptr); | 	    void *ptr); | ||||||
| 
 | 
 | ||||||
|  | int | ||||||
|  | xchat_set_plugin_pref (xchat_plugin *ph, | ||||||
|  | 		char *var, | ||||||
|  | 		char *value); | ||||||
|  | 
 | ||||||
|  | int | ||||||
|  | xchat_get_plugin_pref (xchat_plugin *ph, | ||||||
|  | 		char *var, | ||||||
|  | 		char *dest, | ||||||
|  | 		int dest_len); | ||||||
|  | 
 | ||||||
| #if !defined(PLUGIN_C) && defined(WIN32) | #if !defined(PLUGIN_C) && defined(WIN32) | ||||||
| #ifndef XCHAT_PLUGIN_HANDLE | #ifndef XCHAT_PLUGIN_HANDLE | ||||||
| #define XCHAT_PLUGIN_HANDLE (ph) | #define XCHAT_PLUGIN_HANDLE (ph) | ||||||
|  | @ -326,6 +344,8 @@ xchat_free (xchat_plugin *ph, | ||||||
| #define xchat_send_modes ((XCHAT_PLUGIN_HANDLE)->xchat_send_modes) | #define xchat_send_modes ((XCHAT_PLUGIN_HANDLE)->xchat_send_modes) | ||||||
| #define xchat_strip ((XCHAT_PLUGIN_HANDLE)->xchat_strip) | #define xchat_strip ((XCHAT_PLUGIN_HANDLE)->xchat_strip) | ||||||
| #define xchat_free ((XCHAT_PLUGIN_HANDLE)->xchat_free) | #define xchat_free ((XCHAT_PLUGIN_HANDLE)->xchat_free) | ||||||
|  | #define xchat_set_plugin_pref ((XCHAT_PLUGIN_HANDLE)->xchat_set_plugin_pref) | ||||||
|  | #define xchat_get_plugin_pref ((XCHAT_PLUGIN_HANDLE)->xchat_get_plugin_pref) | ||||||
| #endif | #endif | ||||||
| 
 | 
 | ||||||
| #ifdef __cplusplus | #ifdef __cplusplus | ||||||
|  |  | ||||||
|  | @ -197,7 +197,7 @@ cfg_get_str (char *cfg, char *var, char *dest, int dest_len) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| static int | int | ||||||
| cfg_put_str (int fh, char *var, char *value) | cfg_put_str (int fh, char *var, char *value) | ||||||
| { | { | ||||||
| 	char buf[512]; | 	char buf[512]; | ||||||
|  |  | ||||||
|  | @ -9,6 +9,7 @@ extern char *xdir_fs; | ||||||
| extern char *xdir_utf; | extern char *xdir_utf; | ||||||
| 
 | 
 | ||||||
| char *cfg_get_str (char *cfg, char *var, char *dest, int dest_len); | char *cfg_get_str (char *cfg, char *var, char *dest, int dest_len); | ||||||
|  | int cfg_put_str (int fh, char *var, char *value); | ||||||
| int cfg_get_bool (char *var); | int cfg_get_bool (char *var); | ||||||
| int cfg_get_int_with_result (char *cfg, char *var, int *result); | int cfg_get_int_with_result (char *cfg, char *var, int *result); | ||||||
| int cfg_get_int (char *cfg, char *var); | int cfg_get_int (char *cfg, char *var); | ||||||
|  |  | ||||||
|  | @ -20,6 +20,8 @@ | ||||||
| #include <string.h> | #include <string.h> | ||||||
| #include <stdarg.h> | #include <stdarg.h> | ||||||
| #include <stdio.h> | #include <stdio.h> | ||||||
|  | #include <fcntl.h> | ||||||
|  | #include <sys/stat.h> | ||||||
| 
 | 
 | ||||||
| #include "xchat.h" | #include "xchat.h" | ||||||
| #include "fe.h" | #include "fe.h" | ||||||
|  | @ -262,6 +264,8 @@ plugin_add (session *sess, char *filename, void *handle, void *init_func, | ||||||
| 		pl->xchat_send_modes = xchat_send_modes; | 		pl->xchat_send_modes = xchat_send_modes; | ||||||
| 		pl->xchat_strip = xchat_strip; | 		pl->xchat_strip = xchat_strip; | ||||||
| 		pl->xchat_free = xchat_free; | 		pl->xchat_free = xchat_free; | ||||||
|  | 		pl->xchat_set_plugin_pref = xchat_set_plugin_pref; | ||||||
|  | 		pl->xchat_get_plugin_pref = xchat_get_plugin_pref; | ||||||
| 
 | 
 | ||||||
| 		/* incase new plugins are loaded on older xchat */ | 		/* incase new plugins are loaded on older xchat */ | ||||||
| 		pl->xchat_dummy4 = xchat_dummy; | 		pl->xchat_dummy4 = xchat_dummy; | ||||||
|  | @ -1570,3 +1574,88 @@ xchat_free (xchat_plugin *ph, void *ptr) | ||||||
| { | { | ||||||
| 	g_free (ptr); | 	g_free (ptr); | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | int | ||||||
|  | xchat_set_plugin_pref (xchat_plugin *pl, char *var, char *value) | ||||||
|  | { | ||||||
|  | 	int fh; | ||||||
|  | 	char confname[32]; | ||||||
|  | 	char *canon; | ||||||
|  | 
 | ||||||
|  | 	canon = g_strdup (pl->name); | ||||||
|  | 	canonalize_key (canon); | ||||||
|  | 	sprintf (confname, "plugin_%s.conf", canon); | ||||||
|  | 	g_free (canon); | ||||||
|  | 
 | ||||||
|  | 	/* partly borrowed from palette.c */ | ||||||
|  | 	fh = xchat_open_file (confname, O_TRUNC | O_WRONLY | O_CREAT, 0600, XOF_DOMODE); | ||||||
|  | 	if (fh != -1) | ||||||
|  | 	{ | ||||||
|  | 		cfg_put_str (fh, var, value); | ||||||
|  | 		close (fh); | ||||||
|  | 
 | ||||||
|  | 		return 1; | ||||||
|  | 	} | ||||||
|  | 	else | ||||||
|  | 	{ | ||||||
|  | 		return 0; | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | int | ||||||
|  | xchat_get_plugin_pref (xchat_plugin *pl, char *var, char *dest, int dest_len) | ||||||
|  | { | ||||||
|  | 	//cfg_get_str (char *cfg, char *var, char *dest, int dest_len)
 | ||||||
|  | 	int fh; | ||||||
|  | 	int l; | ||||||
|  | 	char confname[32]; | ||||||
|  | 	//char *buffer;
 | ||||||
|  | 	char *canon; | ||||||
|  | 	char *cfg; | ||||||
|  | 	struct stat st; | ||||||
|  | 
 | ||||||
|  | 	canon = g_strdup (pl->name); | ||||||
|  | 	canonalize_key (canon); | ||||||
|  | 	sprintf (confname, "plugin_%s.conf", canon); | ||||||
|  | 	g_free (canon); | ||||||
|  | 
 | ||||||
|  | 	//buffer = (char*) malloc (dest_len);
 | ||||||
|  | 
 | ||||||
|  | 	/* partly borrowed from palette.c */ | ||||||
|  | 	fh = xchat_open_file (confname, O_RDONLY, 0, 0); | ||||||
|  | 
 | ||||||
|  | 	if (fh != -1) | ||||||
|  | 	{ | ||||||
|  | 		fstat (fh, &st); | ||||||
|  | 		cfg = malloc (st.st_size + 1); | ||||||
|  | 
 | ||||||
|  | 		if (cfg) | ||||||
|  | 		{ | ||||||
|  | 			cfg[0] = '\0'; | ||||||
|  | 			l = read (fh, cfg, st.st_size); | ||||||
|  | 
 | ||||||
|  | 			if (l >= 0) | ||||||
|  | 			{ | ||||||
|  | 				cfg[l] = '\0'; | ||||||
|  | 			} | ||||||
|  | 
 | ||||||
|  | 			if (!cfg_get_str (cfg, var, dest, dest_len)) | ||||||
|  | 			{ | ||||||
|  | 				return 0; | ||||||
|  | 			} | ||||||
|  | 
 | ||||||
|  | 			free (cfg); | ||||||
|  | 		} | ||||||
|  | 		else | ||||||
|  | 		{ | ||||||
|  | 			return 0; | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		close (fh); | ||||||
|  | 		return 1; | ||||||
|  | 	} | ||||||
|  | 	else | ||||||
|  | 	{ | ||||||
|  | 		return 0; | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -98,6 +98,13 @@ struct _xchat_plugin | ||||||
| 	     int flags); | 	     int flags); | ||||||
| 	void (*xchat_free) (xchat_plugin *ph, | 	void (*xchat_free) (xchat_plugin *ph, | ||||||
| 	    void *ptr); | 	    void *ptr); | ||||||
|  | 	int (*xchat_set_plugin_pref) (xchat_plugin *ph, | ||||||
|  | 		char *var, | ||||||
|  | 		char *value); | ||||||
|  | 	int (*xchat_get_plugin_pref) (xchat_plugin *ph, | ||||||
|  | 		char *var, | ||||||
|  | 		char *dest, | ||||||
|  | 		int dest_len); | ||||||
| 	void *(*xchat_dummy4) (xchat_plugin *ph); | 	void *(*xchat_dummy4) (xchat_plugin *ph); | ||||||
| 	void *(*xchat_dummy3) (xchat_plugin *ph); | 	void *(*xchat_dummy3) (xchat_plugin *ph); | ||||||
| 	void *(*xchat_dummy2) (xchat_plugin *ph); | 	void *(*xchat_dummy2) (xchat_plugin *ph); | ||||||
|  |  | ||||||
|  | @ -1830,3 +1830,21 @@ safe_strcpy (char *dest, const char *src, int bytes_left) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | void | ||||||
|  | canonalize_key (char *key) | ||||||
|  | { | ||||||
|  | 	char *pos, token; | ||||||
|  | 
 | ||||||
|  | 	for (pos = key; (token = *pos) != 0; pos++) | ||||||
|  | 	{ | ||||||
|  | 		if (token != '_' && (token < '0' || token > '9') && (token < 'A' || token > 'Z') && (token < 'a' || token > 'z')) | ||||||
|  | 		{ | ||||||
|  | 			*pos = '_'; | ||||||
|  | 		} | ||||||
|  | 		else | ||||||
|  | 		{ | ||||||
|  | 			*pos = tolower(token); | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -56,5 +56,6 @@ int token_foreach (char *str, char sep, int (*callback) (char *str, void *ud), v | ||||||
| guint32 str_hash (const char *key); | guint32 str_hash (const char *key); | ||||||
| guint32 str_ihash (const unsigned char *key); | guint32 str_ihash (const unsigned char *key); | ||||||
| void safe_strcpy (char *dest, const char *src, int bytes_left); | void safe_strcpy (char *dest, const char *src, int bytes_left); | ||||||
|  | void canonalize_key (char *key); | ||||||
| 
 | 
 | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|  | @ -137,6 +137,13 @@ struct _xchat_plugin | ||||||
| 	     int flags); | 	     int flags); | ||||||
| 	void (*xchat_free) (xchat_plugin *ph, | 	void (*xchat_free) (xchat_plugin *ph, | ||||||
| 	    void *ptr); | 	    void *ptr); | ||||||
|  | 	int (*xchat_set_plugin_pref) (xchat_plugin *ph, | ||||||
|  | 		char *var, | ||||||
|  | 		char *value); | ||||||
|  | 	int (*xchat_get_plugin_pref) (xchat_plugin *ph, | ||||||
|  | 		char *var, | ||||||
|  | 		char *dest, | ||||||
|  | 		int dest_len); | ||||||
| }; | }; | ||||||
| #endif | #endif | ||||||
| 
 | 
 | ||||||
|  | @ -292,6 +299,17 @@ void | ||||||
| xchat_free (xchat_plugin *ph, | xchat_free (xchat_plugin *ph, | ||||||
| 	    void *ptr); | 	    void *ptr); | ||||||
| 
 | 
 | ||||||
|  | int | ||||||
|  | xchat_set_plugin_pref (xchat_plugin *ph, | ||||||
|  | 		char *var, | ||||||
|  | 		char *value); | ||||||
|  | 
 | ||||||
|  | int | ||||||
|  | xchat_get_plugin_pref (xchat_plugin *ph, | ||||||
|  | 		char *var, | ||||||
|  | 		char *dest, | ||||||
|  | 		int dest_len); | ||||||
|  | 
 | ||||||
| #if !defined(PLUGIN_C) && defined(WIN32) | #if !defined(PLUGIN_C) && defined(WIN32) | ||||||
| #ifndef XCHAT_PLUGIN_HANDLE | #ifndef XCHAT_PLUGIN_HANDLE | ||||||
| #define XCHAT_PLUGIN_HANDLE (ph) | #define XCHAT_PLUGIN_HANDLE (ph) | ||||||
|  | @ -326,6 +344,8 @@ xchat_free (xchat_plugin *ph, | ||||||
| #define xchat_send_modes ((XCHAT_PLUGIN_HANDLE)->xchat_send_modes) | #define xchat_send_modes ((XCHAT_PLUGIN_HANDLE)->xchat_send_modes) | ||||||
| #define xchat_strip ((XCHAT_PLUGIN_HANDLE)->xchat_strip) | #define xchat_strip ((XCHAT_PLUGIN_HANDLE)->xchat_strip) | ||||||
| #define xchat_free ((XCHAT_PLUGIN_HANDLE)->xchat_free) | #define xchat_free ((XCHAT_PLUGIN_HANDLE)->xchat_free) | ||||||
|  | #define xchat_set_plugin_pref ((XCHAT_PLUGIN_HANDLE)->xchat_set_plugin_pref) | ||||||
|  | #define xchat_get_plugin_pref ((XCHAT_PLUGIN_HANDLE)->xchat_get_plugin_pref) | ||||||
| #endif | #endif | ||||||
| 
 | 
 | ||||||
| #ifdef __cplusplus | #ifdef __cplusplus | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue