File: cache.c

package info (click to toggle)
ctrlproxy 3.0.7-1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 1,668 kB
  • ctags: 2,045
  • sloc: ansic: 15,358; sh: 2,805; xml: 1,078; python: 346; makefile: 302; perl: 18
file content (216 lines) | stat: -rw-r--r-- 5,062 bytes parent folder | download
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
/*
	ctrlproxy: A modular IRC proxy
	(c) 2005 Jelmer Vernooij <jelmer@nl.linux.org>

	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 3 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "internals.h"
#include "irc.h"

static gboolean client_try_cache_mode(struct irc_client *c, struct irc_network_state *net, struct irc_line *l)
{
	int i;
	char m;
	struct irc_channel_state *ch;

	g_assert(l);
	g_assert(c);

	if (l->argc < 2) return FALSE;
	
	/* Only optimize easy queries... */
	if (strchr(l->args[1], ',')) return FALSE;
		
	/* Queries in the form of MODE #channel mode */
	if (l->argc == 3) {
		g_assert(net);

		ch = find_channel(net, l->args[1]);
		if (!ch) return FALSE;

		for (i = 0; (m = l->args[2][i]); i++) {
			switch (m) {
			case 'b': client_send_banlist(c, ch); break;
			default: return FALSE;
			}
		}

		return TRUE;
	/* Queries in the form MODE #channel */
	} else if (l->argc == 2) {
		ch = find_channel(net, l->args[1]);
		if (!ch) return FALSE;

		if (!ch->mode_received) return FALSE;

		client_send_channel_mode(c, ch);

		return TRUE;
	}

	return FALSE;
}

static gboolean client_try_cache_topic(struct irc_client *c, struct irc_network_state *net, struct irc_line *l)
{
	struct irc_channel_state *ch;

	if (l->argc < 2) return FALSE;
	
	/* Only optimize easy queries... */
	if (strchr(l->args[1], ',')) return FALSE;

	/* No set requests */
	if (l->args[2]) return FALSE;
	
	ch = find_channel(net, l->args[1]);
	if (!ch) return FALSE;

	client_send_topic(c, ch, TRUE);

	return TRUE;
}

static gboolean client_try_cache_userhost(struct irc_client *c, struct irc_network_state *net, struct irc_line *l)
{
	return FALSE;
}

static gboolean client_try_cache_who(struct irc_client *c, struct irc_network_state *net, struct irc_line *l)
{
	struct irc_channel_state *ch;
	int max_who_age;

	time_t now;
	GList *gl;

	g_assert(l);
	g_assert(c);

	if (l->argc < 2) return FALSE;
	
	/* Only optimize easy queries... */
	if (strchr(l->args[1], ',')) return FALSE;
	if (strchr(l->args[1], '*')) return FALSE;

	/* Don't cache complex requests.. for now */
	if (l->argc > 2) return FALSE;

	max_who_age = c->network->global->config->max_who_age;

	/* Never cache when max_who_age is set to 0 */
	if (max_who_age == 0)
		return FALSE;

	if (l->argc != 2) 
		return FALSE;

	g_assert(net);

	ch = find_channel(net, l->args[1]);
	if (ch == NULL)
		return FALSE;

	now = time(NULL);

	/* Check that the cache data hasn't expired yet */
	for (gl = ch->nicks; gl; gl = gl->next) {
		struct channel_nick *cn = gl->data;
		
		if (cn->last_update == 0)
			return FALSE;

		if ((cn->last_update + max_who_age) <= now)
			return FALSE;
	}

	for (gl = ch->nicks; gl; gl = gl->next) {
		struct channel_nick *cn = gl->data;
		struct network_nick *nn = cn->global_nick;
		char *info = g_strdup_printf("%d %s", nn->hops, nn->fullname);
		
		client_send_response(c, RPL_WHOREPLY, l->args[1], nn->username, 
							 nn->hostname, nn->server, nn->nick, 
							 cn->last_flags, info, NULL);

		g_free(info);
	}

	client_send_response(c, RPL_ENDOFWHO, l->args[1], "End of /WHO list.", NULL);

	return TRUE;
}

static gboolean client_try_cache_names(struct irc_client *c, struct irc_network_state *net, struct irc_line *l)
{
	struct irc_channel_state *ch;

	g_assert(l);
	g_assert(c);

	if (l->argc < 2) return FALSE;
	
	/* Only optimize easy queries... */
	if (strchr(l->args[1], ',')) return FALSE;

	if (l->argc != 2) return FALSE;

	g_assert(net);

	ch = find_channel(net, l->args[1]);
	if (!ch) return FALSE;

	client_send_nameslist(c, ch);

	return TRUE;
}

/**
 * Table of commands of which the result can be cached.
 */
struct cache_command {
	const char *name;
	/* Should return FALSE if command couldn't be cached */
	gboolean (*try_cache) (struct irc_client *c, struct irc_network_state *net, struct irc_line *l);
} cache_commands[] = {
	{ "MODE", client_try_cache_mode },
	{ "NAMES", client_try_cache_names },
	{ "TOPIC", client_try_cache_topic },
	{ "WHO", client_try_cache_who },
	{ "USERHOST", client_try_cache_userhost },
	{ NULL, NULL }
};

/* Try to answer a client query from cache */
gboolean client_try_cache(struct irc_client *c, struct irc_network_state *net, struct irc_line *l)
{
	g_assert(l);
	int i;

	if (l->argc == 0) 
		return TRUE;

	for (i = 0; cache_commands[i].name; i++) {
		if (!g_strcasecmp(l->args[0], cache_commands[i].name))
			return cache_commands[i].try_cache(c, net, l);
	}
	
	return FALSE;
}