1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
/*
* Copyright (C) 2010 Sebastien Helleu <flashcode@flashtux.org>
*
* This file is part of WeeChat, the extensible chat client.
*
* WeeChat is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* WeeChat is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with WeeChat. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* rmodifier-command.c: rmodifier command
*/
#include <stdlib.h>
#include "../weechat-plugin.h"
#include "rmodifier.h"
#include "rmodifier-command.h"
#include "rmodifier-config.h"
/*
* rmodifier_command_print: print a rmodifier
*/
void
rmodifier_command_print (const char *name, const char *modifiers,
const char *str_regex, const char *groups)
{
weechat_printf (NULL,
" %s[%s%s%s]%s %s%s: \"%s%s%s\" [%s%s%s]",
weechat_color ("chat_delimiters"),
weechat_color ("chat"),
name,
weechat_color ("chat_delimiters"),
weechat_color ("chat"),
modifiers,
weechat_color ("chat_delimiters"),
weechat_color ("chat_host"),
str_regex,
weechat_color ("chat_delimiters"),
weechat_color ("chat"),
groups,
weechat_color ("chat_delimiters"));
}
/*
* rmodifier_command_list: list rmodifiers
*/
void
rmodifier_command_list (const char *message)
{
struct t_rmodifier *ptr_rmodifier;
if (rmodifier_list)
{
weechat_printf (NULL, "");
weechat_printf (NULL, message);
for (ptr_rmodifier = rmodifier_list; ptr_rmodifier;
ptr_rmodifier = ptr_rmodifier->next_rmodifier)
{
rmodifier_command_print (ptr_rmodifier->name,
ptr_rmodifier->modifiers,
ptr_rmodifier->str_regex,
ptr_rmodifier->groups);
}
}
else
weechat_printf (NULL, _("No rmodifier defined"));
}
/*
* rmodifier_command_cb: manage rmodifiers
*/
int
rmodifier_command_cb (void *data, struct t_gui_buffer *buffer, int argc,
char **argv, char **argv_eol)
{
struct t_rmodifier *ptr_rmodifier;
struct t_config_option *ptr_option;
int i, count;
/* make C compiler happy */
(void) data;
(void) buffer;
if ((argc == 1)
|| ((argc == 2) && (weechat_strcasecmp (argv[1], "list") == 0)))
{
/* list all rmodifiers */
rmodifier_command_list (_("List of rmodifiers:"));
return WEECHAT_RC_OK;
}
if (weechat_strcasecmp (argv[1], "listdefault") == 0)
{
/* list default rmodifiers */
weechat_printf (NULL, "");
weechat_printf (NULL, _("Default rmodifiers:"));
for (i = 0; rmodifier_config_default_list[i][0]; i++)
{
rmodifier_command_print (rmodifier_config_default_list[i][0],
rmodifier_config_default_list[i][1],
rmodifier_config_default_list[i][2],
rmodifier_config_default_list[i][3]);
}
return WEECHAT_RC_OK;
}
if (weechat_strcasecmp (argv[1], "add") == 0)
{
/* add a rmodifier */
if (argc < 6)
{
weechat_printf (NULL,
_("%sError: missing arguments for \"%s\" "
"command"),
weechat_prefix ("error"), "rmodifier");
return WEECHAT_RC_ERROR;
}
ptr_rmodifier = rmodifier_new (argv[2], argv[3], argv_eol[5], argv[4]);
if (!ptr_rmodifier)
{
weechat_printf (NULL,
_("%s%s: error creating rmodifier \"%s\""),
weechat_prefix ("error"), RMODIFIER_PLUGIN_NAME,
argv[2]);
return WEECHAT_RC_ERROR;
}
/* create config option */
ptr_option = weechat_config_search_option (rmodifier_config_file,
rmodifier_config_section_modifier,
argv[2]);
if (ptr_option)
weechat_config_option_free (ptr_option);
rmodifier_config_modifier_new_option (ptr_rmodifier->name,
ptr_rmodifier->modifiers,
ptr_rmodifier->str_regex,
ptr_rmodifier->groups);
/* display message */
weechat_printf (NULL, _("Rmodifier \"%s\" created"),
ptr_rmodifier->name);
return WEECHAT_RC_OK;
}
if (weechat_strcasecmp (argv[1], "del") == 0)
{
/* add a rmodifier */
if (argc < 3)
{
weechat_printf (NULL,
_("%sError: missing arguments for \"%s\" "
"command"),
weechat_prefix ("error"), "rmodifier");
return WEECHAT_RC_ERROR;
}
if (weechat_strcasecmp (argv[2], "-all") == 0)
{
count = rmodifier_count;
rmodifier_free_all ();
weechat_config_section_free_options (rmodifier_config_section_modifier);
if (count > 0)
weechat_printf (NULL, _("%d rmodifiers removed"), count);
}
else
{
for (i = 2; i < argc; i++)
{
ptr_rmodifier = rmodifier_search (argv[i]);
if (ptr_rmodifier)
{
ptr_option = weechat_config_search_option (rmodifier_config_file,
rmodifier_config_section_modifier,
argv[i]);
if (ptr_option)
weechat_config_option_free (ptr_option);
rmodifier_free (ptr_rmodifier);
weechat_printf (NULL, _("Rmodifier \"%s\" removed"),
argv[i]);
}
else
{
weechat_printf (NULL, _("%sRmodifier \"%s\" not found"),
weechat_prefix ("error"), argv[i]);
}
}
}
return WEECHAT_RC_OK;
}
/* restore default rmodifiers (only with "-yes", for security reason) */
if (weechat_strcasecmp (argv[1], "default") == 0)
{
if ((argc >= 3) && (weechat_strcasecmp (argv[2], "-yes") == 0))
{
rmodifier_free_all ();
weechat_config_section_free_options (rmodifier_config_section_modifier);
rmodifier_create_default ();
rmodifier_command_list (_("Default rmodifiers restored:"));
}
else
{
weechat_printf (NULL,
_("%sError: \"-yes\" argument is required for "
"restoring default rmodifiers (security reason)"),
weechat_prefix ("error"));
return WEECHAT_RC_ERROR;
}
return WEECHAT_RC_OK;
}
return WEECHAT_RC_OK;
}
/*
* rmodifier_command_init: add /rmodifier command
*/
void
rmodifier_command_init ()
{
weechat_hook_command ("rmodifier",
N_("alter modifier strings with regular expressions"),
N_("[list] | [listdefault] | "
"[add name modifiers groups regex] | "
"[del name|-all [name...]] | [default -yes]"),
N_(" list: list all rmodifiers\n"
"listdefault: list default rmodifiers\n"
" add: add a rmodifier\n"
" name: name of rmodifier\n"
" modifiers: comma separated list of modifiers\n"
" groups: action on groups found: comma separated "
"list of groups (from 1 to 9) with optional \"*\" "
"after number to hide group\n"
" regex: regular expression\n"
" del: delete a rmodifier\n"
" -all: delete all rmodifiers\n"
" default: restore default rmodifiers\n\n"
"Examples:\n"
" hide everything typed after a command /password:\n"
" /rmodifier add password input_text_display 1,2* ^(/password +)(.*)\n"
" delete rmodifier \"password\":\n"
" /rmodifier del password\n"
" delete all rmodifiers:\n"
" /rmodifier del -all"),
"list"
" || listdefault"
" || add %(rmodifier)"
" || del %(rmodifier)|-all %(rmodifier)|%*"
" || default",
&rmodifier_command_cb, NULL);
}
|