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