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