Remove /unsubscribe from htaccess config
[blerg.git] / lib / perl / Blerg-Database / Database.xs
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4
5 #include "ppport.h"
6
7 #include "config.h"
8 #include "database/database.h"
9 #include "database/blergref.h"
10 #include "database/tags.h"
11 #include "database/subscription.h"
12 #include "database/util.h"
13 #include "database/configuration.h"
14 #include "common/auth.h"
15
16 #include "const-c.inc"
17
18 HV * blergref_to_perl_hash(struct blergref *list) {
19         HV *tmp;
20         char buf[21];
21         int n;
22
23         tmp = newHV();
24         hv_store(tmp, "author", 6, newSVpv(list->author, 0), 0);
25         n = snprintf(buf, 21, "%llu", list->record);
26         hv_store(tmp, "record", 6, newSVpv(buf, n), 0);
27
28         return tmp;
29 }
30
31 MODULE = Blerg::Database                PACKAGE = Blerg::Database               
32
33 INCLUDE: const-xs.inc
34 PROTOTYPES: ENABLE
35
36 int init()
37     CODE:
38         RETVAL = blerg_init();
39     OUTPUT:
40         RETVAL
41
42 int exists(const char *name)
43     CODE:
44         RETVAL = blerg_exists(name);
45     OUTPUT:
46         RETVAL
47
48 struct blerg * _open(const char *name)
49     CODE:
50         RETVAL = blerg_open(name);
51     OUTPUT:
52         RETVAL
53
54 int _close(struct blerg *ptr)
55     CODE:
56         RETVAL = blerg_close(ptr);
57     OUTPUT:
58         RETVAL
59
60 const char * _store(struct blerg *ptr, const char *data, int length(data))
61     PROTOTYPE: $$
62     INIT:
63         uint64_t record;
64         char buf[21];
65         int n;
66     PPCODE:
67         record = blerg_store(ptr, data, XSauto_length_of_data);
68         n = snprintf(buf, 21, "%llu", record);
69         XPUSHs(sv_2mortal(newSVpv(buf, n)));
70
71 const char * _fetch(struct blerg *ptr, const char *str_record)
72     INIT:
73         uint64_t record;
74         char *buf;
75         int buflen;
76         int n;
77     PPCODE:
78         record = strtoull(str_record, NULL, 0);
79         n = blerg_fetch(ptr, record, &buf, &buflen);
80         if (!n) {
81             XSRETURN_UNDEF;
82         }
83         XPUSHs(sv_2mortal(newSVpvn(buf, buflen)));
84         free(buf);
85
86 const char * _get_record_count(struct blerg *ptr)
87     INIT:
88         uint64_t count;
89         char buf[21];
90         int n;
91     PPCODE:
92         count = blerg_get_record_count(ptr);
93         n = snprintf(buf, 21, "%llu", count);
94         XPUSHs(sv_2mortal(newSVpv(buf, n)));
95
96 time_t _get_timestamp(struct blerg *ptr, const char *str_record)
97     INIT:
98         uint64_t record;
99     CODE:
100         record = strtoull(str_record, NULL, 0);
101         RETVAL = blerg_get_timestamp(ptr, record);
102     OUTPUT:
103         RETVAL
104
105 int _set_subscription_mark(struct blerg *ptr)
106     CODE:
107         RETVAL = blerg_set_subscription_mark(ptr);
108     OUTPUT:
109         RETVAL
110
111 const char * _get_subscription_mark(struct blerg *ptr)
112     INIT:
113         uint64_t mark;
114         char buf[21];
115         int n;
116     PPCODE:
117         mark = blerg_get_subscription_mark(ptr);
118         n = snprintf(buf, 21, "%llu", mark);
119         XPUSHs(sv_2mortal(newSVpv(buf, n)));
120
121 int _set_status(struct blerg *ptr, int status, int v)
122     CODE:
123         RETVAL = blerg_set_status(ptr, status, v);
124     OUTPUT:
125         RETVAL
126
127 int _get_status(struct blerg *ptr, int status)
128     CODE:
129         RETVAL = blerg_get_status(ptr, status);
130     OUTPUT:
131         RETVAL
132
133 void tag_list(const char *tag, const char *str_offset, int direction)
134     INIT:
135         HV * tmp;
136         struct blergref *list;
137         uint64_t offset;
138         int count, i;
139     PPCODE:
140         offset = strtoull(str_offset, NULL, 0);
141         count = 50;
142         list = tag_list(tag, offset, &count, direction);
143         if (list == NULL) {
144             XSRETURN_EMPTY;
145         }
146
147         i = count - 1;
148         while (i >= 0) {
149             tmp = blergref_to_perl_hash(&list[i]);
150             XPUSHs(sv_2mortal(newRV_noinc((SV*)tmp)));
151             i--;
152         }
153         free(list);
154
155 int subscription_add(const char *from, const char *to)
156
157 int subscription_remove(const char *from, const char *to)
158
159 void _subscription_list(const char *author, const char *str_offset, int direction)
160     INIT:
161         HV *tmp;
162         struct blergref *list;
163         uint64_t offset;
164         int count, i;
165     PPCODE:
166         offset = strtoull(str_offset, NULL, 0);
167         count = 50;
168         list = subscription_list(author, offset, &count, direction);
169         if (list == NULL) {
170             XSRETURN_EMPTY;
171         }
172
173         i = count - 1;
174         while (i >= 0) {
175             tmp = blergref_to_perl_hash(&list[i]);
176             XPUSHs(sv_2mortal(newRV_noinc((SV*)tmp)));
177             i--;
178         }
179         free(list);
180
181 int valid_tag_name(const char *name)
182
183 int valid_name(const char *name)
184
185 int auth_set_password(const char *username, const char *password)
186
187 int auth_check_password(const char *username, const char *password)
188
189 char * auth_login(const char *username, const char *password)
190     INIT:
191         char *token;
192     PPCODE:
193         token = auth_login(username, password);
194         if (token != NULL) {
195             XPUSHs(sv_2mortal(newSVpv(token, TOKEN_SIZE * 2)));
196         } else {
197             XSRETURN_UNDEF;
198         }
199         free(token);
200
201 int auth_logout(const char *username, const char *token)
202
203 int auth_check_token(const char *username, const char *given_token)
204
205 void auth_get_counter(const char *username)
206     INIT:
207         uint32_t counter = 0;
208     PPCODE:
209         if (auth_get_counter(username, &counter)) {
210             XPUSHs(sv_2mortal(newSVuv(counter)));
211         } else {
212             XSRETURN_UNDEF;
213         }
214
215 void configuration()
216     INIT:
217         HV *confhash;
218     PPCODE:
219         confhash = newHV();
220         hv_store(confhash, "base_path", 9, newSVpv(blergconf.base_path, 0), 0);
221         hv_store(confhash, "data_path", 9, newSVpv(blergconf.data_path, 0), 0);
222         hv_store(confhash, "hash_tags_path", 14, newSVpv(blergconf.hash_tags_path, 0), 0);
223         hv_store(confhash, "ref_tags_path", 13, newSVpv(blergconf.ref_tags_path, 0), 0);
224
225         XPUSHs(sv_2mortal(newRV_noinc((SV*)confhash)));