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);
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);
129 signal_emit("window item changed", 2,
130 window_item_window((WI_ITEM_REC *) query), query);
133 static void signal_window_item_server_changed(WINDOW_REC *window,
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);
143 static void sig_server_connected(SERVER_REC *server)
147 if (!IS_SERVER(server))
150 /* check if there's any queries without server */
151 for (tmp = queries; tmp != NULL; tmp = tmp->next) {
152 QUERY_REC *rec = tmp->data;
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);
163 static void cmd_window_server(const char *data)
169 g_return_if_fail(data != NULL);
171 server = server_find_tag(data);
172 query = QUERY(active_win->active);
173 if (server == NULL || query == NULL)
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);
182 query_change_server(query, server);
186 /* SYNTAX: UNQUERY [<nick>] */
187 static void cmd_unquery(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
191 g_return_if_fail(data != NULL);
194 /* remove current query */
196 if (query == NULL) return;
198 query = query_find(server, data);
200 printformat(server, NULL, MSGLEVEL_CLIENTERROR,
206 query_destroy(query);
209 /* SYNTAX: QUERY [-window] [-<server tag>] <nick> [<message>] */
210 static void cmd_query(const char *data, SERVER_REC *server, WI_ITEM_REC *item)
217 g_return_if_fail(data != NULL);
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))
225 /* remove current query */
226 cmd_unquery("", server, item);
227 cmd_params_free(free_arg);
231 server = cmd_options_get_server("query", optlist, server);
232 if (server == NULL) {
233 cmd_params_free(free_arg);
237 if (*nick != '=' && (server == NULL || !server->connected))
238 cmd_param_error(CMDERR_NOT_CONNECTED);
240 if (g_hash_table_lookup(optlist, "window") != NULL) {
241 signal_add("query created",
242 (SIGNAL_FUNC) signal_query_created_curwin);
245 query = query_find(server, nick);
247 query = CHAT_PROTOCOL(server)->
248 query_create(server->tag, nick, FALSE);
250 /* query already exists */
251 WINDOW_REC *window = window_item_window(query);
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);
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
262 printformat_window(active_win, MSGLEVEL_CLIENTNOTICE,
263 TXT_QUERY_MOVE_NOTIFY, query->name,
268 if (g_hash_table_lookup(optlist, "window") != NULL) {
269 signal_remove("query created",
270 (SIGNAL_FUNC) signal_query_created_curwin);
274 msg = g_strdup_printf("-nick %s %s", nick, msg);
275 signal_emit("command msg", 3, msg, server, query);
279 cmd_params_free(free_arg);
282 static void window_reset_query_timestamps(WINDOW_REC *window)
289 for (tmp = window->items; tmp != NULL; tmp = tmp->next) {
290 QUERY_REC *query = QUERY(tmp->data);
293 query->last_unread_msg = time(NULL);
297 static void sig_window_changed(WINDOW_REC *window, WINDOW_REC *old_window)
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);
306 static int sig_query_autoclose(void)
313 for (tmp = queries; tmp != NULL; tmp = next) {
314 QUERY_REC *rec = tmp->data;
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)
325 static void sig_message_private(SERVER_REC *server, const char *msg,
326 const char *nick, const char *address)
330 /* create query window if needed */
331 query = privmsg_get_query(server, nick, FALSE, MSGLEVEL_MSGS);
333 /* reset the query's last_unread_msg timestamp */
335 query->last_unread_msg = time(NULL);
338 static void read_settings(void)
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);
350 void fe_queries_init(void)
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);
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);
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);
373 command_set_options("query", "window");
376 void fe_queries_deinit(void)
378 if (queryclose_tag != -1) g_source_remove(queryclose_tag);
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);
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);