c4c2c4f096660e8923ea4d203e4c86556e4bd3ad
[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                 result = 1;
249         } else {
250                 result =  0;
251         }
252
253         return result;
254 }
255
256 char *ws_get_article_content(int id)
257 {
258         struct json_object *rp, *rq, *content, *item;
259         char *str;
260
261         rq = ws_request_new("getArticle");
262         ws_request_add_att_int(rq, "article_id", id);
263
264         rp = ws_execute(rq);
265
266         json_object_put(rq);
267
268         str = NULL;
269
270         if (rp) {
271                 item = json_object_array_get_idx(rp, 0);
272
273                 if (item) {
274                         content = json_object_object_get(item, "content");
275                         str = strdup(json_object_get_string(content));
276                 }
277
278                 json_object_put(rp);
279         }
280
281         return str;
282 }
283
284 int ws_update_headlines(struct feed *feed)
285 {
286         struct json_object *rp, *rq, *jheadline, *j;
287         int i, n, hid;
288         struct headline *h, **tmp;
289         const char *title, *url;
290
291         rq = ws_request_new("getHeadlines");
292         ws_request_add_att_int(rq, "feed_id", feed->id);
293         ws_request_add_att_bool(rq, "show_excerpt", 1);
294
295         rp = ws_execute(rq);
296
297         json_object_put(rq);
298
299         if (rp) {
300                 n = json_object_array_length(rp);
301                 for (i = 0; i < n; i++) {
302                         jheadline = json_object_array_get_idx(rp, i);
303
304                         j = json_object_object_get(jheadline, "id");
305                         hid = json_object_get_int(j);
306                         h = feed_get_headline(feed, hid);
307
308                         if (!h) {
309                                 j = json_object_object_get(jheadline, "title");
310                                 title = json_object_get_string(j);
311
312                                 j = json_object_object_get(jheadline, "link");
313                                 url = json_object_get_string(j);
314
315                                 h = headline_new(hid, url, title);
316
317                                 j = json_object_object_get(jheadline,
318                                                            "excerpt");
319                                 h->excerpt = strdup(json_object_get_string(j));
320
321                                 j = json_object_object_get(jheadline,
322                                                            "updated");
323                                 h->date = json_object_get_int(j);
324
325                                 tmp = headlines_add(feed->headlines, h);
326                                 if (feed->headlines)
327                                         free(feed->headlines);
328                                 feed->headlines = tmp;
329                         }
330
331                         j = json_object_object_get(jheadline, "unread");
332                         h->unread = json_object_get_boolean(j);
333                 }
334                 json_object_put(rp);
335                 return 1;
336         } else {
337                 return 0;
338         }
339 }
340
341 struct feed **ws_update_feeds(struct feed **feeds)
342 {
343         struct json_object *rp, *rq, *jfeed, *j;
344         int i, n, id;
345         struct feed *feed, **tmp;
346         const char *title, *url;
347
348         log_debug("ws_update_feeds()");
349
350         rq = ws_request_new("getFeeds");
351
352         rp = ws_execute(rq);
353
354         json_object_put(rq);
355
356         if (rp) {
357                 n = json_object_array_length(rp);
358
359                 for (i = 0; i < n; i++) {
360                         jfeed = json_object_array_get_idx(rp, 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                 json_object_put(rp);
387         } else {
388                 feeds_free(feeds);
389                 feeds = NULL;
390         }
391
392         log_debug("ws_update_feeds() done");
393
394         return feeds;
395 }
396
397 struct json_object *ws_request_new_set_article_unread(int id, int unread)
398 {
399         struct json_object *rq;
400
401         rq = ws_request_new("updateArticle");
402         json_object_object_add(rq, "article_ids", json_object_new_int(id));
403         json_object_object_add(rq, "field", json_object_new_int(2));
404         json_object_object_add(rq, "mode", json_object_new_int(unread));
405
406         return rq;
407 }
408
409 void ws_set_article_unread(int id, int unread)
410 {
411         struct json_object *rp, *rq;
412
413         log_debug("ws_set_article_unread(%d,%d)", id, unread);
414
415         rq = ws_request_new_set_article_unread(id, unread);
416
417         rp = ws_execute(rq);
418
419         json_object_put(rq);
420
421         if (rp)
422                 json_object_put(rp);
423
424         log_debug("ws_set_article_unread(%d,%d) done", id, unread);
425 }
426
427 void ws_init()
428 {
429         pthread_mutexattr_t attr;
430         pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
431         pthread_mutex_init(&lock, &attr);
432
433         session = http_session_new();
434 }