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

46
mfapi/apicalls.h Normal file
View File

@@ -0,0 +1,46 @@
/*
* 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.
*
*/
#ifndef _MFSHELL_MFAPI_H_
#define _MFSHELL_FILE_INFO_H_
#include <stdint.h>
#include "file.h"
#include "folder.h"
#include "mfconn.h"
int mfconn_api_file_get_info(mfconn_t *mfconn, file_t *file, char *quickkey);
int mfconn_api_file_get_links(mfconn_t *mfconn, file_t *file, char *quickkey);
int mfconn_api_folder_create(mfconn_t *mfconn, char *parent, char *name);
long mfconn_api_folder_get_content(mfconn_t *mfconn, int mode, folder_t *folder_curr);
int mfconn_api_folder_get_info(mfconn_t *mfconn, folder_t *folder, char *folderkey);
int mfconn_api_user_get_info(mfconn_t *mfconn);
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);
#endif

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;
}
*/

267
mfapi/file.c Normal file
View File

@@ -0,0 +1,267 @@
/*
* 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 <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include "file.h"
struct _file_s
{
char quickkey[18];
char hash[65];
char name[256];
char mtime[16];
uint64_t revision;
char *share_link;
char *direct_link;
char *onetime_link;
};
file_t*
file_alloc(void)
{
file_t *file;
file = (file_t*)calloc(1,sizeof(file_t));
return file;
}
void
file_free(file_t *file)
{
if(file == NULL) return;
if(file->share_link != NULL) free(file->share_link);
if(file->direct_link != NULL) free(file->direct_link);
if(file->onetime_link != NULL) free(file->onetime_link);
free(file);
return;
}
int
file_set_key(file_t *file,const char *key)
{
int len;
if(file == NULL) return -1;
if(key == NULL) return -1;
len = strlen(key);
if(len != 11 && len != 15) return -1;
memset(file->quickkey,0,sizeof(file->quickkey));
strncpy(file->quickkey,key,sizeof(file->quickkey) - 1);
return 0;
}
const char*
file_get_key(file_t *file)
{
if(file == NULL) return NULL;
return file->quickkey;
}
int
file_set_hash(file_t *file,const char *hash)
{
if(file == NULL) return -1;
if(hash == NULL) return -1;
// system supports SHA256 (current) and MD5 (legacy)
if(strlen(hash) < 32) return -1;
memset(file->hash,0,sizeof(file->hash));
strncpy(file->hash,hash,sizeof(file->hash) - 1);
return 0;
}
const char*
file_get_hash(file_t *file)
{
if(file == NULL) return NULL;
return file->hash;
}
int
file_set_name(file_t *file,const char *name)
{
if(file == NULL) return -1;
if(name == NULL) return -1;
if(strlen(name) > 255) return -1;
memset(file->name,0,sizeof(file->name));
strncpy(file->name,name,sizeof(file->name) - 1);
return 0;
}
const char*
file_get_name(file_t *file)
{
if(file == NULL) return NULL;
return file->name;
}
int
file_set_share_link(file_t *file,const char *share_link)
{
if(file == NULL) return -1;
if(share_link == NULL) return -1;
if(file->share_link != NULL)
{
free(file->share_link);
file->share_link = NULL;
}
file->share_link = strdup(share_link);
return 0;
}
const char*
file_get_share_link(file_t *file)
{
if(file == NULL) return NULL;
return file->share_link;
}
int
file_set_direct_link(file_t *file,const char *direct_link)
{
if(file == NULL) return -1;
if(direct_link == NULL) return -1;
if(file->direct_link != NULL)
{
free(file->direct_link);
file->direct_link = NULL;
}
file->direct_link = strdup(direct_link);
return 0;
}
const char*
file_get_direct_link(file_t *file)
{
if(file == NULL) return NULL;
return file->direct_link;
}
int
file_set_onetime_link(file_t *file,const char *onetime_link)
{
if(file == NULL) return -1;
if(onetime_link == NULL) return -1;
if(file->onetime_link != NULL)
{
free(file->onetime_link);
file->onetime_link = NULL;
}
file->onetime_link = strdup(onetime_link);
return 0;
}
const char*
file_get_onetime_link(file_t *file)
{
if(file == NULL) return NULL;
return file->onetime_link;
}
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <curl/curl.h>
#include "mfconn.h"
#include "../utils/http.h"
#include "../utils/strings.h"
ssize_t
file_download_direct(file_t *file, char *local_dir)
{
const char *url;
const char *file_name;
char *file_path;
struct stat file_info;
ssize_t bytes_read = 0;
int retval;
if(file == NULL) return -1;
if(local_dir == NULL) return -1;
url = file_get_direct_link(file);
if(url == NULL) return -1;
file_name = file_get_name(file);
if(file_name == NULL) return -1;
if(strlen(file_name) < 1) return -1;
if(local_dir[strlen(local_dir) - 1] == '/')
file_path = strdup_printf("%s%s",local_dir,file_name);
else
file_path = strdup_printf("%s/%s",local_dir,file_name);
http_t *conn = http_create();
retval = http_get_file(conn, url, file_path);
http_destroy(conn);
/*
it is preferable to have the vfs tell us how many bytes the
transfer actually is. it's really all that matters.
*/
memset(&file_info,0,sizeof(file_info));
retval = stat(file_path,&file_info);
free(file_path);
if(retval != 0) return -1;
bytes_read = file_info.st_size;
return bytes_read;
}

57
mfapi/file.h Normal file
View File

@@ -0,0 +1,57 @@
/*
* 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.
*
*/
#ifndef __MFAPI_FILE_H__
#define __MFAPI_FILE_H__
typedef struct _file_s file_t;
struct _file_s;
file_t* file_alloc(void);
void file_free(file_t *file);
int file_set_key(file_t *file,const char *quickkey);
const char* file_get_key(file_t *file);
int file_set_hash(file_t *file,const char *hash);
const char* file_get_hash(file_t *file);
int file_set_name(file_t *file,const char *name);
const char* file_get_name(file_t *file);
int file_set_share_link(file_t *file,const char *share_link);
const char* file_get_share_link(file_t *file);
int file_set_direct_link(file_t *file,const char *direct_link);
const char* file_get_direct_link(file_t *file);
int file_set_onetime_link(file_t *file,const char *onetime_link);
const char* file_get_onetime_link(file_t *file);
ssize_t file_download_direct(file_t *file, char *local_dir);
#endif

126
mfapi/folder.c Normal file
View File

@@ -0,0 +1,126 @@
/*
* 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 <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include "folder.h"
struct _folder_s
{
char folderkey[20];
char name[41];
char parent[20];
uint64_t revision;
uint32_t folder_count;
uint32_t file_count;
};
folder_t*
folder_alloc(void)
{
folder_t *folder;
folder = (folder_t*)calloc(1,sizeof(folder_t));
return folder;
}
void
folder_free(folder_t *folder)
{
if(folder == NULL) return;
free(folder);
return;
}
int
folder_set_key(folder_t *folder,const char *key)
{
if(folder == NULL) return -1;
if(key == NULL) return -1;
if(strlen(key) != 13) return -1;
memset(folder->folderkey,0,sizeof(folder->folderkey));
strncpy(folder->folderkey,key,sizeof(folder->folderkey) - 1);
return 0;
}
const char*
folder_get_key(folder_t *folder)
{
if(folder == NULL) return NULL;
return folder->folderkey;
}
int
folder_set_parent(folder_t *folder,const char *parent_key)
{
if(folder == NULL) return -1;
if(parent_key == NULL) return -1;
if(strlen(parent_key) != 13)
{
if(strcmp(parent_key,"myfiles") != 0) return -1;
}
memset(folder->parent,0,sizeof(folder->parent));
strncpy(folder->parent,parent_key,sizeof(folder->parent) -1);
return 0;
}
const char*
folder_get_parent(folder_t *folder)
{
if(folder == NULL) return NULL;
return folder->parent;
}
int
folder_set_name(folder_t *folder,const char *name)
{
if(folder == NULL) return -1;
if(name == NULL) return -1;
if(strlen(name) > 40) return -1;
memset(folder->name,0,sizeof(folder->name));
strncpy(folder->name,name,sizeof(folder->name) - 1);
return 0;
}
const char*
folder_get_name(folder_t *folder)
{
if(folder == NULL) return NULL;
return folder->name;
}

43
mfapi/folder.h Normal file
View File

@@ -0,0 +1,43 @@
/*
* 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.
*
*/
#ifndef __MFAPI_FOLDER_H__
#define __MFAPI_FOLDER_H__
typedef struct _folder_s folder_t;
struct _folder_s;
folder_t* folder_alloc(void);
void folder_free(folder_t *folder);
int folder_set_key(folder_t *folder,const char *folderkey);
const char* folder_get_key(folder_t *folder);
int folder_set_parent(folder_t *folder,const char *folderkey);
const char* folder_get_parent(folder_t *folder);
int folder_set_name(folder_t *folder,const char *name);
const char* folder_get_name(folder_t *folder);
#endif

238
mfapi/mfconn.c Normal file
View File

@@ -0,0 +1,238 @@
/*
* 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 <curl/curl.h>
#include <openssl/ssl.h>
#include <openssl/sha.h>
#include <openssl/md5.h>
#include "../utils/strings.h"
#include "mfconn.h"
#include "apicalls.h"
typedef struct _mfconn_s mfconn_t;
struct _mfconn_s
{
char *server;
uint32_t secret_key;
char *secret_time;
char *session_token;
};
mfconn_t*
mfconn_create(char *server, char *username, char *password, int app_id, char *app_key)
{
mfconn_t *mfconn;
int retval;
mfconn = (mfconn_t *)calloc(1,sizeof(mfconn_t));
mfconn->server = strdup(server);
retval = mfconn_api_user_get_session_token(mfconn, mfconn->server,
username, password, app_id, app_key, &(mfconn->secret_key),
&(mfconn->secret_time), &(mfconn->session_token));
if (retval == 0)
return mfconn;
else
return NULL;
}
void
mfconn_destroy(mfconn_t *mfconn)
{
free(mfconn->server);
free(mfconn->secret_time);
free(mfconn->session_token);
free(mfconn);
}
void
mfconn_update_secret_key(mfconn_t *mfconn)
{
uint64_t new_val;
if(mfconn == NULL) return;
new_val = ((uint64_t)mfconn->secret_key) * 16807;
new_val %= 0x7FFFFFFF;
mfconn->secret_key = new_val;
return;
}
char*
mfconn_create_user_signature(mfconn_t *mfconn, char *username, char *password,
int app_id, char *app_key)
{
char *signature_raw;
unsigned char signature_enc[20]; // sha1 is 160 bits
unsigned char signature_hex[41];
int i;
if(mfconn == NULL) return NULL;
signature_raw = strdup_printf("%s%s%d%s",
username, password, app_id, app_key);
SHA1((const unsigned char *)signature_raw,
strlen(signature_raw),signature_enc);
free(signature_raw);
for(i = 0;i < 20;i++)
{
sprintf(&signature_hex[i*2],"%02x",signature_enc[i]);
}
signature_hex[40] = '\0';
return strdup((const char *)signature_hex);
}
char*
mfconn_create_call_signature(mfconn_t *mfconn,char *url,char *args)
{
char *signature_raw;
unsigned char signature_enc[16]; // md5 is 128 bits
unsigned char signature_hex[33];
char *api;
int i;
if(mfconn == NULL) return NULL;
if(url == NULL) return NULL;
if(args == NULL) return NULL;
// printf("url: %s\n\rargs: %s\n\r",url,args);
api = strstr(url,"/api/");
if(api == NULL) return NULL;
signature_raw = strdup_printf("%d%s%s%s",
(mfconn->secret_key % 256),
mfconn->secret_time,
api,args);
MD5((const unsigned char *)signature_raw,
strlen(signature_raw),signature_enc);
free(signature_raw);
for(i = 0;i < 16;i++)
{
sprintf(&signature_hex[i*2],"%02x",signature_enc[i]);
}
signature_hex[32] = '\0';
return strdup((const char *)signature_hex);
}
char*
mfconn_create_signed_get(mfconn_t *mfconn,int ssl,char *api,char *fmt,...)
{
char *api_request = NULL;
char *api_args = NULL;
char *signature;
char *call_hash;
char *session_token;
int bytes_to_alloc;
int api_request_len;
int api_args_len;
int api_len;
va_list ap;
if(mfconn == NULL) return NULL;
if(mfconn->server == NULL) return NULL;
if(mfconn->secret_time == NULL) return NULL;
if(mfconn->session_token == NULL) return NULL;
// make sure the api (ex: user/get_info.php) is sane
if(api == NULL) return NULL;
api_len = strlen(api);
if(api_len < 3) return NULL;
// calculate how big of a buffer we need
va_start(ap, fmt);
api_args_len = (vsnprintf(NULL, 0, fmt, ap) + 1); // + 1 for NULL
va_end(ap);
session_token = strdup_printf("&session_token=%s", mfconn->session_token);
api_args_len += strlen(session_token);
// create the correctly sized buffer and process the args
api_args = (char*)calloc(api_args_len,sizeof(char));
// printf("\n\r%d\n\r",api_args_len);
va_start(ap, fmt);
vsnprintf(api_args, api_args_len, fmt, ap);
va_end(ap);
strcat(api_args, session_token);
free(session_token);
// correct user error of trailing slash
if(api[api_len - 1] == '/') api[api_len - 1] = '\0';
api_request = strdup_printf("%s//%s/api/%s",
(ssl ? "https:" : "http:"), mfconn->server,api);
call_hash = mfconn_create_call_signature(mfconn,api_request,api_args);
signature = strdup_printf("&signature=%s",call_hash);
free(call_hash);
// compute the amount of space requred to realloc() the request
bytes_to_alloc = api_args_len;
bytes_to_alloc += strlen(api_request);
bytes_to_alloc += strlen(signature);
bytes_to_alloc += 1; // null termination
// append api GET args to api request
api_request = (char*)realloc(api_request,bytes_to_alloc);
strncat(api_request,api_args,api_args_len);
strcat(api_request,signature);
free(signature);
free(api_args);
return api_request;
}
const char*
mfconn_get_session_token(mfconn_t *mfconn)
{
return mfconn->session_token;
}
const char*
mfconn_get_secret_time(mfconn_t *mfconn)
{
return mfconn->secret_time;
}
uint32_t
mfconn_get_secret_key(mfconn_t *mfconn)
{
return mfconn->secret_key;
}

48
mfapi/mfconn.h Normal file
View File

@@ -0,0 +1,48 @@
/*
* Copyright (C) 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.
*
*/
#ifndef __MFAPI_MFCONN_H__
#define __MFAPI_MFCONN_H__
#include <stdint.h>
#include "file.h"
typedef struct _mfconn_s mfconn_t;
struct _mfconn_s;
mfconn_t* mfconn_create(char *server, char *username, char *password, int app_id, char *app_key);
void mfconn_destroy(mfconn_t *mfconn);
ssize_t mfconn_download_direct(file_t *file,char *local_dir);
char* mfconn_create_signed_get(mfconn_t *mfconn,int ssl,char *api,char *fmt,...);
char* mfconn_create_user_signature(mfconn_t *mfconn, char *username,
char *password, int app_id, char *app_key);
void mfconn_update_secret_key(mfconn_t *mfconn);
const char* mfconn_get_session_token(mfconn_t *mfconn);
const char* mfconn_get_secret_time(mfconn_t *mfconn);
uint32_t mfconn_get_secret_key(mfconn_t *mfconn);
#endif