1ea056e92340856136d5ac4fac3c96c679432116
[crypto.git] / apps / irssi / src / fe-common / core / fe-queries.c
1 /*
2  fe-queries.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 "modules.h"
24 #include "signals.h"
25 #include "commands.h"
26 #include "levels.h"
27 #include "settings.h"
28
29 #include "chat-protocols.h"
30 #include "servers.h"
31 #include "queries.h"
32
33 #include "fe-windows.h"
34 #include "window-items.h"
35 #include "printtext.h"
36
37 static int queryclose_tag, query_auto_close, querycreate_level;
38
39 /* Return query where to put the private message. */
40 QUERY_REC *privmsg_get_query(SERVER_REC *server, const char *nick,
41                              int own, int level)
42 {
43         QUERY_REC *query;
44
45         g_return_val_if_fail(IS_SERVER(server), NULL);
46         g_return_val_if_fail(nick != NULL, NULL);
47
48         query = query_find(server, nick);
49         if (query == NULL && (querycreate_level & level) != 0 &&
50             (!own || settings_get_bool("autocreate_own_query"))) {
51                 query = CHAT_PROTOCOL(server)->
52                         query_create(server->tag, nick, TRUE);
53         }
54
55         return query;
56 }
57
58 static void signal_query_created(QUERY_REC *query, gpointer automatic)
59 {
60         TEXT_DEST_REC dest;
61
62         g_return_if_fail(IS_QUERY(query));
63
64         if (window_item_window(query) == NULL) {
65                 window_item_create((WI_ITEM_REC *) query,
66                                    GPOINTER_TO_INT(automatic));
67         }
68
69         format_create_dest_tag(&dest, query->server, query->server_tag,
70                                query->name, MSGLEVEL_CLIENTNOTICE, NULL);
71         printformat_dest(&dest, TXT_QUERY_START,
72                          query->name, query->server_tag);
73 }
74
75 static void signal_query_created_curwin(QUERY_REC *query)
76 {
77         g_return_if_fail(IS_QUERY(query));
78
79         window_item_add(active_win, (WI_ITEM_REC *) query, FALSE);
80 }
81
82 static void signal_query_destroyed(QUERY_REC *query)
83 {
84         WINDOW_REC *window;
85         TEXT_DEST_REC dest;
86
87         g_return_if_fail(IS_QUERY(query));
88
89         window = window_item_window((WI_ITEM_REC *) query);
90         if (window == NULL)
91                 return;
92
93         format_create_dest_tag(&dest, query->server, query->server_tag,
94                                query->name, MSGLEVEL_CLIENTNOTICE, NULL);
95         printformat_dest(&dest, TXT_QUERY_STOP, query->name);
96
97         window_item_destroy((WI_ITEM_REC *) query);
98
99         if (!query->unwanted)
100                 window_auto_destroy(window);
101 }
102
103 static void signal_query_server_changed(QUERY_REC *query)
104 {
105         WINDOW_REC *window;
106
107         g_return_if_fail(query != NULL);
108
109         window = window_item_window((WI_ITEM_REC *) query);
110         if (window->active == (WI_ITEM_REC *) query)
111                 window_change_server(window, query->server);
112 }
113
114 static void signal_query_nick_changed(QUERY_REC *query, const char *oldnick)
115 {
116         TEXT_DEST_REC dest;
117
118         g_return_if_fail(query != NULL);
119
120         format_create_dest_tag(&dest, query->server, query->server_tag,
121                                query->name, MSGLEVEL_CLIENTNOTICE, NULL);
122
123         /* don't print the nick change message if only the case was changed */
124         if (g_strcasecmp(query->name, oldnick) != 0) {
125                 printformat_dest(&dest,  TXT_NICK_CHANGED, oldnick,
126                                  query->name, query->name);
127         }
128
129         signal_emit("window item changed", 2,
130                     window_item_window((WI_ITEM_REC *) query), query);
131 }
132
133 static void signal_window_item_server_changed(WINDOW_REC *window,
134                                               QUERY_REC *query)
135 {
136         if (IS_QUERY(query)) {
137                 g_free_and_null(query->server_tag);
138                 if (query->server != NULL)
139                         query->server_tag = g_strdup(query->server->tag);
140         }
141 }
142
143 static void sig_server_connected(SERVER_REC *server)
144 {
145         GSList *tmp;
146
147         if (!IS_SERVER(server))
148                 return;
149
150         /* check if there's any queries without server */
151         for (tmp = queries; tmp != NULL; tmp = tmp->next) {
152                 QUERY_REC *rec = tmp->data;
153
154                 if (rec->server == NULL &&
155                     (rec->server_tag == NULL ||
156                      g_strcasecmp(rec->server_tag, server->tag) == 0)) {
157                         window_item_change_server((WI_ITEM_REC *) rec, server);
158                         server->queries = g_slist_append(server->queries, rec);
159                 }
160         }
161 }
162
163 static void cmd_window_server(const char *data)
164 {
165         SERVER_REC *server;
166         QUERY_REC *query;
167         TEXT_DEST_REC dest;
168
169         g_return_if_fail(data != NULL);
170
171         server = server_find_tag(data);
172         query = QUERY(active_win->active);
173         if (server == NULL || query == NULL)
174                 return;
175
176         /* /WINDOW SERVER used in a query window */
177         format_create_dest_tag(&dest, query->server, query->server_tag,
178                                query->name, MSGLEVEL_CLIENTNOTICE, NULL);
179         printformat_dest(&dest, TXT_QUERY_SERVER_CHANGED,
180                          query->name, server->tag);
181
182         query_change_server(query, server);
183         signal_stop();
184 }
185
186 /* SYNTAX: UNQUERY [<nick>] */
187 static void cmd_unquery(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
188 {
189         QUERY_REC *query;
190
191         g_return_if_fail(data != NULL);
192
193         if (*data == '\0') {
194                 /* remove current query */
195                 query = QUERY(item);
196                 if (query == NULL) return;
197         } else {
198                 query = query_find(server, data);
199                 if (query == NULL) {
200                         printformat(server, NULL, MSGLEVEL_CLIENTERROR,
201                                     TXT_NO_QUERY, data);
202                         return;
203                 }
204         }
205
206         query_destroy(query);
207 }
208
209 /* SYNTAX: QUERY [-window] [-<server tag>] <nick> [<message>] */
210 static void cmd_query(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
211 {
212         GHashTable *optlist;
213         QUERY_REC *query;
214         char *nick, *msg;
215         void *free_arg;
216
217         g_return_if_fail(data != NULL);
218
219         if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST |
220                             PARAM_FLAG_OPTIONS | PARAM_FLAG_UNKNOWN_OPTIONS,
221                             "query", &optlist, &nick, &msg))
222                 return;
223
224         if (*nick == '\0') {
225                 /* remove current query */
226                 cmd_unquery("", server, item);
227                 cmd_params_free(free_arg);
228                 return;
229         }
230
231         server = cmd_options_get_server("query", optlist, server);
232         if (server == NULL) {
233                 cmd_params_free(free_arg);
234                 return;
235         }
236
237         if (*nick != '=' && (server == NULL || !server->connected))
238                 cmd_param_error(CMDERR_NOT_CONNECTED);
239
240         if (g_hash_table_lookup(optlist, "window") != NULL) {
241                 signal_add("query created",
242                            (SIGNAL_FUNC) signal_query_created_curwin);
243         }
244
245         query = query_find(server, nick);
246         if (query == NULL)
247                 query = CHAT_PROTOCOL(server)->
248                         query_create(server->tag, nick, FALSE);
249         else {
250                 /* query already exists */
251                 WINDOW_REC *window = window_item_window(query);
252
253                 if (window == active_win) {
254                         /* query is in active window, set it active */
255                         window_item_set_active(active_win,
256                                                (WI_ITEM_REC *) query);
257                 } else {
258                         /* notify user how to move the query to active
259                            window. this was used to be done automatically
260                            but it just confused everyone who did it
261                            accidentally */
262                         printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
263                                            TXT_QUERY_MOVE_NOTIFY, query->name,
264                                            window->refnum);
265                 }
266         }
267
268         if (g_hash_table_lookup(optlist, "window") != NULL) {
269                 signal_remove("query created",
270                               (SIGNAL_FUNC) signal_query_created_curwin);
271         }
272
273         if (*msg != '\0') {
274                 msg = g_strdup_printf("-nick %s %s", nick, msg);
275                 signal_emit("command msg", 3, msg, server, query);
276                 g_free(msg);
277         }
278
279         cmd_params_free(free_arg);
280 }
281
282 static void window_reset_query_timestamps(WINDOW_REC *window)
283 {
284         GSList *tmp;
285
286         if (window == NULL)
287                 return;
288
289         for (tmp = window->items; tmp != NULL; tmp = tmp->next) {
290                 QUERY_REC *query = QUERY(tmp->data);
291
292                 if (query != NULL)
293                         query->last_unread_msg = time(NULL);
294         }
295 }
296
297 static void sig_window_changed(WINDOW_REC *window, WINDOW_REC *old_window)
298 {
299         /* reset the queries last_unread_msg so query doesn't get closed
300            immediately after switched to the window, or after changed to
301            some other window from it */
302         window_reset_query_timestamps(window);
303         window_reset_query_timestamps(old_window);
304 }
305
306 static int sig_query_autoclose(void)
307 {
308         WINDOW_REC *window;
309         GSList *tmp, *next;
310         time_t now;
311
312         now = time(NULL);
313         for (tmp = queries; tmp != NULL; tmp = next) {
314                 QUERY_REC *rec = tmp->data;
315
316                 next = tmp->next;
317                 window = window_item_window((WI_ITEM_REC *) rec);
318                 if (window != active_win && rec->data_level < DATA_LEVEL_MSG &&
319                     now-rec->last_unread_msg > query_auto_close)
320                         query_destroy(rec);
321         }
322         return 1;
323 }
324
325 static void sig_message_private(SERVER_REC *server, const char *msg,
326                                 const char *nick, const char *address)
327 {
328         QUERY_REC *query;
329
330         /* create query window if needed */
331         query = privmsg_get_query(server, nick, FALSE, MSGLEVEL_MSGS);
332
333         /* reset the query's last_unread_msg timestamp */
334         if (query != NULL)
335                 query->last_unread_msg = time(NULL);
336 }
337
338 static void read_settings(void)
339 {
340         querycreate_level = level2bits(settings_get_str("autocreate_query_level"));
341         query_auto_close = settings_get_int("autoclose_query");
342         if (query_auto_close > 0 && queryclose_tag == -1)
343                 queryclose_tag = g_timeout_add(5000, (GSourceFunc) sig_query_autoclose, NULL);
344         else if (query_auto_close <= 0 && queryclose_tag != -1) {
345                 g_source_remove(queryclose_tag);
346                 queryclose_tag = -1;
347         }
348 }
349
350 void fe_queries_init(void)
351 {
352         settings_add_str("lookandfeel", "autocreate_query_level", "MSGS DCCMSGS");
353         settings_add_bool("lookandfeel", "autocreate_own_query", TRUE);
354         settings_add_int("lookandfeel", "autoclose_query", 0);
355
356         queryclose_tag = -1;
357         read_settings();
358
359         signal_add("query created", (SIGNAL_FUNC) signal_query_created);
360         signal_add("query destroyed", (SIGNAL_FUNC) signal_query_destroyed);
361         signal_add("query server changed", (SIGNAL_FUNC) signal_query_server_changed);
362         signal_add("query nick changed", (SIGNAL_FUNC) signal_query_nick_changed);
363         signal_add("window item server changed", (SIGNAL_FUNC) signal_window_item_server_changed);
364         signal_add("server connected", (SIGNAL_FUNC) sig_server_connected);
365         signal_add("window changed", (SIGNAL_FUNC) sig_window_changed);
366         signal_add_first("message private", (SIGNAL_FUNC) sig_message_private);
367         signal_add("setup changed", (SIGNAL_FUNC) read_settings);
368
369         command_bind("query", NULL, (SIGNAL_FUNC) cmd_query);
370         command_bind("unquery", NULL, (SIGNAL_FUNC) cmd_unquery);
371         command_bind("window server", NULL, (SIGNAL_FUNC) cmd_window_server);
372
373         command_set_options("query", "window");
374 }
375
376 void fe_queries_deinit(void)
377 {
378         if (queryclose_tag != -1) g_source_remove(queryclose_tag);
379
380         signal_remove("query created", (SIGNAL_FUNC) signal_query_created);
381         signal_remove("query destroyed", (SIGNAL_FUNC) signal_query_destroyed);
382         signal_remove("query server changed", (SIGNAL_FUNC) signal_query_server_changed);
383         signal_remove("query nick changed", (SIGNAL_FUNC) signal_query_nick_changed);
384         signal_remove("window item server changed", (SIGNAL_FUNC) signal_window_item_server_changed);
385         signal_remove("server connected", (SIGNAL_FUNC) sig_server_connected);
386         signal_remove("window changed", (SIGNAL_FUNC) sig_window_changed);
387         signal_remove("message private", (SIGNAL_FUNC) sig_message_private);
388         signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
389
390         command_unbind("query", (SIGNAL_FUNC) cmd_query);
391         command_unbind("unquery", (SIGNAL_FUNC) cmd_unquery);
392         command_unbind("window server", (SIGNAL_FUNC) cmd_window_server);
393 }