(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 struct feed **data;
35
36 void ws_request_add_att_str(json_object *rq, const char *k, const char *str)
37 {
38         json_object_object_add(rq, k, json_object_new_string(str));
39 }
40
41 void ws_request_add_att_int(json_object *rq, const char *k, int v)
42 {
43         json_object_object_add(rq, k, json_object_new_int(v));
44 }
45
46 struct json_object *ws_request_new(const char *op)
47 {
48         struct json_object *rq;
49
50         rq = json_object_new_object();
51
52         ws_request_add_att_str(rq, "op", op);
53
54         if (session_id)
55                 ws_request_add_att_str(rq, "sid", session_id);
56
57         return rq;
58 }
59
60 void ws_init(const char *url, const char *user, const char *pwd)
61 {
62         char *tmp;
63
64         if (session_id)
65                 session_id = NULL;
66
67         if (session_user)
68                 free(session_user);
69         session_user = strdup(user);
70
71         if (session_pwd)
72                 free(session_pwd);
73         session_pwd = strdup(pwd);
74
75         if (session_url)
76                 free(session_url);
77
78         tmp = url_normalize(url);
79         session_url = malloc(strlen(tmp) + strlen("/api/") + 1);
80         strcpy(session_url, tmp);
81         strcat(session_url, "/api/");
82         free(tmp);
83 }
84
85 struct json_object *ws_reply_get_content(struct json_object *rp)
86 {
87         return json_object_object_get(rp, "content");
88 }
89
90 struct json_object *ws_execute(struct json_object *rq)
91 {
92         struct json_object *rp, *content;
93
94         rp = http_json_get(session_url, rq);
95
96         if (rp) {
97                 content = ws_reply_get_content(rp);
98
99                 if (content && !json_object_object_get(rp, "error")) {
100                         json_object_get(content);
101                         json_object_put(rp);
102                         return content;
103                 }
104
105                 json_object_put(rp);
106         }
107
108         return NULL;
109 }
110
111 int ws_get_api_version()
112 {
113         struct json_object *rp, *rq;
114         int v;
115
116         rq = ws_request_new("getApiLevel");
117
118         rp = ws_execute(rq);
119
120         json_object_put(rq);
121
122         if (rp) {
123                 v = json_object_get_int(json_object_object_get(rp, "level"));
124
125                 json_object_put(rp);
126         } else {
127                 v = 0;
128         }
129
130         return v;
131 }
132
133 char *ws_login()
134 {
135         struct json_object *rq, *rp, *j;
136         char *str;
137
138         rq = ws_request_new("login");
139         ws_request_add_att_str(rq, "user", session_user);
140         ws_request_add_att_str(rq, "password", session_pwd);
141
142         rp = ws_execute(rq);
143         json_object_put(rq);
144
145         if (rp) {
146                 j = json_object_object_get(rp, "session_id");
147                 str = strdup(json_object_get_string(j));
148
149                 json_object_put(rp);
150         } else {
151                 str = NULL;
152         }
153
154         return str;
155 }
156
157 int ws_open_session()
158 {
159         int version, result;
160
161         if (session_id)
162                 free(session_id);
163
164         session_id = ws_login();
165
166         if (session_id) {
167                 version = ws_get_api_version();
168                 printf("API version: %d\n", version);
169
170                 if (version > 0) {
171                         result = 1;
172                 } else {
173                         free(session_id);
174                         session_id = NULL;
175                         result = 0;
176                 }
177         } else {
178                 result =  0;
179         }
180
181         return result;
182 }
183
184 char *ws_get_article_content(int id)
185 {
186         struct json_object *rp, *rq, *content, *item;
187         char *str;
188
189         rq = ws_request_new("getArticle");
190         ws_request_add_att_int(rq, "article_id", id);
191
192         rp = ws_execute(rq);
193
194         json_object_put(rq);
195
196         str = NULL;
197
198         if (rp) {
199                 item = json_object_array_get_idx(rp, 0);
200
201                 if (item) {
202                         content = json_object_object_get(item, "content");
203                         str = strdup(json_object_get_string(content));
204                 }
205
206                 json_object_put(rp);
207         }
208
209         return str;
210 }
211
212 int ws_update_headlines(struct feed *feed)
213 {
214         struct json_object *rp, *rq, *jheadline, *j;
215         int i, n, err, hid;
216         struct headline *h, **tmp;
217
218         rq = ws_request_new("getHeadlines");
219         ws_request_add_att_int(rq, "feed_id", feed->id);
220
221         rp = ws_execute(rq);
222
223         json_object_put(rq);
224
225         if (rp) {
226                 n = json_object_array_length(rp);
227                 for (i = 0; i < n; i++) {
228                         jheadline = json_object_array_get_idx(rp, i);
229
230                         j = json_object_object_get(jheadline, "id");
231                         hid = json_object_get_int(j);
232                         h = feed_get_headline(feed, hid);
233
234                         if (!h) {
235                                 h = malloc(sizeof(struct headline));
236                                 h->id = hid;
237                                 h->excerpt = NULL;
238                                 h->content = NULL;
239                                 h->title = NULL;
240                                 h->url = NULL;
241
242                                 tmp = headlines_add(feed->headlines, h);
243                                 if (feed->headlines)
244                                         free(feed->headlines);
245                                 feed->headlines = tmp;
246                         }
247
248                         if (!h->title) {
249                                 j = json_object_object_get(jheadline, "title");
250                                 h->title = strdup(json_object_get_string(j));
251                         }
252
253                         if (!h->url) {
254                                 j = json_object_object_get(jheadline, "link");
255                                 h->url = strdup(json_object_get_string(j));
256                         }
257
258                         j = json_object_object_get(jheadline, "unread");
259                         h->unread = json_object_get_boolean(j);
260                 }
261                 err = 0;
262         } else {
263                 err = 1;
264         }
265
266         json_object_put(rp);
267         return !err;
268 }
269
270 const char *ttrss_get_headline_content(struct headline *h)
271 {
272         if (!h->content)
273                 h->content = ws_get_article_content(h->id);
274
275         return h->content;
276 }
277
278 struct feed **ttrss_get_feeds()
279 {
280         struct json_object *rp, *rq, *content, *jfeed, *j;
281         int i, n;
282         struct feed **feeds, *feed, **tmp;
283
284         printf("ttrss_get_feeds\n");
285
286         rq = ws_request_new("getFeeds");
287
288         rp = http_json_get(session_url, rq);
289         json_object_put(rq);
290
291         content = json_object_object_get(rp, "content");
292
293         if (content) {
294                 n = json_object_array_length(content);
295
296                 feeds = NULL;
297                 for (i = 0; i < n; i++) {
298                         jfeed = json_object_array_get_idx(content, i);
299
300                         feed = malloc(sizeof(struct feed));
301
302                         j = json_object_object_get(jfeed, "title");
303                         feed->title = strdup(json_object_get_string(j));
304
305                         j = json_object_object_get(jfeed, "feed_url");
306                         feed->url = strdup(json_object_get_string(j));
307
308                         j = json_object_object_get(jfeed, "id");
309                         feed->id = json_object_get_int(j);
310
311                         j = json_object_object_get(jfeed, "unread");
312                         feed->unread = json_object_get_int(j);
313
314                         feed->headlines = NULL;
315
316                         tmp = feeds_add(feeds, feed);
317                         free(feeds);
318                         feeds = tmp;
319                 }
320         } else {
321                 feeds = NULL;
322         }
323
324         json_object_put(rp);
325
326         printf("ttrss_get_feeds ended\n");
327
328         return feeds;
329 }
330
331 struct headline **ttrss_get_headlines(struct feed *f)
332 {
333         if (!f->headlines)
334                 ws_update_headlines(f);
335
336         return f->headlines;
337 }
338
339 void ttrss_set_article_unread(int id, int unread)
340 {
341         struct json_object *rp, *rq;
342
343         printf("ttrss_set_article_unread %d %d\n", id, unread);
344
345         rq = ws_request_new("updateArticle");
346         json_object_object_add(rq, "article_ids", json_object_new_int(id));
347         json_object_object_add(rq, "field", json_object_new_int(2));
348         json_object_object_add(rq, "mode", json_object_new_int(unread));
349
350         rp = http_json_get(session_url, rq);
351
352         json_object_put(rq);
353         json_object_put(rp);
354 }
355
356 void ttrss_set_config(const char *url, const char *user, const char *pwd)
357 {
358         feeds_free(data);
359         data = NULL;
360         ws_init(url, user, pwd);
361 }