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