snac.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
  3. #define VERSION "2.63"
  4. #define USER_AGENT "snac/" VERSION
  5. #define WHAT_IS_SNAC_URL "https:/" "/comam.es/what-is-snac"
  6. #define DIR_PERM 00770
  7. #define DIR_PERM_ADD 02770
  8. #define ISO_DATE_SPEC "%Y-%m-%dT%H:%M:%SZ"
  9. #ifndef MAX_THREADS
  10. #define MAX_THREADS 256
  11. #endif
  12. #ifndef MAX_CONVERSATION_LEVELS
  13. #define MAX_CONVERSATION_LEVELS 48
  14. #endif
  15. #define MD5_HEX_SIZE 33
  16. extern double disk_layout;
  17. extern xs_str *srv_basedir;
  18. extern xs_dict *srv_config;
  19. extern xs_str *srv_baseurl;
  20. extern xs_str *srv_proxy_token_seed;
  21. extern int dbglevel;
  22. #define L(s) (s)
  23. #define POSTLIKE_OBJECT_TYPE "Note|Question|Page|Article|Video|Event"
  24. int mkdirx(const char *pathname);
  25. int valid_status(int status);
  26. xs_str *tid(int offset);
  27. double ftime(void);
  28. void srv_log(xs_str *str);
  29. #define srv_debug(level, str) do { if (dbglevel >= (level)) \
  30. { srv_log((str)); } } while (0)
  31. typedef struct {
  32. xs_str *uid; /* uid */
  33. xs_str *basedir; /* user base directory */
  34. xs_dict *config; /* user configuration */
  35. xs_dict *config_o; /* user configuration admin override */
  36. xs_dict *key; /* keypair */
  37. xs_dict *links; /* validated links */
  38. xs_str *actor; /* actor url */
  39. xs_str *md5; /* actor url md5 */
  40. } snac;
  41. typedef struct {
  42. int s_size; /* struct size (for double checking) */
  43. int srv_running; /* server running on/off */
  44. int use_fcgi; /* FastCGI use on/off */
  45. time_t srv_start_time; /* start time */
  46. int job_fifo_size; /* job fifo size */
  47. int peak_job_fifo_size; /* maximum job fifo size seen */
  48. int n_threads; /* number of configured threads */
  49. enum { THST_STOP, THST_WAIT, THST_IN, THST_QUEUE } th_state[MAX_THREADS];
  50. } srv_state;
  51. extern srv_state *p_state;
  52. void snac_log(snac *user, xs_str *str);
  53. #define snac_debug(user, level, str) do { if (dbglevel >= (level)) \
  54. { snac_log((user), (str)); } } while (0)
  55. int srv_open(const char *basedir, int auto_upgrade);
  56. void srv_free(void);
  57. int user_open(snac *snac, const char *uid);
  58. void user_free(snac *snac);
  59. xs_list *user_list(void);
  60. int user_open_by_md5(snac *snac, const char *md5);
  61. int user_persist(snac *snac, int publish);
  62. int validate_uid(const char *uid);
  63. xs_str *hash_password(const char *uid, const char *passwd, const char *nonce);
  64. int check_password(const char *uid, const char *passwd, const char *hash);
  65. void srv_archive(const char *direction, const char *url, xs_dict *req,
  66. const char *payload, int p_size,
  67. int status, xs_dict *headers,
  68. const char *body, int b_size);
  69. void srv_archive_error(const char *prefix, const xs_str *err,
  70. const xs_dict *req, const xs_val *data);
  71. void srv_archive_qitem(const char *prefix, xs_dict *q_item);
  72. double mtime_nl(const char *fn, int *n_link);
  73. #define mtime(fn) mtime_nl(fn, NULL)
  74. double f_ctime(const char *fn);
  75. int index_add_md5(const char *fn, const char *md5);
  76. int index_add(const char *fn, const char *id);
  77. int index_gc(const char *fn);
  78. int index_first(const char *fn, char md5[MD5_HEX_SIZE]);
  79. int index_len(const char *fn);
  80. xs_list *index_list(const char *fn, int max);
  81. int index_desc_next(FILE *f, char md5[MD5_HEX_SIZE]);
  82. int index_desc_first(FILE *f, char md5[MD5_HEX_SIZE], int skip);
  83. xs_list *index_list_desc(const char *fn, int skip, int show);
  84. int object_add(const char *id, const xs_dict *obj);
  85. int object_add_ow(const char *id, const xs_dict *obj);
  86. int object_here_by_md5(const char *id);
  87. int object_here(const char *id);
  88. int object_get_by_md5(const char *md5, xs_dict **obj);
  89. int object_get(const char *id, xs_dict **obj);
  90. int object_del(const char *id);
  91. int object_del_if_unref(const char *id);
  92. double object_ctime_by_md5(const char *md5);
  93. double object_ctime(const char *id);
  94. double object_mtime_by_md5(const char *md5);
  95. double object_mtime(const char *id);
  96. void object_touch(const char *id);
  97. int object_admire(const char *id, const char *actor, int like);
  98. int object_unadmire(const char *id, const char *actor, int like);
  99. int object_likes_len(const char *id);
  100. int object_announces_len(const char *id);
  101. xs_list *object_children(const char *id);
  102. xs_list *object_likes(const char *id);
  103. xs_list *object_announces(const char *id);
  104. int object_parent(const char *md5, char parent[MD5_HEX_SIZE]);
  105. int object_user_cache_add(snac *snac, const char *id, const char *cachedir);
  106. int object_user_cache_del(snac *snac, const char *id, const char *cachedir);
  107. int follower_add(snac *snac, const char *actor);
  108. int follower_del(snac *snac, const char *actor);
  109. int follower_check(snac *snac, const char *actor);
  110. xs_list *follower_list(snac *snac);
  111. double timeline_mtime(snac *snac);
  112. int timeline_touch(snac *snac);
  113. int timeline_here(snac *snac, const char *md5);
  114. int timeline_get_by_md5(snac *snac, const char *md5, xs_dict **msg);
  115. int timeline_del(snac *snac, const char *id);
  116. xs_str *user_index_fn(snac *user, const char *idx_name);
  117. xs_list *timeline_simple_list(snac *user, const char *idx_name, int skip, int show);
  118. xs_list *timeline_list(snac *snac, const char *idx_name, int skip, int show);
  119. int timeline_add(snac *snac, const char *id, const xs_dict *o_msg);
  120. int timeline_admire(snac *snac, const char *id, const char *admirer, int like);
  121. xs_list *timeline_top_level(snac *snac, const xs_list *list);
  122. xs_list *local_list(snac *snac, int max);
  123. xs_str *instance_index_fn(void);
  124. xs_list *timeline_instance_list(int skip, int show);
  125. int following_add(snac *snac, const char *actor, const xs_dict *msg);
  126. int following_del(snac *snac, const char *actor);
  127. int following_check(snac *snac, const char *actor);
  128. int following_get(snac *snac, const char *actor, xs_dict **data);
  129. xs_list *following_list(snac *snac);
  130. void mute(snac *snac, const char *actor);
  131. void unmute(snac *snac, const char *actor);
  132. int is_muted(snac *snac, const char *actor);
  133. xs_list *muted_list(snac *user);
  134. int is_bookmarked(snac *user, const char *id);
  135. int bookmark(snac *user, const char *id);
  136. int unbookmark(snac *user, const char *id);
  137. xs_list *bookmark_list(snac *user);
  138. xs_str *bookmark_index_fn(snac *user);
  139. int pin(snac *user, const char *id);
  140. int unpin(snac *user, const char *id);
  141. int is_pinned(snac *user, const char *id);
  142. int is_pinned_by_md5(snac *user, const char *md5);
  143. xs_list *pinned_list(snac *user);
  144. int is_draft(snac *user, const char *id);
  145. void draft_del(snac *user, const char *id);
  146. void draft_add(snac *user, const char *id, const xs_dict *msg);
  147. xs_list *draft_list(snac *user);
  148. int limited(snac *user, const char *id, int cmd);
  149. #define is_limited(user, id) limited((user), (id), 0)
  150. #define limit(user, id) limited((user), (id), 1)
  151. #define unlimit(user, id) limited((user), (id), 2)
  152. void hide(snac *snac, const char *id);
  153. int is_hidden(snac *snac, const char *id);
  154. void tag_index(const char *id, const xs_dict *obj);
  155. xs_str *tag_fn(const char *tag);
  156. xs_list *tag_search(const char *tag, int skip, int show);
  157. xs_val *list_maint(snac *user, const char *list, int op);
  158. xs_str *list_timeline_fn(snac *user, const char *list);
  159. xs_list *list_timeline(snac *user, const char *list, int skip, int show);
  160. xs_val *list_content(snac *user, const char *list_id, const char *actor_md5, int op);
  161. void list_distribute(snac *user, const char *who, const xs_dict *post);
  162. int actor_add(const char *actor, const xs_dict *msg);
  163. int actor_get(const char *actor, xs_dict **data);
  164. int actor_get_refresh(snac *user, const char *actor, xs_dict **data);
  165. int static_get(snac *snac, const char *id, xs_val **data, int *size, const char *inm, xs_str **etag);
  166. void static_put(snac *snac, const char *id, const char *data, int size);
  167. void static_put_meta(snac *snac, const char *id, const char *str);
  168. xs_str *static_get_meta(snac *snac, const char *id);
  169. double history_mtime(snac *snac, const char *id);
  170. void history_add(snac *snac, const char *id, const char *content, int size,
  171. xs_str **etag);
  172. int history_get(snac *snac, const char *id, xs_str **content, int *size,
  173. const char *inm, xs_str **etag);
  174. int history_del(snac *snac, const char *id);
  175. xs_list *history_list(snac *snac);
  176. void lastlog_write(snac *snac, const char *source);
  177. xs_str *notify_check_time(snac *snac, int reset);
  178. void notify_add(snac *snac, const char *type, const char *utype,
  179. const char *actor, const char *objid, const xs_dict *msg);
  180. xs_dict *notify_get(snac *snac, const char *id);
  181. int notify_new_num(snac *snac);
  182. xs_list *notify_list(snac *snac, int skip, int show);
  183. void notify_clear(snac *snac);
  184. void inbox_add(const char *inbox);
  185. void inbox_add_by_actor(const xs_dict *actor);
  186. xs_list *inbox_list(void);
  187. int is_instance_blocked(const char *instance);
  188. int instance_block(const char *instance);
  189. int instance_unblock(const char *instance);
  190. int content_match(const char *file, const xs_dict *msg);
  191. xs_list *content_search(snac *user, const char *regex,
  192. int priv, int skip, int show, int max_secs, int *timeout);
  193. void enqueue_input(snac *snac, const xs_dict *msg, const xs_dict *req, int retries);
  194. void enqueue_shared_input(const xs_dict *msg, const xs_dict *req, int retries);
  195. void enqueue_output_raw(const char *keyid, const char *seckey,
  196. const xs_dict *msg, const xs_str *inbox,
  197. int retries, int p_status);
  198. void enqueue_output(snac *snac, const xs_dict *msg,
  199. const xs_str *inbox, int retries, int p_status);
  200. void enqueue_output_by_actor(snac *snac, const xs_dict *msg,
  201. const xs_str *actor, int retries);
  202. void enqueue_email(const xs_str *msg, int retries);
  203. void enqueue_telegram(const xs_str *msg, const char *bot, const char *chat_id);
  204. void enqueue_ntfy(const xs_str *msg, const char *ntfy_server, const char *ntfy_token);
  205. void enqueue_message(snac *snac, const xs_dict *msg);
  206. void enqueue_close_question(snac *user, const char *id, int end_secs);
  207. void enqueue_object_request(snac *user, const char *id, int forward_secs);
  208. void enqueue_verify_links(snac *user);
  209. void enqueue_actor_refresh(snac *user, const char *actor, int forward_secs);
  210. int was_question_voted(snac *user, const char *id);
  211. xs_list *user_queue(snac *snac);
  212. xs_list *queue(void);
  213. xs_dict *queue_get(const char *fn);
  214. xs_dict *dequeue(const char *fn);
  215. void purge(snac *snac);
  216. void purge_all(void);
  217. xs_dict *http_signed_request_raw(const char *keyid, const char *seckey,
  218. const char *method, const char *url,
  219. const xs_dict *headers,
  220. const char *body, int b_size,
  221. int *status, xs_str **payload, int *p_size,
  222. int timeout);
  223. xs_dict *http_signed_request(snac *snac, const char *method, const char *url,
  224. const xs_dict *headers,
  225. const char *body, int b_size,
  226. int *status, xs_str **payload, int *p_size,
  227. int timeout);
  228. int check_signature(const xs_dict *req, xs_str **err);
  229. srv_state *srv_state_op(xs_str **fname, int op);
  230. void httpd(void);
  231. int webfinger_request_signed(snac *snac, const char *qs, xs_str **actor, xs_str **user);
  232. int webfinger_request(const char *qs, xs_str **actor, xs_str **user);
  233. int webfinger_request_fake(const char *qs, xs_str **actor, xs_str **user);
  234. int webfinger_get_handler(xs_dict *req, const char *q_path,
  235. xs_val **body, int *b_size, char **ctype);
  236. const char *default_avatar_base64(void);
  237. xs_str *process_tags(snac *snac, const char *content, xs_list **tag);
  238. const char *get_atto(const xs_dict *msg);
  239. const char *get_in_reply_to(const xs_dict *msg);
  240. xs_list *get_attachments(const xs_dict *msg);
  241. xs_dict *msg_admiration(snac *snac, const char *object, const char *type);
  242. xs_dict *msg_repulsion(snac *user, const char *id, const char *type);
  243. xs_dict *msg_create(snac *snac, const xs_dict *object);
  244. xs_dict *msg_follow(snac *snac, const char *actor);
  245. xs_dict *msg_note(snac *snac, const xs_str *content, const xs_val *rcpts,
  246. const xs_str *in_reply_to, const xs_list *attach, int priv);
  247. xs_dict *msg_undo(snac *snac, const xs_val *object);
  248. xs_dict *msg_delete(snac *snac, const char *id);
  249. xs_dict *msg_actor(snac *snac);
  250. xs_dict *msg_update(snac *snac, const xs_dict *object);
  251. xs_dict *msg_ping(snac *user, const char *rcpt);
  252. xs_dict *msg_pong(snac *user, const char *rcpt, const char *object);
  253. xs_dict *msg_move(snac *user, const char *new_account);
  254. xs_dict *msg_question(snac *user, const char *content, xs_list *attach,
  255. const xs_list *opts, int multiple, int end_secs);
  256. int activitypub_request(snac *snac, const char *url, xs_dict **data);
  257. int actor_request(snac *user, const char *actor, xs_dict **data);
  258. int send_to_inbox_raw(const char *keyid, const char *seckey,
  259. const xs_str *inbox, const xs_dict *msg,
  260. xs_val **payload, int *p_size, int timeout);
  261. int send_to_inbox(snac *snac, const xs_str *inbox, const xs_dict *msg,
  262. xs_val **payload, int *p_size, int timeout);
  263. xs_str *get_actor_inbox(const char *actor, int shared);
  264. int send_to_actor(snac *snac, const char *actor, const xs_dict *msg,
  265. xs_val **payload, int *p_size, int timeout);
  266. int is_msg_public(const xs_dict *msg);
  267. int is_msg_from_private_user(const xs_dict *msg);
  268. int is_msg_for_me(snac *snac, const xs_dict *msg);
  269. int process_user_queue(snac *snac);
  270. void process_queue_item(xs_dict *q_item);
  271. int process_queue(void);
  272. int activitypub_get_handler(const xs_dict *req, const char *q_path,
  273. char **body, int *b_size, char **ctype);
  274. int activitypub_post_handler(const xs_dict *req, const char *q_path,
  275. char *payload, int p_size,
  276. char **body, int *b_size, char **ctype);
  277. xs_dict *emojis(void);
  278. xs_str *not_really_markdown(const char *content, xs_list **attach, xs_list **tag);
  279. xs_str *sanitize(const char *content);
  280. xs_str *encode_html(const char *str);
  281. xs_str *html_timeline(snac *user, const xs_list *list, int read_only,
  282. int skip, int show, int show_more,
  283. const char *title, const char *page, int utl, const char *error);
  284. int html_get_handler(const xs_dict *req, const char *q_path,
  285. char **body, int *b_size, char **ctype,
  286. xs_str **etag, xs_str **last_modified);
  287. int html_post_handler(const xs_dict *req, const char *q_path,
  288. char *payload, int p_size,
  289. char **body, int *b_size, char **ctype);
  290. xs_str *timeline_to_rss(snac *user, const xs_list *timeline,
  291. const char *title, const char *link, const char *desc);
  292. int write_default_css(void);
  293. int snac_init(const char *_basedir);
  294. int adduser(const char *uid);
  295. int resetpwd(snac *snac);
  296. int deluser(snac *user);
  297. extern const char *snac_blurb;
  298. void job_post(const xs_val *job, int urgent);
  299. void job_wait(xs_val **job);
  300. int oauth_get_handler(const xs_dict *req, const char *q_path,
  301. char **body, int *b_size, char **ctype);
  302. int oauth_post_handler(const xs_dict *req, const char *q_path,
  303. const char *payload, int p_size,
  304. char **body, int *b_size, char **ctype);
  305. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  306. char **body, int *b_size, char **ctype);
  307. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  308. const char *payload, int p_size,
  309. char **body, int *b_size, char **ctype);
  310. int mastoapi_delete_handler(const xs_dict *req, const char *q_path,
  311. const char *payload, int p_size,
  312. char **body, int *b_size, char **ctype);
  313. int mastoapi_put_handler(const xs_dict *req, const char *q_path,
  314. const char *payload, int p_size,
  315. char **body, int *b_size, char **ctype);
  316. void persist_image(const char *key, const xs_val *data, const char *payload, snac *snac);
  317. int mastoapi_patch_handler(const xs_dict *req, const char *q_path,
  318. const char *payload, int p_size,
  319. char **body, int *b_size, char **ctype);
  320. void mastoapi_purge(void);
  321. void verify_links(snac *user);
  322. void export_csv(snac *user);
  323. int migrate_account(snac *user);
  324. void import_csv(snac *user);
  325. typedef enum {
  326. #define HTTP_STATUS(code, name, text) HTTP_STATUS_ ## name = code,
  327. #include "http_codes.h"
  328. #undef HTTP_STATUS
  329. } http_status;
  330. const char *http_status_text(int status);
  331. typedef struct {
  332. double timestamp;
  333. char *text;
  334. } t_announcement;
  335. t_announcement *announcement(double after);
  336. xs_str *make_url(const char *href, const char *proxy, int by_token);