[ADD] Starting DMs XMPP->Matrix

This commit is contained in:
LDA 2024-07-16 20:39:44 +02:00
commit 299f473a81
4 changed files with 108 additions and 5 deletions

View file

@ -431,6 +431,46 @@ ASCreateRoom(const ParseeConfig *conf, char *by, char *alias)
return id;
}
char *
ASCreateDM(const ParseeConfig *conf, char *by, char *with)
{
HttpClientContext *ctx = NULL;
HashMap *json = NULL;
char *path, *id;
if (!conf || !by || !with)
{
return NULL;
}
path = StrConcat(3,
"/_matrix/client/v3/createRoom",
"?user_id=", by
);
ctx = ParseeCreateRequest(
conf,
HTTP_POST, path
);
Free(path);
json = HashMapCreate();
{
Array *invitees = ArrayCreate();
ArrayAdd(invitees, JsonValueString(with));
HashMapSet(json, "invite", JsonValueArray(invitees));
HashMapSet(json, "is_direct", JsonValueBoolean(true));
}
ASAuthenticateRequest(conf, ctx);
ParseeSetRequestJSON(ctx, json);
JsonFree(json);
json = JsonDecode(HttpClientStream(ctx));
id = StrDuplicate(JsonValueAsString(HashMapGet(json, "room_id")));
HttpClientContextFree(ctx);
JsonFree(json);
return id;
}
void
ASSetAvatar(const ParseeConfig *conf, char *user, char *mxc)
{

View file

@ -63,3 +63,49 @@ StrFreeLines(char **split)
Free(orig);
}
size_t
StrLines(char **split)
{
size_t i;
if (!split)
{
return 0;
}
for (i = 0; *split++; i++)
{
/* Left blank */
}
return i;
}
static size_t
StrMaxLine(char **split)
{
size_t max = 0;
if (!split)
{
return 0;
}
while (*split)
{
size_t len = strlen(*split++);
if (len > max)
{
max = len;
}
}
return max;
}
StringRect
StrFullRect(char **split)
{
return ((StringRect) {
.start_line = 0, .start_char = 0,
.end_line = StrLines(split),
.end_char = StrMaxLine(split),
.source_lines = split
});
}

View file

@ -286,10 +286,7 @@ ParseeVerifyAllStanza(ParseeData *args, XMLElement *stanza)
struct XMPPThread;
typedef struct XMPPThreadInfo {
/* A FIFO of stanzas inbound, to be read by dispatcher
* threads.
*
* TODO: Using it's length in !stats can be useful
* for having a "congestion" metric for Parsee admins... */
* threads. */
Array *stanzas;
pthread_mutex_t lock;
@ -583,6 +580,25 @@ MessageStanza(ParseeData *args, XMLElement *stanza, XMPPThread *thr)
room = ParseeFindDMRoom(args, to, from);
data = ArrayGet(body->children, 0);
/* TODO: CLEAN THAT UP */
mroom_id = ParseeGetBridgedRoom(args, stanza);
if (!mroom_id && !room && !XMPPIsParseeStanza(stanza) &&
to && *to == '@')
{
DbRef *room_ref;
HashMap *room_json;
char *from = HashMapGet(stanza->attrs, "from");
room = ASCreateDM(args->config, from_matrix, to);
room_ref = DbCreate(args->db, 3, "rooms", room, "data");
room_json = DbJson(room_ref);
HashMapSet(room_json, "is_direct", JsonValueBoolean(true));
HashMapSet(room_json, "xmpp_user", JsonValueString(from));
DbUnlock(args->db, room_ref);
ParseePushDMRoom(args, to, from, room);
}
/* TODO: THIS IS A HACK. THIS CODE IGNORES ALL MUC MESSAGES EVER
* SENT TO NON-PARSEE PUPPETS, AS A "FIX" TO THE MULTITHREADED
* ISSUE.
@ -596,7 +612,6 @@ MessageStanza(ParseeData *args, XMLElement *stanza, XMPPThread *thr)
}
}
mroom_id = ParseeGetBridgedRoom(args, stanza);
if (mroom_id && !XMPPIsParseeStanza(stanza))
{
char *res = ParseeGetResource(from);

View file

@ -57,6 +57,8 @@ extern void ASSetPL(const ParseeConfig *conf, char *id, HashMap *m);
* returns it's ID if it exists. */
extern char * ASCreateRoom(const ParseeConfig *c, char *by, char *alias);
extern char * ASCreateDM(const ParseeConfig *c, char *by, char *with);
/** Sets the user's global display{name]
* --------
* Returns: NOTHING