summaryrefslogtreecommitdiff
path: root/src/irc/core/irc-queries.c
blob: 9314aaab75446c586dd508206768a7b29646086c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 irc-queries.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 "signals.h"
#include "misc.h"

#include "irc.h"
#include "irc-queries.h"

QUERY_REC *irc_query_create(IRC_SERVER_REC *server,
			    const char *nick, int automatic)
{
	QUERY_REC *rec;

	g_return_val_if_fail(server == NULL || IS_IRC_SERVER(server), NULL);
	g_return_val_if_fail(nick != NULL, NULL);

	rec = g_new0(QUERY_REC, 1);
	rec->chat_type = IRC_PROTOCOL;
	rec->name = g_strdup(nick);
	rec->server = (SERVER_REC *) server;
	query_init(rec, automatic);
	return rec;
}

static void sig_query_create(QUERY_REC **query,
			     void *chat_type, IRC_SERVER_REC *server,
			     const char *nick, void *automatic)
{
	if (chat_protocol_lookup("IRC") != GPOINTER_TO_INT(chat_type))
		return;

	g_return_if_fail(server == NULL || IS_IRC_SERVER(server));
	g_return_if_fail(query != NULL);
	g_return_if_fail(nick != NULL);

	*query = irc_query_create(server, nick, GPOINTER_TO_INT(automatic));
	signal_stop();
}

static void event_privmsg(IRC_SERVER_REC *server, const char *data, const char *nick, const char *addr)
{
	char *params, *target, *msg;
	QUERY_REC *query;

	g_return_if_fail(data != NULL);

	params = event_get_params(data, 2 | PARAM_FLAG_GETREST, &target, &msg);

	if (addr != NULL && !ischannel(*target)) {
		/* save nick's address to query */
		query = irc_query_find(server, nick);
		if (query != NULL && (query->address == NULL || strcmp(query->address, addr) != 0)) {
			g_free_not_null(query->address);
			query->address = g_strdup(addr);

			signal_emit("query address changed", 1, query);
		}
	}

	g_free(params);
}

static void event_nick(IRC_SERVER_REC *server, const char *data, const char *orignick)
{
	char *params, *nick;
	GSList *tmp;

	params = event_get_params(data, 1, &nick);

	for (tmp = server->queries; tmp != NULL; tmp = tmp->next) {
		QUERY_REC *rec = tmp->data;

		if (g_strcasecmp(rec->name, orignick) == 0) {
			g_free(rec->name);
			rec->name = g_strdup(nick);
			signal_emit("query nick changed", 1, rec);
		}
	}

	g_free(params);
}

void irc_queries_init(void)
{
	signal_add("query create", (SIGNAL_FUNC) sig_query_create);
	signal_add_last("event privmsg", (SIGNAL_FUNC) event_privmsg);
	signal_add("event nick", (SIGNAL_FUNC) event_nick);
}

void irc_queries_deinit(void)
{
	signal_remove("query create", (SIGNAL_FUNC) sig_query_create);
	signal_remove("event privmsg", (SIGNAL_FUNC) event_privmsg);
	signal_remove("event nick", (SIGNAL_FUNC) event_nick);
}