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