From 55bfe1fb8992c450d97c97a109563a8b49be7744 Mon Sep 17 00:00:00 2001 From: login Date: Fri, 15 Nov 2019 01:45:07 +0000 Subject: Improving the messaging around sending transaction messages Several changes were made that were difficult to separate into separate commits. Since features are directly committed to master, there are no feature branches. The high-level list of changes is as follows: 1. Fixed a bug in pcoin where using "pcoin send -s" for silentsend would forget to add the transaction message that provides a record of who transferred how much and when, even if there is no custom message appended (send_message(.) was not called). 2. Improved the help text to make it clear that sending messages could be done using silentsend too. 3. In several cases in tcoin and pcoin, certain ways of doing a silentsend would still send with verbose output because, by mistake, the option passed to send_message(.) was not changed from "verbose" to "silent". 4. In pcoin, there was the case of 6 arguments missing, where one does pcoin send -s . This case was handled in tcoin but not pcoin. Now, it is handled in pcoin too. 5. Now, when a custom message is sent in tcoin or pcoin, an additional text confirming the successful sending of the custom message as distinct from the one confirming the sending of the coins is displayed to the user. --- pcoin.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++++++----------- tcoin.cpp | 68 +++++++++++++++++++++++++++++++++++++++++---------------------- 2 files changed, 96 insertions(+), 36 deletions(-) diff --git a/pcoin.cpp b/pcoin.cpp index 9031cb3..fb85cdd 100644 --- a/pcoin.cpp +++ b/pcoin.cpp @@ -539,7 +539,7 @@ bool files_are_same(const char* file_path1, const char* file_path2) //deemed the same. } -int send_message(const char* sender_username, const char* receiver_username, const char* message, const long long int &amount_sent) +int send_message(const char* sender_username, const char* receiver_username, const char* message, const long long int &amount_sent, const char* option) { std::string random_string = std::to_string(rand()); @@ -946,7 +946,15 @@ int send_message(const char* sender_username, const char* receiver_username, con break; } } - + //finally, everything ran well and we can send the message to stdout if verbose is turned on + if(!strcmp(option, "verbose") && strcmp(message, "")) //message should not be empty + { + //since the message to stdout from send() ends with "\n\n", we have commented this below line out + //std::cout << "\n"; + std::cout << "In addition, the transaction message \"" << message << "\" was "; + std::cout << "sent from `" << sender_username << "` to `" << receiver_username << "` successfully."; + std::cout << "\n\n"; + } return 0; } @@ -1336,8 +1344,8 @@ void help() std::cout << "\n`" << PCOIN_BIN_PATH_W_SPACE << "internal_balance ` or `" << PCOIN_BIN_PATH_W_SPACE << "-ib `: print the amount you owe "; std::cout << "\n`" << PCOIN_BIN_PATH_W_SPACE << "add_internal_balance ` or `" << PCOIN_BIN_PATH_W_SPACE << "-aib `: add to the amount you owe "; std::cout << "\n`" << PCOIN_BIN_PATH_W_SPACE << "send ` or `" << PCOIN_BIN_PATH_W_SPACE << "-s `: send tildecoins to "; - std::cout << "\n`" << PCOIN_BIN_PATH_W_SPACE << "send [\"\"]`: optionally, include a message to be sent to "; - std::cout << "\n`" << PCOIN_BIN_PATH_W_SPACE << "silentsend `, `" << PCOIN_BIN_PATH_W_SPACE << "send -s ` or `" << PCOIN_BIN_PATH_W_SPACE << "-ss `: send tildecoins to without printing anything"; + std::cout << "\n`" << PCOIN_BIN_PATH_W_SPACE << "send \"\"`: optionally, include a message to be sent to "; + std::cout << "\n`" << PCOIN_BIN_PATH_W_SPACE << "silentsend [\"\"]`, `" << PCOIN_BIN_PATH_W_SPACE << "send -s [\"\"]` or `" << PCOIN_BIN_PATH_W_SPACE << "-ss [\"\"]`: send tildecoins to with an optional (as indicated by [ and ], which should not be included in the actual comment) message included without printing anything"; std::cout << "\nIn the commands with ` `, switching the two arguments around (i.e., from ` ` to ` `) will also work"; std::cout << "\n`" << PCOIN_BIN_PATH_W_SPACE << "--help`, `" << PCOIN_BIN_PATH_W_SPACE << "help` or `" << PCOIN_BIN_PATH_W_SPACE << "-h`: print this help text"; std::cout << "\nSend an email to `login@tilde.town` (tilde.town local email) or `login@tilde.team` (internet-wide email), or `/query login` on IRC to report any errors or request a key for your program.\n\n"; @@ -1745,10 +1753,20 @@ int main(int argc, char *argv[]) if(argc == 5) { if(!strcmp(argv[2], "-s")) + { + int return_value; if(is_number(argv[3])) - send(get_username().c_str(), argv[4], strtol100(argv[3]), base_amount, "silent"); + return_value = send(get_username().c_str(), argv[2], strtol100(argv[3]), base_amount, "silent"); else - send(get_username().c_str(), argv[3], strtol100(argv[4]), base_amount, "silent"); + return_value = send(get_username().c_str(), argv[3], strtol100(argv[2]), base_amount, "silent"); + if(!return_value) //send was successful + { + if(is_number(argv[3])) + send_message(get_username().c_str(), argv[2], "", strtol100(argv[3]), "silent"); + else + send_message(get_username().c_str(), argv[3], "", strtol100(argv[2]), "silent"); + } + } else //argument count is 5 because a custom message was included { int return_value; @@ -1758,10 +1776,30 @@ int main(int argc, char *argv[]) return_value = send(get_username().c_str(), argv[3], strtol100(argv[2]), base_amount, "verbose"); if(!return_value) //send was successful + { if(is_number(argv[3])) - send_message(get_username().c_str(), argv[2], argv[4], strtol100(argv[3])); + send_message(get_username().c_str(), argv[2], argv[4], strtol100(argv[3]), "verbose"); else - send_message(get_username().c_str(), argv[3], argv[4], strtol100(argv[2])); + send_message(get_username().c_str(), argv[3], argv[4], strtol100(argv[2]), "verbose"); + } + } + } + else if(argc == 6) + { + if(!strcmp(argv[2], "-s")) + { //argument count is 6 because of silent send with custom message included + int return_value; + if(is_number(argv[3])) + return_value = send(get_username().c_str(), argv[4], strtol100(argv[3]), base_amount, "silent"); + else + return_value = send(get_username().c_str(), argv[3], strtol100(argv[4]), base_amount, "silent"); + if(!return_value) //send was successful + { + if(is_number(argv[3])) + send_message(get_username().c_str(), argv[4], argv[5], strtol100(argv[3]), "silent"); + else + send_message(get_username().c_str(), argv[3], argv[5], strtol100(argv[4]), "silent"); + } } } else if(argc < 4) @@ -1782,10 +1820,12 @@ int main(int argc, char *argv[]) else return_value = send(get_username().c_str(), argv[2], strtol100(argv[3]), base_amount, "verbose"); if(!return_value) //send was successful + { if(is_number(argv[2])) - send_message(get_username().c_str(), argv[3], "", strtol100(argv[2])); + send_message(get_username().c_str(), argv[3], "", strtol100(argv[2]), "verbose"); else - send_message(get_username().c_str(), argv[2], "", strtol100(argv[3])); + send_message(get_username().c_str(), argv[2], "", strtol100(argv[3]), "verbose"); + } } } else if(!strcmp(argv[1], "silentsend") || !strcmp(argv[1], "-ss")) @@ -1799,9 +1839,9 @@ int main(int argc, char *argv[]) return_value = send(get_username().c_str(), argv[3], strtol100(argv[2]), base_amount, "silent"); if(!return_value) //send was successful if(is_number(argv[3])) - send_message(get_username().c_str(), argv[2], "", strtol100(argv[3])); + send_message(get_username().c_str(), argv[2], "", strtol100(argv[3]), "silent"); else - send_message(get_username().c_str(), argv[3], "", strtol100(argv[2])); + send_message(get_username().c_str(), argv[3], "", strtol100(argv[2]), "silent"); } else return 2; diff --git a/tcoin.cpp b/tcoin.cpp index 932b84f..4556ca3 100644 --- a/tcoin.cpp +++ b/tcoin.cpp @@ -828,7 +828,7 @@ int initialise_user(const char* username, const long long int &base_amount) return 0; } -int send_message(const char* sender_username, const char* receiver_username, const char* message, const long long int &amount_sent) +int send_message(const char* sender_username, const char* receiver_username, const char* message, const long long int &amount_sent, const char* option) { std::string random_string = std::to_string(rand()); @@ -1144,7 +1144,15 @@ int send_message(const char* sender_username, const char* receiver_username, con break; } } - + //finally, everything ran well and we can send the message to stdout if verbose is turned on + if(!strcmp(option, "verbose") && strcmp(message, "")) //message should not be empty + { + //since the message to stdout from send() ends with "\n\n", we have commented this below line out + //std::cout << "\n"; + std::cout << "In addition, the transaction message \"" << message << "\" was "; + std::cout << "sent from `" << sender_username << "` to `" << receiver_username << "` successfully."; + std::cout << "\n\n"; + } return 0; } @@ -1420,8 +1428,8 @@ void help() std::cout << "\n`tcoin messages ` or `tcoin -m `: print the last messages"; std::cout << "\n`tcoin clear_messages` or `tcoin -cm`: clear all messages"; std::cout << "\n`tcoin send ` or `tcoin -s `: send tildecoins to "; - std::cout << "\n`tcoin send [\"\"]`: optionally, include a message to be sent to "; - std::cout << "\n`tcoin silentsend `, `tcoin send -s ` or `tcoin -ss `: send tildecoins to without printing anything"; + std::cout << "\n`tcoin send \"\"` or `tcoin -s \"\"`: optionally, include a message to be sent to "; + std::cout << "\n`tcoin silentsend [\"\"]`, `tcoin send -s [\"\"]` or `tcoin -ss [\"\"]`: send tildecoins to with an optional (as indicated by [ and ], which should not be included in the actual comment) message included without printing anything"; std::cout << "\nIn the commands with ` `, switching the two arguments around (i.e., from ` ` to ` `) will also work"; std::cout << "\n`tcoin --help`, `tcoin help` or `tcoin -h`: print this help text"; std::cout << "\nSend an email to `login@tilde.town` (tilde.town local email) or `login@tilde.team` (internet-wide email), or `/query login` on IRC to request a passphrase reset.\n\n"; @@ -1430,7 +1438,7 @@ void help() bool is_number(const char* test_string) { char* p; - strtod(test_string, &p); + std::strtod(test_string, &p); return *p == 0; } @@ -1606,18 +1614,20 @@ int main(int argc, char *argv[]) { if(argc == 5) { - if(!strcmp(argv[2], "-s")) + if(!strcmp(argv[2], "-s")) //silent send with no message { int return_value; if(is_number(argv[3])) - return_value = send(get_username().c_str(), argv[4], strtol100(argv[3]), base_amount, "verbose"); + return_value = send(get_username().c_str(), argv[4], strtol100(argv[3]), base_amount, "silent"); else - return_value = send(get_username().c_str(), argv[3], strtol100(argv[4]), base_amount, "verbose"); + return_value = send(get_username().c_str(), argv[3], strtol100(argv[4]), base_amount, "silent"); if(!return_value) //send was successful + { if(is_number(argv[3])) - send_message(get_username().c_str(), argv[4], "", strtol100(argv[3])); + send_message(get_username().c_str(), argv[4], "", strtol100(argv[3]), "silent"); else - send_message(get_username().c_str(), argv[3], "", strtol100(argv[4])); + send_message(get_username().c_str(), argv[3], "", strtol100(argv[4]), "silent"); + } } else //argument count is 5 because a custom message was included { @@ -1627,10 +1637,12 @@ int main(int argc, char *argv[]) else return_value = send(get_username().c_str(), argv[2], strtol100(argv[3]), base_amount, "verbose"); if(!return_value) //send was successful + { if(is_number(argv[2])) - send_message(get_username().c_str(), argv[3], argv[4], strtol100(argv[2])); + send_message(get_username().c_str(), argv[3], argv[4], strtol100(argv[2]), "verbose"); else - send_message(get_username().c_str(), argv[2], argv[4], strtol100(argv[3])); + send_message(get_username().c_str(), argv[2], argv[4], strtol100(argv[3]), "verbose"); + } } } else if(argc == 6) @@ -1639,14 +1651,16 @@ int main(int argc, char *argv[]) { //argument count is 6 because of silent send with custom message included int return_value; if(is_number(argv[3])) - return_value = send(get_username().c_str(), argv[4], strtol100(argv[3]), base_amount, "verbose"); + return_value = send(get_username().c_str(), argv[4], strtol100(argv[3]), base_amount, "silent"); else - return_value = send(get_username().c_str(), argv[3], strtol100(argv[4]), base_amount, "verbose"); + return_value = send(get_username().c_str(), argv[3], strtol100(argv[4]), base_amount, "silent"); if(!return_value) //send was successful + { if(is_number(argv[3])) - send_message(get_username().c_str(), argv[4], argv[5], strtol100(argv[3])); + send_message(get_username().c_str(), argv[4], argv[5], strtol100(argv[3]), "silent"); else - send_message(get_username().c_str(), argv[3], argv[5], strtol100(argv[4])); + send_message(get_username().c_str(), argv[3], argv[5], strtol100(argv[4]), "silent"); + } } } else if(argc < 4) @@ -1667,10 +1681,12 @@ int main(int argc, char *argv[]) else return_value = send(get_username().c_str(), argv[2], strtol100(argv[3]), base_amount, "verbose"); if(!return_value) //send was successful + { if(is_number(argv[2])) - send_message(get_username().c_str(), argv[3], "", strtol100(argv[2])); + send_message(get_username().c_str(), argv[3], "", strtol100(argv[2]), "verbose"); else - send_message(get_username().c_str(), argv[2], "", strtol100(argv[3])); + send_message(get_username().c_str(), argv[2], "", strtol100(argv[3]), "verbose"); + } } } else if(!strcmp(argv[1], "silentsend") || !strcmp(argv[1], "-ss")) @@ -1683,23 +1699,27 @@ int main(int argc, char *argv[]) else return_value = send(get_username().c_str(), argv[2], strtol100(argv[3]), base_amount, "silent"); if(!return_value) //send was successful + { if(is_number(argv[2])) - send_message(get_username().c_str(), argv[3], "", strtol100(argv[2])); + send_message(get_username().c_str(), argv[3], "", strtol100(argv[2]), "silent"); else - send_message(get_username().c_str(), argv[2], "", strtol100(argv[3])); + send_message(get_username().c_str(), argv[2], "", strtol100(argv[3]), "silent"); + } } if(argc==5) //custom message included { int return_value; if(is_number(argv[2])) - return_value = send(get_username().c_str(), argv[3], strtol100(argv[2]), base_amount, "verbose"); + return_value = send(get_username().c_str(), argv[3], strtol100(argv[2]), base_amount, "silent"); else - return_value = send(get_username().c_str(), argv[2], strtol100(argv[3]), base_amount, "verbose"); + return_value = send(get_username().c_str(), argv[2], strtol100(argv[3]), base_amount, "silent"); if(!return_value) //send was successful + { if(is_number(argv[2])) - send_message(get_username().c_str(), argv[3], argv[4], strtol100(argv[2])); + send_message(get_username().c_str(), argv[3], argv[4], strtol100(argv[2]), "silent"); else - send_message(get_username().c_str(), argv[2], argv[4], strtol100(argv[3])); + send_message(get_username().c_str(), argv[2], argv[4], strtol100(argv[3]), "silent"); + } } else return 2; -- cgit 1.4.1-2-gfad0