From 7e47ef07dfe522e556c218053d027fa5d54e0e7d Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 18 Dec 2019 18:09:32 +0300 Subject: [PATCH] tg_cli: automatic delimiter detection. GitOrigin-RevId: da4933c0369cd4d1bb7e181e5f929262365464c3 --- td/telegram/cli.cpp | 58 +++++++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index d274a858..8c0ad6f2 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -56,6 +56,7 @@ #include #include #include +#include #ifdef USE_READLINE /* Standard readline include files. */ @@ -431,6 +432,23 @@ class CliClient final : public Actor { } } + static char get_delimiter(Slice str) { + std::unordered_set chars; + for (auto c : trim(str)) { + if (c < '0' || c > '9') { + chars.insert(c); + } + } + if (chars.empty()) { + return ' '; + } + if (chars.size() == 1) { + return *chars.begin(); + } + LOG(ERROR) << "Failed to determine delimiter in \"" << str << '"'; + return ' '; + } + int64 as_chat_id(Slice str) const { str = trim(str); if (str[0] == '@') { @@ -459,8 +477,9 @@ class CliClient final : public Actor { return td_api::make_object(); } - vector as_chat_ids(Slice chat_ids, char delimiter = ' ') const { - return transform(full_split(trim(chat_ids), delimiter), [this](Slice str) { return as_chat_id(str); }); + vector as_chat_ids(Slice chat_ids) const { + return transform(full_split(trim(chat_ids), get_delimiter(chat_ids)), + [this](Slice str) { return as_chat_id(str); }); } static int64 as_message_id(Slice str) { @@ -471,8 +490,8 @@ class CliClient final : public Actor { return to_integer(str); } - static vector as_message_ids(Slice message_ids, char delimiter = ' ') { - return transform(full_split(trim(message_ids), delimiter), as_message_id); + static vector as_message_ids(Slice message_ids) { + return transform(full_split(trim(message_ids), get_delimiter(message_ids)), as_message_id); } static int32 as_button_id(Slice str) { @@ -495,8 +514,8 @@ class CliClient final : public Actor { return to_integer(str); } - vector as_user_ids(Slice user_ids, char delimiter = ' ') const { - return transform(full_split(user_ids, delimiter), [this](Slice str) { return as_user_id(str); }); + vector as_user_ids(Slice user_ids) const { + return transform(full_split(user_ids, get_delimiter(user_ids)), [this](Slice str) { return as_user_id(str); }); } static int32 as_basic_group_id(Slice str) { @@ -602,8 +621,8 @@ class CliClient final : public Actor { } template - static vector to_integers(Slice ids_string, char delimiter = ' ') { - return transform(transform(full_split(ids_string, delimiter), trim), to_integer); + static vector to_integers(Slice ids_string) { + return transform(transform(full_split(ids_string, get_delimiter(ids_string)), trim), to_integer); } void on_result(uint64 generation, uint64 id, td_api::object_ptr result) { @@ -1159,8 +1178,8 @@ class CliClient final : public Actor { return td_api::make_object(); } - static auto as_passport_element_types(Slice types, char delimiter = ',') { - return transform(full_split(types, delimiter), [](Slice str) { return as_passport_element_type(str); }); + static auto as_passport_element_types(Slice types) { + return transform(full_split(types, get_delimiter(types)), [](Slice str) { return as_passport_element_type(str); }); } static td_api::object_ptr as_input_passport_element(string passport_element_type, @@ -2182,8 +2201,8 @@ class CliClient final : public Actor { std::tie(chat_ids, args) = split(args); std::tie(exclude_chat_ids, chat_ids_limit) = split(args); send_request(td_api::make_object( - 10000000, -1, -1, 0, std::vector>(), as_chat_ids(chat_ids, ','), - as_chat_ids(exclude_chat_ids, ','), to_integer(chat_ids_limit))); + 10000000, -1, -1, 0, std::vector>(), as_chat_ids(chat_ids), + as_chat_ids(exclude_chat_ids), to_integer(chat_ids_limit))); } else if (op == "clean_storage_default") { send_request(td_api::make_object()); } else if (op == "clean_photos") { @@ -2206,7 +2225,7 @@ class CliClient final : public Actor { types.push_back(td_api::make_object()); types.push_back(td_api::make_object()); types.push_back(td_api::make_object()); - send_request(td_api::make_object(0, -1, -1, 0, std::move(types), as_chat_ids(args, ','), + send_request(td_api::make_object(0, -1, -1, 0, std::move(types), as_chat_ids(args), as_chat_ids(""), 20)); } else if (op == "network") { send_request(td_api::make_object()); @@ -2542,7 +2561,7 @@ class CliClient final : public Actor { std::tie(chat_id, args) = split(args); std::tie(message_ids, revoke) = split(args); - send_request(td_api::make_object(as_chat_id(chat_id), as_message_ids(message_ids, ','), + send_request(td_api::make_object(as_chat_id(chat_id), as_message_ids(message_ids), as_bool(revoke))); } else if (op == "fm" || op == "fmg" || op == "cm" || op == "cmg") { string chat_id; @@ -3150,7 +3169,7 @@ class CliClient final : public Actor { if (trim(photo_path).empty()) { photo_path = sticker_file_ids_str; } else { - sticker_file_ids = to_integers(sticker_file_ids_str, ','); + sticker_file_ids = to_integers(sticker_file_ids_str); } send_message(chat_id, td_api::make_object( @@ -3245,7 +3264,7 @@ class CliClient final : public Actor { if (trim(video_path).empty()) { video_path = sticker_file_ids_str; } else { - sticker_file_ids = to_integers(sticker_file_ids_str, ','); + sticker_file_ids = to_integers(sticker_file_ids_str); } send_message(chat_id, td_api::make_object(as_input_file(video_path), nullptr, @@ -3309,7 +3328,7 @@ class CliClient final : public Actor { string title; std::tie(user_ids_string, title) = split(args); - send_request(td_api::make_object(as_user_ids(user_ids_string, ','), title)); + send_request(td_api::make_object(as_user_ids(user_ids_string), title)); } else if (op == "cnch") { send_request(td_api::make_object(args, true, "Description", nullptr)); } else if (op == "cnsg") { @@ -3410,7 +3429,7 @@ class CliClient final : public Actor { string user_ids; std::tie(chat_id, user_ids) = split(args); - send_request(td_api::make_object(as_chat_id(chat_id), as_user_ids(user_ids, ','))); + send_request(td_api::make_object(as_chat_id(chat_id), as_user_ids(user_ids))); } else if (op == "spolla") { string chat_id; string message_id; @@ -3711,8 +3730,7 @@ class CliClient final : public Actor { string group_id; string notification_ids; std::tie(group_id, notification_ids) = split(args); - char delimiter = notification_ids.find(',') != string::npos ? ',' : ' '; - for (auto notification_id : to_integers(notification_ids, delimiter)) { + for (auto notification_id : to_integers(notification_ids)) { send_request(td_api::make_object(to_integer(group_id), notification_id)); } } else if (op == "rng") {