2
ctrlproxy: A modular IRC proxy
3
(c) 2002-2003 Jelmer Vernooij <jelmer@nl.linux.org>
5
This program is free software; you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation; either version 2 of the License, or
8
(at your option) any later version.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with this program; if not, write to the Free Software
17
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
#define MAX_LINE_ARGS 64
22
#include "internals.h"
26
struct line *irc_parse_line_args( char *origin, ... ) {
30
l = virc_parse_line(origin, ap);
35
struct line *irc_parse_linef( char *fmt, ... ) {
40
vasprintf(&ret, fmt, ap);
41
l = irc_parse_line(ret);
47
struct line *virc_parse_line( char *origin, va_list ap)
51
l = calloc(1, sizeof(struct line));
53
if(origin)l->origin = strdup(origin);
54
else l->origin = NULL;
56
l->args = malloc(sizeof(char *) * (MAX_LINE_ARGS+2));
58
while((arg = va_arg(ap, char *))) {
59
if(l->argc > MAX_LINE_ARGS) {
64
l->args[l->argc] = strdup(arg);
65
l->args = realloc(l->args, (((++l->argc)+2)* sizeof(char *)));
67
l->args[l->argc] = NULL;
72
struct line * irc_parse_line(char *d)
77
char *data = strdup(d);
81
l = calloc(1, sizeof(struct line));
85
p = strchr(data, ' ');
86
if(!p){ free(l); return NULL; }
88
l->origin = strdup(data+1);
89
for(; *(p+1) == ' '; p++);
93
for(i = 0; p[i]; i++) if(p[i] == ' ')estimate++;
95
l->args = malloc(sizeof(char *) * (estimate+2));
100
if(*p == ' ' && dosplit) {
103
l->args[l->argc] = p+1;
104
if(*(p+1) == ':'){ dosplit = 0; l->args[l->argc]++; }
114
l->args[l->argc] = NULL;
115
for(i = 0; l->args[i]; i++) l->args[i] = strdup(l->args[i]);
121
char *irc_line_string_nl(struct line *l)
123
char *raw = irc_line_string(l);
124
raw = realloc(raw, strlen(raw)+10);
129
char *irc_line_string(struct line *l) {
130
size_t len = 0; int i;
134
/* Silently ignore empty messages */
135
if(l->argc == 0) return strdup("");
137
if(l->origin)len+=strlen(l->origin);
138
for(i = 0; l->args[i]; i++) len+=strlen(l->args[i])+2;
139
ret = malloc(len+20);
142
if(l->origin) sprintf(ret, ":%s ", l->origin);
144
for(i = 0; i < l->argc; i++) {
145
/* FIXME: xchat _ALWAYS_expects a : before the last argument... */
146
if(strchr(l->args[i],' ') || l->args[i][0] == ':' /*|| (i > 0 && i == l->argc-1)*/)strcat(ret, ":");
147
strcat(ret, l->args[i]);
148
if(i != l->argc-1)strcat(ret, " ");
154
void free_line(struct line *l) {
156
if(l->origin)free(l->origin);
158
for(i = 0; l->args[i]; i++)free(l->args[i]);
166
char *line_get_nick(struct line *l)
168
static char *nick = NULL;
170
if(!l || !l->origin)return NULL;
172
nick = strdup(l->origin);
173
t = strchr(nick, '!');
179
gboolean irc_send_line(struct transport_context *c, struct line *l) {
185
raw = irc_line_string_nl(l);
186
ret = transport_write(c, raw);
192
gboolean irc_sendf(struct transport_context *c, char *fmt, ...)
202
vasprintf(&r, fmt, ap);
203
l = irc_parse_line(r);
205
ret = irc_send_line(c, l);
212
gboolean irc_send_args(struct transport_context *c, ...)
221
l = virc_parse_line(NULL, ap);
224
ret = irc_send_line(c, l);
226
free_line(l); l = NULL;
231
struct line *linedup(struct line *l) {
233
struct line *ret = calloc(1, sizeof(struct line));
234
memcpy(ret, l, sizeof(struct line));
235
if(l->origin)ret->origin = strdup(l->origin);
236
ret->options = l->options;
237
ret->args = malloc(sizeof(char *) * (ret->argc+MAX_LINE_ARGS));
238
for(i = 0; l->args[i]; i++) {
239
ret->args[i] = strdup(l->args[i]);