summaryrefslogtreecommitdiff
path: root/src/irc/core/mode-lists.c
blob: 8d0a9eabe2ef6f687799946a1c0dfe66e1cd3163 (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
/*
 mode-lists.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 "misc.h"
#include "signals.h"

#include "irc-servers.h"
#include "irc-channels.h"
#include "mode-lists.h"

static void ban_free(GSList **list, BAN_REC *rec)
{
	g_return_if_fail(list != NULL);
	g_return_if_fail(rec != NULL);

	g_free(rec->ban);
	g_free_not_null(rec->setby);
	g_free(rec);

	*list = g_slist_remove(*list, rec);
}

void banlist_free(GSList *banlist)
{
	while (banlist != NULL)
		ban_free(&banlist, banlist->data);
}

BAN_REC *banlist_add(IRC_CHANNEL_REC *channel, const char *ban,
		     const char *nick, time_t time)
{
	BAN_REC *rec;

	g_return_val_if_fail(channel != NULL, NULL);
	g_return_val_if_fail(ban != NULL, NULL);

	rec = g_new(BAN_REC, 1);
	rec->ban = g_strdup(ban);
	rec->setby = nick == NULL || *nick == '\0' ? NULL :
		g_strdup(nick);
	rec->time = time;

	channel->banlist = g_slist_append(channel->banlist, rec);

	signal_emit("ban new", 2, channel, rec);
	return rec;
}

static BAN_REC *banlist_find(GSList *list, const char *ban)
{
	GSList *tmp;

	g_return_val_if_fail(ban != NULL, NULL);

	for (tmp = list; tmp != NULL; tmp = tmp->next) {
		BAN_REC *rec = tmp->data;

		if (g_strcasecmp(rec->ban, ban) == 0)
			return rec;
	}

	return NULL;
}

void banlist_remove(IRC_CHANNEL_REC *channel, const char *ban)
{
	BAN_REC *rec;

	g_return_if_fail(channel != NULL);
	g_return_if_fail(ban != NULL);

	rec = banlist_find(channel->banlist, ban);
	if (rec != NULL) {
		signal_emit("ban remove", 2, channel, rec);
		ban_free(&channel->banlist, rec);
	}
}

static void channel_destroyed(IRC_CHANNEL_REC *channel)
{
	if (!IS_IRC_CHANNEL(channel))
                return;

	banlist_free(channel->banlist);
}

static void event_banlist(IRC_SERVER_REC *server, const char *data)
{
	IRC_CHANNEL_REC *chanrec;
	char *params, *channel, *ban, *setby, *tims;
	time_t tim;

	g_return_if_fail(data != NULL);

	params = event_get_params(data, 5, NULL, &channel, &ban, &setby, &tims);
	chanrec = irc_channel_find(server, channel);
	if (chanrec != NULL) {
		tim = (time_t) atol(tims);
		banlist_add(chanrec, ban, setby, tim);
	}
	g_free(params);
}

void mode_lists_init(void)
{
	signal_add("channel destroyed", (SIGNAL_FUNC) channel_destroyed);

	signal_add("chanquery ban", (SIGNAL_FUNC) event_banlist);
}

void mode_lists_deinit(void)
{
	signal_remove("channel destroyed", (SIGNAL_FUNC) channel_destroyed);

	signal_remove("chanquery ban", (SIGNAL_FUNC) event_banlist);
}