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"
33 #include "fe-windows.h"
34 #include "window-items.h"
35 #include "printtext.h"
37 static int queryclose_tag, query_auto_close, querycreate_level;
39 /* Return query where to put the private message. */
40 QUERY_REC *privmsg_get_query(SERVER_REC *server, const char *nick,
45 g_return_val_if_fail(IS_SERVER(server), NULL);
46 g_return_val_if_fail(nick != NULL, NULL);
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);
58 static void signal_query_created(QUERY_REC *query, gpointer automatic)
62 g_return_if_fail(IS_QUERY(query));
64 if (window_item_window(query) == NULL) {
65 window_item_create((WI_ITEM_REC *) query,
66 GPOINTER_TO_INT(automatic));
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);
75 static void signal_query_created_curwin(QUERY_REC *query)
77 g_return_if_fail(IS_QUERY(query));
79 window_item_add(active_win, (WI_ITEM_REC *) query, FALSE);
82 static void signal_query_destroyed(QUERY_REC *query)
87 g_return_if_fail(IS_QUERY(query));
89 window = window_item_window((WI_ITEM_REC *) query);
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);
97 window_item_destroy((WI_ITEM_REC *) query);
100 window_auto_destroy(window);
103 static void signal_query_server_changed(QUERY_REC *query)
107 g_return_if_fail(query != NULL);
109 window = window_item_window((WI_ITEM_REC *) query);
110 if (window->active == (WI_ITEM_REC *) query)
111 window_change_server(window, query->server);
114 static void signal_query_nick_changed(QUERY_REC *query, const char *oldnick)
118 g_return_if_fail(query != NULL);
120 format_create_dest_tag(&dest, query->server, query->server_tag,
121 query->name, MSGLEVEL_CLIENTNOTICE, NULL);
122 printformat_dest(&dest, TXT_NICK_CHANGED, oldnick,
123 query->name, query->name);
125 signal_emit("window item changed", 2,
126 window_item_window((WI_ITEM_REC *) query), query);
129 static void signal_window_item_server_changed(WINDOW_REC *window,
132 if (IS_QUERY(query)) {
133 g_free_and_null(query->server_tag);
134 if (query->server != NULL)
135 query->server_tag = g_strdup(query->server->tag);
139 static void sig_server_connected(SERVER_REC *server)
143 if (!IS_SERVER(server))
146 /* check if there's any queries without server */
147 for (tmp = queries; tmp != NULL; tmp = tmp->next) {
148 QUERY_REC *rec = tmp->data;
150 if (rec->server == NULL &&
151 (rec->server_tag == NULL ||
152 g_strcasecmp(rec->server_tag, server->tag) == 0)) {
153 window_item_change_server((WI_ITEM_REC *) rec, server);
154 server->queries = g_slist_append(server->queries, rec);
159 static void cmd_window_server(const char *data)
165 g_return_if_fail(data != NULL);
167 server = server_find_tag(data);
168 query = QUERY(active_win->active);
169 if (server == NULL || query == NULL)
172 /* /WINDOW SERVER used in a query window */
173 format_create_dest_tag(&dest, query->server, query->server_tag,
174 query->name, MSGLEVEL_CLIENTNOTICE, NULL);
175 printformat_dest(&dest, TXT_QUERY_SERVER_CHANGED,
176 query->name, server->tag);
178 query_change_server(query, server);
182 /* SYNTAX: UNQUERY [<nick>] */
183 static void cmd_unquery(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
187 g_return_if_fail(data != NULL);
190 /* remove current query */
192 if (query == NULL) return;
194 query = query_find(server, data);
196 printformat(server, NULL, MSGLEVEL_CLIENTERROR,
202 query_destroy(query);
205 /* SYNTAX: QUERY [-window] [-<server tag>] <nick> [<message>] */
206 static void cmd_query(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
213 g_return_if_fail(data != NULL);
215 if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST |
216 PARAM_FLAG_OPTIONS | PARAM_FLAG_UNKNOWN_OPTIONS,
217 "query", &optlist, &nick, &msg))
221 /* remove current query */
222 cmd_unquery("", server, item);
223 cmd_params_free(free_arg);
227 server = cmd_options_get_server("query", optlist, server);
228 if (server == NULL) {
229 cmd_params_free(free_arg);
233 if (*nick != '=' && (server == NULL || !server->connected))
234 cmd_param_error(CMDERR_NOT_CONNECTED);
236 if (g_hash_table_lookup(optlist, "window") != NULL) {
237 signal_add("query created",
238 (SIGNAL_FUNC) signal_query_created_curwin);
241 query = query_find(server, nick);
243 query = CHAT_PROTOCOL(server)->
244 query_create(server->tag, nick, FALSE);
246 /* query already exists */
247 WINDOW_REC *window = window_item_window(query);
249 if (window == active_win) {
250 /* query is in active window, set it active */
251 window_item_set_active(active_win,
252 (WI_ITEM_REC *) query);
254 /* notify user how to move the query to active
255 window. this was used to be done automatically
256 but it just confused everyone who did it
258 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
259 TXT_QUERY_MOVE_NOTIFY, query->name,
264 if (g_hash_table_lookup(optlist, "window") != NULL) {
265 signal_remove("query created",
266 (SIGNAL_FUNC) signal_query_created_curwin);
270 msg = g_strdup_printf("-nick %s %s", nick, msg);
271 signal_emit("command msg", 3, msg, server, query);
275 cmd_params_free(free_arg);
278 static void window_reset_query_timestamps(WINDOW_REC *window)
285 for (tmp = window->items; tmp != NULL; tmp = tmp->next) {
286 QUERY_REC *query = QUERY(tmp->data);
289 query->last_unread_msg = time(NULL);
293 static void sig_window_changed(WINDOW_REC *window, WINDOW_REC *old_window)
295 /* reset the queries last_unread_msg so query doesn't get closed
296 immediately after switched to the window, or after changed to
297 some other window from it */
298 window_reset_query_timestamps(window);
299 window_reset_query_timestamps(old_window);
302 static int sig_query_autoclose(void)
309 for (tmp = queries; tmp != NULL; tmp = next) {
310 QUERY_REC *rec = tmp->data;
313 window = window_item_window((WI_ITEM_REC *) rec);
314 if (window != active_win && rec->data_level < DATA_LEVEL_MSG &&
315 now-rec->last_unread_msg > query_auto_close)
321 static void sig_message_private(SERVER_REC *server, const char *msg,
322 const char *nick, const char *address)
326 /* create query window if needed */
327 query = privmsg_get_query(server, nick, FALSE, MSGLEVEL_MSGS);
329 /* reset the query's last_unread_msg timestamp */
331 query->last_unread_msg = time(NULL);
334 static void read_settings(void)
336 querycreate_level = level2bits(settings_get_str("autocreate_query_level"));
337 query_auto_close = settings_get_int("autoclose_query");
338 if (query_auto_close > 0 && queryclose_tag == -1)
339 queryclose_tag = g_timeout_add(5000, (GSourceFunc) sig_query_autoclose, NULL);
340 else if (query_auto_close <= 0 && queryclose_tag != -1) {
341 g_source_remove(queryclose_tag);
346 void fe_queries_init(void)
348 settings_add_str("lookandfeel", "autocreate_query_level", "MSGS DCCMSGS");
349 settings_add_bool("lookandfeel", "autocreate_own_query", TRUE);
350 settings_add_int("lookandfeel", "autoclose_query", 0);
355 signal_add("query created", (SIGNAL_FUNC) signal_query_created);
356 signal_add("query destroyed", (SIGNAL_FUNC) signal_query_destroyed);
357 signal_add("query server changed", (SIGNAL_FUNC) signal_query_server_changed);
358 signal_add("query nick changed", (SIGNAL_FUNC) signal_query_nick_changed);
359 signal_add("window item server changed", (SIGNAL_FUNC) signal_window_item_server_changed);
360 signal_add("server connected", (SIGNAL_FUNC) sig_server_connected);
361 signal_add("window changed", (SIGNAL_FUNC) sig_window_changed);
362 signal_add_first("message private", (SIGNAL_FUNC) sig_message_private);
363 signal_add("setup changed", (SIGNAL_FUNC) read_settings);
365 command_bind("query", NULL, (SIGNAL_FUNC) cmd_query);
366 command_bind("unquery", NULL, (SIGNAL_FUNC) cmd_unquery);
367 command_bind("window server", NULL, (SIGNAL_FUNC) cmd_window_server);
369 command_set_options("query", "window");
372 void fe_queries_deinit(void)
374 if (queryclose_tag != -1) g_source_remove(queryclose_tag);
376 signal_remove("query created", (SIGNAL_FUNC) signal_query_created);
377 signal_remove("query destroyed", (SIGNAL_FUNC) signal_query_destroyed);
378 signal_remove("query server changed", (SIGNAL_FUNC) signal_query_server_changed);
379 signal_remove("query nick changed", (SIGNAL_FUNC) signal_query_nick_changed);
380 signal_remove("window item server changed", (SIGNAL_FUNC) signal_window_item_server_changed);
381 signal_remove("server connected", (SIGNAL_FUNC) sig_server_connected);
382 signal_remove("window changed", (SIGNAL_FUNC) sig_window_changed);
383 signal_remove("message private", (SIGNAL_FUNC) sig_message_private);
384 signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
386 command_unbind("query", (SIGNAL_FUNC) cmd_query);
387 command_unbind("unquery", (SIGNAL_FUNC) cmd_unquery);
388 command_unbind("window server", (SIGNAL_FUNC) cmd_window_server);