Parsee/src/include/Parsee.h
2024-06-24 02:05:11 +02:00

166 lines
5.1 KiB
C

#ifndef PARSEE_PARSEE_H
#define PARSEE_PARSEE_H
#include <Cytoplasm/HttpServer.h>
#include <Cytoplasm/HttpRouter.h>
#include <Cytoplasm/HttpClient.h>
#include <Cytoplasm/Stream.h>
#include <Cytoplasm/Db.h>
#include <pthread.h>
#include <XMPP.h>
typedef struct ParseeConfig {
/* -------- MATRIX -------- */
char *as_token, *hs_token;
/* id here is "Parsee XMPP". */
char *sender_localpart;
char *namespace_base;
char *listen_as;
int port;
/* Homeserver port info */
char *homeserver_host;
int homeserver_port;
/* ------- JABBER -------- */
char *component_host;
char *shared_comp_secret;
int component_port;
/* ------- DB -------- */
char *db_path;
} ParseeConfig;
typedef struct ParseeData {
const ParseeConfig *config;
HttpRouter *router;
XMPPComponent *jabber;
Db *db;
} ParseeData;
#define SECONDS * 1000
#define MINUTES * 60 SECONDS
#define HOURS * 60 MINUTES
/* Initialises a Parsee config from scratch, and writes to it
* as JSON in the CWD. */
extern void ParseeConfigInit(void);
/* Loads a Parsee config from a JSON filepath. */
extern void ParseeConfigLoad(char *);
/* Retrieves the Parsee config if loaded. */
extern const ParseeConfig * ParseeConfigGet(void);
/* Exports the Parsee config as a YAML document. */
extern void ParseeExportConfigYAML(Stream *);
/* Destroys the Parsee configuration */
extern void ParseeConfigFree(void);
/* Creates and destroys a data structure, stored on the heap. */
extern ParseeData * ParseeInitData(XMPPComponent *);
extern void ParseeFreeData(ParseeData *);
extern HttpClientContext * ParseeCreateRequest(const ParseeConfig *, HttpRequestMethod, char *);
/* Sends headers, and writes the JSON object. */
extern HttpStatus ParseeSetRequestJSON(HttpClientContext *, HashMap *);
/* This function is called on every event received, and routes/manages it. */
extern void ParseeEventHandler(ParseeData *, HashMap *);
/* Verifies if a user is a Parsee puppet user. */
extern bool ParseeIsPuppet(const ParseeConfig *, char *);
/* Verifies if a user is a Parsee puppet user on XMPP. */
extern bool ParseeIsJabberPuppet(const ParseeConfig *, char *);
/* Decodes a local JID for a user into a string. */
extern char * ParseeDecodeLocalJID(const ParseeConfig *, char *);
/* Encodes a JID into a Parsee localpart */
extern char * ParseeEncodeJID(const ParseeConfig *, char *, bool);
/* Gets the localpart of a MXID */
extern char * ParseeGetLocal(char *);
/* Encodes an MXID to a valid Jabber ID head */
extern char * ParseeEncodeMXID(char *);
/* Decodes a Jabber ID head into a valid MXID */
extern char * ParseeDecodeMXID(char *);
/* HTTP server handler for Parsee, takes in a config. */
extern void ParseeRequest(HttpServerContext *, void *);
/* A pthread callback used for listening to a component */
extern void * ParseeXMPPThread(void *data);
/* Wakes up the XMPP thread from a "suspend" killer stanza */
extern void ParseeWakeupThread(void);
/* Finds the room a DM is associated to, from a Matrix user and a Jabber
* ID. */
extern char * ParseeFindDMRoom(ParseeData *data, char *mxid, char *jid);
/* Gets a DM ID from a Matrix and Jabber user */
extern char * ParseeGetDMID(char *mxid, char *jid);
/* Creates a DM mapping in the database */
extern void ParseePushDMRoom(ParseeData *, char *mxid, char *jid, char *r);
/* Trims the component from a JID */
extern char * ParseeTrimJID(char *jid);
/* Gets the component from a JID */
extern char * ParseeGetResource(char *jid);
/* Decodes a room alias into a MUC JID */
extern char * ParseeDecodeLocalMUC(const ParseeConfig *c, char *alias);
/* Decodes an encoded string(from an MXID/Room ID) into a JID */
extern char * ParseeDecodeJID(char *str, char term);
/* Creates a Room/MUC mapping, and returns it's chat ID. */
extern char * ParseePushMUC(ParseeData *, char *room_id, char *jid);
/* Finds a chat ID from a MUC JID */
extern char * ParseeGetFromMUCID(ParseeData *, char *jid);
/* Finds a chat ID from a room ID */
extern char * ParseeGetFromRoomID(ParseeData *, char *room_id);
/* Finds the room ID from a chat ID */
extern char * ParseeGetRoomID(ParseeData *, char *chat_id);
/* Finds the MUC JID from a chat ID */
extern char * ParseeGetMUCID(ParseeData *, char *chat_id);
/* Pushes a stanza ID to a chat ID */
extern void ParseePushStanza(ParseeData *, char *chat_id, char *stanza_id, char *event, char *sender);
/* Checks if a stanza is not duplicated in a chat ID */
extern bool ParseeVerifyStanza(ParseeData *, char *chat_id, char *stanza_id);
/* Gets the stanza ID and sender of an event */
extern bool ParseeGetStanzaInfo(ParseeData *, char *c_id, char *e, char **st, char **se);
/* Sends presence requests for every MUC around as a fake JID */
extern void ParseeSendPresence(ParseeData *);
extern bool ParseeInitialiseSignals(HttpServer *, pthread_t, XMPPComponent *);
/* Job used to cleanup Parsee data that isn't necessary anymore. */
extern void ParseeCleanup(void *data);
/* Finds the offset of the first line without a '>' at its start. */
extern int ParseeFindDatastart(char *data);
#endif