server/src/util/goodies.c

138 lines
3.1 KiB
C
Raw Normal View History

2010-08-08 10:06:34 +02:00
/*
Copyright (c) 1998-2010, Enno Rehling <enno@eressea.de>
Katja Zedel <katze@felidae.kn-bremen.de
Christian Schlittchen <corwin@amber.kn-bremen.de>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
**/
#include <platform.h>
#include "goodies.h"
#include "unicode.h"
/* libc includes */
#include <wctype.h>
#include <stdlib.h>
#include <string.h>
/* Simple Integer-Liste */
2011-03-07 08:02:35 +01:00
int *intlist_init(void)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
return (calloc(1, sizeof(int)));
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
int *intlist_add(int *i_p, int i)
2010-08-08 10:06:34 +02:00
{
i_p[0]++;
2011-03-07 08:02:35 +01:00
i_p = realloc(i_p, (i_p[0] + 1) * sizeof(int));
2010-08-08 10:06:34 +02:00
i_p[i_p[0]] = i;
return (i_p);
}
2011-03-07 08:02:35 +01:00
int *intlist_find(int *i_p, int fi)
2010-08-08 10:06:34 +02:00
{
int i;
2011-03-07 08:02:35 +01:00
for (i = 1; i <= i_p[0]; i++) {
if (i_p[i] == fi)
return (&i_p[i]);
2010-08-08 10:06:34 +02:00
}
return NULL;
}
2011-03-07 08:02:35 +01:00
char *set_string(char **s, const char *neu)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
if (neu == NULL) {
2010-08-08 10:06:34 +02:00
free(*s);
*s = NULL;
} else if (*s == NULL) {
2011-03-07 08:02:35 +01:00
*s = malloc(strlen(neu) + 1);
2010-08-08 10:06:34 +02:00
strcpy(*s, neu);
} else {
*s = realloc(*s, strlen(neu) + 1);
strcpy(*s, neu);
}
return *s;
}
2011-03-07 08:02:35 +01:00
static int spc_email_isvalid(const char *address)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
int count = 0;
2010-08-08 10:06:34 +02:00
const char *c, *domain;
2011-03-07 08:02:35 +01:00
static const char *rfc822_specials = "()<>@,;:\\\"[]"; /* STATIC_CONST: contains a constant value */
2010-08-08 10:06:34 +02:00
/* first we validate the name portion (name@domain) */
2011-03-07 08:02:35 +01:00
for (c = address; *c; c++) {
if (*c == '\"' && (c == address || *(c - 1) == '.' || *(c - 1) == '\"')) {
while (*++c) {
if (*c == '\"')
break;
if (*c == '\\' && (*++c == ' '))
continue;
if (*c <= ' ' || *c >= 127)
return 0;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
if (!*c++)
return 0;
if (*c == '@')
break;
if (*c != '.')
return 0;
continue;
}
if (*c == '@')
break;
if (*c <= ' ' || *c >= 127)
return 0;
if (strchr(rfc822_specials, *c))
return 0;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
if (c == address || *(c - 1) == '.')
return 0;
2010-08-08 10:06:34 +02:00
/* next we validate the domain portion (name@domain) */
2011-03-07 08:02:35 +01:00
if (!*(domain = ++c))
return 0;
2010-08-08 10:06:34 +02:00
do {
if (*c == '.') {
2011-03-07 08:02:35 +01:00
if (c == domain || *(c - 1) == '.')
return 0;
2010-08-08 10:06:34 +02:00
count++;
}
2011-03-07 08:02:35 +01:00
if (*c <= ' ' || *c >= 127)
return 0;
if (strchr(rfc822_specials, *c))
return 0;
2010-08-08 10:06:34 +02:00
} while (*++c);
return (count >= 1);
}
2011-03-07 08:02:35 +01:00
int set_email(char **pemail, const char *newmail)
2010-08-08 10:06:34 +02:00
{
if (newmail && *newmail) {
2011-03-07 08:02:35 +01:00
if (spc_email_isvalid(newmail) <= 0)
return -1;
2010-08-08 10:06:34 +02:00
}
2011-03-07 08:02:35 +01:00
if (*pemail)
free(*pemail);
2010-08-08 10:06:34 +02:00
*pemail = 0;
if (newmail) {
*pemail = _strdup(newmail);
2010-08-08 10:06:34 +02:00
}
return 0;
}