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