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