Parsee/src/include/Parsee.h
2024-06-29 02:12:01 +02:00

203 lines
6.6 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 <Command.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;
char *media_base;
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;
/* ------- ADMIN ------- */
char *jabber_admin;
char *matrix_admin;
} ParseeConfig;
typedef struct ParseeData {
const ParseeConfig *config;
HttpRouter *router;
CommandRouter *handler;
XMPPComponent *jabber;
Db *db;
} ParseeData;
#define GrabString(obj, ...) JsonValueAsString(JsonGet(obj, __VA_ARGS__))
#define GrabInteger(obj, ...) JsonValueAsInteger(JsonGet(obj, __VA_ARGS__))
#define GrabBoolean(obj, ...) JsonValueAsBoolean(JsonGet(obj, __VA_ARGS__))
#define GrabObject(obj, ...) JsonValueAsObject(JsonGet(obj, __VA_ARGS__))
#define GrabArray(obj, ...) JsonValueAsArray(JsonGet(obj, __VA_ARGS__))
#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 *origin_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);
/* XMPP-ifies a message event to XEP-0393 if possible. */
extern char * ParseeXMPPify(HashMap *event);
/* Finds an event ID from an ID in the stanza's attributes */
extern char * ParseeEventFromID(ParseeData *d, char *c_id, char *ori_id);
extern char * ParseeEventFromSID(ParseeData *d, char *c_id, char *ori_id);
/* Gets a Parsee "shim" link to an MXC, usable as unauth for a limited time */
extern char * ParseeToUnauth(ParseeData *data, char *mxc);
extern void ParseeInitialiseJIDTable(void);
extern void ParseePushJIDTable(char *muc, char *bare);
extern char *ParseeLookupJID(char *muc);
extern void ParseeDestroyJIDTable(void);
/* Globally bans a Matrix user from ever interacting with Parsee, and bans
* them from bridged rooms where the bot has administrator. */
extern void ParseeGlobalBan(ParseeData *, char *user);
/* Verifies if a user was globally banned. If so, then apply actions to the
* room ID */
extern bool ParseeManageBan(ParseeData *, char *user, char *room);
#endif