(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         const char *title, *url;
218
219         rq = ws_request_new("getHeadlines");
220         ws_request_add_att_int(rq, "feed_id", feed->id);
221
222         rp = ws_execute(rq);
223
224         json_object_put(rq);
225
226         if (rp) {
227                 n = json_object_array_length(rp);
228                 for (i = 0; i < n; i++) {
229                         jheadline = json_object_array_get_idx(rp, i);
230
231                         j = json_object_object_get(jheadline, "id");
232                         hid = json_object_get_int(j);
233                         h = feed_get_headline(feed, hid);
234
235                         if (!h) {
236                                 j = json_object_object_get(jheadline, "title");
237                                 title = json_object_get_string(j);
238                                 
239                                 j = json_object_object_get(jheadline, "link");
240                                 url = json_object_get_string(j);
241
242                                 h = headline_new(hid, url, title);
243
244                                 tmp = headlines_add(feed->headlines, h);
245                                 if (feed->headlines)
246                                         free(feed->headlines);
247                                 feed->headlines = tmp;
248                         }
249
250                         j = json_object_object_get(jheadline, "unread");
251                         h->unread = json_object_get_boolean(j);
252                 }
253                 err = 0;
254         } else {
255                 err = 1;
256         }
257
258         json_object_put(rp);
259         return !err;
260 }
261
262 const char *ttrss_get_headline_content(struct headline *h)
263 {
264         if (!h->content)
265                 h->content = ws_get_article_content(h->id);
266
267         return h->content;
268 }
269
270 struct feed **ttrss_get_feeds()
271 {
272         struct json_object *rp, *rq, *content, *jfeed, *j;
273         int i, n;
274         struct feed **feeds, *feed, **tmp;
275
276         printf("ttrss_get_feeds\n");
277
278         rq = ws_request_new("getFeeds");
279
280         rp = http_json_get(session_url, rq);
281         json_object_put(rq);
282
283         content = json_object_object_get(rp, "content");
284
285         if (content) {
286                 n = json_object_array_length(content);
287
288                 feeds = NULL;
289                 for (i = 0; i < n; i++) {
290                         jfeed = json_object_array_get_idx(content, i);
291
292                         feed = malloc(sizeof(struct feed));
293
294                         j = json_object_object_get(jfeed, "title");
295                         feed->title = strdup(json_object_get_string(j));
296
297                         j = json_object_object_get(jfeed, "feed_url");
298                         feed->url = strdup(json_object_get_string(j));
299
300                         j = json_object_object_get(jfeed, "id");
301                         feed->id = json_object_get_int(j);
302
303                         j = json_object_object_get(jfeed, "unread");
304                         feed->unread = json_object_get_int(j);
305
306                         feed->headlines = NULL;
307
308                         tmp = feeds_add(feeds, feed);
309                         free(feeds);
310                         feeds = tmp;
311                 }
312         } else {
313                 feeds = NULL;
314         }
315
316         json_object_put(rp);
317
318         printf("ttrss_get_feeds ended\n");
319
320         return feeds;
321 }
322
323 struct headline **ttrss_get_headlines(struct feed *f)
324 {
325         if (!f->headlines)
326                 ws_update_headlines(f);
327
328         return f->headlines;
329 }
330
331 void ttrss_set_article_unread(int id, int unread)
332 {
333         struct json_object *rp, *rq;
334
335         printf("ttrss_set_article_unread %d %d\n", id, unread);
336
337         rq = ws_request_new("updateArticle");
338         json_object_object_add(rq, "article_ids", json_object_new_int(id));
339         json_object_object_add(rq, "field", json_object_new_int(2));
340         json_object_object_add(rq, "mode", json_object_new_int(unread));
341
342         rp = http_json_get(session_url, rq);
343
344         json_object_put(rq);
345         json_object_put(rp);
346 }
347
348 void ttrss_set_config(const char *url, const char *user, const char *pwd)
349 {
350         feeds_free(data);
351         data = NULL;
352         ws_init(url, user, pwd);
353 }