#ifndef PARSEE_PARSEE_H #define PARSEE_PARSEE_H #include #include #include #include #include #include #include #include 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; } 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__)) /* Milliseconds to UNIT macros, to be used like 30 SECONDS and 1 MINUTES * in timestamps */ #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 *); /* 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); /** Wait for any stanza with an "id" attribute of {identifier}, * with an optional timeout in milliseconds of {ts} if it is positive. * -------- * UB-If: called in the XMPP dispatcher thread itself * Returns: NULL | XMLElement[LA:HEAP] * Modifies: NONE */ extern XMLElement * ParseeAwaitStanza(char *identifier, int64_t ts); /** Returns the amount of unprocessed stanzas in the XMPP thread, which * can be used by admins to guess load. * -------- * UB-If: called in the XMPP dispatcher thread itself * Returns: amount of stanzas in the FIFO * Modifies: NONE */ extern size_t ParseeCongestion(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); extern void ParseePushDMStanza(ParseeData *, char *room_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); extern bool ParseeGetDMStanzaInfo(ParseeData *, char *r_id, char *e, char **st, char **se); extern bool ParseeGetOrigin(ParseeData *data, char *chat_id, char *ev, char **o); extern bool ParseeGetDMOrigin(ParseeData *data, char *chat_id, char *ev, char **o); /* 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); extern char * ParseeDMEventFromID(ParseeData *d, char *r_id, char *ori_id); extern char * ParseeDMEventFromSID(ParseeData *d, char *r_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); extern void ParseeInitialiseHeadTable(void); extern void ParseePushHeadTable(char *room, char *id); extern char *ParseeLookupHead(char *room); extern void ParseeDestroyHeadTable(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, char *reason); /* Verifies if a user was globally banned. If so, then apply actions to the * room ID */ extern bool ParseeManageBan(ParseeData *, char *user, char *room); /* Same as ParseeVerifyStanza, but DMs */ extern bool ParseeVerifyDMStanza(ParseeData *data, char *room_id, char *id); /* Checks if any user is an admin */ extern bool ParseeIsAdmin(ParseeData *data, char *user); /* Measures Parsee's overall uptime */ extern uint64_t ParseeUptime(void); /** Turns a duration into a nice "X minutes, Y seconds" string * --------- * Returns: A human-readable string showing the duration[LA:HEAP] * Modifies: NOTHING */ extern char * ParseeStringifyDate(uint64_t millis); /* Generates the JID of the Parsee bridge user. */ extern char * ParseeJID(ParseeData *data); extern char * ParseeMXID(ParseeData *data); #endif