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