[FIX] Fix the user ID parser to actually work

This commit is contained in:
lda 2023-11-26 16:25:16 +01:00
parent 30679d7999
commit 4e7554d241

View file

@ -25,14 +25,49 @@
#include <Parser.h> #include <Parser.h>
#include <Cytoplasm/Str.h> #include <Cytoplasm/Str.h>
#include <Cytoplasm/Memory.h>
#include <string.h> #include <string.h>
#include <ctype.h>
/* Parse an extended localpart */
static int
ParseUserLocalpart(char **str, char **out)
{
char c;
char *start;
size_t length;
if (!str || !out)
{
return 0;
}
/* An extended localpart contains every ASCII printable character,
* except an ':'. */
start = *str;
while (isascii((c = (*(*str)++))) && c != ':' && c)
{
/* Do nothing */
}
length = (size_t) (*str - start) -1;
if (c != ':' || length < 1)
{
*str = start;
return 0;
}
*out = Malloc(length + 1);
memcpy(*out, start, length);
*out[length] = '\0';
return 1;
}
int int
ParseCommonID(char *str, CommonID *id) ParseCommonID(char *str, CommonID *id)
{ {
char sigil; char sigil;
char *servstart;
if (!str || !id) if (!str || !id)
{ {
@ -55,16 +90,16 @@ ParseCommonID(char *str, CommonID *id)
} }
id->sigil = sigil; id->sigil = sigil;
/* Find the position of a ':' if there is one. */ switch(sigil)
if ((servstart = strchr(str, ':')))
{ {
id->local = StrSubstr(str, 1, (char) (servstart - str)); case '@':
id->server = StrDuplicate(servstart + 1); if (!ParseUserLocalpart(&str, &id->local))
return 1; {
return 0;
}
/* TODO: Match whenever str is valid. */
id->server = StrDuplicate(str);
break;
} }
/* Otherwise, just take the rest of the string. */
id->local = StrDuplicate(str + 1) ;
id->server = NULL ;
return 1; return 1;
} }