From b16ca3fa64fc1fa83d40d00d0090cc7fd27a9840 Mon Sep 17 00:00:00 2001 From: Berke Viktor Date: Tue, 29 Nov 2011 20:15:56 +0100 Subject: [PATCH] initial plugin config framework, can't save multiple entries --- plugins/xchat-plugin.h | 20 +++++++++ src/common/cfgfiles.c | 2 +- src/common/cfgfiles.h | 1 + src/common/plugin.c | 89 +++++++++++++++++++++++++++++++++++++++ src/common/plugin.h | 7 +++ src/common/util.c | 18 ++++++++ src/common/util.h | 1 + src/common/xchat-plugin.h | 20 +++++++++ 8 files changed, 157 insertions(+), 1 deletion(-) diff --git a/plugins/xchat-plugin.h b/plugins/xchat-plugin.h index 30b19295..e9dd1be8 100644 --- a/plugins/xchat-plugin.h +++ b/plugins/xchat-plugin.h @@ -137,6 +137,13 @@ struct _xchat_plugin int flags); void (*xchat_free) (xchat_plugin *ph, 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 @@ -292,6 +299,17 @@ void xchat_free (xchat_plugin *ph, 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) #ifndef XCHAT_PLUGIN_HANDLE #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_strip ((XCHAT_PLUGIN_HANDLE)->xchat_strip) #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 #ifdef __cplusplus diff --git a/src/common/cfgfiles.c b/src/common/cfgfiles.c index e5d0eaef..ff4f3756 100644 --- a/src/common/cfgfiles.c +++ b/src/common/cfgfiles.c @@ -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) { char buf[512]; diff --git a/src/common/cfgfiles.h b/src/common/cfgfiles.h index 984b9472..59fc34b0 100644 --- a/src/common/cfgfiles.h +++ b/src/common/cfgfiles.h @@ -9,6 +9,7 @@ extern char *xdir_fs; extern char *xdir_utf; 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_int_with_result (char *cfg, char *var, int *result); int cfg_get_int (char *cfg, char *var); diff --git a/src/common/plugin.c b/src/common/plugin.c index b40660a1..ffdc013e 100644 --- a/src/common/plugin.c +++ b/src/common/plugin.c @@ -20,6 +20,8 @@ #include #include #include +#include +#include #include "xchat.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_strip = xchat_strip; 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 */ pl->xchat_dummy4 = xchat_dummy; @@ -1570,3 +1574,88 @@ xchat_free (xchat_plugin *ph, void *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; + } +} diff --git a/src/common/plugin.h b/src/common/plugin.h index b0c89d1b..6a2d62e2 100644 --- a/src/common/plugin.h +++ b/src/common/plugin.h @@ -98,6 +98,13 @@ struct _xchat_plugin int flags); void (*xchat_free) (xchat_plugin *ph, 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_dummy3) (xchat_plugin *ph); void *(*xchat_dummy2) (xchat_plugin *ph); diff --git a/src/common/util.c b/src/common/util.c index 870f4d5c..c16d41f5 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -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); + } + } +} diff --git a/src/common/util.h b/src/common/util.h index 92d2a843..bee6ec3c 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -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_ihash (const unsigned char *key); void safe_strcpy (char *dest, const char *src, int bytes_left); +void canonalize_key (char *key); #endif diff --git a/src/common/xchat-plugin.h b/src/common/xchat-plugin.h index 30b19295..e9dd1be8 100644 --- a/src/common/xchat-plugin.h +++ b/src/common/xchat-plugin.h @@ -137,6 +137,13 @@ struct _xchat_plugin int flags); void (*xchat_free) (xchat_plugin *ph, 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 @@ -292,6 +299,17 @@ void xchat_free (xchat_plugin *ph, 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) #ifndef XCHAT_PLUGIN_HANDLE #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_strip ((XCHAT_PLUGIN_HANDLE)->xchat_strip) #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 #ifdef __cplusplus