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