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