4621ab2628ece9ede9a6a2e85e560434238eb403
[silc.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_append(options, "-except ");
57         if (rec->regexp) {
58                 g_string_append(options, "-regexp ");
59 #ifdef HAVE_REGEX_H
60                 if (!rec->regexp_compiled)
61                         g_string_append(options, "[INVALID!] ");
62 #endif
63         }
64         if (rec->fullword) g_string_append(options, "-full ");
65         if (rec->replies) g_string_append(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         if (index >= 0) {
72                 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
73                             TXT_IGNORE_LINE, index, key != NULL ? key : "",
74                             levels != NULL ? levels : "", options->str);
75         } else {
76                 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
77                             options->len > 0 ? TXT_IGNORED_OPTIONS : TXT_IGNORED,
78                             key != NULL ? key : "",
79                             levels != NULL ? levels : "", options->str);
80         }
81         g_string_free(options, TRUE);
82         g_free(key);
83         g_free(levels);
84 }
85
86 static void cmd_ignore_show(void)
87 {
88         GSList *tmp;
89         int index;
90
91         if (ignores == NULL) {
92                 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP,
93                             TXT_IGNORE_NO_IGNORES);
94                 return;
95         }
96
97         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_IGNORE_HEADER);
98         index = 1;
99         for (tmp = ignores; tmp != NULL; tmp = tmp->next, index++) {
100                 IGNORE_REC *rec = tmp->data;
101
102                 ignore_print(index, rec);
103         }
104         printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_IGNORE_FOOTER);
105 }
106
107 /* SYNTAX: IGNORE [-regexp | -full] [-pattern <pattern>] [-except] [-replies]
108                   [-channels <channel>] [-time <secs>] <mask> [<levels>]
109            IGNORE [-regexp | -full] [-pattern <pattern>] [-except] [-replies]
110                   [-time <secs>] <channels> [<levels>] */
111 static void cmd_ignore(const char *data)
112 {
113         GHashTable *optlist;
114         IGNORE_REC *rec;
115         char *patternarg, *chanarg, *mask, *levels, *timestr;
116         char **channels;
117         void *free_arg;
118         int new_ignore;
119
120         if (*data == '\0') {
121                 cmd_ignore_show();
122                 return;
123         }
124
125         if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_OPTIONS | PARAM_FLAG_GETREST,
126                             "ignore", &optlist, &mask, &levels))
127                 return;
128
129         patternarg = g_hash_table_lookup(optlist, "pattern");
130         chanarg = g_hash_table_lookup(optlist, "channels");
131
132         if (*mask == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
133         if (*levels == '\0') levels = "ALL";
134
135         if (active_win->active_server != NULL &&
136             server_ischannel(active_win->active_server, mask)) {
137                 chanarg = mask;
138                 mask = NULL;
139         }
140         channels = (chanarg == NULL || *chanarg == '\0') ? NULL :
141                 g_strsplit(replace_chars(chanarg, ',', ' '), " ", -1);
142
143         rec = patternarg != NULL ? NULL: ignore_find(NULL, mask, channels);
144         new_ignore = rec == NULL;
145
146         if (rec == NULL) {
147                 rec = g_new0(IGNORE_REC, 1);
148
149                 rec->mask = mask == NULL || *mask == '\0' ||
150                         strcmp(mask, "*") == 0 ? NULL : g_strdup(mask);
151                 rec->channels = channels;
152         } else {
153                 g_free_and_null(rec->pattern);
154                 g_strfreev(channels);
155         }
156
157         rec->level = combine_level(rec->level, levels);
158
159         if (new_ignore && rec->level == 0) {
160                 /* tried to unignore levels from nonexisting ignore */
161                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
162                             TXT_IGNORE_NOT_FOUND, rec->mask);
163                 g_free(rec->mask);
164                 g_strfreev(rec->channels);
165                 g_free(rec);
166                 cmd_params_free(free_arg);
167                 return;
168         }
169
170         rec->pattern = (patternarg == NULL || *patternarg == '\0') ?
171                 NULL : g_strdup(patternarg);
172         rec->exception = g_hash_table_lookup(optlist, "except") != NULL;
173         rec->regexp = g_hash_table_lookup(optlist, "regexp") != NULL;
174         rec->fullword = g_hash_table_lookup(optlist, "full") != NULL;
175         rec->replies = g_hash_table_lookup(optlist, "replies") != NULL;
176         timestr = g_hash_table_lookup(optlist, "time");
177         if (timestr != NULL)
178                 rec->unignore_time = time(NULL)+atoi(timestr);
179
180         if (new_ignore)
181                 ignore_add_rec(rec);
182         else
183                 ignore_update_rec(rec);
184
185         cmd_params_free(free_arg);
186 }
187
188 /* SYNTAX: UNIGNORE <id>|<mask> */
189 static void cmd_unignore(const char *data)
190 {
191         IGNORE_REC *rec;
192         GSList *tmp;
193         char *mask;
194         void *free_arg;
195
196         if (!cmd_get_params(data, &free_arg, 1, &mask))
197                 return;
198
199         if (*mask == '\0')
200                 cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);
201
202         if (is_numeric(mask, ' ')) {
203                 /* with index number */
204                 tmp = g_slist_nth(ignores, atoi(mask)-1);
205                 rec = tmp == NULL ? NULL : tmp->data;
206         } else {
207                 /* with mask */
208                 const char *chans[2] = { "*", NULL };
209
210                 if (active_win->active_server != NULL &&
211                     server_ischannel(active_win->active_server, mask)) {
212                         chans[0] = mask;
213                         mask = NULL;
214                 }
215                 rec = ignore_find("*", mask, (char **) chans);
216         }
217
218         if (rec != NULL) {
219                 rec->level = 0;
220                 ignore_update_rec(rec);
221         } else {
222                 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
223                             TXT_IGNORE_NOT_FOUND, mask);
224         }
225         cmd_params_free(free_arg);
226 }
227
228 static void sig_ignore_created(IGNORE_REC *rec)
229 {
230         ignore_print(-1, rec);
231 }
232
233 static void sig_ignore_destroyed(IGNORE_REC *rec)
234 {
235         char *key;
236
237         key = ignore_get_key(rec);
238         printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, TXT_UNIGNORED, key);
239         g_free(key);
240 }
241
242 void fe_ignore_init(void)
243 {
244         command_bind("ignore", NULL, (SIGNAL_FUNC) cmd_ignore);
245         command_bind("unignore", NULL, (SIGNAL_FUNC) cmd_unignore);
246
247         signal_add("ignore destroyed", (SIGNAL_FUNC) sig_ignore_destroyed);
248         signal_add("ignore created", (SIGNAL_FUNC) sig_ignore_created);
249         signal_add("ignore changed", (SIGNAL_FUNC) sig_ignore_created);
250
251         command_set_options("ignore", "regexp full except replies -time -pattern -channels");
252 }
253
254 void fe_ignore_deinit(void)
255 {
256         command_unbind("ignore", (SIGNAL_FUNC) cmd_ignore);
257         command_unbind("unignore", (SIGNAL_FUNC) cmd_unignore);
258
259         signal_remove("ignore destroyed", (SIGNAL_FUNC) sig_ignore_destroyed);
260         signal_remove("ignore created", (SIGNAL_FUNC) sig_ignore_created);
261         signal_remove("ignore changed", (SIGNAL_FUNC) sig_ignore_created);
262 }