diff options
| author | 2022-10-04 08:51:24 +0200 | |
|---|---|---|
| committer | 2022-10-04 08:51:24 +0200 | |
| commit | 4bb40857b23a57c5be581a5f33591954e2298344 (patch) | |
| tree | 476205329eec92ff7ded1af31cb0e75ec517fb75 | |
| parent | Updated TODO. (diff) | |
| download | snac2-4bb40857b23a57c5be581a5f33591954e2298344.tar.gz snac2-4bb40857b23a57c5be581a5f33591954e2298344.tar.xz snac2-4bb40857b23a57c5be581a5f33591954e2298344.zip | |
New function initdb().
| -rw-r--r-- | Makefile | 3 | ||||
| -rw-r--r-- | main.c | 2 | ||||
| -rw-r--r-- | snac.h | 2 | ||||
| -rw-r--r-- | utils.c | 174 |
4 files changed, 180 insertions, 1 deletions
| @@ -2,7 +2,7 @@ CFLAGS=-g -Wall | |||
| 2 | 2 | ||
| 3 | all: snac | 3 | all: snac |
| 4 | 4 | ||
| 5 | snac: snac.o main.o data.o http.o httpd.o webfinger.o activitypub.o html.o | 5 | snac: snac.o main.o data.o http.o httpd.o webfinger.o activitypub.o html.o utils.o |
| 6 | $(CC) -L/usr/local/lib *.o -lcurl -lcrypto -pthread -o $@ | 6 | $(CC) -L/usr/local/lib *.o -lcurl -lcrypto -pthread -o $@ |
| 7 | 7 | ||
| 8 | .c.o: | 8 | .c.o: |
| @@ -27,4 +27,5 @@ main.o: main.c xs.h xs_io.h xs_encdec.h xs_json.h snac.h | |||
| 27 | snac.o: snac.c xs.h xs_io.h xs_encdec.h xs_json.h xs_curl.h xs_openssl.h \ | 27 | snac.o: snac.c xs.h xs_io.h xs_encdec.h xs_json.h xs_curl.h xs_openssl.h \ |
| 28 | xs_socket.h xs_httpd.h xs_mime.h xs_regex.h xs_set.h xs_time.h xs_glob.h \ | 28 | xs_socket.h xs_httpd.h xs_mime.h xs_regex.h xs_set.h xs_time.h xs_glob.h \ |
| 29 | snac.h | 29 | snac.h |
| 30 | utils.o: utils.c xs.h xs_io.h xs_encdec.h xs_json.h snac.h | ||
| 30 | webfinger.o: webfinger.c xs.h xs_encdec.h xs_json.h xs_curl.h snac.h | 31 | webfinger.o: webfinger.c xs.h xs_encdec.h xs_json.h xs_curl.h snac.h |
| @@ -71,6 +71,8 @@ int main(int argc, char *argv[]) | |||
| 71 | /* ... */ | 71 | /* ... */ |
| 72 | basedir = GET_ARGV(); | 72 | basedir = GET_ARGV(); |
| 73 | 73 | ||
| 74 | initdb(basedir); | ||
| 75 | |||
| 74 | return 0; | 76 | return 0; |
| 75 | } | 77 | } |
| 76 | 78 | ||
| @@ -132,3 +132,5 @@ d_char *not_really_markdown(char *content, d_char **f_content); | |||
| 132 | int html_get_handler(d_char *req, char *q_path, char **body, int *b_size, char **ctype); | 132 | int html_get_handler(d_char *req, char *q_path, char **body, int *b_size, char **ctype); |
| 133 | int html_post_handler(d_char *req, char *q_path, d_char *payload, int p_size, | 133 | int html_post_handler(d_char *req, char *q_path, d_char *payload, int p_size, |
| 134 | char **body, int *b_size, char **ctype); | 134 | char **body, int *b_size, char **ctype); |
| 135 | |||
| 136 | int initdb(const char *_basedir); | ||
| @@ -0,0 +1,174 @@ | |||
| 1 | /* snac - A simple, minimalistic ActivityPub instance */ | ||
| 2 | /* copyright (c) 2022 grunfink - MIT license */ | ||
| 3 | |||
| 4 | #include "xs.h" | ||
| 5 | #include "xs_io.h" | ||
| 6 | #include "xs_encdec.h" | ||
| 7 | #include "xs_json.h" | ||
| 8 | |||
| 9 | #include "snac.h" | ||
| 10 | |||
| 11 | #include <sys/stat.h> | ||
| 12 | |||
| 13 | const char *default_srv_config = "{" | ||
| 14 | "\"host\": \"\"," | ||
| 15 | "\"prefix\": \"\"," | ||
| 16 | "\"address\": \"127.0.0.1\"," | ||
| 17 | "\"port\": 8001," | ||
| 18 | "\"layout\": 2," | ||
| 19 | "\"dbglevel\": 0," | ||
| 20 | "\"queue_retry_minutes\": 2," | ||
| 21 | "\"queue_retry_max\": 10," | ||
| 22 | "\"cssurls\": [\"\"]," | ||
| 23 | "\"max_timeline_entries\": 256," | ||
| 24 | "\"timeline_purge_days\": 120" | ||
| 25 | "}"; | ||
| 26 | |||
| 27 | const char *default_css = | ||
| 28 | "body { max-width: 48em; margin: auto; line-height: 1.5; padding: 0.8em }\n" | ||
| 29 | "img { max-width: 100% }\n" | ||
| 30 | ".snac-origin { font-size: 85% }\n" | ||
| 31 | ".snac-top-user { text-align: center; padding-bottom: 2em }\n" | ||
| 32 | ".snac-top-user-name { font-size: 200% }\n" | ||
| 33 | ".snac-top-user-id { font-size: 150% }\n" | ||
| 34 | ".snac-avatar { float: left; height: 2.5em; padding: 0.25em }\n" | ||
| 35 | ".snac-author { font-size: 90% }\n" | ||
| 36 | ".snac-pubdate { color: #a0a0a0; font-size: 90% }\n" | ||
| 37 | ".snac-top-controls { padding-bottom: 1.5em }\n" | ||
| 38 | ".snac-post { border-top: 1px solid #a0a0a0; }\n" | ||
| 39 | ".snac-children { padding-left: 2em; border-left: 1px solid #a0a0a0; }\n" | ||
| 40 | ".snac-textarea { font-family: inherit; width: 100% }\n" | ||
| 41 | ".snac-history { border: 1px solid #606060; border-radius: 3px; margin: 2.5em 0; padding: 0 2em }\n" | ||
| 42 | ".snac-btn-mute { float: right; margin-left: 0.5em }\n" | ||
| 43 | ".snac-btn-follow { float: right; margin-left: 0.5em }\n" | ||
| 44 | ".snac-btn-unfollow { float: right; margin-left: 0.5em }\n" | ||
| 45 | ".snac-btn-delete { float: right; margin-left: 0.5em }\n" | ||
| 46 | ".snac-footer { margin-top: 2em; font-size: 75% }\n"; | ||
| 47 | |||
| 48 | const char *greeting_html = | ||
| 49 | "<!DOCTYPE html>\n" | ||
| 50 | "<html><head>\n" | ||
| 51 | "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/>\n" | ||
| 52 | "<title>Welcome to %host%</title>\n" | ||
| 53 | "<body style=\"margin: auto; max-width: 50em\">\n" | ||
| 54 | "<h1>Welcome to %host%</h1>\n" | ||
| 55 | "<p>This is a <a href=\"https://en.wikipedia.org/wiki/Fediverse\">Fediverse</a> instance\n" | ||
| 56 | "that uses the <a href=\"https://en.wikipedia.org/wiki/ActivityPub\">ActivityPub</a> protocol.\n" | ||
| 57 | "In other words, users at this host can communicate with people that use software like\n" | ||
| 58 | "Mastodon, Pleroma, Friendica, etc. all around the world.</p>\n" | ||
| 59 | "\n" | ||
| 60 | "<p>There is no automatic sign up process for this server. If you want to be a part of\n" | ||
| 61 | "this community, please write an email to\n" | ||
| 62 | "\n" | ||
| 63 | "the administrator of this instance\n" | ||
| 64 | "\n" | ||
| 65 | "and ask politely indicating what is your preferred user id (alphanumeric characters\n" | ||
| 66 | "only) and the full name you want to appear as.</p>\n" | ||
| 67 | "\n" | ||
| 68 | "<p>The following users are already part of this community:</p>\n" | ||
| 69 | "\n" | ||
| 70 | "%userlist%\n" | ||
| 71 | "\n" | ||
| 72 | "<p>This site is powered by <abbr title=\"Social Networks Are Crap\">snac</abbr>.</p>\n" | ||
| 73 | "</body></html>\n"; | ||
| 74 | |||
| 75 | int initdb(const char *basedir) | ||
| 76 | { | ||
| 77 | FILE *f; | ||
| 78 | |||
| 79 | if (basedir == NULL) { | ||
| 80 | printf("Base directory:\n"); | ||
| 81 | srv_basedir = xs_strip(xs_readline(stdin)); | ||
| 82 | } | ||
| 83 | else | ||
| 84 | srv_basedir = xs_str_new(basedir); | ||
| 85 | |||
| 86 | if (srv_basedir == NULL || *srv_basedir == '\0') | ||
| 87 | return 1; | ||
| 88 | |||
| 89 | if (xs_endswith(srv_basedir, "/")) | ||
| 90 | srv_basedir = xs_crop(srv_basedir, 0, -1); | ||
| 91 | |||
| 92 | if (mtime(srv_basedir) != 0.0) { | ||
| 93 | printf("ERROR: directory '%s' must not exist\n", srv_basedir); | ||
| 94 | return 1; | ||
| 95 | } | ||
| 96 | |||
| 97 | srv_config = xs_json_loads(default_srv_config); | ||
| 98 | |||
| 99 | printf("Network address [%s]:\n", xs_dict_get(srv_config, "address")); | ||
| 100 | { | ||
| 101 | xs *i = xs_strip(xs_readline(stdin)); | ||
| 102 | if (*i) | ||
| 103 | srv_config = xs_dict_set(srv_config, "address", i); | ||
| 104 | } | ||
| 105 | |||
| 106 | printf("Network port [%d]:\n", (int)xs_number_get(xs_dict_get(srv_config, "port"))); | ||
| 107 | { | ||
| 108 | xs *i = xs_strip(xs_readline(stdin)); | ||
| 109 | if (*i) { | ||
| 110 | xs *n = xs_number_new(atoi(i)); | ||
| 111 | srv_config = xs_dict_set(srv_config, "port", i); | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | printf("Host name:\n"); | ||
| 116 | { | ||
| 117 | xs *i = xs_strip(xs_readline(stdin)); | ||
| 118 | if (*i == '\0') | ||
| 119 | return 1; | ||
| 120 | |||
| 121 | srv_config = xs_dict_set(srv_config, "host", i); | ||
| 122 | } | ||
| 123 | |||
| 124 | printf("URL prefix:\n"); | ||
| 125 | { | ||
| 126 | xs *i = xs_strip(xs_readline(stdin)); | ||
| 127 | |||
| 128 | if (*i) { | ||
| 129 | if (xs_endswith(i, "/")) | ||
| 130 | i = xs_crop(i, 0, -1); | ||
| 131 | |||
| 132 | srv_config = xs_dict_set(srv_config, "prefix", i); | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | if (mkdir(srv_basedir, 0755) == -1) { | ||
| 137 | printf("ERROR: cannot create directory '%s'\n", srv_basedir); | ||
| 138 | return 1; | ||
| 139 | } | ||
| 140 | |||
| 141 | xs *udir = xs_fmt("%s/user", srv_basedir); | ||
| 142 | mkdir(udir, 0755); | ||
| 143 | |||
| 144 | xs *gfn = xs_fmt("%s/greeting.html", srv_basedir); | ||
| 145 | if ((f = fopen(gfn, "w")) == NULL) { | ||
| 146 | printf("ERROR: cannot create '%s'\n", gfn); | ||
| 147 | return 1; | ||
| 148 | } | ||
| 149 | |||
| 150 | fwrite(greeting_html, strlen(greeting_html), 1, f); | ||
| 151 | fclose(f); | ||
| 152 | |||
| 153 | xs *sfn = xs_fmt("%s/style.css", srv_basedir); | ||
| 154 | if ((f = fopen(sfn, "w")) == NULL) { | ||
| 155 | printf("ERROR: cannot create '%s'\n", sfn); | ||
| 156 | return 1; | ||
| 157 | } | ||
| 158 | |||
| 159 | fwrite(default_css, strlen(default_css), 1, f); | ||
| 160 | fclose(f); | ||
| 161 | |||
| 162 | xs *cfn = xs_fmt("%s/server.json", srv_basedir); | ||
| 163 | if ((f = fopen(cfn, "w")) == NULL) { | ||
| 164 | printf("ERROR: cannot create '%s'\n", cfn); | ||
| 165 | return 1; | ||
| 166 | } | ||
| 167 | |||
| 168 | xs *j = xs_json_dumps_pp(srv_config, 4); | ||
| 169 | fwrite(j, strlen(j), 1, f); | ||
| 170 | fclose(f); | ||
| 171 | |||
| 172 | printf("Done.\n"); | ||
| 173 | return 0; | ||
| 174 | } | ||