This commit is contained in:
Petr Bena 2014-05-25 14:33:18 +02:00
parent 003f4afdfb
commit 27018e73b4
4 changed files with 97 additions and 2 deletions

View File

@ -8,6 +8,12 @@
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details. //GNU General Public License for more details.
#include <iostream>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include "configuration.hpp" #include "configuration.hpp"
using namespace Stikkit; using namespace Stikkit;
using namespace std; using namespace std;
@ -15,5 +21,52 @@ using namespace std;
unsigned int Configuration::Verbosity = 0; unsigned int Configuration::Verbosity = 0;
string Configuration::Source = ""; string Configuration::Source = "";
string Configuration::URL = ""; string Configuration::URL = "";
string Configuration::Expiry = "60";
string Configuration::Author = ""; string Configuration::Author = "";
string Configuration::Version = "1.0.0"; string Configuration::Version = "1.0.0";
string Configuration::Title = "";
string Configuration::Home = "";
string Configuration::DefaultURL = "";
void Configuration::Init()
{
struct stat st;
struct passwd *pw = getpwuid(getuid());
string homedir = pw->pw_dir;
Configuration::Home = homedir;
if (stat("/etc/stikkit/url", &st) != -1)
{
ifstream f;
f.open("/etc/stikkit/url");
string line;
if (getline(f, line))
{
Configuration::DefaultURL = line;
}
f.close();
}
if (stat(string(homedir + "/.stikkit").c_str(), &st) == -1)
{
mkdir(string(homedir + "/.stikkit").c_str(), 0750);
} else
{
ifstream f;
f.open(string(homedir + "/.stikkit/url").c_str());
string line;
if(getline(f, line))
{
Configuration::DefaultURL = line;
}
f.close();
}
}
void Configuration::Store()
{
ofstream myfile (string(Configuration::Home + "/.stikkit/url").c_str());
if (myfile.is_open())
{
myfile << Configuration::URL << "\n";
myfile.close();
}
}

View File

@ -20,12 +20,18 @@ namespace Stikkit
class Configuration class Configuration
{ {
public: public:
static void Init();
static void Store();
static unsigned int Verbosity; static unsigned int Verbosity;
static string URL; static string URL;
static string Source; static string Source;
static string Input; static string Input;
static string Author; static string Author;
static string Title;
static string Version; static string Version;
static string Expiry;
static string DefaultURL;
static string Home;
}; };
} }

View File

@ -10,6 +10,7 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <unistd.h>
#include <curl/curl.h> #include <curl/curl.h>
#include "configuration.hpp" #include "configuration.hpp"
#include "terminalparser.hpp" #include "terminalparser.hpp"
@ -26,6 +27,7 @@ static size_t WriteCallback(void *contents, size_t size, size_t nmemb)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
Stikkit::Configuration::Init();
Stikkit::TerminalParser *t = new Stikkit::TerminalParser(argc, argv); Stikkit::TerminalParser *t = new Stikkit::TerminalParser(argc, argv);
if (t->Parse()) if (t->Parse())
{ {
@ -33,10 +35,42 @@ int main(int argc, char *argv[])
return 0; return 0;
} }
delete t; delete t;
if (!Stikkit::Configuration::Author.length())
{
char buffer[200];
if (getlogin_r(buffer, 200))
{
Stikkit::Configuration::Author = buffer;
}
}
if (!Stikkit::Configuration::URL.length()) if (!Stikkit::Configuration::URL.length())
{ {
Stikkit::Syslog::ErrorLog("No URL to stikked server provided, use stikkit -b <URL>"); if (Stikkit::Configuration::DefaultURL.length() > 0)
return 20; {
Stikkit::Configuration::URL = Stikkit::Configuration::DefaultURL;
} else
{
Stikkit::Syslog::ErrorLog("No URL to stikked server provided, use stikkit -b <URL>");
return 20;
}
}
if (!Stikkit::Configuration::DefaultURL.length())
{
std::cout << "There is no default URL stored in configuration files, do you want to set " <<
Stikkit::Configuration::URL << " as a default URL? (y/n)";
string response;
std::cin >> response;
if (response == "y" || response == "n")
{
if (response[0] == 'y')
{
Stikkit::Configuration::Store();
}
} else
{
Stikkit::Syslog::ErrorLog("Invalid option!");
return 2;
}
} }
Stikkit::Configuration::URL += "/api/create"; Stikkit::Configuration::URL += "/api/create";
std::string line; std::string line;

View File

@ -106,6 +106,8 @@ void TerminalParser::DisplayHelp()
cout << "Stikkit - open source pastebin uploader for Stikked\n\n"\ cout << "Stikkit - open source pastebin uploader for Stikked\n\n"\
"Parameters:\n"\ "Parameters:\n"\
" -a <name>: Specify author name\n"\
" -b <url>: Specify URL of Stikked server\n"\
" -v: Increases verbosity\n"\ " -v: Increases verbosity\n"\
" --version: Display a version\n"\ " --version: Display a version\n"\
" -h | --help: Display this help\n\n"\ " -h | --help: Display this help\n\n"\