snac.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
  3. #define VERSION "2.68-dev"
  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|Audio|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. void sbox_enter(const char *basedir);
  58. int user_open(snac *snac, const char *uid);
  59. void user_free(snac *snac);
  60. xs_list *user_list(void);
  61. int user_open_by_md5(snac *snac, const char *md5);
  62. int user_persist(snac *snac, int publish);
  63. int validate_uid(const char *uid);
  64. xs_str *hash_password(const char *uid, const char *passwd, const char *nonce);
  65. int check_password(const char *uid, const char *passwd, const char *hash);
  66. void srv_archive(const char *direction, const char *url, xs_dict *req,
  67. const char *payload, int p_size,
  68. int status, xs_dict *headers,
  69. const char *body, int b_size);
  70. void srv_archive_error(const char *prefix, const xs_str *err,
  71. const xs_dict *req, const xs_val *data);
  72. void srv_archive_qitem(const char *prefix, xs_dict *q_item);
  73. double mtime_nl(const char *fn, int *n_link);
  74. #define mtime(fn) mtime_nl(fn, NULL)
  75. double f_ctime(const char *fn);
  76. int index_add_md5(const char *fn, const char *md5);
  77. int index_add(const char *fn, const char *id);
  78. int index_gc(const char *fn);
  79. int index_first(const char *fn, char md5[MD5_HEX_SIZE]);
  80. int index_len(const char *fn);
  81. xs_list *index_list(const char *fn, int max);
  82. int index_desc_next(FILE *f, char md5[MD5_HEX_SIZE]);
  83. int index_desc_first(FILE *f, char md5[MD5_HEX_SIZE], int skip);
  84. xs_list *index_list_desc(const char *fn, int skip, int show);
  85. int object_add(const char *id, const xs_dict *obj);
  86. int object_add_ow(const char *id, const xs_dict *obj);
  87. int object_here_by_md5(const char *id);
  88. int object_here(const char *id);
  89. int object_get_by_md5(const char *md5, xs_dict **obj);
  90. int object_get(const char *id, xs_dict **obj);
  91. int object_del(const char *id);
  92. int object_del_if_unref(const char *id);
  93. double object_ctime_by_md5(const char *md5);
  94. double object_ctime(const char *id);
  95. double object_mtime_by_md5(const char *md5);
  96. double object_mtime(const char *id);
  97. void object_touch(const char *id);
  98. int object_admire(const char *id, const char *actor, int like);
  99. int object_unadmire(const char *id, const char *actor, int like);
  100. int object_likes_len(const char *id);
  101. int object_announces_len(const char *id);
  102. xs_list *object_children(const char *id);
  103. xs_list *object_likes(const char *id);
  104. xs_list *object_announces(const char *id);
  105. int object_parent(const char *md5, char parent[MD5_HEX_SIZE]);
  106. int object_user_cache_add(snac *snac, const char *id, const char *cachedir);
  107. int object_user_cache_del(snac *snac, const char *id, const char *cachedir);
  108. int follower_add(snac *snac, const char *actor);
  109. int follower_del(snac *snac, const char *actor);
  110. int follower_check(snac *snac, const char *actor);
  111. xs_list *follower_list(snac *snac);
  112. int pending_add(snac *user, const char *actor, const xs_dict *msg);
  113. int pending_check(snac *user, const char *actor);
  114. xs_dict *pending_get(snac *user, const char *actor);
  115. void pending_del(snac *user, const char *actor);
  116. xs_list *pending_list(snac *user);
  117. double timeline_mtime(snac *snac);
  118. int timeline_touch(snac *snac);
  119. int timeline_here(snac *snac, const char *md5);
  120. int timeline_get_by_md5(snac *snac, const char *md5, xs_dict **msg);
  121. int timeline_del(snac *snac, const char *id);
  122. xs_str *user_index_fn(snac *user, const char *idx_name);
  123. xs_list *timeline_simple_list(snac *user, const char *idx_name, int skip, int show);
  124. xs_list *timeline_list(snac *snac, const char *idx_name, int skip, int show);
  125. int timeline_add(snac *snac, const char *id, const xs_dict *o_msg);
  126. int timeline_admire(snac *snac, const char *id, const char *admirer, int like);
  127. xs_list *timeline_top_level(snac *snac, const xs_list *list);
  128. xs_list *local_list(snac *snac, int max);
  129. xs_str *instance_index_fn(void);
  130. xs_list *timeline_instance_list(int skip, int show);
  131. int following_add(snac *snac, const char *actor, const xs_dict *msg);
  132. int following_del(snac *snac, const char *actor);
  133. int following_check(snac *snac, const char *actor);
  134. int following_get(snac *snac, const char *actor, xs_dict **data);
  135. xs_list *following_list(snac *snac);
  136. void mute(snac *snac, const char *actor);
  137. void unmute(snac *snac, const char *actor);
  138. int is_muted(snac *snac, const char *actor);
  139. xs_list *muted_list(snac *user);
  140. int is_bookmarked(snac *user, const char *id);
  141. int bookmark(snac *user, const char *id);
  142. int unbookmark(snac *user, const char *id);
  143. xs_list *bookmark_list(snac *user);
  144. xs_str *bookmark_index_fn(snac *user);
  145. int pin(snac *user, const char *id);
  146. int unpin(snac *user, const char *id);
  147. int is_pinned(snac *user, const char *id);
  148. int is_pinned_by_md5(snac *user, const char *md5);
  149. xs_list *pinned_list(snac *user);
  150. int is_draft(snac *user, const char *id);
  151. void draft_del(snac *user, const char *id);
  152. void draft_add(snac *user, const char *id, const xs_dict *msg);
  153. xs_list *draft_list(snac *user);
  154. int limited(snac *user, const char *id, int cmd);
  155. #define is_limited(user, id) limited((user), (id), 0)
  156. #define limit(user, id) limited((user), (id), 1)
  157. #define unlimit(user, id) limited((user), (id), 2)
  158. void hide(snac *snac, const char *id);
  159. int is_hidden(snac *snac, const char *id);
  160. void tag_index(const char *id, const xs_dict *obj);
  161. xs_str *tag_fn(const char *tag);
  162. xs_list *tag_search(const char *tag, int skip, int show);
  163. xs_val *list_maint(snac *user, const char *list, int op);
  164. xs_str *list_timeline_fn(snac *user, const char *list);
  165. xs_list *list_timeline(snac *user, const char *list, int skip, int show);
  166. xs_val *list_content(snac *user, const char *list_id, const char *actor_md5, int op);
  167. void list_distribute(snac *user, const char *who, const xs_dict *post);
  168. int actor_add(const char *actor, const xs_dict *msg);
  169. int actor_get(const char *actor, xs_dict **data);
  170. int actor_get_refresh(snac *user, const char *actor, xs_dict **data);
  171. int static_get(snac *snac, const char *id, xs_val **data, int *size, const char *inm, xs_str **etag);
  172. void static_put(snac *snac, const char *id, const char *data, int size);
  173. void static_put_meta(snac *snac, const char *id, const char *str);
  174. xs_str *static_get_meta(snac *snac, const char *id);
  175. double history_mtime(snac *snac, const char *id);
  176. void history_add(snac *snac, const char *id, const char *content, int size,
  177. xs_str **etag);
  178. int history_get(snac *snac, const char *id, xs_str **content, int *size,
  179. const char *inm, xs_str **etag);
  180. int history_del(snac *snac, const char *id);
  181. xs_list *history_list(snac *snac);
  182. void lastlog_write(snac *snac, const char *source);
  183. xs_str *notify_check_time(snac *snac, int reset);
  184. void notify_add(snac *snac, const char *type, const char *utype,
  185. const char *actor, const char *objid, const xs_dict *msg);
  186. xs_dict *notify_get(snac *snac, const char *id);
  187. int notify_new_num(snac *snac);
  188. xs_list *notify_list(snac *snac, int skip, int show);
  189. void notify_clear(snac *snac);
  190. xs_dict *markers_get(snac *snac, const xs_list *markers);
  191. xs_dict *markers_set(snac *snac, const char *home_marker, const char *notify_marker);
  192. void inbox_add(const char *inbox);
  193. void inbox_add_by_actor(const xs_dict *actor);
  194. xs_list *inbox_list(void);
  195. int is_instance_blocked(const char *instance);
  196. int instance_block(const char *instance);
  197. int instance_unblock(const char *instance);
  198. int content_match(const char *file, const xs_dict *msg);
  199. xs_list *content_search(snac *user, const char *regex,
  200. int priv, int skip, int show, int max_secs, int *timeout);
  201. void enqueue_input(snac *snac, const xs_dict *msg, const xs_dict *req, int retries);
  202. void enqueue_shared_input(const xs_dict *msg, const xs_dict *req, int retries);
  203. void enqueue_output_raw(const char *keyid, const char *seckey,
  204. const xs_dict *msg, const xs_str *inbox,
  205. int retries, int p_status);
  206. void enqueue_output(snac *snac, const xs_dict *msg,
  207. const xs_str *inbox, int retries, int p_status);
  208. void enqueue_output_by_actor(snac *snac, const xs_dict *msg,
  209. const xs_str *actor, int retries);
  210. void enqueue_email(const xs_str *msg, int retries);
  211. void enqueue_telegram(const xs_str *msg, const char *bot, const char *chat_id);
  212. void enqueue_ntfy(const xs_str *msg, const char *ntfy_server, const char *ntfy_token);
  213. void enqueue_message(snac *snac, const xs_dict *msg);
  214. void enqueue_close_question(snac *user, const char *id, int end_secs);
  215. void enqueue_object_request(snac *user, const char *id, int forward_secs);
  216. void enqueue_verify_links(snac *user);
  217. void enqueue_actor_refresh(snac *user, const char *actor, int forward_secs);
  218. int was_question_voted(snac *user, const char *id);
  219. xs_list *user_queue(snac *snac);
  220. xs_list *queue(void);
  221. xs_dict *queue_get(const char *fn);
  222. xs_dict *dequeue(const char *fn);
  223. void purge(snac *snac);
  224. void purge_all(void);
  225. xs_dict *http_signed_request_raw(const char *keyid, const char *seckey,
  226. const char *method, const char *url,
  227. const xs_dict *headers,
  228. const char *body, int b_size,
  229. int *status, xs_str **payload, int *p_size,
  230. int timeout);
  231. xs_dict *http_signed_request(snac *snac, const char *method, const char *url,
  232. const xs_dict *headers,
  233. const char *body, int b_size,
  234. int *status, xs_str **payload, int *p_size,
  235. int timeout);
  236. int check_signature(const xs_dict *req, xs_str **err);
  237. srv_state *srv_state_op(xs_str **fname, int op);
  238. void httpd(void);
  239. int webfinger_request_signed(snac *snac, const char *qs, xs_str **actor, xs_str **user);
  240. int webfinger_request(const char *qs, xs_str **actor, xs_str **user);
  241. int webfinger_request_fake(const char *qs, xs_str **actor, xs_str **user);
  242. int webfinger_get_handler(xs_dict *req, const char *q_path,
  243. xs_val **body, int *b_size, char **ctype);
  244. const char *default_avatar_base64(void);
  245. xs_str *process_tags(snac *snac, const char *content, xs_list **tag);
  246. const char *get_atto(const xs_dict *msg);
  247. const char *get_in_reply_to(const xs_dict *msg);
  248. xs_list *get_attachments(const xs_dict *msg);
  249. xs_dict *msg_admiration(snac *snac, const char *object, const char *type);
  250. xs_dict *msg_repulsion(snac *user, const char *id, const char *type);
  251. xs_dict *msg_create(snac *snac, const xs_dict *object);
  252. xs_dict *msg_follow(snac *snac, const char *actor);
  253. xs_dict *msg_note(snac *snac, const xs_str *content, const xs_val *rcpts,
  254. const xs_str *in_reply_to, const xs_list *attach,
  255. int priv, const char *lang);
  256. xs_dict *msg_undo(snac *snac, const xs_val *object);
  257. xs_dict *msg_delete(snac *snac, const char *id);
  258. xs_dict *msg_actor(snac *snac);
  259. xs_dict *msg_update(snac *snac, const xs_dict *object);
  260. xs_dict *msg_ping(snac *user, const char *rcpt);
  261. xs_dict *msg_pong(snac *user, const char *rcpt, const char *object);
  262. xs_dict *msg_move(snac *user, const char *new_account);
  263. xs_dict *msg_accept(snac *snac, const xs_val *object, const char *to);
  264. xs_dict *msg_question(snac *user, const char *content, xs_list *attach,
  265. const xs_list *opts, int multiple, int end_secs);
  266. int activitypub_request(snac *snac, const char *url, xs_dict **data);
  267. int actor_request(snac *user, const char *actor, xs_dict **data);
  268. int send_to_inbox_raw(const char *keyid, const char *seckey,
  269. const xs_str *inbox, const xs_dict *msg,
  270. xs_val **payload, int *p_size, int timeout);
  271. int send_to_inbox(snac *snac, const xs_str *inbox, const xs_dict *msg,
  272. xs_val **payload, int *p_size, int timeout);
  273. xs_str *get_actor_inbox(const char *actor, int shared);
  274. int send_to_actor(snac *snac, const char *actor, const xs_dict *msg,
  275. xs_val **payload, int *p_size, int timeout);
  276. int is_msg_public(const xs_dict *msg);
  277. int is_msg_from_private_user(const xs_dict *msg);
  278. int is_msg_for_me(snac *snac, const xs_dict *msg);
  279. int process_user_queue(snac *snac);
  280. void process_queue_item(xs_dict *q_item);
  281. int process_queue(void);
  282. int activitypub_get_handler(const xs_dict *req, const char *q_path,
  283. char **body, int *b_size, char **ctype);
  284. int activitypub_post_handler(const xs_dict *req, const char *q_path,
  285. char *payload, int p_size,
  286. char **body, int *b_size, char **ctype);
  287. xs_dict *emojis(void);
  288. xs_str *not_really_markdown(const char *content, xs_list **attach, xs_list **tag);
  289. xs_str *sanitize(const char *content);
  290. xs_str *encode_html(const char *str);
  291. xs_str *html_timeline(snac *user, const xs_list *list, int read_only,
  292. int skip, int show, int show_more,
  293. const char *title, const char *page, int utl, const char *error);
  294. int html_get_handler(const xs_dict *req, const char *q_path,
  295. char **body, int *b_size, char **ctype,
  296. xs_str **etag, xs_str **last_modified);
  297. int html_post_handler(const xs_dict *req, const char *q_path,
  298. char *payload, int p_size,
  299. char **body, int *b_size, char **ctype);
  300. xs_str *timeline_to_rss(snac *user, const xs_list *timeline,
  301. const char *title, const char *link, const char *desc);
  302. int write_default_css(void);
  303. int snac_init(const char *_basedir);
  304. int adduser(const char *uid);
  305. int resetpwd(snac *snac);
  306. int deluser(snac *user);
  307. extern const char *snac_blurb;
  308. void job_post(const xs_val *job, int urgent);
  309. void job_wait(xs_val **job);
  310. int oauth_get_handler(const xs_dict *req, const char *q_path,
  311. char **body, int *b_size, char **ctype);
  312. int oauth_post_handler(const xs_dict *req, const char *q_path,
  313. const char *payload, int p_size,
  314. char **body, int *b_size, char **ctype);
  315. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  316. char **body, int *b_size, char **ctype, xs_str **link);
  317. int mastoapi_post_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. int mastoapi_delete_handler(const xs_dict *req, const char *q_path,
  321. const char *payload, int p_size,
  322. char **body, int *b_size, char **ctype);
  323. int mastoapi_put_handler(const xs_dict *req, const char *q_path,
  324. const char *payload, int p_size,
  325. char **body, int *b_size, char **ctype);
  326. void persist_image(const char *key, const xs_val *data, const char *payload, snac *snac);
  327. int mastoapi_patch_handler(const xs_dict *req, const char *q_path,
  328. const char *payload, int p_size,
  329. char **body, int *b_size, char **ctype);
  330. void mastoapi_purge(void);
  331. void verify_links(snac *user);
  332. void export_csv(snac *user);
  333. int migrate_account(snac *user);
  334. void import_blocked_accounts_csv(snac *user, const char *fn);
  335. void import_following_accounts_csv(snac *user, const char *fn);
  336. void import_list_csv(snac *user, const char *fn);
  337. void import_csv(snac *user);
  338. typedef enum {
  339. #define HTTP_STATUS(code, name, text) HTTP_STATUS_ ## name = code,
  340. #include "http_codes.h"
  341. #undef HTTP_STATUS
  342. } http_status;
  343. const char *http_status_text(int status);
  344. typedef struct {
  345. double timestamp;
  346. char *text;
  347. } t_announcement;
  348. t_announcement *announcement(double after);
  349. xs_str *make_url(const char *href, const char *proxy, int by_token);
  350. int badlogin_check(const char *user, const char *addr);
  351. void badlogin_inc(const char *user, const char *addr);