From 80a60a2095d59111ee9be424e7ea8f505abb5f00 Mon Sep 17 00:00:00 2001 From: digital dreamer Date: Sun, 25 May 2014 18:10:52 +0200 Subject: [PATCH] add RSS support --- Makefile.am | 1 + src/bitcoinrpc.cpp | 23 +++++ src/twister_rss.cpp | 207 ++++++++++++++++++++++++++++++++++++++++++++ src/twister_rss.h | 18 ++++ twister-qt.pro | 2 + 5 files changed, 251 insertions(+) create mode 100644 src/twister_rss.cpp create mode 100644 src/twister_rss.h diff --git a/Makefile.am b/Makefile.am index 4802e0b8..02c30aaf 100644 --- a/Makefile.am +++ b/Makefile.am @@ -157,6 +157,7 @@ BITCOIN_TWISTER_SOURCES = \ src/txdb.cpp \ src/chainparams.cpp \ src/twister.cpp \ + src/twister_rss.cpp \ src/twister_utils.cpp \ $(SSE2_SOURCES) diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index 2b53b3b2..7f16826a 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -13,6 +13,7 @@ #include "db.h" #include "twister_utils.h" +#include "twister_rss.h" #include #include @@ -979,6 +980,28 @@ void ServiceConnection(AcceptedConnection *conn) if(strMethod == "GET" && strURI == "/") strURI="/home.html"; + if(strMethod == "GET" && strURI.substr(0, 4) == "/rss" && !GetBoolArg("-public_server_mode",false)) + { + string rssOutput; + int rssResult = generateRSS(strURI, &rssOutput); + + switch(rssResult) + { + case RSS_OK: + conn->stream() << HTTPReply(HTTP_OK, rssOutput, false, "application/rss+xml") << std::flush; + continue; + case RSS_ERROR_NO_ACCOUNT: + conn->stream() << HTTPReply(HTTP_BAD_REQUEST, "No accounts found - please register a username", false) << std::flush; + continue; + case RSS_ERROR_BAD_ACCOUNT: + conn->stream() << HTTPReply(HTTP_BAD_REQUEST, "Requested account is not registered on this node", false) << std::flush; + continue; + case RSS_ERROR_NOT_A_NUMBER: + conn->stream() << HTTPReply(HTTP_BAD_REQUEST, "Parameter 'max' must be a number", false) << std::flush; + continue; + } + } + if (strURI != "/" && strURI.find("..") == std::string::npos ) { filesystem::path pathFile = filesystem::path(GetHTMLDir()) / strURI; std::string fname = pathFile.string(); diff --git a/src/twister_rss.cpp b/src/twister_rss.cpp new file mode 100644 index 00000000..91aacacb --- /dev/null +++ b/src/twister_rss.cpp @@ -0,0 +1,207 @@ +#include "twister_rss.h" +#include "init.h" +#include "bitcoinrpc.h" +#include "json/json_spirit.h" + +#include +#include +#include +#include +#include + +using namespace std; +using namespace json_spirit; + +int generateRSS(string uri, string *output) +{ + string account = ""; + int max = 20; + + // get URI parameters if available + size_t startPosition, endPosition; + if(uri.find("max=")!=string::npos) + { + startPosition = uri.find("max=")+4; + if(uri.find("&",startPosition)!=string::npos) + endPosition=uri.find("&",startPosition); + else + endPosition=uri.length(); + try + { + max = boost::lexical_cast(uri.substr(startPosition,endPosition-startPosition)); + } + catch(boost::bad_lexical_cast e) + { + return RSS_ERROR_NOT_A_NUMBER; + } + } + if(uri.find("account=")!=string::npos) + { + startPosition = uri.find("account=")+8; + if(uri.find("&",startPosition)!=string::npos) + endPosition=uri.find("&",startPosition); + else + endPosition=uri.length(); + + account = uri.substr(startPosition,endPosition-startPosition); + } + + const Array emptyArray; + Array accountsArray = listwalletusers(emptyArray,false).get_array(); + + // if no account was specified, choose the first one + if(account=="") + { + if(accountsArray.size()>0) + { + account = accountsArray[0].get_str(); + } + else return RSS_ERROR_NO_ACCOUNT; + } + + // if an account name was specified, check that it exists + else + { + bool accountExists = false; + for(int i=0;i outputVector; + + for(int j=0;j\n" + << "\n" + << "\n" + << " Twister Postboard - " << account << "\n" + << " New posts from Twister\n"; + + int outputSize = (outputVector.size()>max)?max:outputVector.size(); + + for(int i=0;i\n" + << " " << find_value(item,"title").get_str() << "\n" + << " " << find_value(item,"author").get_str() << "\n" + << " " << find_value(item,"msg").get_str() << "\n" + << " " << timeString << "\n" + << " \n"; + } + + ret << "\n" + << "\n"; + + *output = ret.str(); + return RSS_OK; +} + +bool sortByTime (Object i,Object j) +{ + return (find_value(i,"time").get_int64()>find_value(j,"time").get_int64()); +} diff --git a/src/twister_rss.h b/src/twister_rss.h new file mode 100644 index 00000000..e618497e --- /dev/null +++ b/src/twister_rss.h @@ -0,0 +1,18 @@ +#ifndef TWISTER_RSS_H +#define TWISTER_RSS_H + +#include "json/json_spirit.h" +#include + +enum RSSResultCode +{ + RSS_OK = 0, + RSS_ERROR_NO_ACCOUNT = -1, + RSS_ERROR_BAD_ACCOUNT = -2, + RSS_ERROR_NOT_A_NUMBER = -3 +}; + +extern bool sortByTime (json_spirit::Object i,json_spirit::Object j); +extern int generateRSS(std::string uri, std::string *output); + +#endif // TWISTER_RSS_H diff --git a/twister-qt.pro b/twister-qt.pro index 0a9a1ebf..f86b9e9e 100644 --- a/twister-qt.pro +++ b/twister-qt.pro @@ -201,6 +201,7 @@ HEADERS += \ src/scrypt.h \ src/utf8core.h \ src/twister.h \ + src/twister_rss.h \ src/twister_utils.h # src/qt/bitcoingui.h @@ -277,6 +278,7 @@ SOURCES += \ #src/qt/bitcoin.cpp \ src/txdb.cpp \ src/scrypt.cpp \ src/twister.cpp \ + src/twister_rss.cpp \ src/twister_utils.cpp # src/qt/guiutil.cpp \