File: log_subst.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 (311 lines) | stat: -rw-r--r-- 8,363 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
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
/* 
	ctrlproxy: A modular IRC proxy
	(c) 2002-2007 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.
*/

#include "internals.h"

struct subst_context {
	const char *identifier;
	struct irc_network *network;
};

/**
 * Translation table
 */
struct log_mapping {
	char *command;
	char subst;
	unsigned int index;
	/* If index is -1 */
	char *(*callback) (struct subst_context *subst_ctx, 
					   const struct irc_line *line,
					   gboolean case_sensitive);
};

static char *get_hours(struct subst_context *subst_ctx, const struct irc_line *line, gboolean case_sensitive) 
{ 
	time_t ti = time(NULL);
	struct tm *t = localtime(&ti);
	return g_strdup_printf("%02d", t->tm_hour);
}

static char *get_minutes(struct subst_context *subst_ctx, const struct irc_line *line, gboolean case_sensitive) 
{ 
	time_t ti = time(NULL);
	struct tm *t = localtime(&ti);
	return g_strdup_printf("%02d", t->tm_min);
}

static char *get_seconds(struct subst_context *subst_ctx, const struct irc_line *line, gboolean case_sensitive) 
{ 
	time_t ti = time(NULL);
	struct tm *t = localtime(&ti);
	return g_strdup_printf("%02d", t->tm_sec);
}

static char *get_seconds_since_1970(struct subst_context *subst_ctx, const struct irc_line *line, gboolean case_sensitive) 
{
	time_t ti = time(NULL);
	return g_strdup_printf("%ld", ti);
}

static char *get_day(struct subst_context *subst_ctx, const struct irc_line *line, gboolean case_sensitive) 
{ 
	time_t ti = time(NULL);
	struct tm *t = localtime(&ti);
	return g_strdup_printf("%02d", t->tm_mday);
}

static char *get_month(struct subst_context *subst_ctx, const struct irc_line *line, gboolean case_sensitive) 
{ 
	time_t ti = time(NULL);
	struct tm *t = localtime(&ti);
	return g_strdup_printf("%02d", t->tm_mon + 1);
}

static char *get_year(struct subst_context *subst_ctx, const struct irc_line *line, gboolean case_sensitive) 
{ 
	time_t ti = time(NULL);
	struct tm *t = localtime(&ti);
	return g_strdup_printf("%04d", t->tm_year + 1900);
}

static char *get_user(struct subst_context *subst_ctx, const struct irc_line *line, gboolean case_sensitive) 
{
	char *user = NULL;

	if (line->origin != NULL) {
		user = strchr(line->origin, '!');
		if (user != NULL)
			user++; 
	}

	if (case_sensitive) 
		return g_ascii_strdown(user, -1);
	else 
		return g_strdup(user);
}

static char *get_monthname(struct subst_context *subst_ctx, const struct irc_line *line, gboolean case_sensitive) 
{ 
	char stime[512];
	time_t ti = time(NULL);
	strftime(stime, sizeof(stime), "%b", localtime(&ti));
	return g_strdup_printf("%s", stime);
}

static char *get_nick(struct subst_context *subst_ctx, const struct irc_line *line, gboolean case_sensitive) 
{
	if (line->origin != NULL) {
		char *n = line_get_nick(line);
		if (case_sensitive) {
			char *r = g_ascii_strdown(n, -1);
			g_free(n);
			return r;
		}
		else return n;
	}
	
	return g_strdup("");
}

static char *get_network(struct subst_context *subst_ctx, const struct irc_line *line, gboolean case_sensitive) 
{
	return g_strdup(subst_ctx->network->info->name); 
}

static char *get_server(struct subst_context *subst_ctx, const struct irc_line *line, gboolean case_sensitive)
{
	if (subst_ctx->network->connection.data.tcp.current_server)
		return g_strdup(subst_ctx->network->connection.data.tcp.current_server->host);
	return g_strdup("");
}

static char *get_percent(struct subst_context *subst_ctx, const struct irc_line *line, gboolean case_sensitive) 
{ 
	return g_strdup("%"); 
}

static char *get_identifier(struct subst_context *subst_ctx, const struct irc_line *line, gboolean case_sensitive) 
{ 
	if (case_sensitive) return g_ascii_strdown(subst_ctx->identifier, -1); 
	else return g_strdup(subst_ctx->identifier); 
}

static char *get_modechanges(struct subst_context *subst_ctx, const struct irc_line *line, gboolean case_sensitive) 
{
	char buf[512] = "";
	int i;

	for (i = 3 ; i+1 < line->argc && line->args[i+1] != NULL; i++) {
		if (i > 3) strncat(buf, " ", sizeof(buf)-1);
		strncat(buf, line->args[i], sizeof(buf)-1);
	}

	return g_strdup(buf);
}

static struct log_mapping mappings[] = {
	{NULL, '@', -1, get_identifier },
	{NULL, 'h', -1, get_hours },
	{NULL, 'M', -1, get_minutes },
	{NULL, 's', -1, get_seconds },
	{NULL, 'd', -1, get_day },
	{NULL, 'B', -1, get_month },
	{NULL, 'Y', -1, get_year },
	{NULL, 'e', -1, get_seconds_since_1970 },
	{NULL, 'b', -1, get_monthname },
	{NULL, 'n', -1, get_nick },
	{NULL, 'u', -1, get_user },
	{NULL, 'N', -1, get_network },
	{NULL, 'S', -1, get_server },
	{NULL, '%', -1, get_percent },
	{"JOIN", 'c', 1, NULL },
	{"PART", 'c', 1, NULL },
	{"PART", 'm', 2, NULL },
	{"KICK", 'c', 1, NULL },
	{"KICK", 't', 2, NULL },
	{"KICK", 'r', 3, NULL },
	{"QUIT", 'm', 1, NULL },
	{"NOTICE", 't', 1, NULL },
	{"NOTICE", 'm', 2, NULL },
	{"PRIVMSG", 't', 1, NULL },
	{"PRIVMSG", 'm', 2, NULL },
	{"MSG", 't', 1, NULL },
	{"MSG", 'm', 2, NULL },
	{"TOPIC", 'c', 1, NULL },
	{"TOPIC", 't', 2, NULL },
	{"MODE", 't', 1, NULL },
	{"MODE", 'p', 2, NULL },
	{"MODE", 'c', -1, get_modechanges },
	{"NICK", 'r', 1, NULL },
	{NULL, '0', 0, NULL },
	{NULL, '1', 1, NULL },
	{NULL, '2', 2, NULL },
	{NULL, '3', 3, NULL },
	{NULL, '4', 4, NULL },
	{NULL, '5', 5, NULL },
	{NULL, '6', 6, NULL },
	{NULL, '7', 7, NULL },
	{NULL, '8', 8, NULL },
	{NULL, '9', 9, NULL },
	{ NULL }
};

/**
 * Given a special character, expand it.
 *
 * @param network IRC Network
 * @param l IRC Line
 * @param c Special character
 * @param case_sensitive Whether or not to be case sensitive
 * @return Expanded string or an empty string if there was no expansion possible
 */
static char *find_mapping(struct subst_context *subst_ctx,
						  const struct irc_line *l, 
						  char c, gboolean case_sensitive)
{
	int i;
	for (i = 0; mappings[i].subst; i++) {
		if (mappings[i].command && strcmp(mappings[i].command, l->args[0])) 
			continue;

		if (mappings[i].subst != c) 
			continue;

		if (mappings[i].index == -1) 
			return mappings[i].callback(subst_ctx, l, case_sensitive);

		if (mappings[i].index < l->argc) {
			if (case_sensitive) 
				return g_ascii_strdown(l->args[mappings[i].index], -1);
			else 
				return g_strdup(l->args[mappings[i].index]);
		}
	}
	return g_strdup("");
}

void convertslashes(char *a)
{
	while (*a) {
		if (*a == '/') 
			*a = '_';
		a++;
	}
}

/**
 * Substitute the special characters in a string.
 *
 * @param network IRC Network
 * @param _new Pointer to store expanded line
 * @param fmt String to expand
 * @param l IRC Line
 * @param case_sensitive Whether or not to be case sensitive
 * @param noslash Whether or not to avoid adding slashes from expansions
 */
char *custom_subst(struct irc_network *network, 
						 const char *fmt, const struct irc_line *l, 
						 const char *_identifier, 
						 gboolean case_sensitive, gboolean noslash)
{
	char **subst;
	char *new;
	size_t len, exp_len, curpos = 0;
	unsigned int i;
	struct subst_context subst_ctx;

	subst_ctx.identifier = _identifier;
	subst_ctx.network = network;

	/* First, find all the mappings */
	exp_len = len = strlen(fmt);
	subst = g_new0(char *, len);
	for (i = 0; i < len; i++) {
		if (fmt[i] == '%') {
			subst[i] = find_mapping(&subst_ctx, l, fmt[i+1], 
												case_sensitive);	
			if (subst[i] == NULL) 
				subst[i] = g_strdup("");
			if (noslash) convertslashes(subst[i]);
			exp_len += strlen(subst[i]);
		}
	}

	exp_len++; /* newline! */

	new = g_new(char, exp_len);
	for (i = 0; i < len; i++) {
		if (fmt[i] == '%') {
			new[curpos] = '\0';
			strncat(new, subst[i], exp_len);
			curpos+=strlen(subst[i]);
			i++;
		} else {
			new[curpos] = fmt[i];
			curpos++;
		}
	}
	new[curpos] = '\0';

	for (i = 0; i < len; i++)
		g_free(subst[i]); 
	g_free(subst);
	return new;
}