[FIX/WIP] Plumbing, -Werror, death to hitmen

Suspend killers are now no more. Except the actual killers to be gone
eventually.
Plumbing is also very basic as of now, but it "works".
This commit is contained in:
LDA 2024-07-05 20:24:15 +02:00
commit bb836789b2
23 changed files with 310 additions and 165 deletions

View file

@ -49,8 +49,8 @@ typedef struct XMPPIdentity {
static int
ICollate(unsigned char *cata, unsigned char *catb)
{
size_t al = cata ? strlen(cata) : 0;
size_t bl = catb ? strlen(catb) : 0;
size_t al = cata ? strlen((char *) cata) : 0;
size_t bl = catb ? strlen((char *) catb) : 0;
if (!al && !bl)
{
@ -89,8 +89,8 @@ IdentitySort(void *idap, void *idbp)
{
XMPPIdentity *ida = idap;
XMPPIdentity *idb = idbp;
unsigned char *cata = ida->category;
unsigned char *catb = idb->category;
unsigned char *cata = (unsigned char *) ida->category;
unsigned char *catb = (unsigned char *) idb->category;
return ICollate(cata, catb);
}
@ -144,7 +144,7 @@ XMPPGenerateVer(void)
Sha = Sha1(S);
Free(S);
S = Base64Encode(Sha, 20);
S = Base64Encode((const char *) Sha, 20);
Free(Sha);
ArrayFree(features);
@ -159,17 +159,6 @@ XMPPGenerateVer(void)
return S;
}
static pthread_mutex_t cond_var_lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond_var = PTHREAD_COND_INITIALIZER;
void
ParseeWakeupThread(void)
{
pthread_mutex_lock(&cond_var_lock);
pthread_cond_signal(&cond_var);
pthread_mutex_unlock(&cond_var_lock);
}
static char *
ParseeGetBridgedRoom(ParseeData *data, XMLElement *stanza)
{
@ -222,7 +211,6 @@ ParseeGetReactedEvent(ParseeData *data, XMLElement *stanza)
XMLElement *reactions = XMLookForTKV(stanza,
"reactions", "xmlns", "urn:xmpp:reactions:0"
);
char *from = (HashMapGet(stanza->attrs, "from"));
char *reacted_id = reactions ? HashMapGet(reactions->attrs, "id") : NULL;
return ParseeGetEventFromID(data, stanza, reacted_id);
@ -237,7 +225,6 @@ ParseePushAllStanza(ParseeData *args, XMLElement *stanza, char *event)
char *id_str = HashMapGet(stanza->attrs, "id");
char *s_id_str = XMPPGetStanzaID(stanza);
char *o_id_str = XMPPGetOriginID(stanza);
if (!chat_id)
{
@ -389,6 +376,8 @@ ManageProfileItem(ParseeData *args, XMLElement *item, XMLElement *stanza, XMPPTh
pthread_mutex_unlock(&jabber->write_lock);
XMLFreeElement(request);
(void) url; /* TODO */
}
end:
@ -406,7 +395,7 @@ MessageStanza(ParseeData *args, XMLElement *stanza, XMPPThread *thr)
XMLElement *event = NULL;
char *to, *room, *from, *from_matrix, *decode_from;
char *chat_id = NULL, *mroom_id = NULL;
char *mroom_id = NULL;
size_t i;
to = NULL;
@ -506,9 +495,6 @@ MessageStanza(ParseeData *args, XMLElement *stanza, XMPPThread *thr)
{
char *res = ParseeGetResource(from);
char *encoded = ParseeEncodeJID(args->config, decode_from, false);
char *s_id_str = XMPPGetStanzaID(stanza);
char *o_id_str = XMPPGetOriginID(stanza);
char *id_str = HashMapGet(stanza->attrs, "id");
char *event_id = NULL;
char *replaced = XMPPGetReplacedID(stanza);
char *reply_to = XMPPGetReply(stanza);
@ -548,7 +534,7 @@ MessageStanza(ParseeData *args, XMLElement *stanza, XMPPThread *thr)
ASSetName(args->config, encoded, res);
}
ASInvite(args->config, mroom_id, encoded);
ASJoin(args->config, mroom_id, encoded);
Free(ASJoin(args->config, mroom_id, encoded));
/* Check if it is a media link */
oob = XMLookForTKV(stanza, "x", "xmlns", "jabber:x:oob");
@ -574,7 +560,6 @@ MessageStanza(ParseeData *args, XMLElement *stanza, XMPPThread *thr)
}
else if (reactions)
{
char *reacted_id = HashMapGet(reactions->attrs, "id");
Array *react_child = reactions->children;
size_t reacts = ArraySize(react_child);
event_id = ParseeGetReactedEvent(args, stanza);
@ -642,8 +627,6 @@ MessageStanza(ParseeData *args, XMLElement *stanza, XMPPThread *thr)
XMLElement *parsee = XMLookForUnique(stanza, "x-parsee");
XMLElement *event = XMLookForUnique(parsee, "event-id");
XMLElement *e_d = ArrayGet(event ? event->children : NULL, 0);
char *s_id_str = XMPPGetStanzaID(stanza);
char *id_str = HashMapGet(stanza->attrs, "id");
ParseePushAllStanza(args, stanza, e_d->data);
}
@ -752,7 +735,6 @@ TrimBase64(char *b64)
static void
IQResult(ParseeData *args, XMLElement *stanza)
{
XMPPComponent *jabber = args->jabber;
XMLElement *vcard = XMLookForTKV(stanza, "vCard", "xmlns", "vcard-temp");
XMLElement *event = XMLookForTKV(stanza, "pubsub",
@ -776,7 +758,7 @@ IQResult(ParseeData *args, XMLElement *stanza)
char *id = HashMapGet(item->attrs, "id");
char *from = HashMapGet(stanza->attrs, "from");
char *base64;
unsigned char *bdata;
char *bdata;
size_t length, b64len;
Stream *datastream;
char *mxc, *from_matrix, *jid;
@ -804,8 +786,8 @@ IQResult(ParseeData *args, XMLElement *stanza)
b64len = base64 ? strlen(base64) : 0;
length = Base64DecodedSize(base64, b64len);
/* TODO: Bound checks! */
bdata = Base64Decode(base64, b64len);
/* TODO: Bound checks for a size limit. */
bdata = (char *) Base64Decode(base64, b64len);
datastream = StrStreamReaderN(bdata, length);
mxc = ASUpload(args->config, datastream, length);
@ -837,13 +819,16 @@ IQResult(ParseeData *args, XMLElement *stanza)
if (nickname)
{
XMLElement *data = ArrayGet(nickname->children, 0);
/* TODO: Use the nickname for something.
* And the rest of the vCard, somewhere. */
(void) data;
}
if (photo)
{
XMLElement *binval = XMLookForUnique(photo, "BINVAL");
XMLElement *data = ArrayGet(binval->children, 0);
char *base64;
unsigned char *bdata;
char *bdata;
size_t length, b64len;
Stream *datastream;
char *mxc, *from_matrix, *jid;
@ -885,7 +870,7 @@ IQGet(ParseeData *args, XMLElement *stanza)
else if (XMLookForTKV(stanza, "query", "xmlns", "jabber:iq:version"))
{
XMLElement *iq_reply, *query;
XMLElement *name, *version, *val;
XMLElement *name, *version;
char *from = HashMapGet(stanza->attrs, "from");
char *to = HashMapGet(stanza->attrs, "to");
char *id = HashMapGet(stanza->attrs, "id");
@ -980,24 +965,22 @@ PresenceStanza(ParseeData *args, XMLElement *stanza)
if ((user_info = XMLookForTKV(stanza, "x", "xmlns", MUC_USER_NS)))
{
XMLElement *item = XMLookForUnique(user_info, "item");
XMPPComponent *jabber = args->jabber;
char *jid = item ? HashMapGet(item->attrs, "jid") : NULL;
char *from, *best = jid ? jid : oid;
char *from;
char *type = HashMapGet(stanza->attrs, "type");
if (StrEquals(type, "unavailable"))
{
/* TODO: Treat as a ban if the role is outcast */
/* TODO: Treat as a ban if the role is outcast.
* Modify the code to be more accurate later. */
char *room = ParseeGetBridgedRoom(args, stanza);
char *decode_from = ParseeLookupJID(oid);
char *from_matrix = ParseeDecodeMXID(decode_from);
char *affiliation = HashMapGet(item->attrs, "affiliation");
if (!from_matrix || *from_matrix != '@')
{
Free(from_matrix);
from_matrix = ParseeEncodeJID(args->config, oid, true);
}
Log(LOG_INFO, "Acting on %s", from_matrix);
Log(LOG_INFO, "OID=%s DF=%s", oid, decode_from);
if (StrEquals(affiliation, "outcast"))
{
@ -1097,11 +1080,11 @@ XMPPDispatcher(void *argp)
{
XMPPThread *thread = argp;
ParseeData *args = thread->info->args;
XMPPComponent *jabber = thread->info->jabber;
while (thread->info->running)
{
XMLElement *stanza = RetrieveStanza(thread);
if (!stanza)
{
UtilSleepMillis(1);
@ -1116,7 +1099,6 @@ XMPPDispatcher(void *argp)
}
else if (StrEquals(stanza->name, "message"))
{
size_t i;
if (!MessageStanza(args, stanza, thread))
{
continue;
@ -1135,8 +1117,19 @@ XMPPDispatcher(void *argp)
}
XMLFreeElement(stanza);
}
return NULL;
}
typedef struct XMPPAwait {
pthread_mutex_t cond_lock;
pthread_cond_t condition;
XMLElement *stanza;
} XMPPAwait;
static pthread_mutex_t await_lock = PTHREAD_MUTEX_INITIALIZER;
static HashMap *await_table = NULL;
void *
ParseeXMPPThread(void *argp)
{
@ -1144,9 +1137,10 @@ ParseeXMPPThread(void *argp)
XMPPComponent *jabber = args->jabber;
XMLElement *stanza = NULL;
XMPPThreadInfo info;
pthread_mutex_t stanzas_lock = PTHREAD_MUTEX_INITIALIZER;
size_t i, j = 0;
size_t i;
/* Initialise the await table */
await_table = HashMapCreate();
/* Initialise the FIFO */
info.stanzas = ArrayCreate();
@ -1174,9 +1168,7 @@ ParseeXMPPThread(void *argp)
while (true)
{
char *to, *room, *from, *from_matrix;
char *chat_id, *mroom_id;
ssize_t cntr;
char *from, *id;
stanza = XMLDecode(jabber->stream, false);
if (!stanza)
@ -1184,25 +1176,37 @@ ParseeXMPPThread(void *argp)
continue;
}
id = HashMapGet(stanza->attrs, "id");
if (id)
{
XMPPAwait *await;
/* Lock out the table to see if we're awaiting. */
pthread_mutex_lock(&await_lock);
if ((await = HashMapGet(await_table, id)))
{
await->stanza = stanza;
pthread_mutex_lock(&await->cond_lock);
pthread_cond_signal(&await->condition);
pthread_mutex_unlock(&await->cond_lock);
HashMapDelete(await_table, id);
pthread_mutex_unlock(&await_lock);
continue;
}
pthread_mutex_unlock(&await_lock);
}
if (StrEquals(stanza->name, "message") && XMPPIsKiller(stanza))
{
const char *killer = "killer";
const char *suspend="suspend";
from = HashMapGet(stanza->attrs, "from");
if (!strncmp(from, killer, strlen(killer)))
{
XMLFreeElement(stanza);
break;
}
else if (!strncmp(from, suspend, strlen(suspend)))
{
/* TODO */
XMLFreeElement(stanza);
pthread_mutex_lock(&cond_var_lock);
pthread_cond_wait(&cond_var, &cond_var_lock);
pthread_mutex_unlock(&cond_var_lock);
continue;
}
}
PushStanza(&info, stanza);
@ -1222,6 +1226,48 @@ ParseeXMPPThread(void *argp)
}
ArrayFree(info.stanzas);
HashMapFree(await_table);
pthread_mutex_destroy(&info.lock);
return NULL;
}
XMLElement *
ParseeAwaitStanza(char *identifier)
{
XMPPAwait awa, *await;
XMLElement *stanza;
if (!identifier)
{
return NULL;
}
/* TODO: Pthreads HATE me using Malloc here, so I'm abusing stackspace.
* Not *too much* of a problem, just a weird oddity. If anyone has a clue
* on why that happens (at least on ARM64 with a Pi4 running Debian), let
* me know! */
await = &awa;
pthread_mutex_lock(&await_lock);
pthread_cond_init(&await->condition, NULL);
pthread_mutex_init(&await->cond_lock, NULL);
await->stanza = NULL;
HashMapSet(await_table, identifier, await);
pthread_mutex_unlock(&await_lock);
pthread_mutex_lock(&await->cond_lock);
while (!await->stanza)
{
pthread_cond_wait(&await->condition, &await->cond_lock);
}
stanza = await->stanza;
pthread_mutex_lock(&await_lock);
pthread_mutex_unlock(&await_lock);
pthread_mutex_unlock(&await->cond_lock);
pthread_cond_destroy(&await->condition);
pthread_mutex_destroy(&await->cond_lock);
return stanza;
}