summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2015-09-09 09:22:40 +0200
committerSébastien Helleu <flashcode@flashtux.org>2015-09-09 09:22:40 +0200
commit8688c48e420cd192ecd2af7a46f564c09bf2364c (patch)
tree185db1d3f8c0d0a5e29c40ec508a08a8984a80bf /src
parent145ec0db35f1936bf6513c0c071014a4b532fa15 (diff)
downloadweechat-8688c48e420cd192ecd2af7a46f564c09bf2364c.zip
core: display a more explicit error when a filter fails to be added (closes #522)
Diffstat (limited to 'src')
-rw-r--r--src/core/wee-command.c17
-rw-r--r--src/gui/gui-filter.c53
2 files changed, 49 insertions, 21 deletions
diff --git a/src/core/wee-command.c b/src/core/wee-command.c
index c128f423c..ec6c6d32a 100644
--- a/src/core/wee-command.c
+++ b/src/core/wee-command.c
@@ -2184,14 +2184,6 @@ COMMAND_CALLBACK(filter)
if (string_strcasecmp (argv[1], "add") == 0)
{
COMMAND_MIN_ARGS(6, "add");
- if (gui_filter_search_by_name (argv[2]))
- {
- gui_chat_printf_date_tags (NULL, 0, GUI_FILTER_TAG_NO_FILTER,
- _("%sError: filter \"%s\" already exists"),
- gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
- argv[2]);
- return WEECHAT_RC_OK;
- }
if ((strcmp (argv[4], "*") == 0) && (strcmp (argv_eol[5], "*") == 0))
{
gui_chat_printf_date_tags (NULL, 0, GUI_FILTER_TAG_NO_FILTER,
@@ -2201,7 +2193,8 @@ COMMAND_CALLBACK(filter)
return WEECHAT_RC_OK;
}
- ptr_filter = gui_filter_new (1, argv[2], argv[3], argv[4], argv_eol[5]);
+ ptr_filter = gui_filter_new (1, argv[2], argv[3], argv[4],
+ argv_eol[5]);
if (ptr_filter)
{
gui_filter_all_buffers ();
@@ -2211,12 +2204,6 @@ COMMAND_CALLBACK(filter)
argv[2]);
command_filter_display (ptr_filter);
}
- else
- {
- gui_chat_printf_date_tags (NULL, 0, GUI_FILTER_TAG_NO_FILTER,
- _("%sError adding filter"),
- gui_chat_prefix[GUI_CHAT_PREFIX_ERROR]);
- }
return WEECHAT_RC_OK;
}
diff --git a/src/gui/gui-filter.c b/src/gui/gui-filter.c
index df0fa1619..a99d98b38 100644
--- a/src/gui/gui-filter.c
+++ b/src/gui/gui-filter.c
@@ -38,6 +38,7 @@
#include "../plugins/plugin.h"
#include "gui-filter.h"
#include "gui-buffer.h"
+#include "gui-chat.h"
#include "gui-line.h"
#include "gui-window.h"
@@ -254,6 +255,21 @@ gui_filter_search_by_name (const char *name)
}
/*
+ * Displays an error when a new filter is created.
+ */
+
+void
+gui_filter_new_error (const char *name, const char *error)
+{
+ gui_chat_printf_date_tags (
+ NULL, 0, GUI_FILTER_TAG_NO_FILTER,
+ _("%sError adding filter \"%s\": %s"),
+ gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
+ (name) ? name : "",
+ error);
+}
+
+/*
* Creates a new filter.
*
* Returns pointer to new filter, NULL if error.
@@ -265,15 +281,22 @@ gui_filter_new (int enabled, const char *name, const char *buffer_name,
{
struct t_gui_filter *new_filter;
regex_t *regex1, *regex2;
- char *pos_tab, *regex_prefix, **tags_array;
+ char *pos_tab, *regex_prefix, **tags_array, buf[512], str_error[512];
const char *ptr_start_regex, *pos_regex_message;
- int i;
+ int i, rc;
if (!name || !buffer_name || !tags || !regex)
+ {
+ gui_filter_new_error (name, _("not enough arguments"));
return NULL;
+ }
if (gui_filter_search_by_name (name))
+ {
+ gui_filter_new_error (name,
+ _("a filter with same name already exists"));
return NULL;
+ }
ptr_start_regex = regex;
if ((ptr_start_regex[0] == '!')
@@ -305,9 +328,16 @@ gui_filter_new (int enabled, const char *name, const char *buffer_name,
regex1 = malloc (sizeof (*regex1));
if (regex1)
{
- if (string_regcomp (regex1, regex_prefix,
- REG_EXTENDED | REG_ICASE | REG_NOSUB) != 0)
+ rc = string_regcomp (regex1, regex_prefix,
+ REG_EXTENDED | REG_ICASE | REG_NOSUB);
+ if (rc != 0)
{
+ regerror (rc, regex1, buf, sizeof (buf));
+ snprintf (str_error, sizeof (str_error),
+ /* TRANSLATORS: %s is the error returned by regerror */
+ _("invalid regular expression (%s)"),
+ buf);
+ gui_filter_new_error (name, str_error);
free (regex_prefix);
free (regex1);
return NULL;
@@ -320,9 +350,16 @@ gui_filter_new (int enabled, const char *name, const char *buffer_name,
regex2 = malloc (sizeof (*regex2));
if (regex2)
{
- if (string_regcomp (regex2, pos_regex_message,
- REG_EXTENDED | REG_ICASE | REG_NOSUB) != 0)
+ rc = string_regcomp (regex2, pos_regex_message,
+ REG_EXTENDED | REG_ICASE | REG_NOSUB);
+ if (rc != 0)
{
+ regerror (rc, regex2, buf, sizeof (buf));
+ snprintf (str_error, sizeof (str_error),
+ /* TRANSLATORS: %s is the error returned by regerror */
+ _("invalid regular expression (%s)"),
+ buf);
+ gui_filter_new_error (name, str_error);
if (regex_prefix)
free (regex_prefix);
if (regex1)
@@ -390,6 +427,10 @@ gui_filter_new (int enabled, const char *name, const char *buffer_name,
(void) hook_signal_send ("filter_added",
WEECHAT_HOOK_SIGNAL_POINTER, new_filter);
}
+ else
+ {
+ gui_filter_new_error (name, _("not enough memory"));
+ }
return new_filter;
}