summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2019-07-19 21:14:51 -0400
committerJakob Kaivo <jkk@ung.org>2019-07-19 21:14:51 -0400
commit339cd70e8b9864a3d575a538112f9a7100b1a750 (patch)
treea2ca784a572c803f4a5d2d8f96041ef58876e007
parent8197bb3617521d63943ac09db969bd7d66e6f9f7 (diff)
remove hard-coding of my name and email address from postings
-rw-r--r--blog.c43
-rw-r--r--blog.h8
-rw-r--r--index.c14
3 files changed, 49 insertions, 16 deletions
diff --git a/blog.c b/blog.c
index fc7a8d5..263cb14 100644
--- a/blog.c
+++ b/blog.c
@@ -20,17 +20,48 @@ int current_year(void)
return year;
}
+void readfile(const char *path, char *buf, size_t n)
+{
+ FILE *f = fopen(path, "r");
+ fgets(buf, n, f);
+ fclose(f);
+ size_t len = strlen(buf);
+ if (buf[len-1] == '\n') {
+ buf[len-1] = '\0';
+ }
+}
+
+char *user_name(const char *user)
+{
+ static char name[BUFSIZ] = {0};
+ char path[FILENAME_MAX];
+ snprintf(path, sizeof(path), "/%s/.name", user);
+ readfile(path, name, sizeof(name));
+ return name;
+}
+
+char *user_email(const char *user)
+{
+ static char email[BUFSIZ] = {0};
+ char path[FILENAME_MAX];
+ snprintf(path, sizeof(path), "/%s/.email", user);
+ readfile(path, email, sizeof(email));
+ return email;
+}
+
int handle_post(void)
{
read_post_data();
- if (!authenticate(find_post_data("username"), find_post_data("password"))) {
+ char *user = find_post_data("username");
+ if (!authenticate(user, find_post_data("password"))) {
printf("Status 403 Forbidden\r\n");
printf("Content-Type: text/plain\r\n\r\n");
puts("Incorrect username or password. Go back and try again.");
return 0;
}
- int blogdir = open(DATA_DIRECTORY, O_DIRECTORY);
+ chdir("/");
+ int blogdir = open(user, O_DIRECTORY);
if (blogdir == -1) {
return 1;
}
@@ -86,12 +117,12 @@ int handle_post(void)
if (write(newpost, body, strlen(body)) != strlen(body)) {
return 1;
}
- dprintf(newpost, HTML_TAIL, current_year());
+ dprintf(newpost, HTML_TAIL, current_year(), user_name(user), user_email(user));
close(newpost);
close(blogdir);
- add_to_index(uri, title);
+ add_to_index(user, uri, title);
printf("Status: 302 Found\r\n");
printf("Location: http%s://%s/%s\r\n\r\n", getenv("HTTPS") ? "s" : "", getenv("HTTP_HOST"), uri);
@@ -114,11 +145,11 @@ int main(void)
puts("<input type=\"text\" name=\"username\" placeholder=\"username\" size=\"80\"><br>");
puts("<input type=\"password\" name=\"password\" placeholder=\"password\" size=\"80\"><br>");
puts("<input type=\"text\" name=\"title\" placeholder=\"title\" size=\"80\"><br>");
- puts("<textarea name=\"body\" cols=\"80\" rows=\"24\"></textarea><br>");
+ puts("<textarea name=\"body\" cols=\"80\" rows=\"24\"><p></p></textarea><br>");
puts("<input type=\"submit\">");
puts("</form>");
- printf(HTML_TAIL, current_year());
+ printf(HTML_TAIL, current_year(), "Jakob Kaivo", "jakob@kaivo.net");
return 0;
}
diff --git a/blog.h b/blog.h
index 590c18b..85bf490 100644
--- a/blog.h
+++ b/blog.h
@@ -2,7 +2,6 @@
#define BLOG_H
#define PASSWORD_FILE "/blog/password"
-#define DATA_DIRECTORY "/jakob"
#define DOCTYPE "<!DOCTYPE html>\n"
#define HTML "<html lang=\"en\">\n"
@@ -14,20 +13,23 @@
#define HTML_HEAD DOCTYPE HTML META TITLE ICON STYLE BODY
-#define ADDRESS "<address>Copyright &copy; %d <a href=\"/\">Jakob Kaivo</a> &lt;<a href=\"mailto:jakob@kaivo.net\">jakob@kaivo.net</a>&gt;\n"
+#define ADDRESS "<address>Copyright &copy; %1$d <a href=\"/\">%2$s</a> &lt;<a href=\"mailto:%3$s\">%3$s</a>&gt;\n"
#define HTML_TAIL "\n" ADDRESS "</body>\n</html>\n"
void read_post_data(void);
char *find_post_data(char *key);
+char *user_name(const char *user);
+char *user_email(const char *user);
+
int authenticate(const char *username, const char *password);
int handle_post(void);
int show_entry(const char *path);
-void add_to_index(const char *path, const char *title);
+void add_to_index(const char *user, const char *path, const char *title);
int current_year(void);
diff --git a/index.c b/index.c
index b5a543e..b8b6385 100644
--- a/index.c
+++ b/index.c
@@ -10,7 +10,7 @@
#include "blog.h"
-static void insert_into(int blogdir, char *index_dir, const char *uri, const char *title)
+static void insert_into(const char *user, int blogdir, char *index_dir, const char *uri, const char *title)
{
char index_path[FILENAME_MAX] = { 0 };
snprintf(index_path, sizeof(index_path), "%s/index.html", index_dir);
@@ -37,16 +37,16 @@ static void insert_into(int blogdir, char *index_dir, const char *uri, const cha
close(old_index);
}
- dprintf(new_index, HTML_TAIL, current_year());
+ dprintf(new_index, HTML_TAIL, current_year(), user_name(user), user_email(user));
close(new_index);
renameat(blogdir, new_path, blogdir, index_path);
}
-void add_to_index(const char *path, const char *title)
+void add_to_index(const char *user, const char *path, const char *title)
{
- int blogdir = open(DATA_DIRECTORY, O_DIRECTORY);
+ int blogdir = open(user, O_DIRECTORY);
if (blogdir == -1) {
return;
}
@@ -57,11 +57,11 @@ void add_to_index(const char *path, const char *title)
}
char *day = dirname(dir);
- insert_into(blogdir, day, path, title);
+ insert_into(user, blogdir, day, path, title);
char *month = dirname(day);
- insert_into(blogdir, month, path, title);
+ insert_into(user, blogdir, month, path, title);
char *year = dirname(month);
- insert_into(blogdir, year, path, title);
+ insert_into(user, blogdir, year, path, title);
free(dir);
close(blogdir);