From 9d7a93c6223eaa39ae4ac3ccb0cdc4f9d66dde70 Mon Sep 17 00:00:00 2001 From: tobast Date: Thu, 24 Nov 2011 22:51:30 +0100 Subject: [PATCH] Added what I coded on my laptop: request building, etc. --- ConfigRead.cpp | 36 +++++++++++++++++++++++++++++++++ ConfigRead.h | 48 +++++++++++++++++++++++++++++++++++++++++++ data.h | 11 ++++++++++ func.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++++++++-- func.h | 6 ++++-- main.cpp | 17 ++++++++++++++++ 6 files changed, 169 insertions(+), 4 deletions(-) create mode 100644 ConfigRead.cpp create mode 100644 ConfigRead.h diff --git a/ConfigRead.cpp b/ConfigRead.cpp new file mode 100644 index 0000000..271bccd --- /dev/null +++ b/ConfigRead.cpp @@ -0,0 +1,36 @@ + +/* + * PROGRAM: + * Command-line pastebin + * + * AUTHOR: + * Théophile BASTIAN (a.k.a. Tobast) + * + * CONTACT & WEBSITE: + * http://tobast.fr/ (contact feature included) + * error-report@tobast.fr (error reporting only) + * + * SHORT DESCRIPTION: + * See first license line. + * + * LICENSE: + * "Command-line pastebin" is a software designed to submit a "paste" on http://pastebin.com/ using a command-line tool + * Copyright (C) 2011 Théophile BASTIAN + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * 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, see http://www.gnu.org/licenses/gpl.txt. +*/ + +#include "ConfigRead.h" + + diff --git a/ConfigRead.h b/ConfigRead.h new file mode 100644 index 0000000..292d844 --- /dev/null +++ b/ConfigRead.h @@ -0,0 +1,48 @@ + +/* + * PROGRAM: + * Command-line pastebin + * + * AUTHOR: + * Théophile BASTIAN (a.k.a. Tobast) + * + * CONTACT & WEBSITE: + * http://tobast.fr/ (contact feature included) + * error-report@tobast.fr (error reporting only) + * + * SHORT DESCRIPTION: + * See first license line. + * + * LICENSE: + * "Command-line pastebin" is a software designed to submit a "paste" on http://pastebin.com/ using a command-line tool + * Copyright (C) 2011 Théophile BASTIAN + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * 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, see http://www.gnu.org/licenses/gpl.txt. +*/ + +#ifndef DEF_CONFIGREAD +#define DEF_CONFIGREAD + +#include +#include +#include "data.h" + +class ConfigRead +{ + public: + ConfigRead(std::string filepath=DEFAULT_CONFIGPATH); +} + +#endif//DEF_CONFIGREAD + diff --git a/data.h b/data.h index 265d239..526f3dc 100644 --- a/data.h +++ b/data.h @@ -47,6 +47,17 @@ #define DEFAULT_USERKEY "" #define DEFAULT_OPTION "paste" +#define PASTEBIN_SUBMIT_URL "http://pastebin.com/api/api_post.php" + +#ifdef UNIX + #define DEFAULT_CONFIGPATH "~/.pastebinclrc" +#endif +#ifdef WINDOWS + #define DEFAULT_CONFIGPATH "%APPLICATION_DATA%/.pastebinclrc" +#endif +#ifndef DEFAULT_CONFIGPATH + #define DEFAULT_CONFIGPATH "" +#endif enum FlagParameter { P_NAME=1, P_SYNTAX=2, P_PRIVATE=4, P_EXPIRE=8, P_GUEST=16, P_HELP=32, P_VERSION=64, P_LICENCE=128 diff --git a/func.cpp b/func.cpp index 6937629..af9e168 100644 --- a/func.cpp +++ b/func.cpp @@ -114,8 +114,59 @@ void showVersion() << "Error report: " << std::endl << std::endl; } -CurlPost* preparePostRequest(const unsigned& flags, const ParameterRead& pr) +CurlPost* preparePostRequest(const unsigned& flags, ParameterRead& pr) { - // TODO! + CurlPost* request=new CurlPost(PASTEBIN_SUBMIT_URL); + + if(flags & P_NAME) + { + if(pr.isSet("n")) + request->setPostData("api_paste_name", pr.getValue("n")); + else if(pr.isSet("name")); + request->setPostData("api_paste_name", pr.getValue("name")); + } + else + request->setPostData("name", DEFAULT_NAME); + + if(flags & P_SYNTAX) + { + if(pr.isSet("s")) + request->setPostData("api_paste_format", pr.getValue("s")); + else if(pr.isSet("syntax")); + request->setPostData("api_paste_format", pr.getValue("syntax")); + } + + if(flags & P_PRIVATE) + request->setPostData("api_paste_private", "1"); + else + request->setPostData("api_paste_private", DEFAULT_PRIVATE); + + if(flags & P_EXPIRE) + { + if(pr.isSet("e")) + request->setPostData("api_paste_expire_date", pr.getValue("e")); + else if(pr.isSet("expire")); + request->setPostData("api_paste_expire_date", pr.getValue("expire")); + } + else + request->setPostData("api_paste_expire_date", DEFAULT_EXPIRE); + + if(!(flags & P_GUEST)) + { + std::string usercode=getUserCode(); + if(!usercode.empty()) + request->setPostData("api_user_key", usercode); + } + + request->setPostData("api_dev_key", DEVELOPPER_CODE); + request->setPostData("api_option", "paste"); + + return request; +} + +std::string getUserCode() +{ + //TODO!!! + return ""; } diff --git a/func.h b/func.h index ebf5c47..5a49bb5 100644 --- a/func.h +++ b/func.h @@ -36,7 +36,7 @@ #include #include -#include +#include "CurlPost.h" #include "data.h" bool initParameterRead(ParameterRead& pr, unsigned& flags); @@ -46,7 +46,9 @@ void showLicence(); void showHelp(); void showVersion(); -CurlPost* preparePostRequest(const unsigned& flags, const ParameterRead& pr); +CurlPost* preparePostRequest(const unsigned& flags, ParameterRead& pr); + +std::string getUserCode(); #endif//DEF_FUNCTION diff --git a/main.cpp b/main.cpp index 0285a53..e2a1214 100644 --- a/main.cpp +++ b/main.cpp @@ -36,6 +36,7 @@ #include "func.h" #include #include +#include int main(int argc, char** argv) { @@ -60,6 +61,22 @@ int main(int argc, char** argv) { // Here we start real stuff! CurlPost::init(); + + CurlPost* request=preparePostRequest(parameterFlags, pr); + std::string result=request->execute(); + delete request; + + if(result.find("http://pastebin.com/") == 0) // SUCCESS ! + { + std::cout << "Paste submitted successfully!" << std::endl + << "URL: " << result << std::endl; + } + else + { + std::cerr << "ERROR: Pastebin.com returned an error to your request." << std::endl + << "Message: " << result << std::endl; + } + CurlPost::clean(); } }