Parsee/src/MatrixID.c

68 lines
1.3 KiB
C

#include <Matrix.h>
#include <Cytoplasm/Memory.h>
#include <Cytoplasm/Http.h>
#include <Cytoplasm/Str.h>
#include <Cytoplasm/Log.h>
#include <string.h>
UserID *
MatrixParseID(char *user)
{
UserID *ret = NULL;
char *localstart, *serverstart;
if (!user || *user != '@')
{
return NULL;
}
localstart = user + 1;
serverstart = strchr(user, ':');
if (!*localstart || !serverstart || localstart == serverstart)
{
return NULL;
}
if (!*++serverstart)
{
return NULL;
}
ret = Malloc(sizeof(*ret));
memset(ret, '\0', sizeof(*ret));
memcpy(ret->localpart, localstart, serverstart - localstart - 1);
memcpy(ret->server, serverstart, strlen(serverstart));
return ret;
}
UserID *
MatrixParseIDFromMTO(Uri *uri)
{
UserID *id = NULL;
char *path, *params, *decoded;
if (!uri)
{
return NULL;
}
if (!StrEquals(uri->proto, "https") || !StrEquals(uri->host, "matrix.to"))
{
return NULL;
}
if (strncmp(uri->path, "/#/", 3))
{
return NULL;
}
path = StrDuplicate(uri->path + 3);
params = path ? strchr(path, '?') : NULL;
if (params)
{
*params = '\0';
}
decoded = HttpUrlDecode(path);
id = MatrixParseID(decoded);
Free(decoded);
Free(path);
return id;
}