2005-10-09 13:47:20 +02:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* This file is part of the bip project
|
2008-01-09 23:45:40 +01:00
|
|
|
* Copyright (C) 2004 2005 Arnaud Cornet and Loïc Gomez
|
2005-10-09 13:47:20 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* See the file "COPYING" for the exact licensing terms.
|
|
|
|
*/
|
|
|
|
|
2005-04-28 10:26:44 +02:00
|
|
|
#include "config.h"
|
|
|
|
#include "line.h"
|
2008-12-20 14:20:50 +01:00
|
|
|
#include "util.h"
|
2005-04-28 10:26:44 +02:00
|
|
|
|
|
|
|
void irc_line_init(struct line *l)
|
|
|
|
{
|
|
|
|
memset(l, 0, sizeof(struct line));
|
2008-12-20 14:20:50 +01:00
|
|
|
array_init(&l->words);
|
2005-04-28 10:26:44 +02:00
|
|
|
}
|
|
|
|
|
2008-12-18 14:27:16 +01:00
|
|
|
void _irc_line_deinit(struct line *l)
|
|
|
|
{
|
2008-12-20 14:20:50 +01:00
|
|
|
array_deinit(&l->words);
|
2008-12-18 14:27:16 +01:00
|
|
|
}
|
|
|
|
|
2005-04-28 10:26:44 +02:00
|
|
|
struct line *irc_line_new()
|
|
|
|
{
|
|
|
|
struct line *l;
|
2008-12-20 14:20:50 +01:00
|
|
|
|
2008-12-10 23:26:37 +01:00
|
|
|
l = bip_malloc(sizeof(struct line));
|
2005-04-28 10:26:44 +02:00
|
|
|
irc_line_init(l);
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
void irc_line_write(struct line *l, connection_t *c)
|
|
|
|
{
|
|
|
|
char *bytes = irc_line_to_string(l);
|
|
|
|
write_line(c, bytes);
|
|
|
|
free(bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct line *irc_line_dup(struct line *line)
|
|
|
|
{
|
2008-12-15 19:19:27 +01:00
|
|
|
int i;
|
2005-04-28 10:26:44 +02:00
|
|
|
struct line *nl = irc_line_new();
|
2008-12-20 14:20:50 +01:00
|
|
|
char *ptr;
|
|
|
|
|
2008-12-15 19:19:27 +01:00
|
|
|
nl->origin = line->origin ? bip_strdup(line->origin) : NULL;
|
2008-12-20 14:20:50 +01:00
|
|
|
array_each(&line->words, i, ptr)
|
|
|
|
array_set(&nl->words, i, bip_strdup(ptr));
|
2005-04-28 10:26:44 +02:00
|
|
|
nl->colon = line->colon;
|
|
|
|
return nl;
|
|
|
|
}
|
|
|
|
|
2008-12-15 19:19:27 +01:00
|
|
|
char *irc_line_pop(struct line *l)
|
|
|
|
{
|
2008-12-20 14:20:50 +01:00
|
|
|
return (char *)array_pop(&l->words);
|
2008-12-15 19:19:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void _irc_line_append(struct line *l, const char *s)
|
2005-04-28 10:26:44 +02:00
|
|
|
{
|
2008-12-20 14:20:50 +01:00
|
|
|
array_push(&l->words, (char *)s);
|
2005-04-28 10:26:44 +02:00
|
|
|
}
|
|
|
|
|
2008-12-15 19:19:27 +01:00
|
|
|
void irc_line_append(struct line *l, const char *s)
|
2005-04-28 10:26:44 +02:00
|
|
|
{
|
2008-12-15 19:19:27 +01:00
|
|
|
_irc_line_append(l, bip_strdup(s));
|
2005-04-28 10:26:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
char *irc_line_to_string(struct line *l)
|
|
|
|
{
|
|
|
|
size_t len = 0;
|
2008-12-15 19:19:27 +01:00
|
|
|
int i;
|
2005-04-28 10:26:44 +02:00
|
|
|
char *ret;
|
|
|
|
|
|
|
|
if (l->origin)
|
|
|
|
len = strlen(l->origin) + 2;
|
2008-12-20 14:20:50 +01:00
|
|
|
for (i = 0; i < array_count(&l->words); i++)
|
|
|
|
len += strlen(array_get(&l->words, i)) + 1;
|
2005-04-28 10:26:44 +02:00
|
|
|
len += 1; /* remove one trailing space and add \r\n */
|
|
|
|
len++; /* last args ":" */
|
2008-12-10 23:26:37 +01:00
|
|
|
ret = bip_malloc(len + 1);
|
2005-04-28 10:26:44 +02:00
|
|
|
ret[0] = 0;
|
|
|
|
|
|
|
|
if (l->origin) {
|
|
|
|
strcat(ret, ":");
|
|
|
|
strcat(ret, l->origin);
|
|
|
|
strcat(ret, " ");
|
|
|
|
}
|
2008-12-20 14:20:50 +01:00
|
|
|
for (i = 0; i < array_count(&l->words) - 1; i++) {
|
|
|
|
strcat(ret, array_get(&l->words, i));
|
2005-04-28 10:26:44 +02:00
|
|
|
strcat(ret, " ");
|
|
|
|
}
|
2008-12-20 14:20:50 +01:00
|
|
|
if (strchr(array_get(&l->words, i), ' ') || l->colon)
|
2005-04-28 10:26:44 +02:00
|
|
|
strcat(ret, ":");
|
|
|
|
|
2008-12-20 14:20:50 +01:00
|
|
|
strcat(ret, array_get(&l->words, i));
|
2005-04-28 10:26:44 +02:00
|
|
|
strcat(ret, "\r\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-12-18 14:27:16 +01:00
|
|
|
char *irc_line_to_string_to(struct line *line, char *nick)
|
|
|
|
{
|
|
|
|
char *tmp;
|
|
|
|
char *l;
|
|
|
|
|
|
|
|
tmp = (char *)irc_line_elem(line, 1);
|
2008-12-20 14:20:50 +01:00
|
|
|
array_set(&line->words, 1, nick);
|
2008-12-18 14:27:16 +01:00
|
|
|
l = irc_line_to_string(line);
|
2008-12-20 14:20:50 +01:00
|
|
|
array_set(&line->words, 1, tmp);
|
2008-12-18 14:27:16 +01:00
|
|
|
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2008-12-15 19:19:27 +01:00
|
|
|
int irc_line_count(struct line *line)
|
|
|
|
{
|
2008-12-20 14:20:50 +01:00
|
|
|
return array_count(&line->words);
|
2008-12-15 19:19:27 +01:00
|
|
|
}
|
|
|
|
|
2008-12-20 14:20:50 +01:00
|
|
|
int irc_line_includes(struct line *line, int elem)
|
2008-12-15 19:19:27 +01:00
|
|
|
{
|
2008-12-20 14:20:50 +01:00
|
|
|
return array_includes(&line->words, elem);
|
2008-12-15 19:19:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *irc_line_elem(struct line *line, int elem)
|
|
|
|
{
|
2008-12-20 14:20:50 +01:00
|
|
|
return array_get(&line->words, elem);
|
2008-12-15 19:19:27 +01:00
|
|
|
}
|
|
|
|
|
2009-02-02 19:47:53 +01:00
|
|
|
void irc_line_drop(struct line *line, int elem)
|
|
|
|
{
|
|
|
|
free(array_drop(&line->words, elem));
|
|
|
|
}
|
|
|
|
|
2008-12-15 19:19:27 +01:00
|
|
|
int irc_line_elem_equals(struct line *line, int elem, const char *cmp)
|
|
|
|
{
|
|
|
|
return !strcmp(irc_line_elem(line, elem), cmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int irc_line_elem_case_equals(struct line *line, int elem, const char *cmp)
|
|
|
|
{
|
|
|
|
return !strcasecmp(irc_line_elem(line, elem), cmp);
|
|
|
|
}
|
|
|
|
|
2005-04-28 10:26:44 +02:00
|
|
|
/*
|
|
|
|
* takes a null terminated string as input w/o \r\n
|
|
|
|
*/
|
2008-12-20 14:20:50 +01:00
|
|
|
struct line *irc_line_new_from_string(char *str)
|
2005-04-28 10:26:44 +02:00
|
|
|
{
|
|
|
|
struct line *line;
|
|
|
|
char *space;
|
|
|
|
size_t len;
|
|
|
|
|
2008-12-20 14:20:50 +01:00
|
|
|
line = irc_line_new();
|
2005-04-28 10:26:44 +02:00
|
|
|
if (str[0] == ':') {
|
|
|
|
space = str + 1;
|
|
|
|
|
|
|
|
while (*space && *space != ' ')
|
|
|
|
space++;
|
2008-12-20 14:20:50 +01:00
|
|
|
if (!*space) {
|
|
|
|
irc_line_free(line);
|
2005-04-28 10:26:44 +02:00
|
|
|
return NULL;
|
2008-12-20 14:20:50 +01:00
|
|
|
}
|
2005-04-28 10:26:44 +02:00
|
|
|
len = space - str - 1; /* leading ':' */
|
2008-12-10 23:26:37 +01:00
|
|
|
line->origin = bip_malloc(len + 1);
|
2005-04-28 10:26:44 +02:00
|
|
|
memcpy(line->origin, str + 1, len);
|
|
|
|
line->origin[len] = 0;
|
|
|
|
str = space;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (*str == ' ')
|
|
|
|
str++;
|
|
|
|
|
|
|
|
while (*str) {
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
space = str;
|
|
|
|
if (*space == ':') {
|
|
|
|
line->colon = 1;
|
|
|
|
str++;
|
|
|
|
while (*space)
|
|
|
|
space++;
|
|
|
|
} else {
|
|
|
|
while (*space && *space != ' ')
|
|
|
|
space++;
|
|
|
|
}
|
|
|
|
len = space - str;
|
2008-12-15 19:19:27 +01:00
|
|
|
tmp = bip_malloc(len + 1);
|
2005-04-28 10:26:44 +02:00
|
|
|
memcpy(tmp, str, len);
|
|
|
|
tmp[len] = 0;
|
2008-12-20 14:20:50 +01:00
|
|
|
if (array_count(&line->words) == 0)
|
2008-12-15 19:19:27 +01:00
|
|
|
strucase(tmp);
|
2008-12-20 14:20:50 +01:00
|
|
|
array_push(&line->words, tmp);
|
2005-04-28 10:26:44 +02:00
|
|
|
|
|
|
|
str = space;
|
|
|
|
while (*str == ' ')
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
void irc_line_free(struct line *l)
|
|
|
|
{
|
2008-12-15 19:19:27 +01:00
|
|
|
int i;
|
|
|
|
|
2008-12-20 14:20:50 +01:00
|
|
|
for (i = 0; i < array_count(&l->words); i++)
|
|
|
|
free(array_get(&l->words, i));
|
|
|
|
array_deinit(&l->words);
|
2005-04-28 10:26:44 +02:00
|
|
|
if (l->origin)
|
|
|
|
free(l->origin);
|
|
|
|
free(l);
|
|
|
|
}
|
2008-12-18 14:27:16 +01:00
|
|
|
|
|
|
|
void irc_line_free_args(char **elemv, int elemc)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (elemc == 0)
|
|
|
|
return;
|
|
|
|
for (i = 0; i < elemc; i++)
|
|
|
|
free(elemv[i]);
|
|
|
|
free(elemv);
|
|
|
|
}
|
|
|
|
|