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