1 /* Blerg is (C) 2011 The Dominion of Awesome, and is distributed under a
2 * BSD-style license. Please see the COPYING file for details.
12 #include "subscription.h"
13 #include "stringbucket.h"
16 #include "configuration.h"
18 int subscription_add(const char *from, const char *to) {
19 char filename[FILENAME_MAX];
20 struct stringbucket * sb;
22 snprintf(filename, FILENAME_MAX, "%s/%s/subscriptions", blergconf.data_path, from);
23 sb = stringbucket_open(filename);
24 stringbucket_add(sb, to);
25 stringbucket_close(sb);
27 snprintf(filename, FILENAME_MAX, "%s/%s/subscribers", blergconf.data_path, to);
28 sb = stringbucket_open(filename);
29 stringbucket_add(sb, from);
30 stringbucket_close(sb);
35 int subscription_remove(const char *from, const char *to) {
36 char filename[FILENAME_MAX];
37 struct stringbucket * sb;
39 snprintf(filename, FILENAME_MAX, "%s/%s/subscriptions", blergconf.data_path, from);
40 sb = stringbucket_open(filename);
41 stringbucket_delete(sb, to);
42 stringbucket_close(sb);
44 snprintf(filename, FILENAME_MAX, "%s/%s/subscribers", blergconf.data_path, to);
45 sb = stringbucket_open(filename);
46 stringbucket_delete(sb, from);
47 stringbucket_close(sb);
52 void subscription_notify_add_item(char *to, void *stuff) {
53 char filename[FILENAME_MAX];
55 snprintf(filename, FILENAME_MAX, "%s/%s/subscription_feed", blergconf.data_path, to);
56 int fd = open(filename, O_WRONLY | O_APPEND | O_CREAT, 0600);
57 write(fd, stuff, sizeof(struct blergref));
61 int subscription_notify(const char *author, uint64_t record) {
62 char filename[FILENAME_MAX];
65 strncpy(r.author, author, 32);
68 snprintf(filename, FILENAME_MAX, "%s/%s/subscribers", blergconf.data_path, author);
69 struct stringbucket * sb = stringbucket_open(filename);
70 stringbucket_iterate(sb, subscription_notify_add_item, &r);
71 stringbucket_close(sb);
76 struct blergref * subscription_list(const char *author, uint64_t offset, int *count, int direction) {
77 char filename[FILENAME_MAX];
79 struct blergref * slist;
80 struct blergref * retlist;
81 uint64_t n_subscription_records;
83 if (!valid_name(author))
86 snprintf(filename, FILENAME_MAX, "%s/%s/subscription_feed", blergconf.data_path, author);
88 int feed_fd = open(filename, O_RDONLY);
90 perror("Could not open subscription feed");
91 goto subscription_list_map_failed;
95 if (st.st_size == 0) {
97 goto subscription_list_map_failed;
99 n_subscription_records = st.st_size / sizeof(struct blergref);
100 if (*count > n_subscription_records - offset)
101 *count = n_subscription_records - offset;
102 if (offset > n_subscription_records) {
103 fprintf(stderr, "Cannot access subscription record beyond end\n");
104 goto subscription_list_map_failed;
109 slist = (struct blergref *) mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, feed_fd, 0);
110 if (slist == MAP_FAILED) {
111 perror("Could not mmap tag_file");
112 goto subscription_list_map_failed;
114 retlist = (struct blergref *) malloc(sizeof(struct blergref) * *count);
115 if (retlist == NULL) {
116 perror("Could not allocate memory for subscription feed list");
117 goto subscription_list_malloc_failed;
122 memcpy(retlist, slist + offset, sizeof(struct blergref) * *count);
125 memcpy(retlist, slist + (n_subscription_records - *count - offset), sizeof(struct blergref) * *count);
128 munmap(slist, st.st_size);
132 subscription_list_malloc_failed:
133 munmap(slist, st.st_size);
134 subscription_list_map_failed:
136 subscription_list_open_failed:
141 int is_subscribed(const char *from, const char *to) {
142 char filename[FILENAME_MAX];
143 struct stringbucket * sb;
146 snprintf(filename, FILENAME_MAX, "%s/%s/subscriptions", blergconf.data_path, from);
147 sb = stringbucket_open(filename);
148 if (stringbucket_find(sb, to) != -1)
150 stringbucket_close(sb);
155 int subscription_count_items(const char *user) {
156 char filename[FILENAME_MAX];
159 if (!valid_name(user))
162 snprintf(filename, FILENAME_MAX, "%s/%s/subscription_feed", blergconf.data_path, user);
164 if (access(filename, R_OK) != 0)
167 return st.st_size / sizeof(struct blergref);