summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/common/command.c15
-rw-r--r--src/common/weechat.c88
-rw-r--r--src/common/weechat.h9
-rw-r--r--src/common/weeconfig.c6
-rw-r--r--src/plugins/plugins.c59
5 files changed, 155 insertions, 22 deletions
diff --git a/src/common/command.c b/src/common/command.c
index bcf8e8156..8e8cc91ea 100644
--- a/src/common/command.c
+++ b/src/common/command.c
@@ -993,6 +993,7 @@ weechat_cmd_perl (int argc, char **argv)
t_plugin_script *ptr_plugin_script;
t_plugin_handler *ptr_plugin_handler;
int handler_found;
+ char *path_script;
#ifdef PLUGIN_PERL
switch (argc)
@@ -1078,7 +1079,19 @@ weechat_cmd_perl (int argc, char **argv)
if (strcmp (argv[0], "load") == 0)
{
/* load Perl script */
- plugin_load (PLUGIN_TYPE_PERL, argv[1]);
+ if (strstr(argv[1], DIR_SEPARATOR))
+ path_script = NULL;
+ else
+ {
+ path_script = (char *) malloc ((strlen (weechat_home) +
+ strlen (argv[1]) + 7) * sizeof (char));
+ sprintf (path_script, "%s%s%s%s%s", weechat_home,
+ DIR_SEPARATOR, "perl", DIR_SEPARATOR, argv[1]);
+ }
+ plugin_load (PLUGIN_TYPE_PERL,
+ (path_script) ? path_script : argv[1]);
+ if (path_script)
+ free (path_script);
}
else
{
diff --git a/src/common/weechat.c b/src/common/weechat.c
index 0d822732c..2da7b32b0 100644
--- a/src/common/weechat.c
+++ b/src/common/weechat.c
@@ -62,7 +62,7 @@
int quit_weechat; /* = 1 if quit request from user... why ? :'( */
char *weechat_home; /* WeeChat home dir. (example: /home/toto/.weechat) */
-FILE *log_file; /* WeeChat log file (~/.weechat/weechat.log */
+FILE *log_file; /* WeeChat log file (~/.weechat/weechat.log) */
/*
@@ -73,6 +73,7 @@ FILE *log_file; /* WeeChat log file (~/.weechat/weechat.log */
void
my_sigint ()
{
+ /* do nothing */
}
/*
@@ -142,31 +143,82 @@ wee_parse_args (int argc, char *argv[])
}
/*
- * wee_create_home_dir: create weechat home directory (if not found)
+ * wee_create_dir: create a directory
+ * return: 1 if ok (or directory already exists)
+ * 0 if error
+ */
+
+int
+wee_create_dir (char *directory)
+{
+ if (mkdir (directory, 0755) < 0)
+ {
+ /* exit if error (except if directory already exists) */
+ if (errno != EEXIST)
+ {
+ fprintf (stderr, _("%s cannot create directory \"%s\"\n"),
+ WEECHAT_ERROR, directory);
+ return 0;
+ }
+ }
+ return 1;
+}
+
+/*
+ * wee_create_home_dirs: create (if not found):
+ * - WeeChat home directory ("~/.weechat")
+ * - "perl" directory (and "autoload")
+ * - "ruby" directory (and "autoload")
+ * - "python" directory (and "autoload")
*/
void
-wee_create_home_dir ()
+wee_create_home_dirs ()
{
- int return_code;
+ char *dir_name;
/* TODO: rewrite this code for Windows version */
weechat_home =
(char *) malloc ((strlen (getenv ("HOME")) + 10) * sizeof (char));
- sprintf (weechat_home, "%s/.weechat", getenv ("HOME"));
+ sprintf (weechat_home, "%s%s.weechat", getenv ("HOME"), DIR_SEPARATOR);
- /* try to create home directory */
- return_code = mkdir (weechat_home, 0755);
- if (return_code < 0)
+ /* create home directory "~/.weechat" ; error is fatal */
+ if (!wee_create_dir (weechat_home))
+ exit (1);
+
+ dir_name = (char *) malloc ((strlen (weechat_home) + 64) * sizeof (char));
+
+ /* create "~/.weechat/perl" */
+ sprintf (dir_name, "%s%s%s", weechat_home, DIR_SEPARATOR, "perl");
+ if (wee_create_dir (dir_name))
{
- /* exit if error (except if directory already exists) */
- if (errno != EEXIST)
- {
- fprintf (stderr, _("%s cannot create directory \"%s\"\n"),
- WEECHAT_ERROR, weechat_home);
- exit (1);
- }
+ /* create "~/.weechat/perl/autoload" */
+ sprintf (dir_name, "%s%s%s%s%s", weechat_home, DIR_SEPARATOR, "perl",
+ DIR_SEPARATOR, "autoload");
+ wee_create_dir (dir_name);
}
+
+ /* create "~/.weechat/python" */
+ sprintf (dir_name, "%s%s%s", weechat_home, DIR_SEPARATOR, "python");
+ if (wee_create_dir (dir_name))
+ {
+ /* create "~/.weechat/python/autoload" */
+ sprintf (dir_name, "%s%s%s%s%s", weechat_home, DIR_SEPARATOR, "python",
+ DIR_SEPARATOR, "autoload");
+ wee_create_dir (dir_name);
+ }
+
+ /* create "~/.weechat/ruby" */
+ sprintf (dir_name, "%s%s%s", weechat_home, DIR_SEPARATOR, "ruby");
+ if (wee_create_dir (dir_name))
+ {
+ /* create "~/.weechat/ruby/autoload" */
+ sprintf (dir_name, "%s%s%s%s%s", weechat_home, DIR_SEPARATOR, "ruby",
+ DIR_SEPARATOR, "autoload");
+ wee_create_dir (dir_name);
+ }
+
+ free (dir_name);
}
/*
@@ -200,8 +252,8 @@ wee_init_log ()
{
free (filename);
fprintf (stderr,
- _("%s unable to create/append to log file (~/.weechat/"
- WEECHAT_LOG_NAME), WEECHAT_ERROR);
+ _("%s unable to create/append to log file (~/.weechat/%s)"),
+ WEECHAT_ERROR, WEECHAT_LOG_NAME);
}
free (filename);
}
@@ -277,7 +329,7 @@ main (int argc, char *argv[])
gui_pre_init (&argc, &argv); /* pre-initiliaze interface */
wee_init_vars (); /* initialize some variables */
wee_parse_args (argc, argv); /* parse command line args */
- wee_create_home_dir (); /* create weechat home directory */
+ wee_create_home_dirs (); /* create WeeChat directories */
wee_init_log (); /* init log file */
index_command_build (); /* build commands index for completion */
diff --git a/src/common/weechat.h b/src/common/weechat.h
index 53fc0dc21..47b9e12ed 100644
--- a/src/common/weechat.h
+++ b/src/common/weechat.h
@@ -86,6 +86,15 @@
" -l, --license display WeeChat license\n" \
" -v, --version display WeeChat version\n\n"
+/* directory separator, depending on OS */
+
+#ifdef _WIN32
+ #define DIR_SEPARATOR "\\"
+#else
+ #define DIR_SEPARATOR "/"
+#endif
+
+/* global variables and functions */
extern int quit_weechat;
extern char *weechat_home;
diff --git a/src/common/weeconfig.c b/src/common/weeconfig.c
index 91fcc792f..4ddbe8d60 100644
--- a/src/common/weeconfig.c
+++ b/src/common/weeconfig.c
@@ -722,7 +722,7 @@ config_read ()
filename =
(char *) malloc ((strlen (weechat_home) + 64) * sizeof (char));
- sprintf (filename, "%s/" WEECHAT_CONFIG_NAME, weechat_home);
+ sprintf (filename, "%s%s" WEECHAT_CONFIG_NAME, weechat_home, DIR_SEPARATOR);
if ((file = fopen (filename, "rt")) == NULL)
{
gui_printf (NULL, _("%s config file \"%s\" not found.\n"),
@@ -938,7 +938,7 @@ config_create_default ()
filename =
(char *) malloc ((strlen (weechat_home) + 64) * sizeof (char));
- sprintf (filename, "%s/" WEECHAT_CONFIG_NAME, weechat_home);
+ sprintf (filename, "%s%s" WEECHAT_CONFIG_NAME, weechat_home, DIR_SEPARATOR);
if ((file = fopen (filename, "wt")) == NULL)
{
gui_printf (NULL, _("%s cannot create file \"%s\"\n"),
@@ -1070,7 +1070,7 @@ config_write (char *config_name)
{
filename =
(char *) malloc ((strlen (weechat_home) + 64) * sizeof (char));
- sprintf (filename, "%s/" WEECHAT_CONFIG_NAME, weechat_home);
+ sprintf (filename, "%s%s" WEECHAT_CONFIG_NAME, weechat_home, DIR_SEPARATOR);
}
if ((file = fopen (filename, "wt")) == NULL)
diff --git a/src/plugins/plugins.c b/src/plugins/plugins.c
index 431dab27c..85d28c93f 100644
--- a/src/plugins/plugins.c
+++ b/src/plugins/plugins.c
@@ -27,7 +27,11 @@
#endif
#include <stdlib.h>
+#include <unistd.h>
#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <dirent.h>
#include "../common/weechat.h"
#include "plugins.h"
#include "../irc/irc.h"
@@ -38,6 +42,8 @@
#endif
+char *plugin_name[3] = { "Perl", "Python", "Ruby" };
+
t_plugin_handler *plugin_msg_handlers = NULL;
t_plugin_handler *last_plugin_msg_handler = NULL;
@@ -46,6 +52,58 @@ t_plugin_handler *last_plugin_cmd_handler = NULL;
/*
+ * plugin_auto_load: auto-load all scripts in a directory
+ */
+
+void
+plugin_auto_load (int plugin_type, char *directory)
+{
+ char *dir_name, *current_dir;
+ DIR *dir;
+ struct dirent *entry;
+ struct stat statbuf;
+
+ /* build directory, adding WeeChat home */
+ dir_name =
+ (char *) malloc ((strlen (weechat_home) + strlen (directory) + 2) * sizeof (char));
+ sprintf (dir_name, "%s%s%s", weechat_home, DIR_SEPARATOR, directory);
+
+ /* save working directory */
+ current_dir = (char *) malloc (1024 * sizeof (char));
+ if (!getcwd (current_dir, 1024 - 1))
+ {
+ free (current_dir);
+ current_dir = NULL;
+ }
+
+ /* browse autoload directory */
+ dir = opendir (dir_name);
+ chdir (dir_name);
+ if (dir)
+ {
+ while ((entry = readdir (dir)))
+ {
+ lstat (entry->d_name, &statbuf);
+ if (! S_ISDIR(statbuf.st_mode))
+ {
+ wee_log_printf (_("auto-loading %s script: %s%s%s\n"),
+ plugin_name[plugin_type],
+ dir_name, DIR_SEPARATOR, entry->d_name);
+ plugin_load (plugin_type, entry->d_name);
+ }
+ }
+ }
+
+ /* restore working directory */
+ if (current_dir)
+ {
+ chdir (current_dir);
+ free (current_dir);
+ }
+ free (dir_name);
+}
+
+/*
* plugin_init: initialize all plugins
*/
@@ -54,6 +112,7 @@ plugin_init ()
{
#ifdef PLUGIN_PERL
wee_perl_init();
+ plugin_auto_load (PLUGIN_TYPE_PERL, "perl/autoload");
#endif
}