Modify backend to use new auth cookie format
[blerg.git] / database / subscription.c
index 5d305d1..c1633fe 100644 (file)
@@ -5,67 +5,76 @@
 #include <sys/stat.h>
 #include <sys/mman.h>
 #include <stdlib.h>
+#include <unistd.h>
 #include <stdio.h>
 #include <fcntl.h>
 #include <string.h>
 #include "subscription.h"
 #include "stringbucket.h"
+#include "util.h"
 #include "config.h"
+#include "configuration.h"
 
 int subscription_add(const char *from, const char *to) {
-       char filename[512];
+       char filename[FILENAME_MAX];
        struct stringbucket * sb;
 
-       snprintf(filename, 512, "%s/%s/subscriptions", DATA_PATH, from);
+       snprintf(filename, FILENAME_MAX, "%s/%s/subscriptions", blergconf.data_path, from);
        sb = stringbucket_open(filename);
        stringbucket_add(sb, to);
        stringbucket_close(sb);
 
-       snprintf(filename, 512, "%s/%s/subscribers", DATA_PATH, to);
+       snprintf(filename, FILENAME_MAX, "%s/%s/subscribers", blergconf.data_path, to);
        sb = stringbucket_open(filename);
        stringbucket_add(sb, from);
        stringbucket_close(sb);
+
+       return 1;
 }
 
 int subscription_remove(const char *from, const char *to) {
-       char filename[512];
+       char filename[FILENAME_MAX];
        struct stringbucket * sb;
 
-       snprintf(filename, 512, "%s/%s/subscriptions", DATA_PATH, from);
+       snprintf(filename, FILENAME_MAX, "%s/%s/subscriptions", blergconf.data_path, from);
        sb = stringbucket_open(filename);
        stringbucket_delete(sb, to);
        stringbucket_close(sb);
 
-       snprintf(filename, 512, "%s/%s/subscribers", DATA_PATH, to);
+       snprintf(filename, FILENAME_MAX, "%s/%s/subscribers", blergconf.data_path, to);
        sb = stringbucket_open(filename);
        stringbucket_delete(sb, from);
        stringbucket_close(sb);
+
+       return 1;
 }
 
 void subscription_notify_add_item(char *to, void *stuff) {
-       char filename[512];
+       char filename[FILENAME_MAX];
 
-       snprintf(filename, 512, "%s/%s/subscription_feed", DATA_PATH, to);
+       snprintf(filename, FILENAME_MAX, "%s/%s/subscription_feed", blergconf.data_path, to);
        int fd = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0600);
        write(fd, stuff, sizeof(struct blergref));
        close(fd);
 }
 
 int subscription_notify(const char *author, uint64_t record) {
-       char filename[512];
+       char filename[FILENAME_MAX];
        struct blergref r;
 
        strncpy(r.author, author, 32);
        r.record = record;
 
-       snprintf(filename, 512, "%s/%s/subscribers", DATA_PATH, author);
+       snprintf(filename, FILENAME_MAX, "%s/%s/subscribers", blergconf.data_path, author);
        struct stringbucket * sb = stringbucket_open(filename);
        stringbucket_iterate(sb, subscription_notify_add_item, &r);
        stringbucket_close(sb);
+
+       return 1;
 }
 
 struct blergref * subscription_list(const char *author, uint64_t offset, int *count, int direction) {
-       char filename[512];
+       char filename[FILENAME_MAX];
        struct stat st;
        struct blergref * slist;
        struct blergref * retlist;
@@ -74,17 +83,17 @@ struct blergref * subscription_list(const char *author, uint64_t offset, int *co
        if (!valid_name(author))
                return NULL;
 
-       snprintf(filename, 512, "%s/%s/subscription_feed", DATA_PATH, author);
+       snprintf(filename, FILENAME_MAX, "%s/%s/subscription_feed", blergconf.data_path, author);
 
        int feed_fd = open(filename, O_RDONLY);
        if (feed_fd == -1) {
                perror("Could not open subscription feed");
-               goto subscription_list_map_failed;
+               goto subscription_list_open_failed;
        }
 
        fstat(feed_fd, &st);
        if (st.st_size == 0) {
-               close(feed_fd);
+               fprintf(stderr, "Feed is empty\n");
                goto subscription_list_map_failed;
        }
        n_subscription_records = st.st_size / sizeof(struct blergref);
@@ -130,11 +139,11 @@ subscription_list_open_failed:
 }
 
 int is_subscribed(const char *from, const char *to) {
-       char filename[512];
+       char filename[FILENAME_MAX];
        struct stringbucket * sb;
        int ret = 0;
 
-       snprintf(filename, 512, "%s/%s/subscriptions", DATA_PATH, from);
+       snprintf(filename, FILENAME_MAX, "%s/%s/subscriptions", blergconf.data_path, from);
        sb = stringbucket_open(filename);
        if (stringbucket_find(sb, to) != -1)
                ret = 1;
@@ -144,13 +153,13 @@ int is_subscribed(const char *from, const char *to) {
 }
 
 int subscription_count_items(const char *user) {
-       char filename[512];
+       char filename[FILENAME_MAX];
        struct stat st;
 
        if (!valid_name(user))
                return -1;
 
-       snprintf(filename, 512, "%s/%s/subscription_feed", DATA_PATH, user);
+       snprintf(filename, FILENAME_MAX, "%s/%s/subscription_feed", blergconf.data_path, user);
 
        if (access(filename, R_OK) != 0)
                return 0;