summaryrefslogtreecommitdiff
path: root/src/irc/notifylist/notifylist.c
blob: 573f7a7f82949be971f137206b7239b7d3d066f6 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
 notifylist.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.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "module.h"
#include "modules.h"
#include "signals.h"
#include "settings.h"

#include "irc.h"
#include "irc-channels.h"
#include "servers-redirect.h"
#include "masks.h"
#include "nicklist.h"

#include "notifylist.h"
#include "notify-setup.h"

GSList *notifies;

NOTIFYLIST_REC *notifylist_add(const char *mask, const char *ircnets,
			       int away_check)
{
	NOTIFYLIST_REC *rec;

	g_return_val_if_fail(mask != NULL, NULL);

	rec = g_new0(NOTIFYLIST_REC, 1);
        rec->mask = g_strdup(mask);
	rec->ircnets = ircnets == NULL || *ircnets == '\0' ? NULL :
		g_strsplit(ircnets, " ", -1);
	rec->away_check = away_check;

        notifylist_add_config(rec);

        notifies = g_slist_append(notifies, rec);
	signal_emit("notifylist new", 1, rec);
	return rec;
}

static void notify_destroy(NOTIFYLIST_REC *rec)
{
	if (rec->ircnets != NULL) g_strfreev(rec->ircnets);
	g_free(rec->mask);
        g_free(rec);
}

void notifylist_destroy_all(void)
{
	g_slist_foreach(notifies, (GFunc) notify_destroy, NULL);
	g_slist_free(notifies);

	notifies = NULL;
}

void notifylist_remove(const char *mask)
{
	NOTIFYLIST_REC *rec;

	g_return_if_fail(mask != NULL);

	rec = notifylist_find(mask, "*");
	if (rec == NULL) return;

	notifylist_remove_config(rec);
	notifies = g_slist_remove(notifies, rec);
	signal_emit("notifylist remove", 1, rec);

        notify_destroy(rec);
}

int notifylist_ircnets_match(NOTIFYLIST_REC *rec, const char *ircnet)
{
	char **tmp;

	if (rec->ircnets == NULL) return TRUE;
	if (ircnet == NULL) return FALSE;
	if (g_strcmp0(ircnet, "*") == 0) return TRUE;

	for (tmp = rec->ircnets; *tmp != NULL; tmp++) {
		if (g_ascii_strcasecmp(*tmp, ircnet) == 0)
			return TRUE;
	}

	return FALSE;
}

NOTIFYLIST_REC *notifylist_find(const char *mask, const char *ircnet)
{
	NOTIFYLIST_REC *best;
	GSList *tmp;
	int len;

	best = NULL;
	len = strlen(mask);
	for (tmp = notifies; tmp != NULL; tmp = tmp->next) {
		NOTIFYLIST_REC *rec = tmp->data;

		/* check mask */
		if (g_ascii_strncasecmp(rec->mask, mask, len) != 0 ||
		    (rec->mask[len] != '\0' && rec->mask[len] != '!')) continue;

		/* check ircnet */
		if (rec->ircnets == NULL) {
			best = rec;
			continue;
		}

		if (notifylist_ircnets_match(rec, ircnet))
			return rec;
	}

	return best;
}

int notifylist_ison_server(IRC_SERVER_REC *server, const char *nick)
{
	NOTIFY_NICK_REC *rec;

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

	rec = notify_nick_find(server, nick);
	return rec != NULL && rec->host_ok && rec->away_ok;
}

static IRC_SERVER_REC *notifylist_ison_serverlist(const char *nick, const char *taglist)
{
	IRC_SERVER_REC *server;
	char **list, **tmp;

	g_return_val_if_fail(nick != NULL, NULL);
	g_return_val_if_fail(taglist != NULL, NULL);

	list = g_strsplit(taglist, " ", -1);

	server = NULL;
	for (tmp = list; *tmp != NULL; tmp++) {
		server = (IRC_SERVER_REC *) server_find_chatnet(*tmp);

		if (IS_IRC_SERVER(server) &&
		    notifylist_ison_server(server, nick))
			break;
	}
	g_strfreev(list);

	return tmp == NULL ? NULL : server;
}

IRC_SERVER_REC *notifylist_ison(const char *nick, const char *serverlist)
{
	GSList *tmp;

	g_return_val_if_fail(nick != NULL, FALSE);
	g_return_val_if_fail(serverlist != NULL, FALSE);

	if (*serverlist != '\0')
		return notifylist_ison_serverlist(nick, serverlist);

	/* any server.. */
	for (tmp = servers; tmp != NULL; tmp = tmp->next) {
		IRC_SERVER_REC *server = tmp->data;

		if (IS_IRC_SERVER(server) &&
		    notifylist_ison_server(server, nick))
			return tmp->data;
	}

	return NULL;
}

static void notifylist_init_server(IRC_SERVER_REC *server)
{
	MODULE_SERVER_REC *rec;

	g_return_if_fail(server != NULL);

	if (!IS_IRC_SERVER(server))
		return;

	rec = g_new0(MODULE_SERVER_REC,1 );
	MODULE_DATA_SET(server, rec);
}

static void notifylist_deinit_server(IRC_SERVER_REC *server)
{
	MODULE_SERVER_REC *mserver;
	NOTIFY_NICK_REC *rec;

	g_return_if_fail(server != NULL);

	if (!IS_IRC_SERVER(server))
		return;

	mserver = MODULE_DATA(server);
	while (mserver->notify_users != NULL) {
		rec = mserver->notify_users->data;

		mserver->notify_users = g_slist_remove(mserver->notify_users, rec);
		notify_nick_destroy(rec);
	}
	g_free(mserver);
	MODULE_DATA_UNSET(server);
}

void notifylist_left(IRC_SERVER_REC *server, NOTIFY_NICK_REC *rec)
{
	MODULE_SERVER_REC *mserver;

	mserver = MODULE_DATA(server);
	mserver->notify_users = g_slist_remove(mserver->notify_users, rec);

	if (rec->host_ok && rec->away_ok) {
		signal_emit("notifylist left", 6,
			    server, rec->nick,
			    rec->user, rec->host,
			    rec->realname, rec->awaymsg);
	}

	notify_nick_destroy(rec);
}

static void event_quit(IRC_SERVER_REC *server, const char *data,
		       const char *nick)
{
	NOTIFY_NICK_REC *rec;

	if (*data == ':') data++; /* quit message */

	rec = notify_nick_find(server, nick);
	if (rec != NULL) notifylist_left(server, rec);
}

static void notifylist_check_join(IRC_SERVER_REC *server, const char *nick,
				  const char *userhost, const char *realname, int away)
{
	NOTIFYLIST_REC *notify;
	NOTIFY_NICK_REC *rec;
	char *user, *host;

	if (nick == NULL)
		return;

	notify = notifylist_find(nick, server->connrec->chatnet);
	if (notify == NULL) return;

	rec = notify_nick_find(server, nick);
	if (rec != NULL && rec->join_announced) return;
	if (rec == NULL) rec = notify_nick_create(server, nick);

	user = g_strdup(userhost == NULL ? "" : userhost);
	host = strchr(user, '@');
	if (host != NULL) *host++ = '\0'; else host = "";

	if (!mask_match(SERVER(server), notify->mask, nick, user, host)) {
		g_free(user);
		return;
	}

	if (notify->away_check && away == -1) {
		/* we need to know if the nick is away */
		g_free(user);
		return;
	}

	g_free_not_null(rec->user);
	g_free_not_null(rec->host);
	g_free_not_null(rec->realname);
	rec->user = g_strdup(user);
	rec->host = g_strdup(host);
	rec->realname = realname == NULL || *realname == '\0' ? NULL : g_strdup(realname);

	if (away != -1) rec->away = away;
	rec->host_ok = TRUE;
	rec->join_announced = TRUE;
	rec->away_ok = !notify->away_check || !rec->away;

	signal_emit("notifylist joined", 6,
		    server, rec->nick, rec->user, rec->host, realname, NULL);
	g_free(user);
}

static void event_privmsg(IRC_SERVER_REC *server, const char *data,
			  const char *nick, const char *address)
{
	if (nick != NULL) {
		notifylist_check_join(server, nick, address, "", -1);
	}
}

static void event_join(IRC_SERVER_REC *server, const char *data,
		       const char *nick, const char *address)
{
	notifylist_check_join(server, nick, address, "", -1);
}

static void sig_channel_wholist(IRC_CHANNEL_REC *channel)
{
	GSList *nicks, *tmp;

	nicks = nicklist_getnicks(CHANNEL(channel));
	for (tmp = nicks; tmp != NULL; tmp = tmp->next) {
		NICK_REC *rec = tmp->data;

		notifylist_check_join(channel->server, rec->nick, rec->host, rec->realname, rec->gone);
	}
        g_slist_free(nicks);
}

void irc_notifylist_init(void)
{
	notifylist_read_config();

	notifylist_commands_init();
	notifylist_ison_init();
	notifylist_whois_init();
	signal_add("server connected", (SIGNAL_FUNC) notifylist_init_server);
	signal_add("server disconnected", (SIGNAL_FUNC) notifylist_deinit_server);
	signal_add("event quit", (SIGNAL_FUNC) event_quit);
	signal_add("event privmsg", (SIGNAL_FUNC) event_privmsg);
	signal_add("event join", (SIGNAL_FUNC) event_join);
	signal_add("channel wholist", (SIGNAL_FUNC) sig_channel_wholist);
	signal_add("setup reread", (SIGNAL_FUNC) notifylist_read_config);

	settings_check();
	module_register("notifylist", "irc");
}

void irc_notifylist_deinit(void)
{
	notifylist_commands_deinit();
	notifylist_ison_deinit();
	notifylist_whois_deinit();

	signal_remove("server connected", (SIGNAL_FUNC) notifylist_init_server);
	signal_remove("server disconnected", (SIGNAL_FUNC) notifylist_deinit_server);
	signal_remove("event quit", (SIGNAL_FUNC) event_quit);
	signal_remove("event privmsg", (SIGNAL_FUNC) event_privmsg);
	signal_remove("event join", (SIGNAL_FUNC) event_join);
	signal_remove("channel wholist", (SIGNAL_FUNC) sig_channel_wholist);
	signal_remove("setup reread", (SIGNAL_FUNC) notifylist_read_config);

	notifylist_destroy_all();
}