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