snac.h 15 KB

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