snac.h 17 KB

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