4 Copyright (C) 1999-2000 Timo Sirainen
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.
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.
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
22 #include "module-formats.h"
29 #include "chat-protocols.h"
32 #include "fe-windows.h"
33 #include "window-items.h"
34 #include "printtext.h"
36 static int queryclose_tag, query_auto_close, querycreate_level;
38 /* Return query where to put the private message. */
39 QUERY_REC *privmsg_get_query(SERVER_REC *server, const char *nick,
44 g_return_val_if_fail(IS_SERVER(server), NULL);
45 g_return_val_if_fail(nick != NULL, NULL);
47 query = query_find(server, nick);
48 if (query == NULL && (querycreate_level & level) != 0 &&
49 (!own || settings_get_bool("autocreate_own_query"))) {
50 query = CHAT_PROTOCOL(server)->
51 query_create(server->tag, nick, TRUE);
57 static void signal_query_created(QUERY_REC *query, gpointer automatic)
59 g_return_if_fail(IS_QUERY(query));
61 if (window_item_window(query) == NULL) {
62 window_item_create((WI_ITEM_REC *) query,
63 GPOINTER_TO_INT(automatic));
64 printformat(query->server, query->name, MSGLEVEL_CLIENTNOTICE,
65 TXT_QUERY_STARTED, query->name);
69 static void signal_query_created_curwin(QUERY_REC *query)
71 g_return_if_fail(IS_QUERY(query));
73 window_item_add(active_win, (WI_ITEM_REC *) query, FALSE);
76 static void signal_query_destroyed(QUERY_REC *query)
80 g_return_if_fail(IS_QUERY(query));
82 window = window_item_window((WI_ITEM_REC *) query);
84 window_item_destroy((WI_ITEM_REC *) query);
87 window_auto_destroy(window);
91 static void signal_query_server_changed(QUERY_REC *query)
95 g_return_if_fail(query != NULL);
97 window = window_item_window((WI_ITEM_REC *) query);
98 if (window->active == (WI_ITEM_REC *) query)
99 window_change_server(window, query->server);
102 static void signal_query_nick_changed(QUERY_REC *query, const char *oldnick)
104 g_return_if_fail(query != NULL);
106 signal_emit("window item changed", 2,
107 window_item_window((WI_ITEM_REC *) query), query);
110 static void signal_window_item_server_changed(WINDOW_REC *window,
113 if (IS_QUERY(query)) {
114 g_free_and_null(query->server_tag);
115 if (query->server != NULL)
116 query->server_tag = g_strdup(query->server->tag);
120 static void signal_window_item_destroy(WINDOW_REC *window, WI_ITEM_REC *item)
124 g_return_if_fail(window != NULL);
127 if (query != NULL) query_destroy(query);
130 static void sig_server_connected(SERVER_REC *server)
134 if (!IS_SERVER(server))
137 /* check if there's any queries without server */
138 for (tmp = queries; tmp != NULL; tmp = tmp->next) {
139 QUERY_REC *rec = tmp->data;
141 if (rec->server == NULL &&
142 (rec->server_tag == NULL ||
143 g_strcasecmp(rec->server_tag, server->tag) == 0)) {
144 window_item_change_server((WI_ITEM_REC *) rec, server);
145 server->queries = g_slist_append(server->queries, rec);
150 static void cmd_window_server(const char *data)
155 g_return_if_fail(data != NULL);
157 server = server_find_tag(data);
158 query = QUERY(active_win->active);
159 if (server == NULL || query == NULL)
162 /* /WINDOW SERVER used in a query window */
163 query_change_server(query, server);
164 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
165 TXT_QUERY_SERVER_CHANGED,
166 query->name, server->tag);
170 /* SYNTAX: UNQUERY [<nick>] */
171 static void cmd_unquery(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
175 g_return_if_fail(data != NULL);
178 /* remove current query */
180 if (query == NULL) return;
182 query = query_find(server, data);
184 printformat(server, NULL, MSGLEVEL_CLIENTERROR,
190 query_destroy(query);
193 /* SYNTAX: QUERY [-window] [-<server tag>] <nick> [<message>] */
194 static void cmd_query(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
201 g_return_if_fail(data != NULL);
204 /* remove current query */
205 cmd_unquery("", server, item);
209 if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST |
210 PARAM_FLAG_OPTIONS | PARAM_FLAG_UNKNOWN_OPTIONS,
211 "query", &optlist, &nick, &msg))
213 if (*nick == '\0') cmd_param_error(CMDERR_NOT_ENOUGH_PARAMS);
215 server = cmd_options_get_server("query", optlist, server);
216 if (server == NULL) {
217 cmd_params_free(free_arg);
221 if (*nick != '=' && (server == NULL || !server->connected))
222 cmd_param_error(CMDERR_NOT_CONNECTED);
224 if (g_hash_table_lookup(optlist, "window") != NULL) {
225 signal_add("query created",
226 (SIGNAL_FUNC) signal_query_created_curwin);
229 query = query_find(server, nick);
231 CHAT_PROTOCOL(server)->query_create(server->tag, nick, FALSE);
233 /* query already exists */
234 WINDOW_REC *window = window_item_window(query);
236 if (window == active_win) {
237 /* query is in active window, set it active */
238 window_item_set_active(active_win,
239 (WI_ITEM_REC *) query);
241 /* notify user how to move the query to active
242 window. this was used to be done automatically
243 but it just confused everyone who did it
245 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
246 TXT_QUERY_MOVE_NOTIFY, query->name,
251 if (g_hash_table_lookup(optlist, "window") != NULL) {
252 signal_remove("query created",
253 (SIGNAL_FUNC) signal_query_created_curwin);
257 /* FIXME: we'll need some function that does both
258 of these. and separate the , and . target handling
259 from own_private messagge.. */
260 server->send_message(server, nick, msg);
262 signal_emit("message own_private", 4,
263 server, msg, nick, nick);
266 cmd_params_free(free_arg);
269 static int window_has_query(WINDOW_REC *window)
273 g_return_val_if_fail(window != NULL, FALSE);
275 for (tmp = window->items; tmp != NULL; tmp = tmp->next) {
276 if (IS_QUERY(tmp->data))
283 static void sig_window_changed(WINDOW_REC *window, WINDOW_REC *old_window)
285 if (query_auto_close <= 0)
288 /* reset the window's last_line timestamp so that query doesn't get
289 closed immediately after switched to the window, or after changed
290 to some other window from it */
291 if (window != NULL && window_has_query(window))
292 window->last_line = time(NULL);
293 if (old_window != NULL && window_has_query(old_window))
294 old_window->last_line = time(NULL);
297 static int sig_query_autoclose(void)
304 for (tmp = queries; tmp != NULL; tmp = next) {
305 QUERY_REC *rec = tmp->data;
308 window = window_item_window((WI_ITEM_REC *) rec);
309 if (window != active_win && rec->data_level == 0 &&
310 now-window->last_line > query_auto_close)
316 static void sig_message_private(SERVER_REC *server, const char *msg,
317 const char *nick, const char *address)
319 /* create query window if needed */
320 privmsg_get_query(server, nick, FALSE, MSGLEVEL_MSGS);
323 static void read_settings(void)
325 querycreate_level = level2bits(settings_get_str("autocreate_query_level"));
326 query_auto_close = settings_get_int("autoclose_query");
327 if (query_auto_close > 0 && queryclose_tag == -1)
328 queryclose_tag = g_timeout_add(5000, (GSourceFunc) sig_query_autoclose, NULL);
329 else if (query_auto_close <= 0 && queryclose_tag != -1) {
330 g_source_remove(queryclose_tag);
335 void fe_queries_init(void)
337 settings_add_str("lookandfeel", "autocreate_query_level", "MSGS DCCMSGS");
338 settings_add_bool("lookandfeel", "autocreate_own_query", TRUE);
339 settings_add_int("lookandfeel", "autoclose_query", 0);
344 signal_add("query created", (SIGNAL_FUNC) signal_query_created);
345 signal_add("query destroyed", (SIGNAL_FUNC) signal_query_destroyed);
346 signal_add("query server changed", (SIGNAL_FUNC) signal_query_server_changed);
347 signal_add("query nick changed", (SIGNAL_FUNC) signal_query_nick_changed);
348 signal_add("window item server changed", (SIGNAL_FUNC) signal_window_item_server_changed);
349 signal_add_last("window item destroy", (SIGNAL_FUNC) signal_window_item_destroy);
350 signal_add("server connected", (SIGNAL_FUNC) sig_server_connected);
351 signal_add("window changed", (SIGNAL_FUNC) sig_window_changed);
352 signal_add_first("message private", (SIGNAL_FUNC) sig_message_private);
353 signal_add("setup changed", (SIGNAL_FUNC) read_settings);
355 command_bind("query", NULL, (SIGNAL_FUNC) cmd_query);
356 command_bind("unquery", NULL, (SIGNAL_FUNC) cmd_unquery);
357 command_bind("window server", NULL, (SIGNAL_FUNC) cmd_window_server);
359 command_set_options("query", "window");
362 void fe_queries_deinit(void)
364 if (queryclose_tag != -1) g_source_remove(queryclose_tag);
366 signal_remove("query created", (SIGNAL_FUNC) signal_query_created);
367 signal_remove("query destroyed", (SIGNAL_FUNC) signal_query_destroyed);
368 signal_remove("query server changed", (SIGNAL_FUNC) signal_query_server_changed);
369 signal_remove("query nick changed", (SIGNAL_FUNC) signal_query_nick_changed);
370 signal_remove("window item server changed", (SIGNAL_FUNC) signal_window_item_server_changed);
371 signal_remove("window item destroy", (SIGNAL_FUNC) signal_window_item_destroy);
372 signal_remove("server connected", (SIGNAL_FUNC) sig_server_connected);
373 signal_remove("window changed", (SIGNAL_FUNC) sig_window_changed);
374 signal_remove("message private", (SIGNAL_FUNC) sig_message_private);
375 signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
377 command_unbind("query", (SIGNAL_FUNC) cmd_query);
378 command_unbind("unquery", (SIGNAL_FUNC) cmd_unquery);
379 command_unbind("window server", (SIGNAL_FUNC) cmd_window_server);