diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index a310dc7b..d3c218fe 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -240,6 +240,7 @@ static const CRPCCommand vRPCCommands[] = { "dhtget", &dhtget, false, true }, { "newpostmsg", &newpostmsg, false, true }, { "newdirectmsg", &newdirectmsg, false, true }, + { "newrtmsg", &newrtmsg, false, true }, }; CRPCTable::CRPCTable() diff --git a/src/bitcoinrpc.h b/src/bitcoinrpc.h index dc58af39..9eb308de 100644 --- a/src/bitcoinrpc.h +++ b/src/bitcoinrpc.h @@ -197,5 +197,6 @@ extern json_spirit::Value dhtput(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value dhtget(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value newpostmsg(const json_spirit::Array& params, bool fHelp); extern json_spirit::Value newdirectmsg(const json_spirit::Array& params, bool fHelp); +extern json_spirit::Value newrtmsg(const json_spirit::Array& params, bool fHelp); #endif diff --git a/src/twister.cpp b/src/twister.cpp index a54c97b6..83ba44ab 100644 --- a/src/twister.cpp +++ b/src/twister.cpp @@ -805,6 +805,31 @@ Value entryToJson(const entry &e) } } +entry jsonToEntry(const Value &v) +{ + entry::list_type lst; + entry::dictionary_type dict; + + switch( v.type() ) { + case int_type: + return v.get_int(); + case str_type: + return v.get_str(); + case array_type: + for (Array::const_iterator i = v.get_array().begin(); i != v.get_array().end(); ++i) { + lst.push_back( jsonToEntry(*i) ); + } + return lst; + case obj_type: + for (Object::const_iterator i = v.get_obj().begin(); i != v.get_obj().end(); ++i) { + dict[ i->name_ ] = jsonToEntry(i->value_); + } + return dict; + default: + return string(""); + } +} + Value dhtput(const Array& params, bool fHelp) { if (fHelp || params.size() < 5 || params.size() > 6) @@ -961,7 +986,7 @@ Value newpostmsg(const Array& params, bool fHelp) } } - return Value(std::string(buf.data(),buf.size())); + return entryToJson(v); } Value newdirectmsg(const Array& params, bool fHelp) @@ -1000,5 +1025,50 @@ Value newdirectmsg(const Array& params, bool fHelp) torrent_handle h = startTorrentUser(strFrom); h.add_piece(k,buf.data(),buf.size()); - return Value(std::string(buf.data(),buf.size())); + return entryToJson(v); +} + +Value newrtmsg(const Array& params, bool fHelp) +{ + if (fHelp || (params.size() != 3)) + throw runtime_error( + "newrtmsg \n" + "Post a new RT to swarm"); + + EnsureWalletIsUnlocked(); + + string strUsername = params[0].get_str(); + string strK = params[1].get_str(); + entry vrt = jsonToEntry(params[2].get_obj()); + int k = atoi( strK.c_str() ); + + entry v; + if( !createSignedUserpost(v, strUsername, k, "", + vrt.find_key("userpost"), vrt.find_key("sig_userpost"), NULL, + std::string(""), 0) ) + throw JSONRPCError(RPC_INTERNAL_ERROR,"error signing post with private key of user"); + + vector buf; + bencode(std::back_inserter(buf), v); + + std::string errmsg; + if( !acceptSignedPost(buf.data(),buf.size(),strUsername,k,errmsg) ) + throw JSONRPCError(RPC_INVALID_PARAMS,errmsg); + + torrent_handle h = startTorrentUser(strUsername); + if( h.is_valid() ) { + // if member of torrent post it directly + h.add_piece(k,buf.data(),buf.size()); + } else { + // TODO: swarm resource forwarding not implemented + ses->dht_putData(strUsername, "swarm", false, + v, strUsername, GetAdjustedTime(), k); + } + + // post to dht as well + ses->dht_putData(strUsername, string("post")+strK, false, + v, strUsername, GetAdjustedTime(), k); + + return entryToJson(v); } +