asynchronous ws call for flagging article as read
[prss.git] / src / ttrss_ws.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 <stdlib.h>
21 #include <string.h>
22 #include <stdio.h>
23
24 #include <json/json.h>
25
26 #include "http.h"
27 #include "log.h"
28 #include "ttrss_ws.h"
29 #include "url.h"
30
31 static char *session_id;
32 static char *session_url;
33 static char *session_user;
34 static char *session_pwd;
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_url && !strcmp(session_url, url)
65             && session_user && !strcmp(session_user, user)
66             && session_pwd && !strcmp(session_pwd, pwd))
67                 return ;
68
69         free(session_id);
70         session_id = NULL;
71
72         free(session_user);
73         session_user = strdup(user);
74
75         free(session_pwd);
76         session_pwd = strdup(pwd);
77
78         tmp = url_normalize(url);
79         free(session_url);
80         session_url = malloc(strlen(tmp) + strlen("/api/") + 1);
81         strcpy(session_url, tmp);
82         strcat(session_url, "/api/");
83         free(tmp);
84 }
85
86 struct json_object *ws_reply_get_content(struct json_object *rp)
87 {
88         return json_object_object_get(rp, "content");
89 }
90
91 static struct json_object *execute(struct json_object *rq, char **err)
92 {
93         struct json_object *rp, *content, *jerror;
94         const char *str;
95
96         rp = http_json_get(session_url, rq);
97
98         content = NULL;
99
100         if (rp) {
101                 content = ws_reply_get_content(rp);
102
103                 if (content) {
104                         jerror = json_object_object_get(content, "error");
105                         if (jerror) {
106                                 if (err) {
107                                         str = json_object_get_string(jerror);
108                                         *err = strdup(str);
109                                 }
110                                 content = NULL;
111                         } else {
112                                 json_object_get(content);
113                         }
114                 }
115
116                 json_object_put(rp);
117         }
118
119         return content;
120 }
121
122 struct json_object *ws_execute(struct json_object *rq)
123 {
124         char *err;
125         struct json_object *result;
126
127         log_debug("ws_execute()");
128
129         err = NULL;
130         result = execute(rq, &err);
131
132         if (err) {
133                 log_debug("ws_execute(): error=%s\n", err);
134
135                 if (!strcmp(err, "NOT_LOGGED_IN")) {
136                         ws_open_session();
137                         result = execute(rq, NULL);
138                 }
139
140                 free(err);
141         }
142
143         return result;
144 }
145
146 int ws_get_api_version()
147 {
148         struct json_object *rp, *rq;
149         int v;
150
151         rq = ws_request_new("getApiLevel");
152
153         rp = ws_execute(rq);
154
155         json_object_put(rq);
156
157         if (rp) {
158                 v = json_object_get_int(json_object_object_get(rp, "level"));
159
160                 json_object_put(rp);
161         } else {
162                 v = 0;
163         }
164
165         return v;
166 }
167
168 char *ws_login()
169 {
170         struct json_object *rq, *rp, *j;
171         char *str;
172
173         rq = ws_request_new("login");
174         ws_request_add_att_str(rq, "user", session_user);
175         ws_request_add_att_str(rq, "password", session_pwd);
176
177         rp = ws_execute(rq);
178         json_object_put(rq);
179
180         str = NULL;
181         if (rp) {
182                 j = json_object_object_get(rp, "session_id");
183
184                 if (j)
185                         str = strdup(json_object_get_string(j));
186
187                 json_object_put(rp);
188         }
189
190         return str;
191 }
192
193 int ws_open_session()
194 {
195         int version, result;
196
197         log_debug("ws_open_session()");
198
199         if (session_id)
200                 free(session_id);
201
202         session_id = NULL;
203         session_id = ws_login();
204
205         if (session_id) {
206                 version = ws_get_api_version();
207                 log_debug("API version= %d", version);
208
209                 if (version > 0) {
210                         result = 1;
211                 } else {
212                         free(session_id);
213                         session_id = NULL;
214                         result = 0;
215                 }
216         } else {
217                 result =  0;
218         }
219
220         return result;
221 }
222
223 char *ws_get_article_content(int id)
224 {
225         struct json_object *rp, *rq, *content, *item;
226         char *str;
227
228         rq = ws_request_new("getArticle");
229         ws_request_add_att_int(rq, "article_id", id);
230
231         rp = ws_execute(rq);
232
233         json_object_put(rq);
234
235         str = NULL;
236
237         if (rp) {
238                 item = json_object_array_get_idx(rp, 0);
239
240                 if (item) {
241                         content = json_object_object_get(item, "content");
242                         str = strdup(json_object_get_string(content));
243                 }
244
245                 json_object_put(rp);
246         }
247
248         return str;
249 }
250
251 int ws_update_headlines(struct feed *feed)
252 {
253         struct json_object *rp, *rq, *jheadline, *j;
254         int i, n, hid;
255         struct headline *h, **tmp;
256         const char *title, *url;
257
258         rq = ws_request_new("getHeadlines");
259         ws_request_add_att_int(rq, "feed_id", feed->id);
260
261         rp = ws_execute(rq);
262
263         json_object_put(rq);
264
265         if (rp) {
266                 n = json_object_array_length(rp);
267                 for (i = 0; i < n; i++) {
268                         jheadline = json_object_array_get_idx(rp, i);
269
270                         j = json_object_object_get(jheadline, "id");
271                         hid = json_object_get_int(j);
272                         h = feed_get_headline(feed, hid);
273
274                         if (!h) {
275                                 j = json_object_object_get(jheadline, "title");
276                                 title = json_object_get_string(j);
277
278                                 j = json_object_object_get(jheadline, "link");
279                                 url = json_object_get_string(j);
280
281                                 h = headline_new(hid, url, title);
282
283                                 tmp = headlines_add(feed->headlines, h);
284                                 if (feed->headlines)
285                                         free(feed->headlines);
286                                 feed->headlines = tmp;
287                         }
288
289                         j = json_object_object_get(jheadline, "unread");
290                         h->unread = json_object_get_boolean(j);
291                 }
292                 json_object_put(rp);
293                 return 1;
294         } else {
295                 return 0;
296         }
297 }
298
299 struct feed **ws_update_feeds(struct feed **feeds)
300 {
301         struct json_object *rp, *rq, *jfeed, *j;
302         int i, n, id;
303         struct feed *feed, **tmp;
304         const char *title, *url;
305
306         log_debug("ws_update_feeds()");
307
308         rq = ws_request_new("getFeeds");
309
310         rp = ws_execute(rq);
311         json_object_put(rq);
312
313         if (rp) {
314                 n = json_object_array_length(rp);
315
316                 for (i = 0; i < n; i++) {
317                         jfeed = json_object_array_get_idx(rp, i);
318
319                         j = json_object_object_get(jfeed, "id");
320                         id = json_object_get_int(j);
321
322                         feed = feeds_get_feed(feeds, id);
323
324                         if (!feed) {
325                                 j = json_object_object_get(jfeed, "title");
326                                 title = json_object_get_string(j);
327
328                                 j = json_object_object_get(jfeed, "feed_url");
329                                 url = json_object_get_string(j);
330
331                                 feed = feed_new(id, url, title);
332
333                                 tmp = feeds_add(feeds, feed);
334                                 free(feeds);
335                                 feeds = tmp;
336                         }
337
338                         j = json_object_object_get(jfeed, "unread");
339                         feed->unread = json_object_get_int(j);
340                 }
341                 json_object_put(rp);
342         } else {
343                 feeds_free(feeds);
344                 feeds = NULL;
345         }
346
347         log_debug("ws_update_feeds() done");
348
349         return feeds;
350 }
351
352 struct json_object *ws_request_new_set_article_unread(int id, int unread)
353 {
354         struct json_object *rq;
355
356         rq = ws_request_new("updateArticle");
357         json_object_object_add(rq, "article_ids", json_object_new_int(id));
358         json_object_object_add(rq, "field", json_object_new_int(2));
359         json_object_object_add(rq, "mode", json_object_new_int(unread));
360
361         return rq;
362 }
363
364 void ws_set_article_unread(int id, int unread)
365 {
366         struct json_object *rp, *rq;
367
368         log_debug("ws_set_article_unread(%d,%d)", id, unread);
369
370         rq = ws_request_new_set_article_unread(id, unread);
371
372         rp = ws_execute(rq);
373
374         json_object_put(rq);
375
376         if (rp)
377                 json_object_put(rp);
378
379         log_debug("ws_set_article_unread(%d,%d) done", id, unread);
380 }