e62fa184c80b3216b18d6920a24319a38794c3d4
[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 <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include <pthread.h>
25
26 #include <json/json.h>
27
28 #include "http.h"
29 #include "log.h"
30 #include "ttrss_ws.h"
31 #include "url.h"
32
33 static char *session_id;
34 static char *session_url;
35 static char *session_user;
36 static char *session_pwd;
37
38 static pthread_mutex_t lock;
39 static struct http_session *session;
40
41 void ws_request_add_att_str(json_object *rq, const char *k, const char *str)
42 {
43         json_object_object_add(rq, k, json_object_new_string(str));
44 }
45
46 void ws_request_add_att_int(json_object *rq, const char *k, int v)
47 {
48         json_object_object_add(rq, k, json_object_new_int(v));
49 }
50
51 void ws_request_add_att_bool(json_object *rq, const char *k, int v)
52 {
53         json_object_object_add(rq, k, json_object_new_boolean(v));
54 }
55
56 struct json_object *ws_request_new(const char *op)
57 {
58         struct json_object *rq;
59
60         rq = json_object_new_object();
61
62         ws_request_add_att_str(rq, "op", op);
63
64         if (session_id)
65                 ws_request_add_att_str(rq, "sid", session_id);
66
67         return rq;
68 }
69
70 void ws_set_config(const char *url, const char *user, const char *pwd)
71 {
72         char *tmp;
73
74         if (session_url && !strcmp(session_url, url)
75             && session_user && !strcmp(session_user, user)
76             && session_pwd && !strcmp(session_pwd, pwd))
77                 return ;
78
79         free(session_id);
80         session_id = NULL;
81
82         free(session_user);
83         session_user = strdup(user);
84
85         free(session_pwd);
86         session_pwd = strdup(pwd);
87
88         tmp = url_normalize(url);
89         free(session_url);
90         session_url = malloc(strlen(tmp) + strlen("/api/") + 1);
91         strcpy(session_url, tmp);
92         strcat(session_url, "/api/");
93         free(tmp);
94 }
95
96 struct json_object *ws_reply_get_content(struct json_object *rp)
97 {
98         log_debug("ws_reply_get_content");
99         return json_object_object_get(rp, "content");
100 }
101
102 static const char *ws_reply_get_error(struct json_object *content)
103 {
104         struct json_object *jerror;
105
106         log_debug("ws_reply_get_error");
107
108         if (json_object_get_type(content) != json_type_object)
109                 return NULL;
110
111         jerror = json_object_object_get(content, "error");
112
113         if (!jerror)
114                 return NULL;
115
116         return json_object_get_string(jerror);
117 }
118
119 static struct json_object *
120 execute(struct http_session *sess, struct json_object *rq, char **err)
121 {
122         struct json_object *rp, *content;
123         const char *str;
124
125         rp = http_json_get(sess, session_url, rq);
126
127         content = NULL;
128
129         if (rp) {
130                 content = ws_reply_get_content(rp);
131
132                 if (content) {
133                         str = ws_reply_get_error(content);
134
135                         if (str) {
136                                 *err = strdup(str);
137                                 content = NULL;
138                         } else {
139                                 json_object_get(content);
140                         }
141                 }
142
143                 json_object_put(rp);
144         }
145
146         return content;
147 }
148
149 struct json_object *ws_execute(struct json_object *rq)
150 {
151         char *err;
152         struct json_object *result;
153
154         log_debug("ws_execute()");
155         pthread_mutex_lock(&lock);
156         log_debug("ws_execute() lock");
157
158         err = NULL;
159         result = execute(session, rq, &err);
160
161         if (err) {
162                 log_debug("ws_execute(): error=%s", err);
163
164                 if (!strcmp(err, "NOT_LOGGED_IN")) {
165                         ws_open_session();
166                         result = execute(session, rq, NULL);
167                 }
168
169                 free(err);
170         }
171
172         log_debug("ws_execute() unlock");
173         pthread_mutex_unlock(&lock);
174
175         log_debug("ws_execute()");
176
177         return result;
178 }
179
180 int ws_get_api_version()
181 {
182         struct json_object *rp, *rq;
183         int v;
184
185         rq = ws_request_new("getApiLevel");
186
187         rp = ws_execute(rq);
188
189         json_object_put(rq);
190
191         if (rp) {
192                 v = json_object_get_int(json_object_object_get(rp, "level"));
193
194                 json_object_put(rp);
195         } else {
196                 v = 0;
197         }
198
199         return v;
200 }
201
202 char *ws_login()
203 {
204         struct json_object *rq, *rp, *j;
205         char *str;
206
207         rq = ws_request_new("login");
208         ws_request_add_att_str(rq, "user", session_user);
209         ws_request_add_att_str(rq, "password", session_pwd);
210
211         rp = ws_execute(rq);
212         json_object_put(rq);
213
214         str = NULL;
215         if (rp) {
216                 j = json_object_object_get(rp, "session_id");
217
218                 if (j)
219                         str = strdup(json_object_get_string(j));
220
221                 json_object_put(rp);
222         }
223
224         return str;
225 }
226
227 int ws_open_session()
228 {
229         int version, result;
230
231         log_debug("ws_open_session()");
232
233         if (session_id)
234                 free(session_id);
235
236         session_id = NULL;
237         session_id = ws_login();
238
239         if (session_id) {
240                 version = ws_get_api_version();
241                 log_debug("API version= %d", version);
242
243                 if (version > 0) {
244                         result = 1;
245                 } else {
246                         free(session_id);
247                         session_id = NULL;
248                         result = 0;
249                 }
250         } else {
251                 result =  0;
252         }
253
254         return result;
255 }
256
257 char *ws_get_article_content(int id)
258 {
259         struct json_object *rp, *rq, *content, *item;
260         char *str;
261
262         rq = ws_request_new("getArticle");
263         ws_request_add_att_int(rq, "article_id", id);
264
265         rp = ws_execute(rq);
266
267         json_object_put(rq);
268
269         str = NULL;
270
271         if (rp) {
272                 item = json_object_array_get_idx(rp, 0);
273
274                 if (item) {
275                         content = json_object_object_get(item, "content");
276                         str = strdup(json_object_get_string(content));
277                 }
278
279                 json_object_put(rp);
280         }
281
282         return str;
283 }
284
285 int ws_update_headlines(struct feed *feed)
286 {
287         struct json_object *rp, *rq, *jheadline, *j;
288         int i, n, hid;
289         struct headline *h, **tmp;
290         const char *title, *url;
291
292         rq = ws_request_new("getHeadlines");
293         ws_request_add_att_int(rq, "feed_id", feed->id);
294         ws_request_add_att_bool(rq, "show_excerpt", 1);
295
296         rp = ws_execute(rq);
297
298         json_object_put(rq);
299
300         if (rp) {
301                 n = json_object_array_length(rp);
302                 for (i = 0; i < n; i++) {
303                         jheadline = json_object_array_get_idx(rp, i);
304
305                         j = json_object_object_get(jheadline, "id");
306                         hid = json_object_get_int(j);
307                         h = feed_get_headline(feed, hid);
308
309                         if (!h) {
310                                 j = json_object_object_get(jheadline, "title");
311                                 title = json_object_get_string(j);
312
313                                 j = json_object_object_get(jheadline, "link");
314                                 url = json_object_get_string(j);
315
316                                 h = headline_new(hid, url, title);
317
318                                 j = json_object_object_get(jheadline,
319                                                            "excerpt");
320                                 h->excerpt = strdup(json_object_get_string(j));
321
322                                 j = json_object_object_get(jheadline,
323                                                            "updated");
324                                 h->date = json_object_get_int(j);
325
326                                 tmp = headlines_add(feed->headlines, h);
327                                 if (feed->headlines)
328                                         free(feed->headlines);
329                                 feed->headlines = tmp;
330                         }
331
332                         j = json_object_object_get(jheadline, "unread");
333                         h->unread = json_object_get_boolean(j);
334                 }
335
336                 if (!feed->headlines) {
337                         feed->headlines = malloc(sizeof(struct headline *));
338                         *(feed->headlines) = NULL;
339                 }
340
341                 json_object_put(rp);
342                 return 1;
343         } else {
344                 return 0;
345         }
346 }
347
348 static struct feed **
349 feeds_update(struct feed **feeds, struct json_object *jarray)
350 {
351         int i, n, id;
352         struct json_object *jfeed, *j;
353         const char *url, *title;
354         struct feed *feed, **tmp;
355
356         if (jarray) {
357                 n = json_object_array_length(jarray);
358
359                 for (i = 0; i < n; i++) {
360                         jfeed = json_object_array_get_idx(jarray, i);
361
362                         j = json_object_object_get(jfeed, "id");
363                         id = json_object_get_int(j);
364
365                         feed = feeds_get_feed(feeds, id);
366
367                         if (!feed) {
368                                 j = json_object_object_get(jfeed, "title");
369                                 title = json_object_get_string(j);
370
371                                 j = json_object_object_get(jfeed, "feed_url");
372                                 url = json_object_get_string(j);
373
374                                 feed = feed_new(id, url, title);
375
376                                 tmp = feeds_add(feeds, feed);
377                                 free(feeds);
378                                 feeds = tmp;
379                         }
380
381                         j = json_object_object_get(jfeed, "unread");
382                         feed->unread = json_object_get_int(j);
383
384                         ws_update_headlines(feed);
385                 }
386         }
387
388         return feeds;
389 }
390
391 struct feed **ws_update_feeds(struct feed **feeds)
392 {
393         struct json_object *rp, *rq;
394
395         log_debug("ws_update_feeds()");
396
397         rq = ws_request_new("getFeeds");
398         ws_request_add_att_int(rq, "cat_id", 0);
399
400         rp = ws_execute(rq);
401
402         if (rp) {
403                 feeds = feeds_update(feeds, rp);
404                 json_object_put(rp);
405         }
406
407         ws_request_add_att_int(rq, "cat_id", -3);
408         rp = ws_execute(rq);
409         json_object_put(rq);
410
411         if (rp) {
412                 feeds = feeds_update(feeds, rp);
413                 json_object_put(rp);
414         }
415
416         log_debug("ws_update_feeds() done");
417
418         return feeds;
419 }
420
421 struct json_object *ws_request_new_set_article_unread(int id, int unread)
422 {
423         struct json_object *rq;
424
425         rq = ws_request_new("updateArticle");
426         json_object_object_add(rq, "article_ids", json_object_new_int(id));
427         json_object_object_add(rq, "field", json_object_new_int(2));
428         json_object_object_add(rq, "mode", json_object_new_int(unread));
429
430         return rq;
431 }
432
433 void ws_set_article_unread(int id, int unread)
434 {
435         struct json_object *rp, *rq;
436
437         log_debug("ws_set_article_unread(%d,%d)", id, unread);
438
439         rq = ws_request_new_set_article_unread(id, unread);
440
441         rp = ws_execute(rq);
442
443         json_object_put(rq);
444
445         if (rp)
446                 json_object_put(rp);
447
448         log_debug("ws_set_article_unread(%d,%d) done", id, unread);
449 }
450
451 void ws_init()
452 {
453         pthread_mutexattr_t attr;
454         pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
455         pthread_mutex_init(&lock, &attr);
456
457         session = http_session_new();
458 }