Use <configdir>/scripts to (auto)load Lua/Perl/Python/Tcl scripts

This commit is contained in:
Berke Viktor 2012-07-21 19:16:31 +02:00
parent 9ea3ac9ddd
commit 2b3e1f46e3
5 changed files with 45 additions and 26 deletions

8
README
View File

@ -84,10 +84,10 @@ Perl Scripts:
Autoloading Perl Scripts and Plugins Autoloading Perl Scripts and Plugins
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* HexChat automatically loads, at startup: * HexChat automatically loads, at startup:
~/.config/hexchat/*.pl Perl scripts ~/.config/hexchat/scripts/*.lua Lua scripts
~/.config/hexchat/plugins/*.pl Perl scripts ~/.config/hexchat/scripts/*.pl Perl scripts
~/.config/hexchat/*.py Python scripts ~/.config/hexchat/scripts/*.py Python scripts
~/.config/hexchat/plugins/*.py Python scripts ~/.config/hexchat/scripts/*.tcl Tcl scripts
~/.config/hexchat/*.so Plugins ~/.config/hexchat/*.so Plugins
$(libdir)/hexchat/plugins/*.so plugins $(libdir)/hexchat/plugins/*.so plugins
(this usually translates to /usr/lib/hexchat/plugins/*.so) (this usually translates to /usr/lib/hexchat/plugins/*.so)

View File

@ -518,14 +518,16 @@ static int lxc_cb_load(char *word[], char *word_eol[], void *userdata)
strncpy(file, word[2], PATH_MAX); strncpy(file, word[2], PATH_MAX);
else { else {
if (stat(word[2], st) == 0) if (stat(word[2], st) == 0)
{
xdir = getcwd (buf, PATH_MAX); xdir = getcwd (buf, PATH_MAX);
else {
xdir = xchat_get_info(ph, "xchatdirfs");
if (!xdir) /* xchatdirfs is new for 2.0.9, will fail on older */
xdir = xchat_get_info (ph, "xchatdir");
}
snprintf (file, PATH_MAX, "%s/%s", xdir, word[2]); snprintf (file, PATH_MAX, "%s/%s", xdir, word[2]);
} }
else
{
xdir = xchat_get_info (ph, "xchatdirfs");
snprintf (file, PATH_MAX, "%s/scripts/%s", xdir, word[2]);
}
}
if (lxc_load_file((const char *)file) == 0) { if (lxc_load_file((const char *)file) == 0) {
free(st); free(st);
@ -663,6 +665,7 @@ int xchat_plugin_init(xchat_plugin *plugin_handle,
lua_State *L; lua_State *L;
const char *xdir; const char *xdir;
const char *name, *desc, *vers; const char *name, *desc, *vers;
char *xsubdir;
/* we need to save this for use with any xchat_* functions */ /* we need to save this for use with any xchat_* functions */
ph = plugin_handle; ph = plugin_handle;
@ -676,10 +679,12 @@ int xchat_plugin_init(xchat_plugin *plugin_handle,
xchat_hook_command(ph, "LUA", XCHAT_PRI_NORM, lxc_cb_lua, "Usage: LUA <code>, executes <code> in a new lua state", NULL); xchat_hook_command(ph, "LUA", XCHAT_PRI_NORM, lxc_cb_lua, "Usage: LUA <code>, executes <code> in a new lua state", NULL);
xdir = xchat_get_info (ph, "xchatdirfs"); xdir = xchat_get_info (ph, "xchatdirfs");
if (!xdir) /* xchatdirfs is new for 2.0.9, will fail on older */ xsubdir = g_build_filename (xdir, "scripts", NULL);
xdir = xchat_get_info (ph, "xchatdir"); lxc_autoload_from_path (xsubdir);
g_free (xsubdir);
lxc_autoload_from_path(xdir); /* put this here, otherwise it's only displayed when a script is autoloaded upon start */
xchat_printf(ph, "Lua interface loaded");
if (!lxc_states) /* no scripts loaded */ if (!lxc_states) /* no scripts loaded */
return 1; return 1;
@ -716,7 +721,6 @@ int xchat_plugin_init(xchat_plugin *plugin_handle,
} }
state = state->next; state = state->next;
} }
xchat_printf(ph, "Lua interface (v%s) loaded", LXC_VERSION);
return 1; return 1;
} }
@ -732,7 +736,7 @@ int xchat_plugin_deinit(xchat_plugin *plug_handle)
state = state->next; state = state->next;
free(st); free(st);
} }
xchat_printf(plug_handle, "Lua plugin v%s removed", LXC_VERSION); xchat_printf(plug_handle, "Lua interface unloaded");
return 1; return 1;
} }

View File

@ -144,15 +144,20 @@ perl_auto_load (void *unused)
if (!xdir) /* xchatdirfs is new for 2.0.9, will fail on older */ if (!xdir) /* xchatdirfs is new for 2.0.9, will fail on older */
xdir = xchat_get_info (ph, "xchatdir"); xdir = xchat_get_info (ph, "xchatdir");
/* don't pollute the filesystem with script files, this only causes misuse of the folders
* only use ~/.config/hexchat/scripts/ and %APPDATA%\HexChat\scripts */
#if 0
/* autoload from ~/.config/hexchat/ or %APPDATA%\HexChat\ on win32 */ /* autoload from ~/.config/hexchat/ or %APPDATA%\HexChat\ on win32 */
perl_auto_load_from_path (xdir); perl_auto_load_from_path (xdir);
#endif
sub_dir = malloc (strlen (xdir) + 9); sub_dir = malloc (strlen (xdir) + 9);
strcpy (sub_dir, xdir); strcpy (sub_dir, xdir);
strcat (sub_dir, "/plugins"); strcat (sub_dir, "/scripts");
perl_auto_load_from_path (sub_dir); perl_auto_load_from_path (sub_dir);
free (sub_dir); free (sub_dir);
#if 0
#ifdef WIN32 #ifdef WIN32
/* autoload from C:\Program Files\HexChat\plugins\ */ /* autoload from C:\Program Files\HexChat\plugins\ */
sub_dir = malloc (1025 + 9); sub_dir = malloc (1025 + 9);
@ -164,6 +169,7 @@ perl_auto_load (void *unused)
} }
perl_auto_load_from_path ( strncat (sub_dir, "\\plugins", 9)); perl_auto_load_from_path ( strncat (sub_dir, "\\plugins", 9));
free (sub_dir); free (sub_dir);
#endif
#endif #endif
return 0; return 0;
} }

View File

@ -389,20 +389,27 @@ Util_Autoload()
char *sub_dir; char *sub_dir;
/* we need local filesystem encoding for chdir, opendir etc */ /* we need local filesystem encoding for chdir, opendir etc */
/* auto-load from ~/.config/hexchat/ or %APPDATA%\HexChat\ */
xdir = xchat_get_info(ph, "xchatdirfs"); xdir = xchat_get_info(ph, "xchatdirfs");
Util_Autoload_from(xchat_get_info(ph, "xchatdirfs"));
/* auto-load from subdirectory plugins */ /* don't pollute the filesystem with script files, this only causes misuse of the folders
* only use ~/.config/hexchat/scripts/ and %APPDATA%\HexChat\scripts */
#if 0
/* auto-load from ~/.config/hexchat/ or %APPDATA%\HexChat\ */
Util_Autoload_from(xchat_get_info(ph, "xchatdirfs"));
#endif
/* auto-load from subdirectory scripts */
sub_dir = malloc (strlen (xdir) + 9); sub_dir = malloc (strlen (xdir) + 9);
strcpy (sub_dir, xdir); strcpy (sub_dir, xdir);
strcat (sub_dir, "/plugins"); strcat (sub_dir, "/scripts");
Util_Autoload_from(sub_dir); Util_Autoload_from(sub_dir);
free (sub_dir); free (sub_dir);
#if 0
#ifdef WIN32 /* also auto-load C:\Program Files\HexChat\Plugins\*.py */ #ifdef WIN32 /* also auto-load C:\Program Files\HexChat\Plugins\*.py */
Util_Autoload_from(HEXCHATLIBDIR"/plugins"); Util_Autoload_from(HEXCHATLIBDIR"/plugins");
#endif #endif
#endif
} }
static char * static char *
@ -437,9 +444,9 @@ Util_Expand(char *filename)
return expanded; return expanded;
g_free(expanded); g_free(expanded);
/* Check if ~/.config/hexchat/<filename> exists. */ /* Check if ~/.config/hexchat/scripts/<filename> exists. */
expanded = g_build_filename(xchat_get_info(ph, "xchatdir"), expanded = g_build_filename(xchat_get_info(ph, "xchatdir"),
filename, NULL); "scripts", filename, NULL);
if (g_file_test(expanded, G_FILE_TEST_EXISTS)) if (g_file_test(expanded, G_FILE_TEST_EXISTS))
return expanded; return expanded;
g_free(expanded); g_free(expanded);

View File

@ -89,8 +89,10 @@ static char unknown[] = {
"}\n" "}\n"
}; };
/* don't pollute the filesystem with script files, this only causes misuse of the folders
* only use ~/.config/hexchat/scripts/ and %APPDATA%\HexChat\scripts */
static char sourcedirs[] = { static char sourcedirs[] = {
"set files [lsort [glob -nocomplain -directory [xchatdir] \"*.tcl\"]]\n" "set files [lsort [glob -nocomplain -directory [xchatdir] \"/scripts/*.tcl\"]]\n"
"set init [lsearch -glob $files \"*/init.tcl\"]\n" "set init [lsearch -glob $files \"*/init.tcl\"]\n"
"if { $init > 0 } {\n" "if { $init > 0 } {\n"
"set initfile [lindex $files $init]\n" "set initfile [lindex $files $init]\n"
@ -2037,7 +2039,7 @@ static int Command_Source(char *word[], char *word_eol[], void *userdata)
} else { } else {
if (!strchr(word_eol[2], '/')) { if (!strchr(word_eol[2], '/')) {
Tcl_DStringAppend(&ds, xchatdir, strlen(xchatdir)); Tcl_DStringAppend(&ds, xchatdir, strlen(xchatdir));
Tcl_DStringAppend(&ds, "/", 1); Tcl_DStringAppend(&ds, "/scripts/", 9);
Tcl_DStringAppend(&ds, word_eol[2], strlen(word_eol[2])); Tcl_DStringAppend(&ds, word_eol[2], strlen(word_eol[2]));
} }
} }