Merged 0.7.99 irssi.
[crypto.git] / apps / irssi / src / fe-common / core / fe-ignore.c
1 /*
2  fe-ignore.c : irssi
3
4     Copyright (C) 1999-2000 Timo Sirainen
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "module.h"
22 #include "module-formats.h"
23 #include "signals.h"
24 #include "commands.h"
25 #include "levels.h"
26 #include "misc.h"
27
28 #include "servers.h"
29 #include "ignore.h"
30 #include "printtext.h"
31
32 static char *ignore_get_key(IGNORE_REC *rec)
33 {
34         char *chans, *ret;
35
36         if (rec->channels == NULL)
37                 return g_strdup(rec->mask != NULL ? rec->mask : "*" );
38
39         chans = g_strjoinv(",", rec->channels);
40         if (rec->mask == NULL) return chans;
41
42         ret = g_strdup_printf("%s %s", rec->mask, chans);
43         g_free(chans);
44         return ret;
45 }
46
47 static void ignore_print(int index, IGNORE_REC *rec)
48 {
49         GString *options;
50         char *key, *levels;
51
52         key = ignore_get_key(rec);
53         levels = bits2level(rec->level);
54
55         options = g_string_new(NULL);
56         if (rec->exception) g_string_sprintfa(options, "-except ");
57         if (rec->regexp) {
58                 g_string_sprintfa(options, "-regexp ");
59 #ifdef HAVE_REGEX_H
60                 if (!rec->regexp_compiled)
61                         g_string_sprintfa(options, "[INVALID!] ");
62 #endif
63         }
64         if (rec->fullword) g_string_sprintfa(options, "-full ");
65         if (rec->replies) g_string_sprintfa(options, "-replies ");
66         if (rec->pattern != NULL)
67                 g_string_sprintfa(options, "-pattern %s ", rec->pattern);
68
69         if (options->len > 1) g_string_truncate(options, options->len-1);
70
71         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
72                     TXT_IGNORE_LINE, index,
73                     key != NULL ? key : "",
74                     levels != NULL ? levels : "", options->str);
75         g_string_free(options, TRUE);
76         g_free(key);
77         g_free(levels);
78 }
79
80 static void cmd_ignore_show(void)
81 {
82         GSList *tmp;
83         int index;
84
85         if (ignores == NULL) {
86                 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
87                             TXT_IGNORE_NO_IGNORES);
88                 return;
89         }
90
91         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_IGNORE_HEADER);
92         index = 1;
93         for (tmp = ignores; tmp != NULL; tmp = tmp->next, index++) {
94                 IGNORE_REC *rec = tmp->data;
95
96                 ignore_print(index, rec);
97         }
98         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_IGNORE_FOOTER);
99 }
100
101 /* SYNTAX: IGNORE [-regexp | -full] [-pattern <pattern>] [-except] [-replies]
102                   [-channels <channel>] [-time <secs>] <mask> [<levels>]
103            IGNORE [-regexp | -full] [-pattern <pattern>] [-except] [-replies]
104                   [-time <secs>] <channels> [<levels>] */
105 static void cmd_ignore(const char *data)
106 {
107         GHashTable *optlist;
108         IGNORE_REC *rec;
109         char *patternarg, *chanarg, *mask, *levels, *timestr;
110         char **channels;
111         void *free_arg;
112         int new_ignore;
113
114         if (*data == '\0') {
115                 cmd_ignore_show();
116                 return;
117         }
118
119         if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_OPTIONS | PARAM_FLAG_GETREST,
120                             "ignore", &optlist, &mask, &levels))
121                 return;
122
123         patternarg = g_hash_table_lookup(optlist, "pattern");
124         chanarg = g_hash_table_lookup(optlist, "channels");
125
126         if (*mask == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
127         if (*levels == '\0') levels = "ALL";
128
129         if (active_win->active_server != NULL &&
130             server_ischannel(active_win->active_server, mask)) {
131                 chanarg = mask;
132                 mask = NULL;
133         }
134         channels = (chanarg == NULL || *chanarg == '\0') ? NULL :
135                 g_strsplit(replace_chars(chanarg, ',', ' '), " ", -1);
136
137         rec = ignore_find(NULL, mask, channels);
138         new_ignore = rec == NULL;
139
140         if (rec == NULL) {
141                 rec = g_new0(IGNORE_REC, 1);
142
143                 rec->mask = mask == NULL || *mask == '\0' ||
144                         strcmp(mask, "*") == 0 ? NULL : g_strdup(mask);
145                 rec->channels = channels;
146         } else {
147                 g_free_and_null(rec->pattern);
148                 g_strfreev(channels);
149         }
150
151         rec->level = combine_level(rec->level, levels);
152
153         if (new_ignore && rec->level == 0) {
154                 /* tried to unignore levels from nonexisting ignore */
155                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
156                             TXT_IGNORE_NOT_FOUND, rec->mask);
157                 g_free(rec->mask);
158                 g_strfreev(rec->channels);
159                 g_free(rec);
160                 cmd_params_free(free_arg);
161                 return;
162         }
163
164         rec->pattern = (patternarg == NULL || *patternarg == '\0') ?
165                 NULL : g_strdup(patternarg);
166         rec->exception = g_hash_table_lookup(optlist, "except") != NULL;
167         rec->regexp = g_hash_table_lookup(optlist, "regexp") != NULL;
168         rec->fullword = g_hash_table_lookup(optlist, "full") != NULL;
169         rec->replies = g_hash_table_lookup(optlist, "replies") != NULL;
170         timestr = g_hash_table_lookup(optlist, "time");
171         if (timestr != NULL)
172                 rec->unignore_time = time(NULL)+atoi(timestr);
173
174         if (new_ignore)
175                 ignore_add_rec(rec);
176         else
177                 ignore_update_rec(rec);
178
179         cmd_params_free(free_arg);
180 }
181
182 /* SYNTAX: UNIGNORE <id>|<mask> */
183 static void cmd_unignore(const char *data)
184 {
185         IGNORE_REC *rec;
186         GSList *tmp;
187         char *mask;
188         void *free_arg;
189
190         if (!cmd_get_params(data, &free_arg, 1, &mask))
191                 return;
192
193         if (*mask == '\0')
194                 cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);
195
196         if (is_numeric(mask, ' ')) {
197                 /* with index number */
198                 tmp = g_slist_nth(ignores, atoi(mask)-1);
199                 rec = tmp == NULL ? NULL : tmp->data;
200         } else {
201                 /* with mask */
202                 const char *chans[2] = { "*", NULL };
203
204                 if (active_win->active_server != NULL &&
205                     server_ischannel(active_win->active_server, mask)) {
206                         chans[0] = mask;
207                         mask = NULL;
208                 }
209                 rec = ignore_find("*", mask, (char **) chans);
210         }
211
212         if (rec != NULL) {
213                 rec->level = 0;
214                 ignore_update_rec(rec);
215         } else {
216                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
217                             TXT_IGNORE_NOT_FOUND, mask);
218         }
219         cmd_params_free(free_arg);
220 }
221
222 static void sig_ignore_created(IGNORE_REC *rec)
223 {
224         char *key, *levels;
225
226         key = ignore_get_key(rec);
227         levels = bits2level(rec->level);
228         printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
229                     TXT_IGNORED, key, levels);
230         g_free(key);
231         g_free(levels);
232 }
233
234 static void sig_ignore_destroyed(IGNORE_REC *rec)
235 {
236         char *key;
237
238         key = ignore_get_key(rec);
239         printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_UNIGNORED, key);
240         g_free(key);
241 }
242
243 void fe_ignore_init(void)
244 {
245         command_bind("ignore", NULL, (SIGNAL_FUNC) cmd_ignore);
246         command_bind("unignore", NULL, (SIGNAL_FUNC) cmd_unignore);
247
248         signal_add("ignore destroyed", (SIGNAL_FUNC) sig_ignore_destroyed);
249         signal_add("ignore created", (SIGNAL_FUNC) sig_ignore_created);
250         signal_add("ignore changed", (SIGNAL_FUNC) sig_ignore_created);
251
252         command_set_options("ignore", "regexp full except replies -time -pattern -channels");
253 }
254
255 void fe_ignore_deinit(void)
256 {
257         command_unbind("ignore", (SIGNAL_FUNC) cmd_ignore);
258         command_unbind("unignore", (SIGNAL_FUNC) cmd_unignore);
259
260         signal_remove("ignore destroyed", (SIGNAL_FUNC) sig_ignore_destroyed);
261         signal_remove("ignore created", (SIGNAL_FUNC) sig_ignore_created);
262         signal_remove("ignore changed", (SIGNAL_FUNC) sig_ignore_created);
263 }