File: ctcp.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 (225 lines) | stat: -rw-r--r-- 4,898 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
/* 
	ctrlproxy: A modular IRC proxy
	(c) 2002-2003 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"
#ifndef _WIN32
#include <sys/utsname.h>
#endif

/**
 * Represents a CTCP request
 */
struct ctcp_handle 
{
	struct irc_network *network;
	char *nick;
};

static char *toarg(va_list ap)
{
	char *arg;
	GList *list = NULL, *gl;
	int len = 4;
	char *ret, *pos;

	while((arg = va_arg(ap, char *))) {
		list = g_list_append(list, arg);
		len+=strlen(arg) + 2;
	}

	g_assert(list);

	ret = g_new0(char, len);
	ret[0] = '\001';
	pos = ret+1;
	strcpy(pos, list->data); pos += strlen(list->data);
	for (gl = list->next; gl; gl = gl->next) {
		*pos = ' '; 
		strcpy(pos+1, gl->data); 
		pos+=strlen(gl->data)+1;
	}

	*pos = '\001';

	return ret;
}

void ctcp_reply(struct ctcp_handle *h, ...) 
{
	va_list ap;
	char *msg;
	va_start(ap, h);
	msg = toarg(ap);
	va_end(ap);

	network_send_args(h->network, "NOTICE", h->nick, msg, NULL);
	g_free(msg);
}

void ctcp_send(struct irc_network *n, const char *nick, ...)
{
	va_list ap;
	char *msg;
	va_start(ap, nick);
	msg = toarg(ap);
	va_end(ap);

	network_send_args(n, "PRIVMSG", nick, msg, NULL);
	g_free(msg);
}

static void handle_time(struct ctcp_handle *h, char **args)
{
	time_t ti = time(NULL);
	char *t, *msg = g_strdup(ctime(&ti));
	t = strchr(msg, '\n');
	if (t)*t = '\0';
	ctcp_reply(h, "TIME", msg, NULL);
	g_free(msg);
}

static void handle_finger(struct ctcp_handle *h, char **args)
{
	ctcp_reply(h, "FINGER", h->network->state->me.fullname, NULL);
}

static void handle_source(struct ctcp_handle *h, char **args)
{
	ctcp_reply(h, "SOURCE", "www.ctrlproxy.org:pub/ctrlproxy:ctrlproxy-latest.tar.gz", NULL);
}

static void handle_version(struct ctcp_handle *h, char **args)
{
	char *msg;
#ifndef _WIN32
	struct utsname u;
	uname(&u);
	msg = g_strdup_printf("ctrlproxy:%s:%s %s", ctrlproxy_version(), u.sysname, u.release);
#else
	msg = g_strdup_printf("ctrlproxy:%s:Windows %d.%d", ctrlproxy_version(), _winmajor, _winminor);
#endif
	ctcp_reply(h, "VERSION", msg, NULL);
	g_free(msg);
}

static void handle_ping(struct ctcp_handle *h, char **args)
{
	ctcp_reply(h, "PING", args[1], NULL);
}

static void handle_clientinfo(struct ctcp_handle *h, char **args);

static const struct ctcp_handler builtins[] = {
	{ "VERSION", handle_version },
	{ "CLIENTINFO", handle_clientinfo },
	{ "SOURCE", handle_source },
	{ "PING", handle_ping },
	{ "FINGER", handle_finger },
	{ "TIME", handle_time },
	{ "ACTION", NULL },
	{ NULL, NULL }
};

static GList *cmds = NULL;

static void handle_clientinfo(struct ctcp_handle *h, char **args)
{
	char **supported = g_new0(char *, sizeof(builtins)/sizeof(builtins[0])+1+g_list_length(cmds));
	char *tmp;
	int i;
	GList *gl;

	for (i = 0; builtins[i].name; i++) 
		supported[i] = builtins[i].name;

	for (gl = cmds; gl; gl = gl->next) {
		supported[i] = ((struct ctcp_handler *)gl->data)->name;
		i++;
	}

	supported[i] = NULL;

	tmp = g_strjoinv(" ", supported);
	ctcp_reply(h, "CLIENTINFO", tmp, NULL);
	g_free(tmp);
	g_free(supported);
}

void ctcp_register_handler(const struct ctcp_handler *h)
{
	cmds = g_list_append(cmds, g_memdup(h, sizeof(*h)));
}

gboolean ctcp_process_request (struct irc_network *n, const struct irc_line *l) 
{
	GList *gl;
	int i;
	char *t;
	char *data;
	char **args;
	gboolean ret = FALSE;
	struct ctcp_handle h;

	h.network = n;
	h.nick = line_get_nick(l);

	data = g_strdup(l->args[2]+1);
	t = strchr(data, '\001');
	if (!t) { 
		g_free(data); 
		network_log(LOG_WARNING, n, "Malformed CTCP request from %s!", h.nick);
		g_free(h.nick);
		return FALSE;
	}
	*t = '\0';

	args = g_strsplit(data, " ", 0);
	for (gl = cmds; gl; gl = gl->next) {
		struct ctcp_handler *hl = gl->data;

		if (!g_strcasecmp(hl->name, args[0])) {
			if (hl->fn != NULL)
				hl->fn(&h, args);
			ret = TRUE;
			break;
		}
	}

	for (i = 0; !ret && builtins[i].name; i++) {
		if (!g_strcasecmp(builtins[i].name, args[0])) {
			if (builtins[i].fn != NULL) 
				builtins[i].fn(&h, args);
			ret = TRUE;
			break;
		}
	}

	if (!ret) {
		ctcp_reply(&h, "ERRMSG", NULL);
		network_log(LOG_WARNING, n, "Received unknown CTCP request '%s'!", data);
		ret = FALSE;
	}


	g_strfreev(args);
	g_free(data);
	g_free(h.nick);

	return ret;
}