2000-04-26 04:03:38 -04:00
|
|
|
/*
|
|
|
|
fe-query.c : irssi
|
|
|
|
|
|
|
|
Copyright (C) 1999-2000 Timo Sirainen
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "module.h"
|
|
|
|
#include "module-formats.h"
|
|
|
|
#include "modules.h"
|
|
|
|
#include "signals.h"
|
|
|
|
#include "commands.h"
|
2000-05-04 06:32:42 -04:00
|
|
|
#include "settings.h"
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
#include "irc.h"
|
|
|
|
#include "levels.h"
|
|
|
|
#include "query.h"
|
|
|
|
|
|
|
|
#include "windows.h"
|
|
|
|
#include "window-items.h"
|
|
|
|
|
2000-05-04 06:32:42 -04:00
|
|
|
static int queryclose_tag, query_auto_close;
|
|
|
|
|
2000-06-12 18:57:53 -04:00
|
|
|
/* Return query where to put the private message. */
|
2000-06-14 15:05:51 -04:00
|
|
|
QUERY_REC *privmsg_get_query(IRC_SERVER_REC *server, const char *nick, int own)
|
2000-06-12 18:57:53 -04:00
|
|
|
{
|
|
|
|
QUERY_REC *query;
|
|
|
|
|
|
|
|
query = query_find(server, nick);
|
2000-06-14 15:05:51 -04:00
|
|
|
if (query == NULL && settings_get_bool("autocreate_query") &&
|
|
|
|
(!own || settings_get_bool("autocreate_own_query")))
|
2000-06-12 18:57:53 -04:00
|
|
|
query = query_create(server, nick, TRUE);
|
|
|
|
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
static void signal_query_created(QUERY_REC *query, gpointer automatic)
|
|
|
|
{
|
2000-07-31 20:39:53 -04:00
|
|
|
if (window_item_find(query->server, query->nick) != NULL)
|
|
|
|
return;
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
window_item_create((WI_ITEM_REC *) query, GPOINTER_TO_INT(automatic));
|
2000-07-31 20:39:53 -04:00
|
|
|
printformat(query->server, query->nick, MSGLEVEL_CLIENTNOTICE,
|
|
|
|
IRCTXT_QUERY_STARTED, query->nick);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void signal_query_created_curwin(QUERY_REC *query)
|
|
|
|
{
|
|
|
|
g_return_if_fail(query != NULL);
|
|
|
|
|
|
|
|
window_add_item(active_win, (WI_ITEM_REC *) query, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void signal_query_destroyed(QUERY_REC *query)
|
|
|
|
{
|
|
|
|
WINDOW_REC *window;
|
|
|
|
|
|
|
|
g_return_if_fail(query != NULL);
|
|
|
|
|
|
|
|
window = window_item_window((WI_ITEM_REC *) query);
|
2000-06-17 08:22:56 -04:00
|
|
|
if (window != NULL) {
|
|
|
|
window_remove_item(window, (WI_ITEM_REC *) query);
|
|
|
|
|
2000-08-10 07:25:34 -04:00
|
|
|
if (window->items == NULL && windows->next != NULL &&
|
|
|
|
!query->unwanted && settings_get_bool("autoclose_windows"))
|
2000-06-17 08:22:56 -04:00
|
|
|
window_destroy(window);
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void signal_window_item_removed(WINDOW_REC *window, WI_ITEM_REC *item)
|
|
|
|
{
|
|
|
|
QUERY_REC *query;
|
|
|
|
|
|
|
|
g_return_if_fail(window != NULL);
|
|
|
|
|
|
|
|
query = irc_item_query(item);
|
2000-06-17 08:22:56 -04:00
|
|
|
if (query != NULL) query_destroy(query);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sig_server_connected(IRC_SERVER_REC *server)
|
|
|
|
{
|
|
|
|
GSList *tmp;
|
|
|
|
|
|
|
|
if (!irc_server_check(server))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* check if there's any queries without server */
|
|
|
|
for (tmp = queries; tmp != NULL; tmp = tmp->next) {
|
|
|
|
QUERY_REC *rec = tmp->data;
|
|
|
|
|
|
|
|
if (rec->server == NULL &&
|
|
|
|
g_strcasecmp(rec->server_tag, server->tag) == 0) {
|
|
|
|
window_item_change_server((WI_ITEM_REC *) rec, server);
|
|
|
|
server->queries = g_slist_append(server->queries, rec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_window_server(const char *data)
|
|
|
|
{
|
|
|
|
SERVER_REC *server;
|
|
|
|
|
|
|
|
g_return_if_fail(data != NULL);
|
|
|
|
|
|
|
|
server = server_find_tag(data);
|
|
|
|
if (irc_server_check(server) && irc_item_query(active_win->active)) {
|
|
|
|
/* /WINDOW SERVER used in a query window */
|
|
|
|
query_change_server((QUERY_REC *) active_win->active,
|
|
|
|
(IRC_SERVER_REC *) server);
|
|
|
|
window_change_server(active_win, server);
|
|
|
|
|
|
|
|
printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE, IRCTXT_QUERY_SERVER_CHANGED, server->tag, server->connrec->address,
|
|
|
|
server->connrec->ircnet == NULL ? "" : server->connrec->ircnet);
|
|
|
|
|
|
|
|
signal_stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-11 22:16:52 -04:00
|
|
|
static void cmd_wquery_pre(const char *data)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2000-08-11 22:16:52 -04:00
|
|
|
GHashTable *optlist;
|
|
|
|
char *nick;
|
|
|
|
void *free_arg;
|
|
|
|
|
|
|
|
if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS |
|
|
|
|
PARAM_FLAG_UNKNOWN_OPTIONS | PARAM_FLAG_GETREST,
|
|
|
|
"query", &optlist, &nick))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (g_hash_table_lookup(optlist, "window") != NULL) {
|
|
|
|
signal_add("query created",
|
|
|
|
(SIGNAL_FUNC) signal_query_created_curwin);
|
|
|
|
}
|
|
|
|
cmd_params_free(free_arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cmd_wquery_post(const char *data)
|
|
|
|
{
|
|
|
|
GHashTable *optlist;
|
|
|
|
char *nick;
|
|
|
|
void *free_arg;
|
|
|
|
|
|
|
|
if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS |
|
|
|
|
PARAM_FLAG_UNKNOWN_OPTIONS | PARAM_FLAG_GETREST,
|
|
|
|
"query", &optlist, &nick))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (g_hash_table_lookup(optlist, "window") != NULL) {
|
|
|
|
signal_remove("query created",
|
|
|
|
(SIGNAL_FUNC) signal_query_created_curwin);
|
|
|
|
}
|
|
|
|
cmd_params_free(free_arg);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-06-14 13:41:45 -04:00
|
|
|
static int window_has_query(WINDOW_REC *window)
|
2000-05-04 06:32:42 -04:00
|
|
|
{
|
|
|
|
GSList *tmp;
|
|
|
|
|
2000-06-15 14:33:08 -04:00
|
|
|
g_return_val_if_fail(window != NULL, FALSE);
|
|
|
|
|
2000-05-04 06:32:42 -04:00
|
|
|
for (tmp = window->items; tmp != NULL; tmp = tmp->next) {
|
|
|
|
if (irc_item_query(tmp->data))
|
2000-06-14 13:41:45 -04:00
|
|
|
return TRUE;
|
2000-05-04 06:32:42 -04:00
|
|
|
}
|
2000-06-14 13:41:45 -04:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sig_window_changed(WINDOW_REC *window, WINDOW_REC *old_window)
|
|
|
|
{
|
|
|
|
if (query_auto_close <= 0)
|
|
|
|
return;
|
2000-05-04 06:32:42 -04:00
|
|
|
|
|
|
|
/* reset the window's last_line timestamp so that query doesn't get
|
2000-06-14 13:41:45 -04:00
|
|
|
closed immediately after switched to the window, or after changed
|
|
|
|
to some other window from it */
|
2000-06-15 14:33:08 -04:00
|
|
|
if (window != NULL && window_has_query(window))
|
2000-06-14 13:41:45 -04:00
|
|
|
window->last_line = time(NULL);
|
2000-06-15 14:33:08 -04:00
|
|
|
if (old_window != NULL && window_has_query(old_window))
|
2000-06-14 13:41:45 -04:00
|
|
|
old_window->last_line = time(NULL);
|
2000-05-04 06:32:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int sig_query_autoclose(void)
|
|
|
|
{
|
|
|
|
WINDOW_REC *window;
|
|
|
|
GSList *tmp, *next;
|
|
|
|
time_t now;
|
|
|
|
|
|
|
|
now = time(NULL);
|
|
|
|
for (tmp = queries; tmp != NULL; tmp = next) {
|
|
|
|
QUERY_REC *rec = tmp->data;
|
|
|
|
|
|
|
|
next = tmp->next;
|
|
|
|
window = window_item_window((WI_ITEM_REC *) rec);
|
|
|
|
if (window != active_win && rec->new_data == 0 &&
|
|
|
|
now-window->last_line > query_auto_close)
|
2000-06-17 08:22:56 -04:00
|
|
|
query_destroy(rec);
|
2000-05-04 06:32:42 -04:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void read_settings(void)
|
|
|
|
{
|
2000-06-14 15:05:51 -04:00
|
|
|
query_auto_close = settings_get_int("autoclose_query");
|
2000-05-04 06:32:42 -04:00
|
|
|
if (query_auto_close > 0 && queryclose_tag == -1)
|
|
|
|
queryclose_tag = g_timeout_add(5000, (GSourceFunc) sig_query_autoclose, NULL);
|
|
|
|
else if (query_auto_close <= 0 && queryclose_tag != -1) {
|
|
|
|
g_source_remove(queryclose_tag);
|
|
|
|
queryclose_tag = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
void fe_query_init(void)
|
|
|
|
{
|
2000-06-14 15:05:51 -04:00
|
|
|
settings_add_bool("lookandfeel", "autocreate_query", TRUE);
|
|
|
|
settings_add_bool("lookandfeel", "autocreate_own_query", TRUE);
|
|
|
|
settings_add_int("lookandfeel", "autoclose_query", 0);
|
2000-05-04 06:32:42 -04:00
|
|
|
|
|
|
|
queryclose_tag = -1;
|
|
|
|
read_settings();
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
signal_add("query created", (SIGNAL_FUNC) signal_query_created);
|
|
|
|
signal_add("query destroyed", (SIGNAL_FUNC) signal_query_destroyed);
|
|
|
|
signal_add("window item remove", (SIGNAL_FUNC) signal_window_item_removed);
|
|
|
|
signal_add("server connected", (SIGNAL_FUNC) sig_server_connected);
|
2000-05-04 06:32:42 -04:00
|
|
|
signal_add("window changed", (SIGNAL_FUNC) sig_window_changed);
|
|
|
|
signal_add("setup changed", (SIGNAL_FUNC) read_settings);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-08-11 22:16:52 -04:00
|
|
|
command_bind_first("query", NULL, (SIGNAL_FUNC) cmd_wquery_pre);
|
|
|
|
command_bind_last("query", NULL, (SIGNAL_FUNC) cmd_wquery_post);
|
2000-04-26 04:03:38 -04:00
|
|
|
command_bind("window server", NULL, (SIGNAL_FUNC) cmd_window_server);
|
2000-08-11 22:16:52 -04:00
|
|
|
|
|
|
|
command_set_options("query", "window");
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void fe_query_deinit(void)
|
|
|
|
{
|
2000-05-04 06:32:42 -04:00
|
|
|
if (queryclose_tag != -1) g_source_remove(queryclose_tag);
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
signal_remove("query created", (SIGNAL_FUNC) signal_query_created);
|
|
|
|
signal_remove("query destroyed", (SIGNAL_FUNC) signal_query_destroyed);
|
|
|
|
signal_remove("window item remove", (SIGNAL_FUNC) signal_window_item_removed);
|
|
|
|
signal_remove("server connected", (SIGNAL_FUNC) sig_server_connected);
|
2000-05-04 06:32:42 -04:00
|
|
|
signal_remove("window changed", (SIGNAL_FUNC) sig_window_changed);
|
|
|
|
signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-08-11 22:16:52 -04:00
|
|
|
command_unbind("query", (SIGNAL_FUNC) cmd_wquery_pre);
|
|
|
|
command_unbind("query", (SIGNAL_FUNC) cmd_wquery_post);
|
2000-04-26 04:03:38 -04:00
|
|
|
command_unbind("window server", (SIGNAL_FUNC) cmd_window_server);
|
|
|
|
}
|