snac.h 18 KB

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