[ADD] Add basic Matrix ID parser

This commit is contained in:
LDA 2024-08-22 00:04:50 +02:00
commit 8042ccc0cc
8 changed files with 70 additions and 15 deletions

3
.gitignore vendored
View file

@ -12,3 +12,6 @@ tools/out/*
ayaya/*
ayaya
#ctags
tags

View file

@ -41,6 +41,8 @@ all: binary utils
binary: $(OBJ_FILES)
$(CC) $(LDFLAGS) $(OBJ_FILES) -o $(BINARY)
tags:
@ctags --recurse $(SOURCE)/
clean:
rm -rf $(OBJECT) $(BINARY) $(AYAS)

View file

@ -11,6 +11,8 @@
#include <Matrix.h>
#include <AS.h>
#include <ctype.h>
static void
JoinMUC(ParseeData *data, HashMap *event, char *jid, char *muc, char *name)
{
@ -20,7 +22,7 @@ JoinMUC(ParseeData *data, HashMap *event, char *jid, char *muc, char *name)
char *rev = StrConcat(3, muc, "/", nick);
int nonce = 0;
while (!XMPPJoinMUC(data->jabber, jid, rev, true) && nonce < 20)
while (!XMPPJoinMUC(data->jabber, jid, rev, true) && nonce < 32)
{
char *nonce_str = StrInt(nonce);
char *input = StrConcat(4, sender, name, data->id, nonce_str);
@ -122,10 +124,6 @@ ParseeMemberHandler(ParseeData *data, HashMap *event)
goto end;
}
/* TODO: We need to deal with the nick properly, as XMPP
* requires us to provide it whenever we want to even think
* about leaving...
* I love how this is the last place victim of the dreaded [p]... */
name = StrDuplicate(ParseeLookupNick(muc_id, sender));
rev = StrConcat(3, muc_id, "/", name);

34
src/MatrixID.c Normal file
View file

@ -0,0 +1,34 @@
#include <Matrix.h>
#include <Cytoplasm/Memory.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;
}

View file

@ -10,32 +10,36 @@
#include <stdlib.h>
#include <ctype.h>
#include <Matrix.h>
bool
ParseeIsPuppet(const ParseeConfig *conf, char *user)
{
char *localpart;
UserID *id;
bool flag = true;
size_t len;
if (!user || !conf || *user != '@')
if (!user ||
!conf ||
*user != '@' ||
!(id = MatrixParseID(user)))
{
return false;
}
localpart = user + 1;
len = strlen(conf->namespace_base);
if (strncmp(localpart, conf->namespace_base, len))
if (strncmp(id->localpart, conf->namespace_base, len))
{
flag = false;
}
len = strlen(conf->sender_localpart);
if (!strncmp(localpart, conf->sender_localpart, len))
if (StrEquals(id->localpart, conf->sender_localpart))
{
flag = true;
}
/* TODO: Check serverpart. 2 Parsee instances may be running in the same
* room. */
flag = flag && StrEquals(id->server, conf->server_base);
Free(id);
return flag;
}
char *

View file

@ -154,6 +154,7 @@ XMPPAuthenticateCompStream(XMPPComponent *comp, char *shared)
if (ev->type != XML_LEXER_ELEM ||
!StrEquals(ev->element, "handshake"))
{
Log(LOG_DEBUG, "type=%d elem='%s'", ev->type, ev->element);
Log(LOG_ERR, "Excepted empty handshake reply, got nonsense.");
Log(LOG_ERR, "Another service (possibly Parsee) may have taken over.");
Log(LOG_ERR, "");

View file

@ -162,7 +162,6 @@ end_error:
PEPManagerHandle(thr->info->pep_manager, stanza);
/* TODO: Separate the chatstate processing code. */
ProcessChatstates(args, stanza);
to = ParseeDecodeMXID(HashMapGet(stanza->attrs, "to"));
@ -317,7 +316,6 @@ end_error:
reaction = ArrayGet(react_child, i);
react_data = ArrayGet(reaction->children, 0);
/* TODO: We should manage removed reactions. */
Free(ASSend(
args->config, mroom_id, encoded,
"m.reaction",

View file

@ -3,6 +3,21 @@
#include <Cytoplasm/Json.h>
/* A simple representation of everything. It is not as complex as
* Telodendria's CommonID parser, simply because Parsee does not
* need such complexity. */
typedef struct UserID {
/* We're being extra leinent. */
char localpart[256];
char server[256];
} UserID;
/** Parses a Matrix user ID, with no attention to detail.
* -----------
* Returns: a valid user ID[HEAP] | NULL
* Thrasher: Free */
extern UserID * MatrixParseID(char *user);
/* Creates an error message JSON, with the specified code and message. */
extern HashMap * MatrixCreateError(char *err, char *msg);