tildefriends/src/ssb.h

104 lines
4.2 KiB
C
Raw Normal View History

#pragma once
#include <quickjs.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#define ID_BASE64_LEN 57
#define ID_BIN_LEN 32
#define BLOB_ID_LEN 53
enum
{
k_ssb_rpc_flag_binary = 0x0,
k_ssb_rpc_flag_utf8 = 0x1,
k_ssb_rpc_flag_json = 0x2,
k_ssb_rpc_mask_type = 0x3,
k_ssb_rpc_flag_end_error = 0x4,
k_ssb_rpc_flag_stream = 0x8,
k_ssb_blob_bytes_max = 5 * 1024 * 1024,
};
typedef enum _tf_ssb_change_t
{
k_tf_ssb_change_create,
k_tf_ssb_change_connect,
k_tf_ssb_change_remove,
} tf_ssb_change_t;
typedef struct _tf_ssb_t tf_ssb_t;
typedef struct _tf_ssb_rpc_t tf_ssb_rpc_t;
typedef struct _tf_ssb_connection_t tf_ssb_connection_t;
typedef struct _tf_trace_t tf_trace_t;
typedef struct sqlite3 sqlite3;
typedef struct uv_loop_s uv_loop_t;
struct sockaddr_in;
enum {
k_id_base64_len = 57,
k_id_bin_len = 32,
};
tf_ssb_t* tf_ssb_create(uv_loop_t* loop, JSContext* context, sqlite3* db, const char* secrets_path);
void tf_ssb_destroy(tf_ssb_t* ssb);
sqlite3* tf_ssb_get_db(tf_ssb_t* ssb);
uv_loop_t* tf_ssb_get_loop(tf_ssb_t* ssb);
tf_ssb_rpc_t* tf_ssb_get_rpc(tf_ssb_t* ssb);
void tf_ssb_generate_keys(tf_ssb_t* ssb);
void tf_ssb_set_trace(tf_ssb_t* ssb, tf_trace_t* trace);
tf_trace_t* tf_ssb_get_trace(tf_ssb_t* ssb);
JSContext* tf_ssb_get_context(tf_ssb_t* ssb);
void tf_ssb_broadcast_listener_start(tf_ssb_t* ssb, bool linger);
void tf_ssb_run(tf_ssb_t* ssb);
void tf_ssb_append_message(tf_ssb_t* ssb, JSValue message);
void tf_ssb_append_post(tf_ssb_t* ssb, const char* text);
bool tf_ssb_whoami(tf_ssb_t* ssb, char* out_id, size_t out_id_size);
void tf_ssb_set_broadcasts_changed_callback(tf_ssb_t* ssb, void (*callback)(tf_ssb_t* ssb, void* user_data), void* user_data);
void tf_ssb_visit_broadcasts(tf_ssb_t* ssb, void (*callback)(const struct sockaddr_in* addr, const uint8_t* pub, void* user_data), void* user_data);
typedef void (tf_ssb_connections_changed_callback_t)(tf_ssb_t* ssb, tf_ssb_change_t change, tf_ssb_connection_t* connection, void* user_data);
void tf_ssb_add_connections_changed_callback(tf_ssb_t* ssb, tf_ssb_connections_changed_callback_t callback, void* user_data);
const char** tf_ssb_get_connection_ids(tf_ssb_t* ssb);
int tf_ssb_get_connections(tf_ssb_t* ssb, tf_ssb_connection_t** out_connections, int out_connections_count);
void tf_ssb_connect(tf_ssb_t* ssb, const char* host, int port, const uint8_t* key);
void tf_ssb_connect_str(tf_ssb_t* ssb, const char* address);
void tf_ssb_server_open(tf_ssb_t* ssb, int port);
void tf_ssb_server_close(tf_ssb_t* ssb);
void tf_ssb_send_createHistoryStream(tf_ssb_t* ssb, const char* id);
void tf_ssb_send_close(tf_ssb_t* ssb);
bool tf_ssb_id_str_to_bin(uint8_t* bin, const char* str);
bool tf_ssb_id_bin_to_str(char* str, size_t str_size, const uint8_t* bin);
void tf_ssb_test();
typedef void (tf_ssb_rpc_callback_t)(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, JSValue args, const uint8_t* message, size_t size, void* user_data);
void tf_ssb_register_rpc(tf_ssb_t* ssb, const char** name, tf_ssb_rpc_callback_t* callback, void* user_data);
bool tf_ssb_verify_and_strip_signature(JSContext* context, JSValue val, char* out_signature, size_t out_signature_size);
void tf_ssb_calculate_message_id(JSContext* context, JSValue message, char* out_id, size_t out_id_size);
const char* tf_ssb_connection_get_host(tf_ssb_connection_t* connection);
int tf_ssb_connection_get_port(tf_ssb_connection_t* connection);
tf_ssb_t* tf_ssb_connection_get_ssb(tf_ssb_connection_t* connection);
JSContext* tf_ssb_connection_get_context(tf_ssb_connection_t* connection);
sqlite3* tf_ssb_connection_get_db(tf_ssb_connection_t* connection);
void tf_ssb_connection_rpc_send(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, const uint8_t* message, size_t size, tf_ssb_rpc_callback_t* callback, void* user_data);
int32_t tf_ssb_connection_next_request_number(tf_ssb_connection_t* connection);
bool tf_ssb_connection_get_id(tf_ssb_connection_t* connection, char* out_id, size_t out_id_size);
void tf_ssb_connection_add_request(tf_ssb_connection_t* connection, int32_t request_number, tf_ssb_rpc_callback_t* callback, void* user_data);
void tf_ssb_connection_remove_request(tf_ssb_connection_t* connection, int32_t request_number);