(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 <stdlib.h>
21 #include <string.h>
22 #include <stdio.h>
23
24 #include <json/json.h>
25
26 #include "http.h"
27 #include "log.h"
28 #include "ttrss_ws.h"
29 #include "url.h"
30
31 static char *session_id;
32 static char *session_url;
33 static char *session_user;
34 static char *session_pwd;
35
36 void ws_request_add_att_str(json_object *rq, const char *k, const char *str)
37 {
38         json_object_object_add(rq, k, json_object_new_string(str));
39 }
40
41 void ws_request_add_att_int(json_object *rq, const char *k, int v)
42 {
43         json_object_object_add(rq, k, json_object_new_int(v));
44 }
45
46 struct json_object *ws_request_new(const char *op)
47 {
48         struct json_object *rq;
49
50         rq = json_object_new_object();
51
52         ws_request_add_att_str(rq, "op", op);
53
54         if (session_id)
55                 ws_request_add_att_str(rq, "sid", session_id);
56
57         return rq;
58 }
59
60 void ws_init(const char *url, const char *user, const char *pwd)
61 {
62         char *tmp;
63
64         if (session_url && !strcmp(session_url, url)
65             && session_user && !strcmp(session_user, user)
66             && session_pwd && !strcmp(session_pwd, pwd))
67                 return ;
68
69         free(session_id);
70         session_id = NULL;
71
72         free(session_user);
73         session_user = strdup(user);
74
75         free(session_pwd);
76         session_pwd = strdup(pwd);
77
78         tmp = url_normalize(url);
79         free(session_url);
80         session_url = malloc(strlen(tmp) + strlen("/api/") + 1);
81         strcpy(session_url, tmp);
82         strcat(session_url, "/api/");
83         free(tmp);
84 }
85
86 struct json_object *ws_reply_get_content(struct json_object *rp)
87 {
88         return json_object_object_get(rp, "content");
89 }
90
91 struct json_object *ws_execute(struct json_object *rq)
92 {
93         struct json_object *rp, *content;
94
95         rp = http_json_get(session_url, rq);
96
97         if (rp) {
98                 content = ws_reply_get_content(rp);
99
100                 if (content && !json_object_object_get(rp, "error")) {
101                         json_object_get(content);
102                         json_object_put(rp);
103                         return content;
104                 }
105
106                 json_object_put(rp);
107         }
108
109         return NULL;
110 }
111
112 int ws_get_api_version()
113 {
114         struct json_object *rp, *rq;
115         int v;
116
117         rq = ws_request_new("getApiLevel");
118
119         rp = ws_execute(rq);
120
121         json_object_put(rq);
122
123         if (rp) {
124                 v = json_object_get_int(json_object_object_get(rp, "level"));
125
126                 json_object_put(rp);
127         } else {
128                 v = 0;
129         }
130
131         return v;
132 }
133
134 char *ws_login()
135 {
136         struct json_object *rq, *rp, *j;
137         char *str;
138
139         rq = ws_request_new("login");
140         ws_request_add_att_str(rq, "user", session_user);
141         ws_request_add_att_str(rq, "password", session_pwd);
142
143         rp = ws_execute(rq);
144         json_object_put(rq);
145
146         str = NULL;
147         if (rp) {
148                 j = json_object_object_get(rp, "session_id");
149
150                 if (j)
151                         str = strdup(json_object_get_string(j));
152
153                 json_object_put(rp);
154         } 
155
156         return str;
157 }
158
159 int ws_open_session()
160 {
161         int version, result;
162
163         if (session_id)
164                 free(session_id);
165
166         session_id = ws_login();
167
168         if (session_id) {
169                 version = ws_get_api_version();
170                 log_debug("API version= %d", version);
171
172                 if (version > 0) {
173                         result = 1;
174                 } else {
175                         free(session_id);
176                         session_id = NULL;
177                         result = 0;
178                 }
179         } else {
180                 result =  0;
181         }
182
183         return result;
184 }
185
186 char *ws_get_article_content(int id)
187 {
188         struct json_object *rp, *rq, *content, *item;
189         char *str;
190
191         rq = ws_request_new("getArticle");
192         ws_request_add_att_int(rq, "article_id", id);
193
194         rp = ws_execute(rq);
195
196         json_object_put(rq);
197
198         str = NULL;
199
200         if (rp) {
201                 item = json_object_array_get_idx(rp, 0);
202
203                 if (item) {
204                         content = json_object_object_get(item, "content");
205                         str = strdup(json_object_get_string(content));
206                 }
207
208                 json_object_put(rp);
209         }
210
211         return str;
212 }
213
214 int ws_update_headlines(struct feed *feed)
215 {
216         struct json_object *rp, *rq, *jheadline, *j;
217         int i, n, err, hid;
218         struct headline *h, **tmp;
219         const char *title, *url;
220
221         rq = ws_request_new("getHeadlines");
222         ws_request_add_att_int(rq, "feed_id", feed->id);
223
224         rp = ws_execute(rq);
225
226         json_object_put(rq);
227
228         if (rp) {
229                 n = json_object_array_length(rp);
230                 for (i = 0; i < n; i++) {
231                         jheadline = json_object_array_get_idx(rp, i);
232
233                         j = json_object_object_get(jheadline, "id");
234                         hid = json_object_get_int(j);
235                         h = feed_get_headline(feed, hid);
236
237                         if (!h) {
238                                 j = json_object_object_get(jheadline, "title");
239                                 title = json_object_get_string(j);
240
241                                 j = json_object_object_get(jheadline, "link");
242                                 url = json_object_get_string(j);
243
244                                 h = headline_new(hid, url, title);
245
246                                 tmp = headlines_add(feed->headlines, h);
247                                 if (feed->headlines)
248                                         free(feed->headlines);
249                                 feed->headlines = tmp;
250                         }
251
252                         j = json_object_object_get(jheadline, "unread");
253                         h->unread = json_object_get_boolean(j);
254                 }
255                 err = 0;
256         } else {
257                 err = 1;
258         }
259
260         json_object_put(rp);
261         return !err;
262 }
263
264 struct feed **ws_update_feeds(struct feed **feeds)
265 {
266         struct json_object *rp, *rq, *jfeed, *j;
267         int i, n, id;
268         struct feed *feed, **tmp;
269         const char *title, *url;
270
271         log_debug("ttrss_get_feeds()");
272
273         rq = ws_request_new("getFeeds");
274
275         rp = ws_execute(rq);
276         json_object_put(rq);
277
278         if (rp) {
279                 n = json_object_array_length(rp);
280
281                 for (i = 0; i < n; i++) {
282                         jfeed = json_object_array_get_idx(rp, i);
283
284                         j = json_object_object_get(jfeed, "id");
285                         id = json_object_get_int(j);
286
287                         feed = feeds_get_feed(feeds, id);
288
289                         if (!feed) {
290                                 j = json_object_object_get(jfeed, "title");
291                                 title = json_object_get_string(j);
292
293                                 j = json_object_object_get(jfeed, "feed_url");
294                                 url = json_object_get_string(j);
295
296                                 feed = feed_new(id, url, title);
297
298                                 tmp = feeds_add(feeds, feed);
299                                 free(feeds);
300                                 feeds = tmp;
301                         }
302
303                         j = json_object_object_get(jfeed, "unread");
304                         feed->unread = json_object_get_int(j);
305                 }
306         } else {
307                 feeds_free(feeds);
308                 feeds = NULL;
309         }
310
311         json_object_put(rp);
312
313         log_debug("ttrss_get_feeds() done");
314
315         return feeds;
316 }