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

@@ -2,10 +2,11 @@ cmake_minimum_required(VERSION 2.8)
project(mediafire-tools)
set(mf-sources account.c cmd_auth.c cmd_chdir.c cmd_debug.c cmd_file.c cmd_get.c cmd_help.c cmd_host.c cmd_lcd.c cmd_links.c cmd_list.c cmd_lpwd.c cmd_mkdir.c cmd_pwd.c cmd_whoami.c connection.c console.c download.c file.c file_info.c file_links.c folder.c folder_create.c folder_info.c json.c keys.c list.c mfshell.c signals.c signature.c strings.c stringv.c user_session.c)
set(mf-headers account.h chdir.h command.h connection.h console.h download.h file_info.h file_links.h folder_create.h folder_info.h json.h list.h macros.h mfshell.h private.h signals.h strings.h stringv.h user_session.h)
add_library(mf-obj OBJECT ${mf-sources} ${mf-headers})
add_library(mfapi SHARED mfapi/mfconn.c mfapi/file.c mfapi/folder.c mfapi/apicalls/file_get_info.c mfapi/apicalls/user_get_info.c mfapi/apicalls/file_get_links.c mfapi/apicalls/user_session.c mfapi/apicalls/folder_get_info.c mfapi/apicalls/folder_create.c mfapi/apicalls/folder_get_content.c)
add_executable(mfshell main.c $<TARGET_OBJECTS:mf-obj>)
target_link_libraries(mfshell curl ssl crypto jansson)
add_library(mfutils SHARED utils/http.c utils/json.c utils/strings.c utils/stringv.c)
add_executable(mfshell mfshell/main.c mfshell/mfshell.c mfshell/commands/auth.c mfshell/commands/chdir.c mfshell/commands/debug.c mfshell/commands/file.c mfshell/commands/get.c mfshell/commands/help.c mfshell/commands/host.c mfshell/commands/lcd.c mfshell/commands/links.c mfshell/commands/list.c mfshell/commands/lpwd.c mfshell/commands/mkdir.c mfshell/commands/pwd.c mfshell/commands/whoami.c)
target_link_libraries(mfshell curl ssl crypto jansson mfapi mfutils)

View File

@@ -1,28 +0,0 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
*
* 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_ACCOUNT_H_
#define _MFSHELL_ACCOUNT_H_
#include "mfshell.h"
int _user_get_info(_mfshell_t *mfshell);
#endif

28
chdir.h
View File

@@ -1,28 +0,0 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
*
* 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_CHDIR_H_
#define _MFSHELL_CHDIR_H_
#include "mfshell.h"
int
_folder_chdir(mfshell_t *mfshell,char *folder_key);
#endif

150
console.c
View File

@@ -1,150 +0,0 @@
/*
* 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 <termios.h>
#include <unistd.h>
#include <wordexp.h>
#include <sys/ioctl.h>
#include "mfshell.h"
#include "console.h"
#include "private.h"
#include "stringv.h"
int
console_control_mode(int mode)
{
static struct termios saved_state;
struct termios new_state;
int retval = 0;
switch(mode)
{
case CONSOLE_STATE_SAVE:
{
retval = tcgetattr(STDIN_FILENO,&saved_state);
break;
}
case CONSOLE_STATE_RESTORE:
{
retval = tcsetattr(STDIN_FILENO,TCSANOW,&saved_state);
break;
}
case CONSOLE_ECHO_OFF:
{
tcgetattr(STDIN_FILENO,&new_state);
new_state.c_lflag &= (~ECHO);
// new_state.c_cc[VERASE] = '\b';
// new_state.c_lflag &= (~ECHOE);
tcsetattr(STDIN_FILENO,TCSANOW,&new_state);
break;
}
case CONSOLE_ECHO_ON:
{
tcgetattr(STDIN_FILENO,&new_state);
new_state.c_lflag |= ECHO;
tcsetattr(STDIN_FILENO,TCSANOW,&new_state);
break;
}
default:
retval = -1;
}
return retval;
}
int
console_get_metrics(int *height,int *width)
{
struct winsize metrics;
if(height == NULL || width == NULL) return -1;
ioctl(0,TIOCGWINSZ,&metrics);
*height = metrics.ws_row;
*width = metrics.ws_col;
return 0;
}
int
_execute(mfshell_t *mfshell, int argc, char **argv)
{
_cmd_t* curr_cmd;
for (curr_cmd = mfshell->commands; curr_cmd->name != NULL; curr_cmd++) {
if (strcmp(argv[0], curr_cmd->name) == 0) {
return curr_cmd->handler(mfshell, argc, argv);
}
}
}
int
_execute_shell_command(mfshell_t *mfshell,char *command)
{
extern int term_resized;
extern int term_height;
extern int term_width;
wordexp_t p;
int retval;
if(mfshell == NULL) return -1;
if(command == NULL) return -1;
// check to see if the terminal has been resized
if(term_resized == 1)
{
if(console_get_metrics(&term_height,&term_width) == 0)
{
term_resized = 1;
}
}
if(term_width < 30)
{
printf("[EE] terminal size to small\n\r");
return -1;
}
// FIXME: handle non-zero return value of wordexp
retval = wordexp(command, &p, WRDE_SHOWERR | WRDE_UNDEF);
if (p.we_wordc < 1)
return 0;
if (p.we_wordv[0] == NULL)
return 0;
// TODO: handle retval
retval = _execute(mfshell, p.we_wordc, p.we_wordv);
wordfree(&p);
return 0;
}

View File

@@ -1,47 +0,0 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
*
* 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_CONSOLE_H_
#define _MFSHELL_CONSOLE_H_
enum
{
CONSOLE_STATE_SAVE = 0x01,
CONSOLE_STATE_RESTORE,
CONSOLE_ECHO_OFF,
CONSOLE_ECHO_ON
};
int console_control_mode(int mode);
#define console_save_state() \
console_control_mode(CONSOLE_STATE_SAVE)
#define console_restore_state() \
console_control_mode(CONSOLE_STATE_RESTORE)
#define console_echo_off() \
console_control_mode(CONSOLE_ECHO_OFF)
#define console_echo_on() \
console_control_mode(CONSOLE_ECHO_ON)
int console_get_metrics(int *height,int *width);
#endif

View File

@@ -1,82 +0,0 @@
/*
* 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 <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <curl/curl.h>
#include "mfshell.h"
#include "private.h"
#include "account.h"
#include "strings.h"
#include "download.h"
#include "connection.h"
ssize_t
download_direct(mfshell_t *mfshell, 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);
conn_t *conn = conn_create();
retval = conn_get_file(conn, url, file_path);
conn_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;
}

View File

@@ -1,27 +0,0 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
*
* 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_FILE_INFO_H_
#define _MFSHELL_FILE_INFO_H_
#include "mfshell.h"
int _file_get_info(mfshell_t *mfshell,file_t *file,char *quickkey);
#endif

View File

@@ -1,27 +0,0 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
*
* 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_FILE_LINKS_H_
#define _MFSHELL_FILE_LINKS_H_
#include "mfshell.h"
int _file_get_links(mfshell_t *mfshell,file_t *file,char *quickkey);
#endif

View File

@@ -1,27 +0,0 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
*
* 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_FOLDER_CREATE_H_
#define _MFSHELL_FOLDER_CREATE_H_
#include "mfshell.h"
int _folder_create(mfshell_t *mfshell,char *parent,char *name);
#endif

View File

@@ -1,27 +0,0 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
*
* 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_FOLDER_INFO_H_
#define _MFSHELL_FOLDER_INFO_H_
#include "mfshell.h"
int _folder_get_info(mfshell_t *mfshell,folder_t *folder,char *folderkey);
#endif

40
keys.c
View File

@@ -1,40 +0,0 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
*
* 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 <inttypes.h>
#include "mfshell.h"
#include "private.h"
void
_update_secret_key(mfshell_t *mfshell)
{
uint64_t new_val;
if(mfshell == NULL) return;
new_val = ((uint64_t)mfshell->secret_key) * 16807;
new_val %= 0x7FFFFFFF;
mfshell->secret_key = new_val;
return;
}

28
list.h
View File

@@ -1,28 +0,0 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
*
* 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_LIST_H_
#define _MFSHELL_LIST_H_
#include "mfshell.h"
long
_folder_get_content(mfshell_t *mfshell,int mode);
#endif

View File

@@ -1,53 +0,0 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
*
* 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 _MACROS_H_
#define _MACROS_H_
// calculate the absolute value on a 2s-complement system
#define ABSINT(x) ((x^(x>>((sizeof(x)*8)-1)))-(x>>((sizeof(x)*8)-1)))
// count the number of elements in the first axis of an array
#define DIM(x) (sizeof(x)/sizeof(x[0]))
// portable implementation of getpagesize(). see NOTE 1 below.
#if (!defined getpagesize) && (defined _SC_PAGESIZE)
#define getpagesize(x) (sysconf(_SC_PAGESIZE))
#endif
#if (!defined getpagesize) && (defined _SC_PAGE_SIZE)
#define getpagesize(x) (sysconf(_SC_PAGE_SIZE))
#endif
/*
Note 1: Regarding getpagesize()
In SUSv2 the getpagesize() call is labeled LEGACY, and in POSIX.1-2001
it has been dropped; HP-UX does not have this call.
I suspect on most systems that do have getpagesize() it is a macro so
we'll assume it to be. If not, we'll define it ourselves.
Note: That on some systems both _SC_PAGESIZE and _SC_PAGE_SIZE are
interchangeable so we'll try both.
*/
#endif

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

@@ -25,26 +25,23 @@
#include <curl/curl.h>
#include <jansson.h>
#include "mfshell.h"
#include "private.h"
#include "account.h"
#include "strings.h"
#include "json.h"
#include "connection.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(conn_t *conn, void *data);
_decode_file_get_info(http_t *conn, void *data);
int
_file_get_info(mfshell_t *mfshell,file_t *file,char *quickkey)
mfconn_api_file_get_info(mfconn_t *mfconn,file_t *file,char *quickkey)
{
char *api_call;
int retval;
int len;
if(mfshell == NULL) return -1;
if(mfshell->user_signature == NULL) return -1;
if(mfshell->session_token == NULL) return -1;
if(mfconn == NULL) return -1;
if(file == NULL) return -1;
if(quickkey == NULL) return -1;
@@ -54,21 +51,18 @@ _file_get_info(mfshell_t *mfshell,file_t *file,char *quickkey)
// key must either be 11 or 15 chars
if(len != 11 && len != 15) return -1;
api_call = mfshell->create_signed_get(mfshell, 1, "file/get_info.php",
"?quick_key=%s"
"&session_token=%s"
"&response_format=json",
quickkey,mfshell->session_token);
api_call = mfconn_create_signed_get(mfconn, 1, "file/get_info.php",
"?quick_key=%s&response_format=json", quickkey);
conn_t *conn = conn_create();
retval = conn_get_buf(conn, api_call, _decode_file_get_info, file);
conn_destroy(conn);
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(conn_t *conn, void *data)
_decode_file_get_info(http_t *conn, void *data)
{
json_error_t error;
json_t *root;
@@ -84,7 +78,7 @@ _decode_file_get_info(conn_t *conn, void *data)
file = (file_t *)data;
root = json_loadb(conn->write_buf, conn->write_buf_len, 0, &error);
root = http_parse_buf_json(conn, 0, &error);
node = json_object_by_path(root,"response/file_info");

View File

@@ -25,26 +25,23 @@
#include <curl/curl.h>
#include <jansson.h>
#include "mfshell.h"
#include "private.h"
#include "account.h"
#include "strings.h"
#include "json.h"
#include "connection.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(conn_t *conn, void *data);
_decode_file_get_links(http_t *conn, void *data);
int
_file_get_links(mfshell_t *mfshell,file_t *file,char *quickkey)
mfconn_api_file_get_links(mfconn_t *mfconn,file_t *file,char *quickkey)
{
char *api_call;
int retval;
int len;
if(mfshell == NULL) return -1;
if(mfshell->user_signature == NULL) return -1;
if(mfshell->session_token == NULL) return -1;
if(mfconn == NULL) return -1;
if(file == NULL) return -1;
if(quickkey == NULL) return -1;
@@ -54,21 +51,18 @@ _file_get_links(mfshell_t *mfshell,file_t *file,char *quickkey)
// key must either be 11 or 15 chars
if(len != 11 && len != 15) return -1;
api_call = mfshell->create_signed_get(mfshell,0,"file/get_links.php",
"?quick_key=%s"
"&session_token=%s"
"&response_format=json",
quickkey,mfshell->session_token);
api_call = mfconn_create_signed_get(mfconn,0,"file/get_links.php",
"?quick_key=%s&response_format=json", quickkey);
conn_t *conn = conn_create();
retval = conn_get_buf(conn, api_call, _decode_file_get_links, file);
conn_destroy(conn);
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(conn_t *conn, void *data)
_decode_file_get_links(http_t *conn, void *data)
{
json_error_t error;
json_t *root;
@@ -85,7 +79,7 @@ _decode_file_get_links(conn_t *conn, void *data)
file = (file_t *)data;
root = json_loadb(conn->write_buf, conn->write_buf_len, 0, &error);
root = http_parse_buf_json(conn, 0, &error);
node = json_object_by_path(root,"response");

View File

@@ -24,21 +24,18 @@
#include <curl/curl.h>
#include "mfshell.h"
#include "private.h"
#include "account.h"
#include "connection.h"
#include "strings.h"
#include "../apicalls.h"
#include "../mfconn.h"
#include "../../utils/http.h"
#include "../../utils/strings.h"
int
_folder_create(mfshell_t *mfshell,char *parent,char *name)
mfconn_api_folder_create(mfconn_t *mfconn,char *parent,char *name)
{
char *api_call;
int retval;
if(mfshell == NULL) return -1;
if(mfshell->user_signature == NULL) return -1;
if(mfshell->session_token == NULL) return -1;
if(mfconn == NULL) return -1;
if(name == NULL) return -1;
if(strlen(name) < 1) return -1;
@@ -55,25 +52,21 @@ _folder_create(mfshell_t *mfshell,char *parent,char *name)
if(parent != NULL)
{
api_call = mfshell->create_signed_get(mfshell,0,"folder/create.php",
api_call = mfconn_create_signed_get(mfconn,0,"folder/create.php",
"?parent_key=%s"
"&foldername=%s"
"&session_token=%s"
"&response_format=json",
parent,name,mfshell->session_token);
parent,name);
}
else
{
api_call = mfshell->create_signed_get(mfshell,0,"folder/create.php",
"?foldername=%s",
"&session_token=%s"
"&response_format=json",
name,mfshell->session_token);
api_call = mfconn_create_signed_get(mfconn,0,"folder/create.php",
"?foldername=%s&response_format=json", name);
}
conn_t *conn = conn_create();
retval = conn_get_buf(conn, api_call, NULL, NULL);
conn_destroy(conn);
http_t *conn = http_create();
retval = http_get_buf(conn, api_call, NULL, NULL);
http_destroy(conn);
return retval;
}

View File

@@ -25,61 +25,64 @@
#include <curl/curl.h>
#include <jansson.h>
#include "mfshell.h"
#include "private.h"
#include "strings.h"
#include "json.h"
#include "list.h"
#include "connection.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(conn_t *conn, void *data);
_decode_folder_get_content_folders(http_t *conn, void *data);
static int
_decode_folder_get_content_files(conn_t *conn, void *data);
_decode_folder_get_content_files(http_t *conn, void *data);
long
_folder_get_content(mfshell_t *mfshell,int mode)
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(mfshell == NULL) return -1;
if(mfshell->user_signature == NULL) return -1;
if(mfshell->session_token == NULL) return -1;
if(mfconn == NULL) return -1;
if(mode == 0)
content_type = "folders";
else
content_type = "files";
api_call = mfshell->create_signed_get(mfshell,0,"folder/get_content.php",
"?session_token=%s"
"&folder_key=%s"
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",
mfshell->session_token,
folder_get_key(mfshell->folder_curr),
folderkey,
content_type);
conn_t* conn = conn_create();
http_t* conn = http_create();
if(mode == 0)
retval = conn_get_buf(conn, api_call, _decode_folder_get_content_folders, NULL);
retval = http_get_buf(conn, api_call, _decode_folder_get_content_folders, NULL);
else
retval = conn_get_buf(conn, api_call, _decode_folder_get_content_files, NULL);
conn_destroy(conn);
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(conn_t *conn, void *user_ptr)
_decode_folder_get_content_folders(http_t *conn, void *user_ptr)
{
extern int term_width;
json_error_t error;
json_t *root;
json_t *node;
@@ -93,7 +96,10 @@ _decode_folder_get_content_folders(conn_t *conn, void *user_ptr)
int array_sz;
int i = 0;
root = json_loadb(conn->write_buf, conn->write_buf_len, 0, &error);
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");
@@ -120,9 +126,8 @@ _decode_folder_get_content_folders(conn_t *conn, void *user_ptr)
folder_name_tmp = strdup_printf("< %s >",
json_string_value(folder_name));
printf(" %-15.13s %-*.*s\n\r",
printf(" %-15.13s %s\n\r",
json_string_value(folderkey),
term_width - 22,term_width - 22,
folder_name_tmp);
free(folder_name_tmp);
@@ -136,10 +141,8 @@ _decode_folder_get_content_folders(conn_t *conn, void *user_ptr)
}
static int
_decode_folder_get_content_files(conn_t *conn, void *user_ptr)
_decode_folder_get_content_files(http_t *conn, void *user_ptr)
{
extern int term_width;
json_error_t error;
json_t *root;
json_t *node;
@@ -151,7 +154,7 @@ _decode_folder_get_content_files(conn_t *conn, void *user_ptr)
int array_sz;
int i = 0;
root = json_loadb(conn->write_buf, conn->write_buf_len, 0, &error);
root = http_parse_buf_json(conn, 0, &error);
node = json_object_by_path(root,"response/folder_content");
@@ -175,9 +178,8 @@ _decode_folder_get_content_files(conn_t *conn, void *user_ptr)
if(quickkey != NULL && file_name != NULL)
{
printf(" %-15.15s %-*.*s\n\r",
printf(" %-15.15s %s\n\r",
json_string_value(quickkey),
term_width - 22,term_width - 22,
json_string_value(file_name));
}
}

View File

@@ -25,25 +25,23 @@
#include <curl/curl.h>
#include <jansson.h>
#include "mfshell.h"
#include "private.h"
#include "account.h"
#include "strings.h"
#include "json.h"
#include "connection.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(conn_t *conn, void *data);
_decode_folder_get_info(http_t *conn, void *data);
int
_folder_get_info(mfshell_t *mfshell,folder_t *folder,char *folderkey)
mfconn_api_folder_get_info(mfconn_t *mfconn,folder_t *folder,char *folderkey)
{
char *api_call;
int retval;
if(mfshell == NULL) return -1;
if(mfshell->user_signature == NULL) return -1;
if(mfshell->session_token == NULL) return -1;
if(mfconn == NULL) return -1;
if(folder == NULL) return -1;
if(folderkey == NULL) return -1;
@@ -54,21 +52,18 @@ _folder_get_info(mfshell_t *mfshell,folder_t *folder,char *folderkey)
if(strcmp(folderkey,"myfiles") == 0) return -1;
}
api_call = mfshell->create_signed_get(mfshell,0,"folder/get_info.php",
"?folder_key=%s"
"&session_token=%s"
"&response_format=json",
folderkey,mfshell->session_token);
api_call = mfconn_create_signed_get(mfconn,0,"folder/get_info.php",
"?folder_key=%s&response_format=json", folderkey);
conn_t *conn = conn_create();
retval = conn_get_buf(conn, api_call, _decode_folder_get_info, folder);
conn_destroy(conn);
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(conn_t *conn, void *data)
_decode_folder_get_info(http_t *conn, void *data)
{
json_error_t error;
json_t *root;
@@ -83,7 +78,7 @@ _decode_folder_get_info(conn_t *conn, void *data)
folder = (folder_t *)data;
root = json_loadb(conn->write_buf, conn->write_buf_len, 0, &error);
root = http_parse_buf_json(conn, 0, &error);
node = json_object_by_path(root,"response/folder_info");

View File

@@ -25,40 +25,36 @@
#include <curl/curl.h>
#include <jansson.h>
#include "mfshell.h"
#include "private.h"
#include "account.h"
#include "strings.h"
#include "json.h"
#include "connection.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(conn_t *conn, void *data);
_decode_user_get_info(http_t *conn, void *data);
int
_user_get_info(mfshell_t *mfshell)
mfconn_api_user_get_info(mfconn_t *mfconn)
{
char *api_call;
int retval;
// char *rx_buffer;
if(mfshell == NULL) return -1;
if(mfshell->user_signature == NULL) return -1;
if(mfconn == NULL) return -1;
api_call = mfshell->create_signed_get(mfshell,0,"user/get_info.php",
"?session_token=%s"
"&response_format=json",
mfshell->session_token);
api_call = mfconn_create_signed_get(mfconn,0,"user/get_info.php",
"&response_format=json");
conn_t* conn = conn_create();
retval = conn_get_buf(conn, api_call, _decode_user_get_info, NULL);
conn_destroy(conn);
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(conn_t *conn, void *data)
_decode_user_get_info(http_t *conn, void *data)
{
json_error_t error;
json_t *root;
@@ -67,7 +63,7 @@ _decode_user_get_info(conn_t *conn, void *data)
json_t *first_name;
json_t *last_name;
root = json_loadb(conn->write_buf, conn->write_buf_len, 0, &error);
root = http_parse_buf_json(conn, 0, &error);
node = json_object_by_path(root,"response/user_info");

View File

@@ -25,39 +25,41 @@
#include <curl/curl.h>
#include <jansson.h>
#include "mfshell.h"
#include "private.h"
#include "user_session.h"
#include "connection.h"
#include "strings.h"
#include "json.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(conn_t *conn, void *data);
_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
_get_session_token(mfshell_t *mfshell)
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(mfshell == NULL) return -1;
if(mfconn == NULL) return -1;
// configure url for operation
login_url = strdup_printf("https://%s/api/user/get_session_token.php",
mfshell->server);
// invalidate an existing user signature
if(mfshell->user_signature != NULL)
{
free(mfshell->user_signature);
mfshell->user_signature = NULL;
}
server);
// create user signature
if(mfshell->user_signature == NULL)
mfshell->user_signature = mfshell->create_user_signature(mfshell);
user_signature = mfconn_create_user_signature(mfconn, username, password, app_id, app_key);
post_args = strdup_printf(
"email=%s"
@@ -66,20 +68,24 @@ _get_session_token(mfshell_t *mfshell)
"&signature=%s"
"&token_version=2"
"&response_format=json",
mfshell->user,mfshell->passwd,mfshell->user_signature);
username, password, user_signature);
conn_t *conn = conn_create();
retval = conn_post_buf(conn, login_url, post_args, _decode_get_session_token, (void *)mfshell);
conn_destroy(conn);
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(conn_t *conn, void *user_ptr)
_decode_get_session_token(http_t *conn, void *user_ptr)
{
json_error_t error;
json_t *root = NULL;
@@ -87,13 +93,13 @@ _decode_get_session_token(conn_t *conn, void *user_ptr)
json_t *session_token;
json_t *secret_key;
json_t *secret_time;
mfshell_t *mfshell;
struct user_get_session_token_response *response;
if(user_ptr == NULL) return -1;
mfshell = (mfshell_t *)user_ptr;
response = (struct user_get_session_token_response *)user_ptr;
root = json_loadb(conn->write_buf, conn->write_buf_len, 0, &error);
root = http_parse_buf_json(conn, 0, &error);
data = json_object_by_path(root,"response");
if(data == NULL) return -1;
@@ -105,11 +111,11 @@ _decode_get_session_token(conn_t *conn, void *user_ptr)
return -1;
}
mfshell->session_token = strdup(json_string_value(session_token));
response->session_token = strdup(json_string_value(session_token));
secret_key = json_object_get(data,"secret_key");
if(secret_key != NULL)
mfshell->secret_key = atoll(json_string_value(secret_key));
response->secret_key = atoll(json_string_value(secret_key));
/*
time looks like a float but we must store it as a string to
@@ -117,7 +123,7 @@ _decode_get_session_token(conn_t *conn, void *user_ptr)
*/
secret_time = json_object_get(data,"time");
if(secret_time != NULL)
mfshell->secret_time = strdup(json_string_value(secret_time));
response->secret_time = strdup(json_string_value(secret_time));
if(root != NULL) json_decref(root);

View File

@@ -1,5 +1,6 @@
/*
* 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
@@ -21,11 +22,20 @@
#include <inttypes.h>
#include <string.h>
#include "private.h"
#include "file.h"
#ifndef file_t
#define file_t _file_t
#endif
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)
@@ -196,3 +206,62 @@ file_get_onetime_link(file_t *file)
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;
}

View File

@@ -17,39 +17,12 @@
*
*/
#ifndef __MFAPI_FILE_H__
#define __MFAPI_FILE_H__
#ifndef _MFSHELL_H_
#define _MFSHELL_H_
typedef struct _mfshell_s mfshell_t;
typedef struct _folder_s folder_t;
typedef struct _file_s file_t;
typedef struct _cmd_s cmd_t;
typedef struct _conn_s conn_t;
mfshell_t* mfshell_create(int app_id,char *app_key,char *server);
int mfshell_set_login(mfshell_t *mfshell,char *user,char *passwd);
int mfshell_authenticate_user(mfshell_t *mfshell);
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);
struct _file_s;
file_t* file_alloc(void);
@@ -79,6 +52,6 @@ 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

View File

@@ -1,5 +1,6 @@
/*
* 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
@@ -21,11 +22,17 @@
#include <inttypes.h>
#include <string.h>
#include "private.h"
#include "folder.h"
#ifndef folder_t
#define folder_t _folder_t
#endif
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)

View File

@@ -3,7 +3,7 @@
* 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
* 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
@@ -11,16 +11,33 @@
* 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
* 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__
#ifndef _MFSHELL_DOWNLOAD_H_
#define _MFSHELL_DOWNLOAD_H_
typedef struct _folder_s folder_t;
ssize_t download_direct(mfshell_t *mfshell, file_t *file,char *local_dir);
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

View File

@@ -17,7 +17,6 @@
*
*/
#include <stdio.h>
#include <curl/curl.h>
@@ -25,30 +24,76 @@
#include <openssl/sha.h>
#include <openssl/md5.h>
#include "strings.h"
#include "private.h"
#include "../utils/strings.h"
#include "mfconn.h"
#include "apicalls.h"
#ifndef mfshell_t
#define mfshell_t _mfshell_t
#endif
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*
_create_user_signature(mfshell_t *mfshell)
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(mfshell == NULL) return NULL;
if(mfshell->app_id <= 0) return NULL;
if(mfshell->app_key == NULL) return NULL;
if(mfshell->user == NULL) return NULL;
if(mfshell->passwd == NULL) return NULL;
if(mfconn == NULL) return NULL;
signature_raw = strdup_printf("%s%s%d%s",
mfshell->user,mfshell->passwd,mfshell->app_id,mfshell->app_key);
username, password, app_id, app_key);
SHA1((const unsigned char *)signature_raw,
strlen(signature_raw),signature_enc);
@@ -65,7 +110,7 @@ _create_user_signature(mfshell_t *mfshell)
}
char*
_create_call_signature(mfshell_t *mfshell,char *url,char *args)
mfconn_create_call_signature(mfconn_t *mfconn,char *url,char *args)
{
char *signature_raw;
unsigned char signature_enc[16]; // md5 is 128 bits
@@ -73,7 +118,7 @@ _create_call_signature(mfshell_t *mfshell,char *url,char *args)
char *api;
int i;
if(mfshell == NULL) return NULL;
if(mfconn == NULL) return NULL;
if(url == NULL) return NULL;
if(args == NULL) return NULL;
@@ -84,8 +129,8 @@ _create_call_signature(mfshell_t *mfshell,char *url,char *args)
if(api == NULL) return NULL;
signature_raw = strdup_printf("%d%s%s%s",
(mfshell->secret_key % 256),
mfshell->secret_time,
(mfconn->secret_key % 256),
mfconn->secret_time,
api,args);
MD5((const unsigned char *)signature_raw,
@@ -103,22 +148,23 @@ _create_call_signature(mfshell_t *mfshell,char *url,char *args)
}
char*
_create_signed_get(mfshell_t *mfshell,int ssl,char *api,char *fmt,...)
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(mfshell == NULL) return NULL;
if(mfshell->server == NULL) return NULL;
if(mfshell->secret_time == NULL) return NULL;
if(mfshell->session_token == NULL) return NULL;
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;
@@ -130,6 +176,10 @@ _create_signed_get(mfshell_t *mfshell,int ssl,char *api,char *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));
@@ -139,13 +189,16 @@ _create_signed_get(mfshell_t *mfshell,int ssl,char *api,char *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:"), mfshell->server,api);
(ssl ? "https:" : "http:"), mfconn->server,api);
call_hash = mfshell->create_call_signature(mfshell,api_request,api_args);
call_hash = mfconn_create_call_signature(mfconn,api_request,api_args);
signature = strdup_printf("&signature=%s",call_hash);
free(call_hash);
@@ -165,3 +218,21 @@ _create_signed_get(mfshell_t *mfshell,int ssl,char *api,char *fmt,...)
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

View File

@@ -21,12 +21,11 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <termios.h>
#include "strings.h"
#include "mfshell.h"
#include "private.h"
#include "command.h"
#include "console.h"
#include "../mfshell.h"
#include "../commands.h"
static char*
_get_login_from_user(void);
@@ -38,52 +37,41 @@ int
mfshell_cmd_auth(mfshell_t *mfshell, int argc, char **argv)
{
int retval;
char *username;
char *password;
if(mfshell == NULL) return -1;
if(mfshell->server == NULL) return -1;
// free and invalidate existing user name
if(mfshell->user != NULL)
{
free(mfshell->user);
mfshell->user = NULL;
}
// free and invalidate existing passwd
if(mfshell->passwd != NULL)
{
free(mfshell->passwd);
mfshell->passwd = NULL;
}
switch (argc) {
case 1:
mfshell->user = _get_login_from_user();
mfshell->passwd = _get_passwd_from_user();
username = _get_login_from_user();
password = _get_passwd_from_user();
break;
case 2:
mfshell->user = argv[1];
mfshell->passwd = _get_passwd_from_user();
username = argv[1];
password = _get_passwd_from_user();
break;
case 3:
mfshell->user = argv[1];
mfshell->passwd = argv[2];
username = argv[1];
password = argv[2];
break;
default:
fprintf(stderr, "Invalid number of arguments\n");
return -1;
}
if(mfshell->user == NULL || mfshell->passwd == NULL) return -1;
if(username == NULL || password == NULL) return -1;
retval = mfshell->get_session_token(mfshell);
mfshell->mfconn = mfconn_create(mfshell->server, username, password,
mfshell->app_id, mfshell->app_key);
if(retval == 0)
if (mfshell->mfconn != NULL)
printf("\n\rAuthentication SUCCESS\n\r");
else
printf("\n\rAuthentication FAILURE\n\r");
return retval;
return (mfshell->mfconn != NULL);
}
char*
@@ -95,7 +83,6 @@ _get_login_from_user(void)
printf("login: ");
bytes_read = getline(&login,&len,stdin);
string_chomp(login);
if(bytes_read < 3)
{
@@ -106,6 +93,10 @@ _get_login_from_user(void)
}
}
if (login[strlen(login)-1] == '\n')
login[strlen(login)-1] = '\0';
return login;
}
@@ -115,18 +106,20 @@ _get_passwd_from_user(void)
char *passwd = NULL;
size_t len;
ssize_t bytes_read;
struct termios old, new;
printf("passwd: ");
// disable screen echo
console_save_state();
console_echo_off();
if (tcgetattr(STDIN_FILENO, &old) != 0)
return NULL;
new = old;
new.c_lflag &= ~ECHO;
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &new) != 0)
return NULL;
bytes_read = getline(&passwd,&len,stdin);
string_chomp(passwd);
// re-enable screen echo
console_restore_state();
tcsetattr(STDIN_FILENO, TCSAFLUSH, &old);
if(bytes_read < 3)
{
@@ -137,6 +130,9 @@ _get_passwd_from_user(void)
}
}
if (passwd[strlen(passwd)-1] == '\n')
passwd[strlen(passwd)-1] = '\0';
return passwd;
}

View File

@@ -22,10 +22,9 @@
#include <string.h>
#include <stdlib.h>
#include "strings.h"
#include "mfshell.h"
#include "private.h"
#include "command.h"
#include "../mfshell.h"
#include "../commands.h"
#include "../../mfapi/apicalls.h"
int
mfshell_cmd_chdir(mfshell_t *mfshell, int argc, char **argv)
@@ -84,9 +83,9 @@ mfshell_cmd_chdir(mfshell_t *mfshell, int argc, char **argv)
}
else
{
retval = mfshell->folder_get_info(mfshell,
retval = mfconn_api_folder_get_info(mfshell->mfconn,
folder_new,(char*)folderkey);
mfshell->update_secret_key(mfshell);
mfconn_update_secret_key(mfshell->mfconn);
}
if(retval == 0)

View File

@@ -22,10 +22,10 @@
#include <string.h>
#include <stdlib.h>
#include "strings.h"
#include "mfshell.h"
#include "private.h"
#include "command.h"
#include <inttypes.h>
#include "../mfshell.h"
#include "../commands.h"
int
mfshell_cmd_debug(mfshell_t *mfshell, int argc, char **argv)
@@ -39,15 +39,18 @@ mfshell_cmd_debug(mfshell_t *mfshell, int argc, char **argv)
"server:",
mfshell->server);
if(mfshell->session_token != NULL && mfshell->secret_time != NULL)
const char *session_token = mfconn_get_session_token(mfshell->mfconn);
const char *secret_time = mfconn_get_secret_time(mfshell->mfconn);
uint32_t secret_key = mfconn_get_secret_key(mfshell->mfconn);
if(session_token != NULL && secret_time != NULL)
{
printf(" %-15.15s %u\n\r",
printf(" %-15.15s %"PRIu32"\n\r",
"secret key:",
mfshell->secret_key);
secret_key);
printf(" %-15.15s %s\n\r",
"secret time:",
mfshell->secret_time);
secret_time);
printf(" %-15.15s %s\n\r",
"status:",

View File

@@ -21,18 +21,19 @@
#include <stdio.h>
#include <string.h>
#include "command.h"
#include "mfshell.h"
#include "private.h"
#include "../mfshell.h"
#include "../commands.h"
#include "../../mfapi/apicalls.h"
int
mfshell_cmd_file(mfshell_t *mfshell, int argc, char **argv)
{
extern int term_width;
file_t *file;
int len;
int retval;
const char *quickkey;
const char *name;
const char *hash;
if(mfshell == NULL) return -1;
@@ -50,26 +51,24 @@ mfshell_cmd_file(mfshell_t *mfshell, int argc, char **argv)
file = file_alloc();
retval = mfshell->file_get_info(mfshell,file,(char*)quickkey);
mfshell->update_secret_key(mfshell);
retval = mfconn_api_file_get_info(mfshell->mfconn,file,(char*)quickkey);
mfconn_update_secret_key(mfshell->mfconn);
if(file->name[0] != '\0')
printf(" %-15.15s %-*.*s\n\r",
"filename:",
term_width - 22,term_width -22,
file->name);
quickkey = file_get_key(file);
name = file_get_name(file);
hash = file_get_hash(file);
if(file->quickkey[0] != '\0')
printf(" %-15.15s %-*.*s\n\r",
"quickkey:",
term_width - 22,term_width - 22,
file->quickkey);
if(name != NULL && name[0] != '\0')
printf(" %-15.15s %s\n\r",
"filename:", name);
if(file->hash[0] != '\0')
printf(" %-15.15s %-*.*s\n\r",
"hash:",
term_width - 22,term_width - 22,
file->hash);
if(quickkey != NULL && quickkey[0] != '\0')
printf(" %-15.15s %s\n\r",
"quickkey:", quickkey);
if(hash != NULL && hash[0] != '\0')
printf(" %-15.15s %s\n\r",
"hash:", hash);
file_free(file);

View File

@@ -24,15 +24,13 @@
#include <stdlib.h>
#include <limits.h>
#include "command.h"
#include "mfshell.h"
#include "private.h"
#include "download.h"
#include "../mfshell.h"
#include "../commands.h"
#include "../../mfapi/apicalls.h"
int
mfshell_cmd_get(mfshell_t *mfshell, int argc, char **argv)
{
extern int term_width;
file_t *file;
int len;
int retval;
@@ -56,8 +54,8 @@ mfshell_cmd_get(mfshell_t *mfshell, int argc, char **argv)
file = file_alloc();
// get file name
retval = mfshell->file_get_info(mfshell,file,(char*)quickkey);
mfshell->update_secret_key(mfshell);
retval = mfconn_api_file_get_info(mfshell->mfconn,file,(char*)quickkey);
mfconn_update_secret_key(mfshell->mfconn);
if(retval == -1)
{
@@ -66,8 +64,8 @@ mfshell_cmd_get(mfshell_t *mfshell, int argc, char **argv)
}
// request a direct download (streaming) link
retval = mfshell->file_get_links(mfshell,file,(char*)quickkey);
mfshell->update_secret_key(mfshell);
retval = mfconn_api_file_get_links(mfshell->mfconn,file,(char*)quickkey);
mfconn_update_secret_key(mfshell->mfconn);
if(retval == -1)
{
@@ -82,7 +80,7 @@ mfshell_cmd_get(mfshell_t *mfshell, int argc, char **argv)
getcwd(mfshell->local_working_dir,PATH_MAX);
}
retval = download_direct(mfshell, file, mfshell->local_working_dir);
retval = file_download_direct(file, mfshell->local_working_dir);
if(retval != -1)
printf("\r Downloaded %zd bytes OK!\n\r",bytes_read);

View File

@@ -22,10 +22,8 @@
#include <string.h>
#include <stdlib.h>
#include "strings.h"
#include "mfshell.h"
#include "private.h"
#include "command.h"
#include "../mfshell.h"
#include "../commands.h"
int
mfshell_cmd_help(mfshell_t *mfshell, int argc, char **argv)

View File

@@ -22,10 +22,8 @@
#include <stdlib.h>
#include <string.h>
#include "mfshell.h"
#include "command.h"
#include "private.h"
#include "strings.h"
#include "../mfshell.h"
#include "../commands.h"
static char*
_get_host_from_user(void);
@@ -38,7 +36,7 @@ mfshell_cmd_host(mfshell_t *mfshell, int argc, char **argv)
switch (argc) {
case 1:
alt_host = _get_host_from_user();
alt_host = _get_host_from_user();
host = alt_host;
break;
case 2:
@@ -67,18 +65,7 @@ mfshell_cmd_host(mfshell_t *mfshell, int argc, char **argv)
mfshell->server = strdup(host);
// invalidate server auth credentials
if(mfshell->session_token != NULL)
{
free(mfshell->session_token);
mfshell->session_token = NULL;
}
if(mfshell->secret_time != NULL)
{
free(mfshell->secret_time);
mfshell->secret_time = NULL;
}
mfconn_destroy(mfshell->mfconn);
if(alt_host != NULL) free(alt_host);
@@ -93,7 +80,8 @@ _get_host_from_user(void)
printf("host: [www.mediafire.com] ");
getline(&host,&len,stdin);
string_chomp(host);
if (host[strlen(host)-1] == '\n')
host[strlen(host)-1] = '\0';
if(host == NULL)
return strdup("www.mediafire.com");

View File

@@ -23,9 +23,8 @@
#include <unistd.h>
#include <string.h>
#include "command.h"
#include "private.h"
#include "mfshell.h"
#include "../mfshell.h"
#include "../commands.h"
int
mfshell_cmd_lcd(mfshell_t *mfshell, int argc, char **argv)

View File

@@ -21,18 +21,20 @@
#include <stdio.h>
#include <string.h>
#include "command.h"
#include "mfshell.h"
#include "private.h"
#include "../mfshell.h"
#include "../commands.h"
#include "../../mfapi/apicalls.h"
int
mfshell_cmd_links(mfshell_t *mfshell, int argc, char **argv)
{
extern int term_width;
file_t *file;
int len;
int retval;
const char *quickkey;
const char *share_link;
const char *direct_link;
const char *onetime_link;
if(mfshell == NULL) return -1;
@@ -50,26 +52,24 @@ mfshell_cmd_links(mfshell_t *mfshell, int argc, char **argv)
file = file_alloc();
retval = mfshell->file_get_links(mfshell,file,(char*)quickkey);
mfshell->update_secret_key(mfshell);
retval = mfconn_api_file_get_links(mfshell->mfconn,file,(char*)quickkey);
mfconn_update_secret_key(mfshell->mfconn);
if(file->share_link != NULL)
printf(" %-15.15s %-*.*s\n\r",
"sharing url:",
term_width - 22,term_width -22,
file->share_link);
share_link = file_get_share_link(file);
direct_link = file_get_direct_link(file);
onetime_link = file_get_onetime_link(file);
if(file->direct_link != NULL)
printf(" %-15.15s %-*.*s\n\r",
"direct url:",
term_width - 22,term_width -22,
file->direct_link);
if(share_link != NULL && share_link[0] != '\0')
printf(" %-15.15s %s\n\r",
"sharing url:", share_link);
if(file->onetime_link != NULL)
printf(" %-15.15s %-*.*s\n\r",
"1-time url:",
term_width - 22,term_width -22,
file->onetime_link);
if(direct_link != NULL && direct_link[0] != '\0')
printf(" %-15.15s %s\n\r",
"direct url:", direct_link);
if(onetime_link != NULL && onetime_link[0] != '\0')
printf(" %-15.15s %s\n\r",
"1-time url:", onetime_link);
file_free(file);

View File

@@ -22,10 +22,9 @@
#include <string.h>
#include <stdlib.h>
#include "strings.h"
#include "mfshell.h"
#include "private.h"
#include "command.h"
#include "../mfshell.h"
#include "../commands.h"
#include "../../mfapi/apicalls.h"
int
mfshell_cmd_list(mfshell_t *mfshell, int argc, char **argv)
@@ -51,12 +50,12 @@ mfshell_cmd_list(mfshell_t *mfshell, int argc, char **argv)
folder_set_key(mfshell->folder_curr,"myfiles");
// first folders
retval = mfshell->folder_get_content(mfshell,0);
mfshell->update_secret_key(mfshell);
retval = mfconn_api_folder_get_content(mfshell->mfconn, 0, mfshell->folder_curr);
mfconn_update_secret_key(mfshell->mfconn);
// then files
retval = mfshell->folder_get_content(mfshell,1);
mfshell->update_secret_key(mfshell);
retval = mfconn_api_folder_get_content(mfshell->mfconn, 1, mfshell->folder_curr);
mfconn_update_secret_key(mfshell->mfconn);
return retval;
}

View File

@@ -23,16 +23,12 @@
#include <unistd.h>
#include <limits.h>
#include "command.h"
#include "mfshell.h"
#include "private.h"
#include "../mfshell.h"
#include "../commands.h"
int
mfshell_cmd_lpwd(mfshell_t *mfshell, int argc, char **argv)
{
extern int term_width;
int trim_size;
if(mfshell == NULL) return -1;
if (argc != 1) {
@@ -46,11 +42,7 @@ mfshell_cmd_lpwd(mfshell_t *mfshell, int argc, char **argv)
getcwd(mfshell->local_working_dir,PATH_MAX);
}
trim_size = term_width - 1;
printf("%-*.*s\n\r",
trim_size,trim_size,
mfshell->local_working_dir);
printf("%s\n\r", mfshell->local_working_dir);
return 0;
}

View File

@@ -22,10 +22,9 @@
#include <string.h>
#include <stdlib.h>
#include "strings.h"
#include "mfshell.h"
#include "private.h"
#include "command.h"
#include "../mfshell.h"
#include "../commands.h"
#include "../../mfapi/apicalls.h"
int
mfshell_cmd_mkdir(mfshell_t *mfshell, int argc, char **argv)
@@ -56,8 +55,8 @@ mfshell_cmd_mkdir(mfshell_t *mfshell, int argc, char **argv)
folder_curr = folder_get_key(mfshell->folder_curr);
retval = mfshell->folder_create(mfshell,(char*)folder_curr,(char*)name);
mfshell->update_secret_key(mfshell);
retval = mfconn_api_folder_create(mfshell->mfconn,(char*)folder_curr,(char*)name);
mfconn_update_secret_key(mfshell->mfconn);
return retval;
}

View File

@@ -22,10 +22,9 @@
#include <string.h>
#include <stdlib.h>
#include "strings.h"
#include "mfshell.h"
#include "private.h"
#include "command.h"
#include "../commands.h"
#include "../mfshell.h"
#include "../../utils/strings.h"
int
mfshell_cmd_pwd(mfshell_t *mfshell, int argc, char **argv)

View File

@@ -22,10 +22,9 @@
#include <string.h>
#include <stdlib.h>
#include "strings.h"
#include "mfshell.h"
#include "private.h"
#include "command.h"
#include "../commands.h"
#include "../mfshell.h"
#include "../../mfapi/apicalls.h"
int
mfshell_cmd_whoami(mfshell_t *mfshell, int argc, char **argv)
@@ -37,8 +36,8 @@ mfshell_cmd_whoami(mfshell_t *mfshell, int argc, char **argv)
return -1;
}
retval = mfshell->user_get_info(mfshell);
mfshell->update_secret_key(mfshell);
retval = mfconn_api_user_get_info(mfshell->mfconn);
mfconn_update_secret_key(mfshell->mfconn);
return retval;
}

View File

@@ -27,10 +27,7 @@
#include <openssl/ssl.h>
#include "mfshell.h"
#include "console.h"
#include "private.h"
#include "strings.h"
#include "signals.h"
#include "../utils/strings.h"
static void
mfshell_run(mfshell_t *mfshell);
@@ -38,10 +35,6 @@ mfshell_run(mfshell_t *mfshell);
static void
mfshell_parse_commands(mfshell_t *mfshell, char *command);
int term_resized = 0;
int term_height = 0;
int term_width = 0;
void print_help(mfshell_t *mfshell, char *cmd)
{
fprintf(stderr, "A shell to access a MediaFire account.\n");
@@ -127,8 +120,6 @@ void parse_argv(mfshell_t *mfshell, int argc, char **argv, char **username,
int main(int argc,char **argv)
{
extern int term_height;
extern int term_width;
mfshell_t *mfshell;
char *server = "www.mediafire.com";
char *username = NULL;
@@ -139,16 +130,6 @@ int main(int argc,char **argv)
SSL_library_init();
retval = console_get_metrics(&term_height,&term_width);
if(retval != 0)
{
// maybe the system doesn't support it. we'll guess at it.
term_height = 25;
term_width = 80;
}
sig_install_SIGWINCH();
parse_argv(mfshell, argc, argv, &username, &password, &server, &command);
mfshell = mfshell_create(35860,
@@ -179,7 +160,7 @@ mfshell_parse_commands(mfshell_t *mfshell, char *command)
fprintf(stderr, "Need more than zero arguments\n");
exit(1);
}
mfshell->exec(mfshell, p.we_wordc, p.we_wordv);
mfshell_exec(mfshell, p.we_wordc, p.we_wordv);
wordfree(&p);
}
}
@@ -218,7 +199,7 @@ mfshell_run(mfshell_t *mfshell)
continue;
}
retval = mfshell->exec_string(mfshell,cmd);
retval = mfshell_exec_shell_command(mfshell,cmd);
free(cmd);
cmd = NULL;
}

View File

@@ -22,21 +22,20 @@
#include <string.h>
#include <inttypes.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <wordexp.h>
#include <sys/ioctl.h>
#include "mfshell.h"
#include "../utils/stringv.h"
#include <curl/curl.h>
#include "strings.h"
#include "mfshell.h"
#include "private.h"
#include "user_session.h"
#include "command.h"
#include "account.h"
#include "list.h"
#include "folder_info.h"
#include "file_info.h"
#include "file_links.h"
#include "folder_create.h"
#include "connection.h"
#include "commands.h"
struct _cmd_s commands[] = {
{"help", "", "show this help", mfshell_cmd_help},
@@ -83,25 +82,6 @@ mfshell_create(int app_id,char *app_key,char *server)
mfshell->app_key = strdup(app_key);
mfshell->server = strdup(server);
mfshell->update_secret_key = _update_secret_key;
mfshell->create_user_signature = _create_user_signature;
mfshell->create_call_signature = _create_call_signature;
mfshell->create_signed_get = _create_signed_get;
mfshell->exec = _execute;
mfshell->exec_string = _execute_shell_command;
// configure REST API callbacks
mfshell->get_session_token = _get_session_token;
mfshell->user_get_info = _user_get_info;
mfshell->folder_get_content = _folder_get_content;
mfshell->folder_get_info = _folder_get_info;
mfshell->folder_create = _folder_create;
mfshell->file_get_info = _file_get_info;
mfshell->file_get_links = _file_get_links;
// object to track folder location
mfshell->folder_curr = folder_alloc();
folder_set_key(mfshell->folder_curr,"myfiles");
@@ -113,14 +93,38 @@ mfshell_create(int app_id,char *app_key,char *server)
}
int
mfshell_set_login(mfshell_t *mfshell,char *user,char *passwd)
mfshell_exec(mfshell_t *mfshell, int argc, char **argv)
{
if(mfshell == NULL) return -1;
if(user == NULL) return -1;
if(passwd == NULL) return -1;
_cmd_t* curr_cmd;
for (curr_cmd = mfshell->commands; curr_cmd->name != NULL; curr_cmd++) {
if (strcmp(argv[0], curr_cmd->name) == 0) {
return curr_cmd->handler(mfshell, argc, argv);
}
}
}
mfshell->user = strdup(user);
mfshell->passwd = strdup(passwd);
int
mfshell_exec_shell_command(mfshell_t *mfshell,char *command)
{
wordexp_t p;
int retval;
if(mfshell == NULL) return -1;
if(command == NULL) return -1;
// FIXME: handle non-zero return value of wordexp
retval = wordexp(command, &p, WRDE_SHOWERR | WRDE_UNDEF);
if (p.we_wordc < 1)
return 0;
if (p.we_wordv[0] == NULL)
return 0;
// TODO: handle retval
retval = mfshell_exec(mfshell, p.we_wordc, p.we_wordv);
wordfree(&p);
return 0;
}

69
mfshell/mfshell.h Normal file
View File

@@ -0,0 +1,69 @@
/*
* 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_H_
#define _MFSHELL_H_
#include "../mfapi/folder.h"
#include "../mfapi/mfconn.h"
typedef struct _cmd_s _cmd_t;
typedef struct _mfshell_s mfshell_t;
struct _cmd_s
{
char *name;
char *argstring;
char *help;
int (*handler) (mfshell_t *mfshell, int argc, char **argv);
};
struct _mfshell_s
{
int app_id;
char *app_key;
char *server;
/* REST API tracking */
folder_t *folder_curr;
/* Local tracking */
char *local_working_dir;
/* shell commands */
_cmd_t *commands;
mfconn_t *mfconn;
};
typedef struct _mfshell_s mfshell_t;
typedef struct _folder_s folder_t;
typedef struct _cmd_s cmd_t;
mfshell_t* mfshell_create(int app_id,char *app_key,char *server);
int mfshell_authenticate_user(mfshell_t *mfshell);
int mfshell_exec(mfshell_t *mfshell, int argc, char **argv);
int mfshell_exec_shell_command(mfshell_t *mfshell,char *command);
#endif

147
private.h
View File

@@ -1,147 +0,0 @@
/*
* 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_PRIVATE_H_
#define _MFSHELL_PRIVATE_H_
#include <inttypes.h>
#include <stdbool.h>
#include <curl/curl.h>
typedef struct _mfshell_s _mfshell_t;
typedef struct _folder_s _folder_t;
typedef struct _file_s _file_t;
typedef struct _cmd_s _cmd_t;
typedef struct _conn_s _conn_t;
struct _cmd_s
{
char *name;
char *argstring;
char *help;
int (*handler) (_mfshell_t *mfshell, int argc, char **argv);
};
struct _folder_s
{
char folderkey[20];
char name[41];
char parent[20];
uint64_t revision;
uint32_t folder_count;
uint32_t file_count;
};
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;
};
struct _conn_s
{
CURL *curl_handle;
char *write_buf;
size_t write_buf_len;
double ul_len;
double ul_now;
double dl_len;
double dl_now;
bool show_progress;
char error_buf[CURL_ERROR_SIZE];
FILE *stream;
};
struct _mfshell_s
{
int app_id;
char *app_key;
char *server;
char *user;
char *passwd;
char *user_signature;
char *session_token;
char *secret_time;
uint32_t secret_key;
void (*update_secret_key) (_mfshell_t *);
//char* (*set_sever) (_mfshell_t *,const char*);
char* (*set_login) (_mfshell_t*,const char*);
char* (*set_passwd) (_mfshell_t*,const char*);
char* (*create_user_signature) (_mfshell_t*);
char* (*create_call_signature) (_mfshell_t*,char*,char*);
char* (*create_signed_get) (_mfshell_t*,int,char*,char*,...);
char* (*create_signed_post) (_mfshell_t*,int,char*,char*,...);
int (*exec) (_mfshell_t*, int argc, char **argv);
int (*exec_string) (_mfshell_t*,char*);
/* REST API calls */
int (*get_session_token) (_mfshell_t*);
int (*user_get_info) (_mfshell_t*);
long (*folder_get_content) (_mfshell_t*,int);
int (*folder_get_info) (_mfshell_t*,_folder_t*,char*);
int (*folder_create) (_mfshell_t*,char*,char*);
int (*file_get_info) (_mfshell_t*,_file_t*,char*);
int (*file_get_links) (_mfshell_t*,_file_t*,char*);
/* REST API tracking */
_folder_t *folder_curr;
/* Local tracking */
char *local_working_dir;
/* shell commands */
_cmd_t *commands;
};
void
_update_secret_key(_mfshell_t *mfshell);
char*
_create_user_signature(_mfshell_t *mfshell);
char*
_create_call_signature(_mfshell_t *mfshell,char *url,char *args);
char*
_create_signed_get(_mfshell_t *mfshell,int ssl,char *api,char *fmt,...);
int
_execute(_mfshell_t *mfshell, int argc, char **argv);
int
_execute_shell_command(_mfshell_t *mfshell,char *command);
#endif

View File

@@ -1,73 +0,0 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
*
* 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 <signal.h>
#include <string.h>
#include "signals.h"
void
sig_install_SIGWINCH(void)
{
signal(SIGWINCH,sig_handler_SIGWINCH);
return;
}
void
sig_handler_SIGWINCH(int signum)
{
extern int term_resized;
term_resized = 1;
return;
}
/*
void
sig_hanlder_SIGWINCH(int signum,siginfo_t *info,void *context)
{
extern int term_resized;
term_resized = 1;
return;
}*/
/*
void
sig_install_SIGWINCH(void)
{
static struct sigaction handler;
memset(&handler,0,sizeof(handler));
handler.sa_sigaction = sig_hanlder_SIGWINCH;
sigfillset(&handler.sa_mask);
handler.sa_flags = SA_SIGINFO;
sigaction(SIGWINCH,&handler,NULL);
return;
}
*/

View File

@@ -1,31 +0,0 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
*
* 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_SIGNALS_H_
#define _MFSHELL_SINGALS_H_
#include <signal.h>
// int sig_handler_SIGWINCH(int signum,siginfo_t *info,void *context);
void sig_handler_SIGWINCH(int signum);
void sig_install_SIGWINCH(void);
#endif

View File

@@ -1,27 +0,0 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
*
* 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_SESSION_H_
#define _MFSHELL_SESSION_H_
#include "mfshell.h"
int _get_session_token(mfshell_t *mfshell);
#endif

View File

@@ -18,37 +18,52 @@
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <stdbool.h>
#include "private.h"
#include "mfshell.h"
#include "http.h"
static int conn_progress_cb(void *user_ptr, double dltotal, double dlnow, double ultotal, double ulnow);
static size_t conn_read_buf_cb(char *data, size_t size, size_t nmemb, void *user_ptr);
static size_t conn_write_buf_cb(char *data, size_t size, size_t nmemb, void *user_ptr);
static size_t conn_write_file_cb(char *data, size_t size, size_t nmemb, void *user_ptr);
static int http_progress_cb(void *user_ptr, double dltotal, double dlnow, double ultotal, double ulnow);
static size_t http_read_buf_cb(char *data, size_t size, size_t nmemb, void *user_ptr);
static size_t http_write_buf_cb(char *data, size_t size, size_t nmemb, void *user_ptr);
static size_t http_write_file_cb(char *data, size_t size, size_t nmemb, void *user_ptr);
struct _http_s
{
CURL *curl_handle;
char *write_buf;
size_t write_buf_len;
double ul_len;
double ul_now;
double dl_len;
double dl_now;
bool show_progress;
char error_buf[CURL_ERROR_SIZE];
FILE *stream;
};
/*
* This set of functions is made such that the conn_t struct and the curl
* This set of functions is made such that the http_t struct and the curl
* handle it stores can be reused for multiple operations
*
* We do not use a single global instance because mediafire does not support
* keep-alive anyways.
*/
conn_t*
conn_create(void)
http_t*
http_create(void)
{
conn_t *conn;
http_t *conn;
CURL *curl_handle;
curl_handle = curl_easy_init();
if(curl_handle == NULL) return NULL;
conn = (conn_t*)calloc(1,sizeof(conn_t));
conn = (http_t*)calloc(1,sizeof(http_t));
conn->curl_handle = curl_handle;
conn->show_progress = false;
curl_easy_setopt(conn->curl_handle, CURLOPT_NOPROGRESS,0);
curl_easy_setopt(conn->curl_handle, CURLOPT_PROGRESSFUNCTION, conn_progress_cb);
curl_easy_setopt(conn->curl_handle, CURLOPT_PROGRESSFUNCTION, http_progress_cb);
curl_easy_setopt(conn->curl_handle, CURLOPT_PROGRESSDATA, (void *)conn);
curl_easy_setopt(conn->curl_handle, CURLOPT_FOLLOWLOCATION, 1);
@@ -61,22 +76,28 @@ conn_create(void)
return conn;
}
json_t *
http_parse_buf_json(http_t* conn, size_t flags, json_error_t *error)
{
return json_loadb(conn->write_buf, conn->write_buf_len, flags, error);
}
void
conn_destroy(conn_t* conn)
http_destroy(http_t* conn)
{
curl_easy_cleanup(conn->curl_handle);
free(conn->write_buf);
free(conn);
}
static int conn_progress_cb(void *user_ptr, double dltotal, double dlnow,
static int http_progress_cb(void *user_ptr, double dltotal, double dlnow,
double ultotal, double ulnow)
{
conn_t *conn;
http_t *conn;
if (user_ptr == NULL) return 0;
conn = (conn_t *)user_ptr;
conn = (http_t *)user_ptr;
conn->ul_len = ultotal;
conn->ul_now = ulnow;
@@ -88,15 +109,16 @@ static int conn_progress_cb(void *user_ptr, double dltotal, double dlnow,
}
int
conn_get_buf(conn_t *conn, char *url, int (*data_handler)(conn_t *conn, void *data), void *data)
http_get_buf(http_t *conn, const char *url, int (*data_handler)(http_t *conn, void *data), void *data)
{
int retval;
curl_easy_reset(conn->curl_handle);
curl_easy_setopt(conn->curl_handle, CURLOPT_URL, url);
curl_easy_setopt(conn->curl_handle, CURLOPT_READFUNCTION, conn_read_buf_cb);
curl_easy_setopt(conn->curl_handle, CURLOPT_READFUNCTION, http_read_buf_cb);
curl_easy_setopt(conn->curl_handle, CURLOPT_READDATA, (void*)conn);
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEFUNCTION, conn_write_buf_cb);
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEFUNCTION, http_write_buf_cb);
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEDATA, (void*)conn);
//fprintf(stderr, "GET: %s\n", url);
retval = curl_easy_perform(conn->curl_handle);
if(retval != CURLE_OK) {
fprintf(stderr, "error curl_easy_perform %s\n\r", conn->error_buf);
@@ -109,14 +131,14 @@ conn_get_buf(conn_t *conn, char *url, int (*data_handler)(conn_t *conn, void *da
}
static size_t
conn_read_buf_cb(char *data, size_t size, size_t nmemb, void *user_ptr)
http_read_buf_cb(char *data, size_t size, size_t nmemb, void *user_ptr)
{
conn_t *conn;
http_t *conn;
size_t data_len;
if (user_ptr == NULL) return 0;
conn = (conn_t*)user_ptr;
conn = (http_t*)user_ptr;
data_len = size*nmemb;
if (data_len > 0) {
@@ -128,17 +150,18 @@ conn_read_buf_cb(char *data, size_t size, size_t nmemb, void *user_ptr)
}
static size_t
conn_write_buf_cb(char *data, size_t size, size_t nmemb, void *user_ptr)
http_write_buf_cb(char *data, size_t size, size_t nmemb, void *user_ptr)
{
conn_t *conn;
http_t *conn;
size_t data_len;
if (user_ptr == NULL) return 0;
conn = (conn_t*)user_ptr;
conn = (http_t*)user_ptr;
data_len = size*nmemb;
if (data_len > 0) {
//fwrite(data, size, nmemb, stderr);
conn->write_buf = (char *)realloc(conn->write_buf,
conn->write_buf_len + data_len);
memcpy(conn->write_buf + conn->write_buf_len, data, data_len);
@@ -149,14 +172,14 @@ conn_write_buf_cb(char *data, size_t size, size_t nmemb, void *user_ptr)
}
int
conn_post_buf(conn_t *conn, char *url, char *post_args, int (*data_handler)(conn_t *conn, void *data), void *data)
http_post_buf(http_t *conn, const char *url, const char *post_args, int (*data_handler)(http_t *conn, void *data), void *data)
{
int retval;
curl_easy_reset(conn->curl_handle);
curl_easy_setopt(conn->curl_handle, CURLOPT_URL, url);
curl_easy_setopt(conn->curl_handle, CURLOPT_READFUNCTION, conn_read_buf_cb);
curl_easy_setopt(conn->curl_handle, CURLOPT_READFUNCTION, http_read_buf_cb);
curl_easy_setopt(conn->curl_handle, CURLOPT_READDATA, (void*)conn);
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEFUNCTION, conn_write_buf_cb);
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEFUNCTION, http_write_buf_cb);
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEDATA, (void*)conn);
curl_easy_setopt(conn->curl_handle, CURLOPT_POSTFIELDS, post_args);
retval = curl_easy_perform(conn->curl_handle);
@@ -171,14 +194,14 @@ conn_post_buf(conn_t *conn, char *url, char *post_args, int (*data_handler)(conn
}
int
conn_get_file(conn_t *conn, char *url, char *path)
http_get_file(http_t *conn, const char *url, const char *path)
{
int retval;
curl_easy_reset(conn->curl_handle);
curl_easy_setopt(conn->curl_handle, CURLOPT_URL, url);
curl_easy_setopt(conn->curl_handle, CURLOPT_READFUNCTION, conn_read_buf_cb);
curl_easy_setopt(conn->curl_handle, CURLOPT_READFUNCTION, http_read_buf_cb);
curl_easy_setopt(conn->curl_handle, CURLOPT_READDATA, (void*)conn);
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEFUNCTION, conn_write_file_cb);
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEFUNCTION, http_write_file_cb);
curl_easy_setopt(conn->curl_handle, CURLOPT_WRITEDATA, (void*)conn);
// FIXME: handle fopen() return value
conn->stream = fopen(path, "w+");
@@ -192,12 +215,12 @@ conn_get_file(conn_t *conn, char *url, char *path)
}
static size_t
conn_write_file_cb(char *data, size_t size, size_t nmemb, void *user_ptr)
http_write_file_cb(char *data, size_t size, size_t nmemb, void *user_ptr)
{
conn_t *conn;
http_t *conn;
if (user_ptr == NULL) return 0;
conn = (conn_t*)user_ptr;
conn = (http_t*)user_ptr;
fwrite(data, size, nmemb, conn->stream);
@@ -205,6 +228,6 @@ conn_write_file_cb(char *data, size_t size, size_t nmemb, void *user_ptr)
}
int
conn_post_file(conn_t *conn, char *url, char *post_args, FILE *fd)
http_post_file(http_t *conn, const char *url, const char *post_args, FILE *fd)
{
}

View File

@@ -16,14 +16,22 @@
*
*/
#ifndef _MFSHELL_CONNECTION_H_
#define _MFSHELL_CONNECTION_H_
#ifndef _MFSHELL_HTTP_H_
#define _MFSHELL_HTTP_H_
#include <jansson.h>
typedef struct _http_s http_t;
struct _http_s;
http_t* http_create(void);
void http_destroy(http_t* conn);
int http_get_buf(http_t *conn, const char *url, int (*data_handler)(http_t *conn, void *data), void *data);
int http_post_buf(http_t *conn, const char *url, const char *post_args, int (*data_handler)(http_t *conn, void *data), void *data);
int http_get_file(http_t *conn, const char *url, const char *path);
int http_post_file(http_t *conn, const char *url, const char *post_args, FILE *fd);
json_t *http_parse_buf_json(http_t* conn, size_t flags, json_error_t *error);
conn_t* conn_create(void);
void conn_destroy(conn_t* conn);
int conn_get_buf(conn_t *conn, const char *url, int (*data_handler)(conn_t *conn, void *data), void *data);
int conn_post_buf(conn_t *conn, const char *url, char *post_args, int (*data_handler)(conn_t *conn, void *data), void *data);
int conn_get_file(conn_t *conn, const char *url, char *path);
int conn_post_file(conn_t *conn, const char *url, char *post_args, FILE *fd);
#endif

View File

@@ -23,7 +23,6 @@
#include <string.h>
#include <ctype.h>
#include "macros.h"
#include "strings.h"
#include "stringv.h"