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