Fix scrolling on Winphone IE
[blerg.git] / database / subscription.c
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.
3  */
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <sys/mman.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <stdio.h>
10 #include <fcntl.h>
11 #include <string.h>
12 #include "subscription.h"
13 #include "stringbucket.h"
14 #include "util.h"
15 #include "config.h"
16 #include "configuration.h"
17
18 int subscription_add(const char *from, const char *to) {
19         char filename[FILENAME_MAX];
20         struct stringbucket * sb;
21
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);
26
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);
31
32         return 1;
33 }
34
35 int subscription_remove(const char *from, const char *to) {
36         char filename[FILENAME_MAX];
37         struct stringbucket * sb;
38
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);
43
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);
48
49         return 1;
50 }
51
52 void subscription_notify_add_item(char *to, void *stuff) {
53         char filename[FILENAME_MAX];
54
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));
58         close(fd);
59 }
60
61 int subscription_notify(const char *author, uint64_t record) {
62         char filename[FILENAME_MAX];
63         struct blergref r;
64
65         strncpy(r.author, author, 32);
66         r.record = record;
67
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);
72
73         return 1;
74 }
75
76 struct blergref * subscription_list(const char *author, uint64_t offset, int *count, int direction) {
77         char filename[FILENAME_MAX];
78         struct stat st;
79         struct blergref * slist;
80         struct blergref * retlist;
81         uint64_t n_subscription_records;
82
83         if (!valid_name(author))
84                 return NULL;
85
86         snprintf(filename, FILENAME_MAX, "%s/%s/subscription_feed", blergconf.data_path, author);
87
88         int feed_fd = open(filename, O_RDONLY);
89         if (feed_fd == -1) {
90                 perror("Could not open subscription feed");
91                 goto subscription_list_open_failed;
92         }
93
94         fstat(feed_fd, &st);
95         if (st.st_size == 0) {
96                 fprintf(stderr, "Feed is empty\n");
97                 goto subscription_list_map_failed;
98         }
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;
105                 close(feed_fd);
106                 return NULL;
107         }
108
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;
113         }
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;
118         }
119         
120         switch(direction) {
121         case 1:
122                 memcpy(retlist, slist + offset, sizeof(struct blergref) * *count);
123                 break;
124         case -1:
125                 memcpy(retlist, slist + (n_subscription_records - *count - offset), sizeof(struct blergref) * *count);
126         }
127
128         munmap(slist, st.st_size);
129         close(feed_fd);
130         return retlist;
131
132 subscription_list_malloc_failed:
133         munmap(slist, st.st_size);
134 subscription_list_map_failed:
135         close(feed_fd);
136 subscription_list_open_failed:
137         *count = 0;
138         return NULL;
139 }
140
141 int is_subscribed(const char *from, const char *to) {
142         char filename[FILENAME_MAX];
143         struct stringbucket * sb;
144         int ret = 0;
145
146         snprintf(filename, FILENAME_MAX, "%s/%s/subscriptions", blergconf.data_path, from);
147         sb = stringbucket_open(filename);
148         if (stringbucket_find(sb, to) != -1)
149                 ret = 1;
150         stringbucket_close(sb);
151
152         return ret;
153 }
154
155 int subscription_count_items(const char *user) {
156         char filename[FILENAME_MAX];
157         struct stat st;
158
159         if (!valid_name(user))
160                 return -1;
161
162         snprintf(filename, FILENAME_MAX, "%s/%s/subscription_feed", blergconf.data_path, user);
163
164         if (access(filename, R_OK) != 0)
165                 return 0;
166         stat(filename, &st);
167         return st.st_size / sizeof(struct blergref);
168 }