From 1070b8de5c6cdf5e5958675a1b90f0af3e3f4688 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Orsini Date: Tue, 23 Apr 2013 19:54:53 +0000 Subject: [PATCH] --- src/http.c | 13 ++++- src/main.c | 7 +-- src/ttrss.c | 163 ++++++++++++++++++++++++++++++++++++++++++++++-------------- src/ttrss.h | 5 +- 4 files changed, 146 insertions(+), 42 deletions(-) diff --git a/src/http.c b/src/http.c index e663b86..55eab0f 100644 --- a/src/http.c +++ b/src/http.c @@ -28,6 +28,8 @@ #include "http.h" +static int debug = 1; + struct ucontent { char *data; size_t len; @@ -54,7 +56,8 @@ static size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp) void http_init() { - curl = curl_easy_init(); + if (!curl) + curl = curl_easy_init(); } void http_cleanup() @@ -97,12 +100,18 @@ json_object *http_json_get(const char *url, struct json_object *j) in = json_object_to_json_string(j); out = http_get(url, in); + + if (debug) + printf("HTTP request= %s\n", + json_object_to_json_string(j)); if (out) { result = json_tokener_parse(out); + if (debug) + printf("HTTP reply= %s\n", out); free(out); return result; - } + } return NULL; } diff --git a/src/main.c b/src/main.c index c9f15a9..9de84d4 100644 --- a/src/main.c +++ b/src/main.c @@ -81,9 +81,10 @@ void update() GtkTreeIter iter; char *title; - ttrss_login(g_settings_get_string(settings, "url"), - g_settings_get_string(settings, "user"), - g_settings_get_string(settings, "password")); + ws_init(g_settings_get_string(settings, "url"), + g_settings_get_string(settings, "user"), + g_settings_get_string(settings, "password")); + ws_open_session(); model = gtk_tree_view_get_model(GTK_TREE_VIEW(w_treeview)); diff --git a/src/ttrss.c b/src/ttrss.c index 18b6369..ae4a1c9 100644 --- a/src/ttrss.c +++ b/src/ttrss.c @@ -21,7 +21,6 @@ #include #include -#include #include "http.h" #include "ttrss.h" @@ -29,27 +28,48 @@ static char *session_id; static char *session_url; +static char *session_user; +static char *session_pwd; -static struct json_object *create_op(const char *op) +void ws_request_add_att_str(json_object *rq, const char *k, const char *str) { - struct json_object *j; + json_object_object_add(rq, k, json_object_new_string(str)); +} + +void ws_request_add_att_int(json_object *rq, const char *k, int v) +{ + json_object_object_add(rq, k, json_object_new_int(v)); +} + +struct json_object *ws_request_new(const char *op) +{ + struct json_object *rq; + + rq = json_object_new_object(); - j = json_object_new_object(); - json_object_object_add(j, "op", json_object_new_string(op)); + ws_request_add_att_str(rq, "op", op); - if (session_id && strcmp(op, "login")) - json_object_object_add(j, - "sid", - json_object_new_string(session_id)); + if (session_id) + ws_request_add_att_str(rq, "sid", session_id); - return j; + return rq; } -void ttrss_login(const char *url, const char *user, const char *password) +void ws_init(const char *url, const char *user, const char *pwd) { - struct json_object *content, *rp, *error, *sid, *rq; char *tmp; + if (session_id) + session_id = NULL; + + if (session_user) + free(session_user); + session_user = strdup(user); + + if (session_pwd) + free(session_pwd); + session_pwd = strdup(pwd); + if (session_url) free(session_url); @@ -58,40 +78,111 @@ void ttrss_login(const char *url, const char *user, const char *password) strcpy(session_url, tmp); strcat(session_url, "/api/"); free(tmp); +} + +struct json_object *ws_reply_get_content(struct json_object *rp) +{ + return json_object_object_get(rp, "content"); +} - rq = create_op("login"); - json_object_object_add(rq, "user", json_object_new_string(user)); - json_object_object_add(rq, - "password", - json_object_new_string(password)); +struct json_object *ws_execute(struct json_object *rq) +{ + struct json_object *rp, *content; rp = http_json_get(session_url, rq); + + if (rp) { + content = ws_reply_get_content(rp); + + if (content && !json_object_object_get(rp, "error")) + return content; + + json_object_put(rp); + } + + return NULL; +} + +int ws_get_api_version() +{ + struct json_object *rp, *rq, *j; + int v; + + rq = ws_request_new("getApiLevel"); + + rp = ws_execute(rq); + json_object_put(rq); - content = json_object_object_get(rp, "content"); - if (!content) { - fprintf(stderr, "Login failed: no content\n"); - return ; + if (rp) { + j = json_object_object_get(rp, "level"); + + if (j) + v = json_object_get_int(j); + else + v = 0; + + json_object_put(rp); + } else { + v = 0; } - error = json_object_object_get(content, "error"); - if (error) { - fprintf(stderr, "Login failed\n"); - return ; + return v; +} + +char *ws_login() +{ + struct json_object *rq, *rp, *j; + char *str; + + rq = ws_request_new("login"); + ws_request_add_att_str(rq, "user", session_user); + ws_request_add_att_str(rq, "password", session_pwd); + + rp = ws_execute(rq); + json_object_put(rq); + + if (rp) { + j = json_object_object_get(rp, "session_id"); + + if (j) + str = strdup(json_object_get_string(j)); + else + str = NULL; + + json_object_put(rp); + } else { + str = NULL; } - sid = json_object_object_get(content, "session_id"); + return str; +} - if (session_id) { +int ws_open_session() +{ + int version, result; + + if (session_id) free(session_id); - session_id = NULL; - } - session_id = strdup(json_object_get_string(sid)); + session_id = ws_login(); - printf("Session id: %s\n", session_id); + if (session_id) { + version = ws_get_api_version(); + printf("API version: %d\n", version); + + if (version > 0) { + result = 1; + } else { + free(session_id); + session_id = NULL; + result = 0; + } + } else { + result = 0; + } - json_object_put(rp); + return result; } const char *ttrss_get_headline_content(struct headline *h) @@ -101,7 +192,7 @@ const char *ttrss_get_headline_content(struct headline *h) printf("get_headlines %d\n", h->id); if (!h->content) { - rq = create_op("getArticle"); + rq = ws_request_new("getArticle"); json_object_object_add(rq, "article_id", json_object_new_int(h->id)); @@ -137,7 +228,7 @@ static struct headline **get_headlines(int feed_id) printf("get_headlines %d\n", feed_id); - rq = create_op("getHeadlines"); + rq = ws_request_new("getHeadlines"); json_object_object_add(rq, "feed_id", json_object_new_int(feed_id)); rp = http_json_get(session_url, rq); @@ -225,7 +316,7 @@ struct feed **ttrss_get_feeds() printf("ttrss_get_feeds\n"); - rq = create_op("getFeeds"); + rq = ws_request_new("getFeeds"); rp = http_json_get(session_url, rq); json_object_put(rq); @@ -284,7 +375,7 @@ void ttrss_set_article_unread(int id, int unread) printf("ttrss_set_article_unread %d %d\n", id, unread); - rq = create_op("updateArticle"); + rq = ws_request_new("updateArticle"); json_object_object_add(rq, "article_ids", json_object_new_int(id)); json_object_object_add(rq, "field", json_object_new_int(2)); json_object_object_add(rq, "mode", json_object_new_int(unread)); diff --git a/src/ttrss.h b/src/ttrss.h index 817be41..f200e59 100644 --- a/src/ttrss.h +++ b/src/ttrss.h @@ -39,10 +39,13 @@ struct feed { struct headline **headlines; }; -void ttrss_login(const char *url, const char *user, const char *password); struct feed **ttrss_get_feeds(); struct headline **ttrss_get_headlines(struct feed *); const char *ttrss_get_headline_content(struct headline *); void ttrss_set_article_unread(int id, int unread); + +void ws_init(const char *url, const char *user, const char *pwd); +int ws_open_session(); + #endif -- 2.7.4