(no commit message)
[prss.git] / src / ttrss.c
1 /*
2  * Copyright (C) 2010-2013 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19
20 #include <stdio.h>
21 #include <string.h>
22
23 #include <json/json.h>
24
25 #include "http.h"
26 #include "ttrss.h"
27 #include "url.h"
28
29 static char *session_id;
30 static char *session_url;
31 static char *session_user;
32 static char *session_pwd;
33
34 static int list_length(void **list)
35 {
36         int n;
37
38         if (!list)
39                 return 0;
40
41         n = 0;
42         while (*list) {
43                 n++;
44                 list++;
45         }
46
47         return n;
48 }
49
50 static void **list_add(void **list, void *item)
51 {
52         int n;
53         void **result;
54
55         n = list_length(list);
56
57         result = malloc((n + 1 + 1) * sizeof(void *));
58
59         if (list)
60                 memcpy(result, list, n * sizeof(void *));
61
62         result[n] = item;
63         result[n + 1] = NULL;
64
65         return result;
66 }
67
68 struct headline **
69 headlines_add(struct headline **list, struct headline *h)
70 {
71         return (struct headline **)list_add((void **)list, (void *)h);
72 }
73
74 struct headline *feed_get_headline(struct feed *feed, int id)
75 {
76         struct headline **headlines;
77
78         headlines = feed->headlines;
79         if (headlines)
80                 while (*headlines) {
81                         if ((*headlines)->id == id)
82                                 return *headlines;
83                         headlines++;
84                 }
85
86         return NULL;
87 }
88
89 void ws_request_add_att_str(json_object *rq, const char *k, const char *str)
90 {
91         json_object_object_add(rq, k, json_object_new_string(str));
92 }
93
94 void ws_request_add_att_int(json_object *rq, const char *k, int v)
95 {
96         json_object_object_add(rq, k, json_object_new_int(v));
97 }
98
99 struct json_object *ws_request_new(const char *op)
100 {
101         struct json_object *rq;
102
103         rq = json_object_new_object();
104
105         ws_request_add_att_str(rq, "op", op);
106
107         if (session_id)
108                 ws_request_add_att_str(rq, "sid", session_id);
109
110         return rq;
111 }
112
113 void ws_init(const char *url, const char *user, const char *pwd)
114 {
115         char *tmp;
116
117         if (session_id)
118                 session_id = NULL;
119
120         if (session_user)
121                 free(session_user);
122         session_user = strdup(user);
123
124         if (session_pwd)
125                 free(session_pwd);
126         session_pwd = strdup(pwd);
127
128         if (session_url)
129                 free(session_url);
130
131         tmp = url_normalize(url);
132         session_url = malloc(strlen(tmp) + strlen("/api/") + 1);
133         strcpy(session_url, tmp);
134         strcat(session_url, "/api/");
135         free(tmp);
136 }
137
138 struct json_object *ws_reply_get_content(struct json_object *rp)
139 {
140         return json_object_object_get(rp, "content");
141 }
142
143 struct json_object *ws_execute(struct json_object *rq)
144 {
145         struct json_object *rp, *content;
146
147         rp = http_json_get(session_url, rq);
148
149         if (rp) {
150                 content = ws_reply_get_content(rp);
151
152                 if (content && !json_object_object_get(rp, "error")) {
153                         json_object_get(content);
154                         json_object_put(rp);
155                         return content;
156                 }
157
158                 json_object_put(rp);
159         }
160
161         return NULL;
162 }
163
164 int ws_get_api_version()
165 {
166         struct json_object *rp, *rq;
167         int v;
168
169         rq = ws_request_new("getApiLevel");
170
171         rp = ws_execute(rq);
172
173         json_object_put(rq);
174
175         if (rp) {
176                 v = json_object_get_int(json_object_object_get(rp, "level"));
177
178                 json_object_put(rp);
179         } else {
180                 v = 0;
181         }
182
183         return v;
184 }
185
186 char *ws_login()
187 {
188         struct json_object *rq, *rp, *j;
189         char *str;
190
191         rq = ws_request_new("login");
192         ws_request_add_att_str(rq, "user", session_user);
193         ws_request_add_att_str(rq, "password", session_pwd);
194
195         rp = ws_execute(rq);
196         json_object_put(rq);
197
198         if (rp) {
199                 j = json_object_object_get(rp, "session_id");
200                 str = strdup(json_object_get_string(j));
201
202                 json_object_put(rp);
203         } else {
204                 str = NULL;
205         }
206
207         return str;
208 }
209
210 int ws_open_session()
211 {
212         int version, result;
213
214         if (session_id)
215                 free(session_id);
216
217         session_id = ws_login();
218
219         if (session_id) {
220                 version = ws_get_api_version();
221                 printf("API version: %d\n", version);
222
223                 if (version > 0) {
224                         result = 1;
225                 } else {
226                         free(session_id);
227                         session_id = NULL;
228                         result = 0;
229                 }
230         } else {
231                 result =  0;
232         }
233
234         return result;
235 }
236
237 char *ws_get_article_content(int id)
238 {
239         struct json_object *rp, *rq, *content, *item;
240         char *str;
241
242         rq = ws_request_new("getArticle");
243         ws_request_add_att_int(rq, "article_id", id);
244
245         rp = ws_execute(rq);
246
247         json_object_put(rq);
248
249         str = NULL;
250
251         if (rp) {
252                 item = json_object_array_get_idx(rp, 0);
253
254                 if (item) {
255                         content = json_object_object_get(item, "content");
256                         str = strdup(json_object_get_string(content));
257                 }
258
259                 json_object_put(rp);
260         }
261
262         return str;
263 }
264
265 int ws_update_headlines(struct feed *feed)
266 {
267         struct json_object *rp, *rq, *jheadline, *j;
268         int i, n, err, hid;
269         struct headline *h, **tmp;
270
271         rq = ws_request_new("getHeadlines");
272         ws_request_add_att_int(rq, "feed_id", feed->id);
273
274         rp = ws_execute(rq);
275
276         json_object_put(rq);
277
278         if (rp) {
279                 n = json_object_array_length(rp);
280                 for (i = 0; i < n; i++) {
281                         jheadline = json_object_array_get_idx(rp, i);
282
283                         j = json_object_object_get(jheadline, "id");
284                         hid = json_object_get_int(j);
285                         h = feed_get_headline(feed, hid);
286
287                         if (!h) {
288                                 h = malloc(sizeof(struct headline));
289                                 h->id = hid;
290                                 h->excerpt = NULL;
291                                 h->content = NULL;
292                                 h->title = NULL;
293                                 h->url = NULL;
294
295                                 tmp = headlines_add(feed->headlines, h);
296                                 if (feed->headlines)
297                                         free(feed->headlines);
298                                 feed->headlines = tmp;
299                         }
300
301                         if (!h->title) {
302                                 j = json_object_object_get(jheadline, "title");
303                                 h->title = strdup(json_object_get_string(j));
304                         }
305
306                         if (!h->url) {
307                                 j = json_object_object_get(jheadline, "link");
308                                 h->url = strdup(json_object_get_string(j));
309                         }
310
311                         j = json_object_object_get(jheadline, "unread");
312                         h->unread = json_object_get_boolean(j);
313                 }
314                 err = 0;
315         } else {
316                 err = 1;
317         }
318
319         json_object_put(rp);
320         return !err;
321 }
322
323 const char *ttrss_get_headline_content(struct headline *h)
324 {
325         if (!h->content)
326                 h->content = ws_get_article_content(h->id);
327
328         return h->content;
329 }
330
331 struct feed **ttrss_get_feeds()
332 {
333         struct json_object *rp, *rq, *content, *jfeed, *j;
334         int i, n;
335         struct feed **feeds, *feed, **tmp;
336
337         printf("ttrss_get_feeds\n");
338
339         rq = ws_request_new("getFeeds");
340
341         rp = http_json_get(session_url, rq);
342         json_object_put(rq);
343
344         content = json_object_object_get(rp, "content");
345
346         if (content) {
347                 n = json_object_array_length(content);
348
349                 feeds = NULL;
350                 for (i = 0; i < n; i++) {
351                         jfeed = json_object_array_get_idx(content, i);
352
353                         feed = malloc(sizeof(struct feed));
354
355                         j = json_object_object_get(jfeed, "title");
356                         feed->title = strdup(json_object_get_string(j));
357
358                         j = json_object_object_get(jfeed, "feed_url");
359                         feed->url = strdup(json_object_get_string(j));
360
361                         j = json_object_object_get(jfeed, "id");
362                         feed->id = json_object_get_int(j);
363
364                         j = json_object_object_get(jfeed, "unread");
365                         feed->unread = json_object_get_int(j);
366
367                         feed->headlines = NULL;
368
369                         tmp = (struct feed **)list_add((void **)feeds, feed);
370                         free(feeds);
371                         feeds = tmp;
372                 }
373         } else {
374                 feeds = NULL;
375         }
376
377         json_object_put(rp);
378
379         printf("ttrss_get_feeds ended\n");
380
381         return feeds;
382 }
383
384 struct headline **ttrss_get_headlines(struct feed *f)
385 {
386         if (!f->headlines)
387                 ws_update_headlines(f);
388
389         return f->headlines;
390 }
391
392 void ttrss_set_article_unread(int id, int unread)
393 {
394         struct json_object *rp, *rq;
395
396         printf("ttrss_set_article_unread %d %d\n", id, unread);
397
398         rq = ws_request_new("updateArticle");
399         json_object_object_add(rq, "article_ids", json_object_new_int(id));
400         json_object_object_add(rq, "field", json_object_new_int(2));
401         json_object_object_add(rq, "mode", json_object_new_int(unread));
402
403         rp = http_json_get(session_url, rq);
404
405         json_object_put(rq);
406         json_object_put(rp);
407 }