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

56
mfshell/commands.h Normal file
View File

@@ -0,0 +1,56 @@
/*
* 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_COMMAND_H_
#define _MFSHELL_COMMAND_H_
#include "mfshell.h"
int mfshell_cmd_help(mfshell_t *mfshell, int argc, char **argv);
int mfshell_cmd_debug(mfshell_t *mfshell, int argc, char **argv);
int mfshell_cmd_host(mfshell_t *mfshell, int argc, char **argv);
int mfshell_cmd_auth(mfshell_t *mfshell, int argc, char **argv);
int mfshell_cmd_whomai(mfshell_t *mfshell, int argc, char **argv);
int mfshell_cmd_list(mfshell_t *mfshell, int argc, char **argv);
int mfshell_cmd_chdir(mfshell_t *mfshell, int argc, char **argv);
int mfshell_cmd_pwd(mfshell_t *mfshell, int argc, char **argv);
int mfshell_cmd_lpwd(mfshell_t *mfshell, int argc, char **argv);
int mfshell_cmd_lcd(mfshell_t *mfshell, int argc, char **argv);
int mfshell_cmd_file(mfshell_t *mfshell, int argc, char **argv);
int mfshell_cmd_links(mfshell_t *mfshell, int argc, char **argv);
int mfshell_cmd_mkdir(mfshell_t *mfshell, int argc, char **argv);
int mfshell_cmd_get(mfshell_t *mfshell, int argc, char **argv);
int mfshell_cmd_whoami(mfshell_t *mfshell, int argc, char **argv);
#endif

138
mfshell/commands/auth.c Normal file
View File

@@ -0,0 +1,138 @@
/*
* 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 <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <termios.h>
#include "../mfshell.h"
#include "../commands.h"
static char*
_get_login_from_user(void);
static char*
_get_passwd_from_user(void);
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;
switch (argc) {
case 1:
username = _get_login_from_user();
password = _get_passwd_from_user();
break;
case 2:
username = argv[1];
password = _get_passwd_from_user();
break;
case 3:
username = argv[1];
password = argv[2];
break;
default:
fprintf(stderr, "Invalid number of arguments\n");
return -1;
}
if(username == NULL || password == NULL) return -1;
mfshell->mfconn = mfconn_create(mfshell->server, username, password,
mfshell->app_id, mfshell->app_key);
if (mfshell->mfconn != NULL)
printf("\n\rAuthentication SUCCESS\n\r");
else
printf("\n\rAuthentication FAILURE\n\r");
return (mfshell->mfconn != NULL);
}
char*
_get_login_from_user(void)
{
char *login = NULL;
size_t len;
ssize_t bytes_read;
printf("login: ");
bytes_read = getline(&login,&len,stdin);
if(bytes_read < 3)
{
if(login != NULL)
{
free(login);
login = NULL;
}
}
if (login[strlen(login)-1] == '\n')
login[strlen(login)-1] = '\0';
return login;
}
char*
_get_passwd_from_user(void)
{
char *passwd = NULL;
size_t len;
ssize_t bytes_read;
struct termios old, new;
printf("passwd: ");
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);
tcsetattr(STDIN_FILENO, TCSAFLUSH, &old);
if(bytes_read < 3)
{
if(passwd != NULL)
{
free(passwd);
passwd = NULL;
}
}
if (passwd[strlen(passwd)-1] == '\n')
passwd[strlen(passwd)-1] = '\0';
return passwd;
}

109
mfshell/commands/chdir.c Normal file
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 <stdlib.h>
#include "../mfshell.h"
#include "../commands.h"
#include "../../mfapi/apicalls.h"
int
mfshell_cmd_chdir(mfshell_t *mfshell, int argc, char **argv)
{
folder_t *folder_new;
const char *folder_curr;
const char *folder_parent;
const char *folderkey;
int retval;
if(mfshell == NULL) return -1;
if (argc != 2) {
fprintf(stderr, "Invalid number of arguments\n");
return -1;
}
folderkey = argv[1];
if(folderkey == NULL) return -1;
// change to root
if(strcmp(folderkey,"/") == 0) folderkey = "myfiles";
// user wants to navigate up a level
if(strcmp(folderkey,"..") == 0)
{
// do several sanity checks to see if we're already at the root
folder_curr = folder_get_key(mfshell->folder_curr);
if(folder_curr == NULL) return 0;
if(strcmp(folder_curr,"myfiles") == 0) return 0;
folder_parent = folder_get_parent(mfshell->folder_curr);
if(folder_parent == NULL) return 0;
// it's pretty sure that we're not at the root
folderkey = folder_parent;
}
// check the lenght of the key
if(strlen(folderkey) != 13)
{
// as a folder moniker, "myfiles" is an exception
if(strcmp(folderkey,"myfiles") != 0) return -1;
}
// create a new folder object to store the results
folder_new = folder_alloc();
// navigate to root is a special case
if(strcmp(folderkey,"myfiles") == 0)
{
folder_set_key(folder_new,"myfiles");
retval = 0;
}
else
{
retval = mfconn_api_folder_get_info(mfshell->mfconn,
folder_new,(char*)folderkey);
mfconn_update_secret_key(mfshell->mfconn);
}
if(retval == 0)
{
if(mfshell->folder_curr != NULL)
{
folder_free(mfshell->folder_curr);
mfshell->folder_curr = NULL;
}
mfshell->folder_curr = folder_new;
}
else
{
folder_free(folder_new);
}
return retval;
}

69
mfshell/commands/debug.c 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.
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
#include "../mfshell.h"
#include "../commands.h"
int
mfshell_cmd_debug(mfshell_t *mfshell, int argc, char **argv)
{
if (argc != 1) {
fprintf(stderr, "Invalid number of arguments\n");
return -1;
}
printf(" %-15.15s %s\n\r",
"server:",
mfshell->server);
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 %"PRIu32"\n\r",
"secret key:",
secret_key);
printf(" %-15.15s %s\n\r",
"secret time:",
secret_time);
printf(" %-15.15s %s\n\r",
"status:",
"Authenticated");
}
else
{
printf(" %-15.15s %s\n\r",
"status:",
"Not authenticated");
}
return 0;
}

77
mfshell/commands/file.c Normal file
View File

@@ -0,0 +1,77 @@
/*
* 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 "../mfshell.h"
#include "../commands.h"
#include "../../mfapi/apicalls.h"
int
mfshell_cmd_file(mfshell_t *mfshell, int argc, char **argv)
{
file_t *file;
int len;
int retval;
const char *quickkey;
const char *name;
const char *hash;
if(mfshell == NULL) return -1;
if (argc != 2) {
fprintf(stderr, "Invalid number of arguments\n");
return -1;
}
quickkey = argv[1];
if(quickkey == NULL) return -1;
len = strlen(quickkey);
if(len != 11 && len != 15) return -1;
file = file_alloc();
retval = mfconn_api_file_get_info(mfshell->mfconn,file,(char*)quickkey);
mfconn_update_secret_key(mfshell->mfconn);
quickkey = file_get_key(file);
name = file_get_name(file);
hash = file_get_hash(file);
if(name != NULL && name[0] != '\0')
printf(" %-15.15s %s\n\r",
"filename:", name);
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);
return 0;
}

94
mfshell/commands/get.c Normal file
View File

@@ -0,0 +1,94 @@
/*
* 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 <unistd.h>
#include <stdlib.h>
#include <limits.h>
#include "../mfshell.h"
#include "../commands.h"
#include "../../mfapi/apicalls.h"
int
mfshell_cmd_get(mfshell_t *mfshell, int argc, char **argv)
{
file_t *file;
int len;
int retval;
ssize_t bytes_read;
const char *quickkey;
if(mfshell == NULL) return -1;
if (argc != 2) {
fprintf(stderr, "Invalid number of arguments\n");
return -1;
}
quickkey = argv[1];
if(quickkey == NULL) return -1;
len = strlen(quickkey);
if(len != 11 && len != 15) return -1;
file = file_alloc();
// get file name
retval = mfconn_api_file_get_info(mfshell->mfconn,file,(char*)quickkey);
mfconn_update_secret_key(mfshell->mfconn);
if(retval == -1)
{
file_free(file);
return -1;
}
// request a direct download (streaming) link
retval = mfconn_api_file_get_links(mfshell->mfconn,file,(char*)quickkey);
mfconn_update_secret_key(mfshell->mfconn);
if(retval == -1)
{
file_free(file);
return -1;
}
// make sure we have a valid working directory to download to
if(mfshell->local_working_dir == NULL)
{
mfshell->local_working_dir = (char*)calloc(PATH_MAX + 1,sizeof(char));
getcwd(mfshell->local_working_dir,PATH_MAX);
}
retval = file_download_direct(file, mfshell->local_working_dir);
if(retval != -1)
printf("\r Downloaded %zd bytes OK!\n\r",bytes_read);
else
printf("\r\n Download FAILED!\n\r");
file_free(file);
return 0;
}

56
mfshell/commands/help.c Normal file
View File

@@ -0,0 +1,56 @@
/*
* 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 <stdlib.h>
#include "../mfshell.h"
#include "../commands.h"
int
mfshell_cmd_help(mfshell_t *mfshell, int argc, char **argv)
{
printf(
" arguments:\n\r"
" <optional>\n\r"
" [required]\n\r");
printf("\n\r");
int column1_width = 0;
int column2_width = 0;
_cmd_t* curr_cmd;
for (curr_cmd = mfshell->commands; curr_cmd->name != NULL; curr_cmd++) {
if (strlen(curr_cmd->name) > column1_width)
column1_width = strlen(curr_cmd->name);
if (strlen(curr_cmd->argstring) > column2_width)
column2_width = strlen(curr_cmd->argstring);
}
for (curr_cmd = mfshell->commands; curr_cmd->name != NULL; curr_cmd++) {
printf("%*s %*s %s\n", column1_width, curr_cmd->name, column2_width, curr_cmd->argstring, curr_cmd->help);
}
return 0;
}

96
mfshell/commands/host.c Normal file
View File

@@ -0,0 +1,96 @@
/*
* 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 <stdlib.h>
#include <string.h>
#include "../mfshell.h"
#include "../commands.h"
static char*
_get_host_from_user(void);
int
mfshell_cmd_host(mfshell_t *mfshell, int argc, char **argv)
{
char *alt_host = NULL;
char *host;
switch (argc) {
case 1:
alt_host = _get_host_from_user();
host = alt_host;
break;
case 2:
host = argv[1];
break;
default:
fprintf(stderr, "Invalid number of arguments\n");
return -1;
}
if(mfshell->server != NULL)
{
// do nothing if the server is exactly the same
if(strcmp(mfshell->server,host) == 0)
{
if(alt_host != NULL) free(alt_host);
return 0;
}
if(mfshell->server != NULL)
{
free(mfshell->server);
mfshell->server = NULL;
}
}
mfshell->server = strdup(host);
mfconn_destroy(mfshell->mfconn);
if(alt_host != NULL) free(alt_host);
return 0;
}
char*
_get_host_from_user(void)
{
size_t len = 0;
char *host = NULL;
printf("host: [www.mediafire.com] ");
getline(&host,&len,stdin);
if (host[strlen(host)-1] == '\n')
host[strlen(host)-1] = '\0';
if(host == NULL)
return strdup("www.mediafire.com");
if(strlen(host) < 2)
{
free(host);
return strdup("www.mediafire.com");
}
return host;
}

61
mfshell/commands/lcd.c Normal file
View File

@@ -0,0 +1,61 @@
/*
* 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 <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "../mfshell.h"
#include "../commands.h"
int
mfshell_cmd_lcd(mfshell_t *mfshell, int argc, char **argv)
{
int retval;
const char *dir;
if(mfshell == NULL) return -1;
if (argc != 2) {
fprintf(stderr, "Invalid number of arguments\n");
return -1;
}
dir = argv[1];
if(dir == NULL) return -1;
if(strlen(dir) < 1) return -1;
retval = chdir(dir);
if(retval == 0)
{
if(mfshell->local_working_dir != NULL)
{
free(mfshell->local_working_dir);
mfshell->local_working_dir = NULL;
}
mfshell->local_working_dir = strdup(dir);
}
return retval;
}

78
mfshell/commands/links.c Normal file
View File

@@ -0,0 +1,78 @@
/*
* 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 "../mfshell.h"
#include "../commands.h"
#include "../../mfapi/apicalls.h"
int
mfshell_cmd_links(mfshell_t *mfshell, int argc, char **argv)
{
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;
if (argc != 2) {
fprintf(stderr, "Invalid number of arguments\n");
return -1;
}
quickkey = argv[1];
if(quickkey == NULL) return -1;
len = strlen(quickkey);
if(len != 11 && len != 15) return -1;
file = file_alloc();
retval = mfconn_api_file_get_links(mfshell->mfconn,file,(char*)quickkey);
mfconn_update_secret_key(mfshell->mfconn);
share_link = file_get_share_link(file);
direct_link = file_get_direct_link(file);
onetime_link = file_get_onetime_link(file);
if(share_link != NULL && share_link[0] != '\0')
printf(" %-15.15s %s\n\r",
"sharing url:", share_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);
return 0;
}

63
mfshell/commands/list.c Normal file
View File

@@ -0,0 +1,63 @@
/*
* 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 <stdlib.h>
#include "../mfshell.h"
#include "../commands.h"
#include "../../mfapi/apicalls.h"
int
mfshell_cmd_list(mfshell_t *mfshell, int argc, char **argv)
{
int retval;
const char *folder_curr;
if(mfshell == NULL) return -1;
if (argc != 1) {
fprintf(stderr, "Invalid number of arguments\n");
return -1;
}
folder_curr = folder_get_key(mfshell->folder_curr);
// safety check... this should never happen
if(folder_curr == NULL)
folder_set_key(mfshell->folder_curr,"myfiles");
// safety check... this should never happen
if(folder_curr[0] == '\0')
folder_set_key(mfshell->folder_curr,"myfiles");
// first folders
retval = mfconn_api_folder_get_content(mfshell->mfconn, 0, mfshell->folder_curr);
mfconn_update_secret_key(mfshell->mfconn);
// then files
retval = mfconn_api_folder_get_content(mfshell->mfconn, 1, mfshell->folder_curr);
mfconn_update_secret_key(mfshell->mfconn);
return retval;
}

49
mfshell/commands/lpwd.c Normal file
View File

@@ -0,0 +1,49 @@
/*
* 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 <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include "../mfshell.h"
#include "../commands.h"
int
mfshell_cmd_lpwd(mfshell_t *mfshell, int argc, char **argv)
{
if(mfshell == NULL) return -1;
if (argc != 1) {
fprintf(stderr, "Invalid number of argumens\n");
return -1;
}
if(mfshell->local_working_dir == NULL)
{
mfshell->local_working_dir = (char*)calloc(PATH_MAX + 1,sizeof(char));
getcwd(mfshell->local_working_dir,PATH_MAX);
}
printf("%s\n\r", mfshell->local_working_dir);
return 0;
}

64
mfshell/commands/mkdir.c Normal file
View File

@@ -0,0 +1,64 @@
/*
* 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 <stdlib.h>
#include "../mfshell.h"
#include "../commands.h"
#include "../../mfapi/apicalls.h"
int
mfshell_cmd_mkdir(mfshell_t *mfshell, int argc, char **argv)
{
int retval;
const char *folder_curr;
const char *name;
if(mfshell == NULL) return -1;
if (argc != 2) {
fprintf(stderr, "Invalid number of arguments\n");
return -1;
}
name = argv[1];
if (name == NULL) return -1;
folder_curr = folder_get_key(mfshell->folder_curr);
// safety check... this should never happen
if(folder_curr == NULL)
folder_set_key(mfshell->folder_curr,"myfiles");
// safety check... this should never happen
if(folder_curr[0] == '\0')
folder_set_key(mfshell->folder_curr,"myfiles");
folder_curr = folder_get_key(mfshell->folder_curr);
retval = mfconn_api_folder_create(mfshell->mfconn,(char*)folder_curr,(char*)name);
mfconn_update_secret_key(mfshell->mfconn);
return retval;
}

56
mfshell/commands/pwd.c Normal file
View File

@@ -0,0 +1,56 @@
/*
* 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 <stdlib.h>
#include "../commands.h"
#include "../mfshell.h"
#include "../../utils/strings.h"
int
mfshell_cmd_pwd(mfshell_t *mfshell, int argc, char **argv)
{
const char *folder_name;
char *folder_name_tmp = NULL;
if(mfshell == NULL) return -1;
if(mfshell->folder_curr == NULL) return -1;
if (argc != 1) {
fprintf(stderr, "Invalid number of arguments\n");
return -1;
}
folder_name = folder_get_name(mfshell->folder_curr);
if(folder_name[0] == '\0') return -1;
folder_name_tmp = strdup_printf("< %s >",folder_name);
printf("%-15.13s %-50.50s\n\r",
folder_get_key(mfshell->folder_curr),
folder_name_tmp);
free(folder_name_tmp);
return 0;
}

45
mfshell/commands/whoami.c Normal file
View File

@@ -0,0 +1,45 @@
/*
* 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 <stdlib.h>
#include "../commands.h"
#include "../mfshell.h"
#include "../../mfapi/apicalls.h"
int
mfshell_cmd_whoami(mfshell_t *mfshell, int argc, char **argv)
{
int retval;
if (argc != 1) {
fprintf(stderr, "Invalid number of arguments\n");
return -1;
}
retval = mfconn_api_user_get_info(mfshell->mfconn);
mfconn_update_secret_key(mfshell->mfconn);
return retval;
}

209
mfshell/main.c Normal file
View File

@@ -0,0 +1,209 @@
/*
* 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 <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <getopt.h>
#include <wordexp.h>
#include <openssl/ssl.h>
#include "mfshell.h"
#include "../utils/strings.h"
static void
mfshell_run(mfshell_t *mfshell);
static void
mfshell_parse_commands(mfshell_t *mfshell, char *command);
void print_help(mfshell_t *mfshell, char *cmd)
{
fprintf(stderr, "A shell to access a MediaFire account.\n");
fprintf(stderr, "\n");
fprintf(stderr, "Usage: %s [options]\n", cmd);
fprintf(stderr, "\n");
fprintf(stderr, " -h, --help Print this help\n");
fprintf(stderr, " -v, --version Print version information\n");
fprintf(stderr, " -c, --command=<CMD> Run command CMD and exit\n");
fprintf(stderr, " -u, --username=<USER> Login username\n");
fprintf(stderr, " -p, --password=<PASS> Login password\n");
fprintf(stderr, " -s, --server=<SERVER> Login server\n");
fprintf(stderr, "\n");
fprintf(stderr, "Username and password are optional. If not given, they\n"
"have to be entered via standard input.\n");
fprintf(stderr, "\n");
fprintf(stderr, "You should not pass your password as a commandline\n"
"argument as other users with access to the list of\n"
"running processes will then be able to see it.\n");
fprintf(stderr, "\n");
fprintf(stderr, "Use the \"help\" command to print a list of available\n"
"commands in the interactive environment:\n");
fprintf(stderr, "\n");
fprintf(stderr, " %s -c help\n", cmd);
fprintf(stderr, "\n");
}
void parse_argv(mfshell_t *mfshell, int argc, char **argv, char **username,
char **password, char **server, char **command)
{
static struct option long_options[] = {
{"command", required_argument, 0, 'c'},
{"username", required_argument, 0, 'u'},
{"password", required_argument, 0, 'p'},
{"server", required_argument, 0, 's'},
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'v'}
};
int c;
for (;;) {
c = getopt_long(argc, argv, "c:u:p:s:hv", long_options, NULL);
if (c == -1)
break;
switch (c) {
case 'c':
*command = strdup(optarg);
break;
case 'u':
*username = strdup(optarg);
break;
case 'p':
*password = strdup(optarg);
break;
case 's':
*server = strdup(optarg);
break;
case 'h':
print_help(mfshell, argv[0]);
exit(0);
case 'v':
exit(0);
case '?':
exit(1);
break;
default:
fprintf(stderr, "getopt_long returned character code %c\n", c);
}
}
if (optind < argc) {
// TODO: handle non-option argv elements
fprintf(stderr, "Unexpected positional arguments\n");
exit(1);
}
if (*password != NULL && *username == NULL) {
fprintf(stderr, "You cannot pass the password without the username\n");
exit(1);
}
}
int main(int argc,char **argv)
{
mfshell_t *mfshell;
char *server = "www.mediafire.com";
char *username = NULL;
char *password = NULL;
char *command = NULL;
size_t len;
int retval;
SSL_library_init();
parse_argv(mfshell, argc, argv, &username, &password, &server, &command);
mfshell = mfshell_create(35860,
"2c6dq0gb2sr8rgsue5a347lzpjnaay46yjazjcjg",server);
if (command == NULL) {
// begin shell mode
mfshell_run(mfshell);
} else {
// interpret command
mfshell_parse_commands(mfshell, command);
}
return 0;
}
static void
mfshell_parse_commands(mfshell_t *mfshell, char *command)
{
char *next;
int ret;
wordexp_t p;
// FIXME: don't split by semicolon but by unescaped/unquoted semicolon
while ((next = strsep(&command, ";")) != NULL) {
// FIXME: handle non-zero return value of wordexp
ret = wordexp(next, &p, WRDE_SHOWERR | WRDE_UNDEF);
if (p.we_wordc < 1) {
fprintf(stderr, "Need more than zero arguments\n");
exit(1);
}
mfshell_exec(mfshell, p.we_wordc, p.we_wordv);
wordfree(&p);
}
}
static void
mfshell_run(mfshell_t *mfshell)
{
char *cmd = NULL;
size_t len;
int abort = 0;
int retval;
do
{
printf("\n\rmfshell > ");
retval = getline(&cmd,&len,stdin);
if (retval == -1) {
exit(1);
}
if (cmd[strlen(cmd)-1] == '\n')
cmd[strlen(cmd)-1] = '\0';
printf("\n\r");
if(strcmp(cmd,"exit") == 0)
{
abort = 1;
continue;
}
if(strcmp(cmd,"quit") == 0)
{
abort = 1;
continue;
}
retval = mfshell_exec_shell_command(mfshell,cmd);
free(cmd);
cmd = NULL;
}
while(abort == 0);
return;
}

131
mfshell/mfshell.c Normal file
View File

@@ -0,0 +1,131 @@
/*
* 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 <termios.h>
#include <unistd.h>
#include <wordexp.h>
#include <sys/ioctl.h>
#include "mfshell.h"
#include "../utils/stringv.h"
#include <curl/curl.h>
#include "mfshell.h"
#include "commands.h"
struct _cmd_s commands[] = {
{"help", "", "show this help", mfshell_cmd_help},
{"debug", "", "show debug information", mfshell_cmd_debug},
{"host", "<server>", "change target server", mfshell_cmd_host},
{"auth", "<user <pass>>", "authenticate with active server",
mfshell_cmd_auth},
{"whoami", "", "show basic user info", mfshell_cmd_whoami},
{"ls", "", "show contents of active folder",
mfshell_cmd_list},
{"cd", "[folderkey]", "change active folder", mfshell_cmd_chdir},
{"pwd", "", "show the active folder", mfshell_cmd_pwd},
{"lpwd", "", "show the local working directory",
mfshell_cmd_lpwd},
{"lcd", "[dir]", "change the local working directory",
mfshell_cmd_lcd},
{"mkdir", "[folder name]", "create a new folder", mfshell_cmd_mkdir},
{"file", "[quickkey]", "show file information", mfshell_cmd_file},
{"links", "[quickkey]", "show access urls for the file",
mfshell_cmd_links},
{"get", "[quickkey]", "download a file", mfshell_cmd_get},
{NULL, NULL, NULL, NULL}
};
mfshell_t*
mfshell_create(int app_id,char *app_key,char *server)
{
mfshell_t *mfshell;
if(app_id <= 0) return NULL;
if(app_key == NULL) return NULL;
if(server == NULL) return NULL;
/*
check to see if the server contains a forward-slash. if so,
the caller did not understand the API and passed in the wrong
type of server resource.
*/
if(strchr(server,'/') != NULL) return NULL;
mfshell = (mfshell_t*)calloc(1,sizeof(mfshell_t));
mfshell->app_id = app_id;
mfshell->app_key = strdup(app_key);
mfshell->server = strdup(server);
// object to track folder location
mfshell->folder_curr = folder_alloc();
folder_set_key(mfshell->folder_curr,"myfiles");
// shell commands
mfshell->commands = commands;
return mfshell;
}
int
mfshell_exec(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
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