diff options
Diffstat (limited to 'src/common/plugin.c')
-rw-r--r-- | src/common/plugin.c | 191 |
1 files changed, 191 insertions, 0 deletions
diff --git a/src/common/plugin.c b/src/common/plugin.c index ada4d3be..5ed20b87 100644 --- a/src/common/plugin.c +++ b/src/common/plugin.c @@ -20,6 +20,14 @@ #include <string.h> #include <stdarg.h> #include <stdio.h> +#include <fcntl.h> +#include <sys/stat.h> + +#ifdef WIN32 +#include <io.h> +#else +#include <unistd.h> +#endif #include "xchat.h" #include "fe.h" @@ -262,6 +270,10 @@ 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_pluginpref_str = xchat_set_pluginpref_str; + pl->xchat_get_pluginpref_str = xchat_get_pluginpref_str; + pl->xchat_set_pluginpref_int = xchat_set_pluginpref_int; + pl->xchat_get_pluginpref_int = xchat_get_pluginpref_int; /* incase new plugins are loaded on older xchat */ pl->xchat_dummy4 = xchat_dummy; @@ -996,13 +1008,20 @@ xchat_get_info (xchat_plugin *ph, const char *id) return XCHATLIBDIR; case 0x14f51cd8: /* version */ +#ifdef WIN32 + return XCHAT_RELEASE; +#else return PACKAGE_VERSION; +#endif case 0xdd9b1abd: /* xchatdir */ return get_xdir_utf8 (); case 0xe33f6c4a: /* xchatdirfs */ return get_xdir_fs (); + + case 0x3d1e70d7: /* wdk_version */ + return PACKAGE_VERSION; } sess = ph->context; @@ -1567,3 +1586,175 @@ xchat_free (xchat_plugin *ph, void *ptr) { g_free (ptr); } + +int +xchat_set_pluginpref_str (xchat_plugin *pl, const char *var, const char *value) +{ + FILE *fpIn; + int fhOut; + int prevConfig; + char confname[64]; + char confname_tmp[69]; + char buffer[512]; /* the same as in cfg_put_str */ + char buffer_tmp[512]; + char *canon; + + canon = g_strdup (pl->name); + canonalize_key (canon); + sprintf (confname, "plugin_%s.conf", canon); + g_free (canon); + sprintf (confname_tmp, "%s.new", confname); + + fhOut = xchat_open_file (confname_tmp, O_TRUNC | O_WRONLY | O_CREAT, 0600, XOF_DOMODE); + fpIn = xchat_fopen_file (confname, "r", 0); + + if (fhOut == -1) /* unable to save, abort */ + { + return 0; + } + else if (fpIn == NULL) /* no previous config, no parsing */ + { + sprintf (buffer, "%s = %s\n", var, value); + write (fhOut, buffer, strlen (buffer)); + close (fhOut); + + sprintf (buffer, "%s/%s", get_xdir_fs (), confname); + sprintf (buffer_tmp, "%s/%s", get_xdir_fs (), confname_tmp); + +#ifdef WIN32 + unlink (buffer); +#endif + + if (rename (buffer_tmp, buffer) == 0) + { + return 1; + } + else + { + return 0; + } + } + else /* existing config, preserve settings and find & replace current var value if any */ + { + prevConfig = 0; + + while (fscanf (fpIn, " %[^\n]", &buffer) != EOF) /* read whole lines including whitespaces */ + { + sprintf (buffer_tmp, "%s ", var); /* add one space, this way it works against var - var2 checks too */ + + if (strncmp (buffer_tmp, buffer, strlen (var) + 1) == 0) /* given setting already exists */ + { + sprintf (buffer, "%s = %s\n", var, value); + prevConfig = 1; + } + else + { + strcat (buffer, "\n"); + } + + write (fhOut, buffer, strlen (buffer)); + } + + fclose (fpIn); + + if (!prevConfig) /* var doesn't exist currently, append */ + { + sprintf (buffer, "%s = %s\n", var, value); + write (fhOut, buffer, strlen (buffer)); + } + + close (fhOut); + + sprintf (buffer, "%s/%s", get_xdir_fs (), confname); + sprintf (buffer_tmp, "%s/%s", get_xdir_fs (), confname_tmp); + +#ifdef WIN32 + unlink (buffer); +#endif + + if (rename (buffer_tmp, buffer) == 0) + { + return 1; + } + else + { + return 0; + } + } +} + +int +xchat_get_pluginpref_str (xchat_plugin *pl, const char *var, char *dest) +{ + int fh; + int l; + char confname[64]; + char *canon; + char *cfg; + struct stat st; + + 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_RDONLY, 0, 0); + + if (fh == -1) + { + return 0; + } + + fstat (fh, &st); + cfg = malloc (st.st_size + 1); + + if (!cfg) + { + close (fh); + return 0; + } + + cfg[0] = '\0'; + l = read (fh, cfg, st.st_size); + + if (l >= 0) + { + cfg[l] = '\0'; + } + + if (!cfg_get_str (cfg, var, dest, 512)) /* dest_len is the same as buffer size in set */ + { + free (cfg); + close (fh); + return 0; + } + + free (cfg); + close (fh); + return 1; +} + +int +xchat_set_pluginpref_int (xchat_plugin *pl, const char *var, int value) +{ + char buffer[12]; + + sprintf (buffer, "%d", value); + return xchat_set_pluginpref_str (pl, var, buffer); +} + +int +xchat_get_pluginpref_int (xchat_plugin *pl, const char *var) +{ + char buffer[12]; + + if (xchat_get_pluginpref_str (pl, var, buffer)) + { + return atoi (buffer); + } + else + { + return -1; + } +} |