Refactor code into utils, mfapi and mfshell

- utils is the low level library used by the others
 - mfapi uses utils and implements the api and some wrappers
 - mfshell uses mfapi and utils to provide a shell
This commit is contained in:
josch
2014-09-18 09:11:00 +02:00
parent 2d6a79062a
commit ef3b2aa844
57 changed files with 767 additions and 1345 deletions

View File

@@ -0,0 +1,105 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
* 2014 Johannes Schauer <j.schauer@email.de>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2, as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <curl/curl.h>
#include <jansson.h>
#include "../apicalls.h"
#include "../mfconn.h"
#include "../../utils/strings.h"
#include "../../utils/json.h"
#include "../../utils/http.h"
static int
_decode_file_get_info(http_t *conn, void *data);
int
mfconn_api_file_get_info(mfconn_t *mfconn,file_t *file,char *quickkey)
{
char *api_call;
int retval;
int len;
if(mfconn == NULL) return -1;
if(file == NULL) return -1;
if(quickkey == NULL) return -1;
len = strlen(quickkey);
// key must either be 11 or 15 chars
if(len != 11 && len != 15) return -1;
api_call = mfconn_create_signed_get(mfconn, 1, "file/get_info.php",
"?quick_key=%s&response_format=json", quickkey);
http_t *conn = http_create();
retval = http_get_buf(conn, api_call, _decode_file_get_info, file);
http_destroy(conn);
return retval;
}
static int
_decode_file_get_info(http_t *conn, void *data)
{
json_error_t error;
json_t *root;
json_t *node;
json_t *quickkey;
json_t *file_hash;
json_t *file_name;
json_t *file_folder;
int retval = 0;
file_t *file;
if(data == NULL) return -1;
file = (file_t *)data;
root = http_parse_buf_json(conn, 0, &error);
node = json_object_by_path(root,"response/file_info");
quickkey = json_object_get(node,"quickkey");
if(quickkey != NULL)
file_set_key(file,(char*)json_string_value(quickkey));
file_name = json_object_get(node,"filename");
if(file_name != NULL)
file_set_name(file,(char*)json_string_value(file_name));
file_hash = json_object_get(node,"hash");
if(file_hash != NULL)
{
file_set_hash(file,(char*)json_string_value(file_hash));
}
if(quickkey == NULL) retval = -1;
if(root != NULL) json_decref(root);
return retval;
}

View File

@@ -0,0 +1,123 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
* 2014 Johannes Schauer <j.schauer@email.de>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2, as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <curl/curl.h>
#include <jansson.h>
#include "../apicalls.h"
#include "../mfconn.h"
#include "../../utils/strings.h"
#include "../../utils/json.h"
#include "../../utils/http.h"
static int
_decode_file_get_links(http_t *conn, void *data);
int
mfconn_api_file_get_links(mfconn_t *mfconn,file_t *file,char *quickkey)
{
char *api_call;
int retval;
int len;
if(mfconn == NULL) return -1;
if(file == NULL) return -1;
if(quickkey == NULL) return -1;
len = strlen(quickkey);
// key must either be 11 or 15 chars
if(len != 11 && len != 15) return -1;
api_call = mfconn_create_signed_get(mfconn,0,"file/get_links.php",
"?quick_key=%s&response_format=json", quickkey);
http_t *conn = http_create();
retval = http_get_buf(conn, api_call, _decode_file_get_links, file);
http_destroy(conn);
return retval;
}
static int
_decode_file_get_links(http_t *conn, void *data)
{
json_error_t error;
json_t *root;
json_t *node;
json_t *quickkey;
json_t *share_link;
json_t *direct_link;
json_t *onetime_link;
json_t *links_array;
int retval = 0;
file_t *file;
if(data == NULL) return -1;
file = (file_t *)data;
root = http_parse_buf_json(conn, 0, &error);
node = json_object_by_path(root,"response");
links_array = json_object_get(node,"links");
if(!json_is_array(links_array))
{
json_decref(root);
return -1;
}
// just get the first one. maybe later support multi-quickkey
node = json_array_get(links_array,0);
quickkey = json_object_get(node,"quickkey");
if(quickkey != NULL)
file_set_key(file,(char*)json_string_value(quickkey));
share_link = json_object_get(node,"normal_download");
if(share_link != NULL)
file_set_share_link(file,(char*)json_string_value(share_link));
direct_link = json_object_get(node,"direct_download");
if(direct_link != NULL)
{
file_set_direct_link(file,(char*)json_string_value(direct_link));
}
onetime_link = json_object_get(node,"one_time_download");
if(onetime_link != NULL)
{
file_set_onetime_link(file,(char*)json_string_value(onetime_link));
}
// if this is false something went horribly wrong
if(share_link == NULL) retval = -1;
if(root != NULL) json_decref(root);
return retval;
}

View File

@@ -0,0 +1,73 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
* 2014 Johannes Schauer <j.schauer@email.de>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2, as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <curl/curl.h>
#include "../apicalls.h"
#include "../mfconn.h"
#include "../../utils/http.h"
#include "../../utils/strings.h"
int
mfconn_api_folder_create(mfconn_t *mfconn,char *parent,char *name)
{
char *api_call;
int retval;
if(mfconn == NULL) return -1;
if(name == NULL) return -1;
if(strlen(name) < 1) return -1;
// key must either be 11 chars or "myfiles"
if(parent != NULL)
{
if(strlen(parent) != 13)
{
// if it is myfiles, set paret to NULL
if(strcmp(parent,"myfiles") == 0) parent = NULL;
}
}
if(parent != NULL)
{
api_call = mfconn_create_signed_get(mfconn,0,"folder/create.php",
"?parent_key=%s"
"&foldername=%s"
"&response_format=json",
parent,name);
}
else
{
api_call = mfconn_create_signed_get(mfconn,0,"folder/create.php",
"?foldername=%s&response_format=json", name);
}
http_t *conn = http_create();
retval = http_get_buf(conn, api_call, NULL, NULL);
http_destroy(conn);
return retval;
}

View File

@@ -0,0 +1,191 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
* 2014 Johannes Schauer <j.schauer@email.de>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2, as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <curl/curl.h>
#include <jansson.h>
#include "../apicalls.h"
#include "../mfconn.h"
#include "../folder.h"
#include "../../utils/strings.h"
#include "../../utils/json.h"
#include "../../utils/http.h"
static int
_decode_folder_get_content_folders(http_t *conn, void *data);
static int
_decode_folder_get_content_files(http_t *conn, void *data);
long
mfconn_api_folder_get_content(mfconn_t *mfconn, int mode, folder_t *folder_curr)
{
char *api_call;
int retval;
char *rx_buffer;
char *content_type;
if(mfconn == NULL) return -1;
if(mode == 0)
content_type = "folders";
else
content_type = "files";
const char *folderkey = folder_get_key(folder_curr);
if (folderkey == NULL) {
fprintf(stderr, "folder_get_key NULL\n");
return 0;
}
/*if (folderkey[0] == '\0') {
fprintf(stderr, "folder_get_key '\\0'\n");
return 0;
}*/
api_call = mfconn_create_signed_get(mfconn,0,"folder/get_content.php",
"?folder_key=%s"
"&content_type=%s"
"&response_format=json",
folderkey,
content_type);
http_t* conn = http_create();
if(mode == 0)
retval = http_get_buf(conn, api_call, _decode_folder_get_content_folders, NULL);
else
retval = http_get_buf(conn, api_call, _decode_folder_get_content_files, NULL);
http_destroy(conn);
return retval;
}
static int
_decode_folder_get_content_folders(http_t *conn, void *user_ptr)
{
json_error_t error;
json_t *root;
json_t *node;
json_t *data;
json_t *folders_array;
json_t *folderkey;
json_t *folder_name;
char *folder_name_tmp;
int array_sz;
int i = 0;
root = http_parse_buf_json(conn, 0, &error);
/*json_t *result = json_object_by_path(root, "response/action");
fprintf(stderr, "response/action: %s\n", (char*)json_string_value(result));*/
node = json_object_by_path(root,"response/folder_content");
folders_array = json_object_get(node,"folders");
if(!json_is_array(folders_array))
{
json_decref(root);
return -1;
}
array_sz = json_array_size(folders_array);
for(i = 0;i < array_sz;i++)
{
data = json_array_get(folders_array,i);
if(json_is_object(data))
{
folderkey = json_object_get(data,"folderkey");
folder_name = json_object_get(data,"name");
if(folderkey != NULL && folder_name != NULL)
{
folder_name_tmp = strdup_printf("< %s >",
json_string_value(folder_name));
printf(" %-15.13s %s\n\r",
json_string_value(folderkey),
folder_name_tmp);
free(folder_name_tmp);
}
}
}
if(root != NULL) json_decref(root);
return 0;
}
static int
_decode_folder_get_content_files(http_t *conn, void *user_ptr)
{
json_error_t error;
json_t *root;
json_t *node;
json_t *data;
json_t *files_array;
json_t *quickkey;
json_t *file_name;
int array_sz;
int i = 0;
root = http_parse_buf_json(conn, 0, &error);
node = json_object_by_path(root,"response/folder_content");
files_array = json_object_get(node,"files");
if(!json_is_array(files_array))
{
json_decref(root);
return -1;
}
array_sz = json_array_size(files_array);
for(i = 0;i < array_sz;i++)
{
data = json_array_get(files_array,i);
if(json_is_object(data))
{
quickkey = json_object_get(data,"quickkey");
file_name = json_object_get(data,"filename");
if(quickkey != NULL && file_name != NULL)
{
printf(" %-15.15s %s\n\r",
json_string_value(quickkey),
json_string_value(file_name));
}
}
}
if(root != NULL) json_decref(root);
return 0;
}

View File

@@ -0,0 +1,130 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
* 2014 Johannes Schauer <j.schauer@email.de>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2, as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <curl/curl.h>
#include <jansson.h>
#include "../apicalls.h"
#include "../mfconn.h"
#include "../folder.h"
#include "../../utils/strings.h"
#include "../../utils/json.h"
#include "../../utils/http.h"
static int
_decode_folder_get_info(http_t *conn, void *data);
int
mfconn_api_folder_get_info(mfconn_t *mfconn,folder_t *folder,char *folderkey)
{
char *api_call;
int retval;
if(mfconn == NULL) return -1;
if(folder == NULL) return -1;
if(folderkey == NULL) return -1;
// key must either be 11 chars or "myfiles"
if(strlen(folderkey) != 13)
{
if(strcmp(folderkey,"myfiles") == 0) return -1;
}
api_call = mfconn_create_signed_get(mfconn,0,"folder/get_info.php",
"?folder_key=%s&response_format=json", folderkey);
http_t *conn = http_create();
retval = http_get_buf(conn, api_call, _decode_folder_get_info, folder);
http_destroy(conn);
return retval;
}
static int
_decode_folder_get_info(http_t *conn, void *data)
{
json_error_t error;
json_t *root;
json_t *node;
json_t *folderkey;
json_t *folder_name;
json_t *parent_folder;
int retval = 0;
folder_t *folder;
if(data == NULL) return -1;
folder = (folder_t *)data;
root = http_parse_buf_json(conn, 0, &error);
node = json_object_by_path(root,"response/folder_info");
folderkey = json_object_get(node,"folderkey");
if(folderkey != NULL)
folder_set_key(folder,(char*)json_string_value(folderkey));
folder_name = json_object_get(node,"name");
if(folder_name != NULL)
folder_set_name(folder,(char*)json_string_value(folder_name));
parent_folder = json_object_get(node,"parent_folderkey");
if(parent_folder != NULL)
{
folder_set_parent(folder,(char*)json_string_value(parent_folder));
}
// infer that the parent folder must be "myfiles" root
if(parent_folder == NULL && folderkey != NULL)
folder_set_parent(folder,"myfiles");
if(folderkey == NULL) retval = -1;
if(root != NULL) json_decref(root);
return retval;
}
// sample user callback
/*
static void
_mycallback(char *data,size_t sz,cfile_t *cfile)
{
double bytes_read;
double bytes_total;
bytes_read = cfile_get_rx_count(cfile);
bytes_total = cfile_get_rx_length(cfile);
printf("bytes read: %.0f\n\r",bytes_read);
if(bytes_read == bytes_total)
{
printf("transfer complete!\n\r");
}
return;
}
*/

View File

@@ -0,0 +1,109 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
* 2014 Johannes Schauer <j.schauer@email.de>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2, as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <curl/curl.h>
#include <jansson.h>
#include "../apicalls.h"
#include "../mfconn.h"
#include "../../utils/strings.h"
#include "../../utils/json.h"
#include "../../utils/http.h"
static int
_decode_user_get_info(http_t *conn, void *data);
int
mfconn_api_user_get_info(mfconn_t *mfconn)
{
char *api_call;
int retval;
// char *rx_buffer;
if(mfconn == NULL) return -1;
api_call = mfconn_create_signed_get(mfconn,0,"user/get_info.php",
"&response_format=json");
http_t* conn = http_create();
retval = http_get_buf(conn, api_call, _decode_user_get_info, NULL);
http_destroy(conn);
return retval;
}
static int
_decode_user_get_info(http_t *conn, void *data)
{
json_error_t error;
json_t *root;
json_t *node;
json_t *email;
json_t *first_name;
json_t *last_name;
root = http_parse_buf_json(conn, 0, &error);
node = json_object_by_path(root,"response/user_info");
email = json_object_get(node,"email");
if(email != NULL)
printf("Email: %s\n\r",(char*)json_string_value(email));
first_name = json_object_get(node,"first_name");
if(first_name != NULL)
printf("Name: %s ",(char*)json_string_value(first_name));
last_name = json_object_get(node,"last_name");
if(node != NULL)
printf("%s",(char*)json_string_value(last_name));
printf("\n\r");
if(root != NULL) json_decref(root);
return 0;
}
// sample user callback
/*
static void
_mycallback(char *data,size_t sz,cfile_t *cfile)
{
double bytes_read;
double bytes_total;
bytes_read = cfile_get_rx_count(cfile);
bytes_total = cfile_get_rx_length(cfile);
printf("bytes read: %.0f\n\r",bytes_read);
if(bytes_read == bytes_total)
{
printf("transfer complete!\n\r");
}
return;
}
*/

View File

@@ -0,0 +1,153 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
* 2014 Johannes Schauer <j.schauer@email.de>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2, as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <curl/curl.h>
#include <jansson.h>
#include "../apicalls.h"
#include "../mfconn.h"
#include "../../utils/http.h"
#include "../../utils/strings.h"
#include "../../utils/json.h"
static int
_decode_get_session_token(http_t *conn, void *data);
struct user_get_session_token_response
{
uint32_t secret_key;
char *secret_time;
char *session_token;
};
int
mfconn_api_user_get_session_token(mfconn_t *mfconn, char *server,
char *username, char *password, int app_id, char *app_key,
uint32_t *secret_key, char **secret_time, char **session_token)
{
char *login_url;
char *post_args;
char *user_signature;
int retval;
struct user_get_session_token_response response;
if(mfconn == NULL) return -1;
// configure url for operation
login_url = strdup_printf("https://%s/api/user/get_session_token.php",
server);
// create user signature
user_signature = mfconn_create_user_signature(mfconn, username, password, app_id, app_key);
post_args = strdup_printf(
"email=%s"
"&password=%s"
"&application_id=35860"
"&signature=%s"
"&token_version=2"
"&response_format=json",
username, password, user_signature);
http_t *conn = http_create();
retval = http_post_buf(conn, login_url, post_args, _decode_get_session_token, (void *)(&response));
http_destroy(conn);
free(login_url);
free(post_args);
*secret_key = response.secret_key;
*secret_time = response.secret_time;
*session_token = response.session_token;
return retval;
}
static int
_decode_get_session_token(http_t *conn, void *user_ptr)
{
json_error_t error;
json_t *root = NULL;
json_t *data;
json_t *session_token;
json_t *secret_key;
json_t *secret_time;
struct user_get_session_token_response *response;
if(user_ptr == NULL) return -1;
response = (struct user_get_session_token_response *)user_ptr;
root = http_parse_buf_json(conn, 0, &error);
data = json_object_by_path(root,"response");
if(data == NULL) return -1;
session_token = json_object_get(data,"session_token");
if(session_token == NULL)
{
json_decref(root);
return -1;
}
response->session_token = strdup(json_string_value(session_token));
secret_key = json_object_get(data,"secret_key");
if(secret_key != NULL)
response->secret_key = atoll(json_string_value(secret_key));
/*
time looks like a float but we must store it as a string to
remain congruent with the server on decimal place presentation.
*/
secret_time = json_object_get(data,"time");
if(secret_time != NULL)
response->secret_time = strdup(json_string_value(secret_time));
if(root != NULL) json_decref(root);
return 0;
}
// sample user callback
/*
static void
_mycallback(char *data,size_t sz,cfile_t *cfile)
{
double bytes_read;
double bytes_total;
bytes_read = cfile_get_rx_count(cfile);
bytes_total = cfile_get_rx_length(cfile);
printf("bytes read: %.0f\n\r",bytes_read);
if(bytes_read == bytes_total)
{
printf("transfer complete!\n\r");
}
return;
}
*/