#include "config.h" #include <sys/types.h> #include <cerrno>#include <ctime>#include <limits>#include <regex>#include <torrent/utils/log.h> #include "core/manager.h"#include "core/view_manager.h"#include "display/canvas.h"#include "ui/root.h"#include "ui/download_list.h"#include "display/color_map.h"#include "rpc/parse.h" #include "globals.h"#include "control.h"#include "command_helpers.h" typedef void (core::ViewManager::*view_event_slot)(const std::string&, const torrent::Object&); torrent::Objectapply_view_filter_on(const torrent::Object::list_type& args) { if (args.size() < 1) throw torrent::input_error("Too few arguments."); const std::string& name = args.front().as_string(); if (name.empty()) throw torrent::input_error("First argument must be a string."); core::ViewManager::filter_args filterArgs; for (torrent::Object::list_const_iterator itr = ++args.begin(), last = args.end(); itr != last; itr++) filterArgs.push_back(itr->as_string()); control->view_manager()->set_filter_on(name, filterArgs); return torrent::Object();} torrent::Objectapply_view_event(view_event_slot viewFilterSlot, const torrent::Object::list_type& args) { if (args.size() != 2) throw torrent::input_error("Wrong argument count."); (control->view_manager()->*viewFilterSlot)(args.front().as_string(), args.back()); return torrent::Object();} torrent::Objectapply_view_sort(const torrent::Object::list_type& args) { if (args.size() <= 0 || args.size() > 2) throw torrent::input_error("Wrong argument count."); const std::string& name = args.front().as_string(); if (name.empty()) throw torrent::input_error("First argument must be a string."); int32_t value = 0; if (args.size() == 2) value = rpc::convert_to_value(args.back()); control->view_manager()->sort(name, value); return torrent::Object();} torrent::Objectapply_view_list() { torrent::Object rawResult = torrent::Object::create_list(); torrent::Object::list_type& result = rawResult.as_list(); for (auto itr : *control->view_manager()) result.push_back(itr->name()); return rawResult;} torrent::Objectapply_view_set(const torrent::Object::list_type& args) { if (args.size() != 2) throw torrent::input_error("Wrong argument count."); core::ViewManager::iterator itr = control->view_manager()->find(args.back().as_string()); if (itr == control->view_manager()->end()) throw torrent::input_error("Could not find view \"" + args.back().as_string() + "\"."); // if (args.front().as_string() == "main")// control->ui()->download_list()->set_view(*itr);// else throw torrent::input_error("No such target.");} torrent::Objectapply_print([[maybe_unused]] rpc::target_type target, const torrent::Object& rawArgs) { char buffer[1024]; rpc::print_object(buffer, buffer + 1024, &rawArgs, 0); control->core()->push_log(buffer); return torrent::Object();} torrent::Objectapply_cat([[maybe_unused]] rpc::target_type target, const torrent::Object& rawArgs) { std::string result; rpc::print_object_std(&result, &rawArgs, 0); return result;} torrent::Objectapply_value([[maybe_unused]] rpc::target_type target, const torrent::Object::list_type& args) { if (args.size() < 1) throw torrent::input_error("'value' takes at least a number argument!"); if (args.size() > 2) throw torrent::input_error("'value' takes at most two arguments!"); torrent::Object::value_type val = 0; if (args.front().is_value()) { val = args.front().as_value(); } else { int64_t base = args.size() > 1 ? args.back().is_value() ? args.back().as_value() : strtoll(args.back().as_string().c_str(), NULL, 10) : 10; if (base != 0 && (base < 2 || base > 36)) throw torrent::input_error("'value' base must be 0 or between 2 and 36!"); char* endptr = 0; errno = 0; val = strtoll(args.front().as_string().c_str(), &endptr, base); if (errno == ERANGE) throw torrent::input_error("Number out of range: " + args.front().as_string()); while (*endptr == ' ' || *endptr == '\n') ++endptr; if (*endptr) throw torrent::input_error("Junk at end of number: " + args.front().as_string()); } return val;} torrent::Objectapply_try(rpc::target_type target, const torrent::Object& args) { try { return rpc::call_object(args, target); } catch (torrent::input_error& e) { lt_log_print(torrent::LOG_RPC_EVENTS, "try command caught input_error: %s", e.what()); } return torrent::Object();} // Move these boolean operators to a new file. inline boolas_boolean(const torrent::Object& rawArgs) { switch (rawArgs.type()) { case torrent::Object::TYPE_VALUE: return rawArgs.as_value(); case torrent::Object::TYPE_STRING: return !rawArgs.as_string().empty(); // We need to properly handle argument lists that are single-object // at a higher level. case torrent::Object::TYPE_LIST: return !rawArgs.as_list().empty() && as_boolean(rawArgs.as_list().front()); // case torrent::Object::TYPE_MAP: return !rawArgs.as_map().empty(); default: return false; }} torrent::Objectapply_not(rpc::target_type target, const torrent::Object& rawArgs) { bool result; if (rawArgs.is_dict_key()) result = as_boolean(rpc::commands.call_command(rawArgs.as_dict_key().c_str(), rawArgs.as_dict_obj(), target)); // TODO: Thus we should clean this up... else if (rawArgs.is_list() && !rawArgs.as_list().empty()) return apply_not(target, rawArgs.as_list().front()); else result = as_boolean(rawArgs); return (int64_t)!result;} torrent::Objectapply_false([[maybe_unused]] rpc::target_type target, [[maybe_unused]] const torrent::Object& rawArgs) { return (int64_t)0;} torrent::Objectapply_and(rpc::target_type target, const torrent::Object& rawArgs) { if (rawArgs.type() != torrent::Object::TYPE_LIST) return as_boolean(rawArgs); for (const auto& itr : rawArgs.as_list()) if (itr.is_dict_key()) { if (!as_boolean(rpc::commands.call_command(itr.as_dict_key().c_str(), itr.as_dict_obj(), target))) return (int64_t)false; } else if (itr.is_value()) { if (!itr.as_value()) return (int64_t)false; } else { // TODO: Switch to new versions that only accept the new command syntax. if (!as_boolean(rpc::parse_command_single(target, itr.as_string()))) return (int64_t)false; } return (int64_t)true;} torrent::Objectapply_or(rpc::target_type target, const torrent::Object& rawArgs) { if (rawArgs.type() != torrent::Object::TYPE_LIST) return as_boolean(rawArgs); for (const auto& itr : rawArgs.as_list()) if (itr.is_dict_key()) { if (as_boolean(rpc::commands.call_command(itr.as_dict_key().c_str(), itr.as_dict_obj(), target))) return (int64_t)true; } else if (itr.is_value()) { if (itr.as_value()) return (int64_t)true; } else { if (as_boolean(rpc::parse_command_single(target, itr.as_string()))) return (int64_t)true; } return (int64_t)false;} torrent::Objectapply_cmp(rpc::target_type target, const torrent::Object::list_type& args) { // We only need to check if empty() since if size() == 1 it calls // the same command for both, or if size() == 2 then each side of // the comparison has different commands. if (args.empty()) throw torrent::input_error("Wrong argument count."); // This really should be converted to using args flagged as // commands, so that we can compare commands and statics values. torrent::Object result1; torrent::Object result2; rpc::target_type target1 = rpc::is_target_pair(target) ? rpc::get_target_left(target) : target; rpc::target_type target2 = rpc::is_target_pair(target) ? rpc::get_target_right(target) : target; if (args.front().is_dict_key()) result1 = rpc::commands.call_command(args.front().as_dict_key().c_str(), args.front().as_dict_obj(), target1); else result1 = rpc::parse_command_single(target1, args.front().as_string()); if (args.back().is_dict_key()) result2 = rpc::commands.call_command(args.back().as_dict_key().c_str(), args.back().as_dict_obj(), target2); else result2 = rpc::parse_command_single(target2, args.back().as_string()); if (result1.type() != result2.type()) throw torrent::input_error("Type mismatch."); switch (result1.type()) { case torrent::Object::TYPE_VALUE: return result1.as_value() - result2.as_value(); case torrent::Object::TYPE_STRING: return result1.as_string().compare(result2.as_string()); default: return torrent::Object(); }} torrent::Object apply_less(rpc::target_type target, const torrent::Object::list_type& args) { torrent::Object result = apply_cmp(target, args); return result.is_value() ? result.as_value() < 0 : (int64_t)false;} torrent::Object apply_greater(rpc::target_type target, const torrent::Object::list_type& args) { torrent::Object result = apply_cmp(target, args); return result.is_value() ? result.as_value() > 0 : (int64_t)false;} torrent::Object apply_equal(rpc::target_type target, const torrent::Object::list_type& args) { torrent::Object result = apply_cmp(target, args); return result.is_value() ? result.as_value() == 0 : (int64_t)false;} torrent::Objectapply_compare(rpc::target_type target, const torrent::Object::list_type& args) { if (!rpc::is_target_pair(target)) throw torrent::input_error("Can only compare a target pair."); if (args.size() < 2) throw torrent::input_error("Need at least order and one field."); torrent::Object::list_const_iterator itr = args.begin(); std::string order = (itr++)->as_string(); const char* current = order.c_str(); torrent::Object result1; torrent::Object result2; for (torrent::Object::list_const_iterator last = args.end(); itr != last; itr++) { std::string field = itr->as_string(); result1 = rpc::parse_command_single(rpc::get_target_left(target), field); result2 = rpc::parse_command_single(rpc::get_target_right(target), field); if (result1.type() != result2.type()) throw torrent::input_error(std::string("Type mismatch in compare of ") + field); bool descending = *current == 'd' || *current == 'D' || *current == '-'; if (*current) { if (!descending && !(*current == 'a' || *current == 'A' || *current == '+')) throw torrent::input_error(std::string("Bad order '") + *current + "' in " + order); ++current; } switch (result1.type()) { case torrent::Object::TYPE_VALUE: if (result1.as_value() != result2.as_value()) return (int64_t) (descending ^ (result1.as_value() < result2.as_value())); break; case torrent::Object::TYPE_STRING: if (result1.as_string() != result2.as_string()) return (int64_t) (descending ^ (result1.as_string() < result2.as_string())); break; default: break; // treat unknown types as equal } } // if all else is equal, ensure stable sort order based on memory location return (int64_t) (target.second < target.third);} // Regexp based 'match' function.// arg1: the text to match.// arg2: the regexp pattern.// eg: match{d.name=,.*linux.*iso}torrent::Object apply_match(rpc::target_type target, const torrent::Object::list_type& args) { if (args.size() != 2) throw torrent::input_error("Wrong argument count for 'match': 2 arguments needed."); // This really should be converted to using args flagged as // commands, so that we can compare commands and statics values. torrent::Object result1; torrent::Object result2; rpc::target_type target1 = rpc::is_target_pair(target) ? rpc::get_target_left(target) : target; rpc::target_type target2 = rpc::is_target_pair(target) ? rpc::get_target_right(target) : target; if (args.front().is_dict_key()) result1 = rpc::commands.call_command(args.front().as_dict_key().c_str(), args.front().as_dict_obj(), target1); else result1 = rpc::parse_command_single(target1, args.front().as_string()); if (args.back().is_dict_key()) result2 = rpc::commands.call_command(args.back().as_dict_key().c_str(), args.back().as_dict_obj(), target2); else result2 = args.back().as_string(); if (result1.type() != result2.type()) throw torrent::input_error("Type mismatch for 'match' arguments."); std::string text = result1.as_string(); std::string pattern = result2.as_string(); std::transform(text.begin(), text.end(), text.begin(), ::tolower); std::transform(pattern.begin(), pattern.end(), pattern.begin(), ::tolower); bool isAMatch = false; try { std::regex re(pattern); isAMatch = std::regex_match(text, re); } catch (const std::regex_error& exc) { control->core()->push_log_std("regex_error: " + std::string(exc.what())); } return isAMatch ? (int64_t)true : (int64_t)false;} torrent::Objectapply_to_time(const torrent::Object& rawArgs, int flags) { std::tm *u; time_t t = (uint64_t)rawArgs.as_value(); if (flags & 0x1) u = std::localtime(&t); else u = std::gmtime(&t); if (u == NULL) return torrent::Object(); char buffer[11]; if (flags & 0x2) snprintf(buffer, 11, "%02u/%02u/%04u", u->tm_mday, (u->tm_mon + 1), (1900 + u->tm_year)); else snprintf(buffer, 9, "%2d:%02d:%02d", u->tm_hour, u->tm_min, u->tm_sec); return std::string(buffer);} torrent::Objectapply_to_elapsed_time(const torrent::Object& rawArgs) { auto cached_seconds = torrent::this_thread::cached_seconds().count(); auto value = rawArgs.as_value(); uint64_t arg = value >= 0 && value <= cached_seconds ? cached_seconds - value : 0; char buffer[48]; snprintf(buffer, 48, "%2d:%02d:%02d", (int)(arg / 3600), (int)((arg / 60) % 60), (int)(arg % 60)); return std::string(buffer);} torrent::Objectapply_to_kb(const torrent::Object& rawArgs) { char buffer[32]; snprintf(buffer, 32, "%5.1f", (double)rawArgs.as_value() / (1 << 10)); return std::string(buffer);} torrent::Objectapply_to_mb(const torrent::Object& rawArgs) { char buffer[32]; snprintf(buffer, 32, "%8.1f", (double)rawArgs.as_value() / (1 << 20)); return std::string(buffer);} torrent::Objectapply_to_xb(const torrent::Object& rawArgs) { char buffer[48]; int64_t arg = rawArgs.as_value(); if (arg < (int64_t(1000) << 10)) snprintf(buffer, 48, "%5.1f KB", (double)arg / (int64_t(1) << 10)); else if (arg < (int64_t(1000) << 20)) snprintf(buffer, 48, "%5.1f MB", (double)arg / (int64_t(1) << 20)); else if (arg < (int64_t(1000) << 30)) snprintf(buffer, 48, "%5.1f GB", (double)arg / (int64_t(1) << 30)); else snprintf(buffer, 48, "%5.1f TB", (double)arg / (int64_t(1) << 40)); return std::string(buffer);} torrent::Objectapply_to_throttle(const torrent::Object& rawArgs) { int64_t arg = rawArgs.as_value(); if (arg < 0) return "---"; else if (arg == 0) return "off"; char buffer[32]; snprintf(buffer, 32, "%3d", (int)(arg / (1 << 10))); return std::string(buffer);} // A series of if/else statements. Every even arguments are// conditionals and odd arguments are branches to be executed, except// the last one which is always a branch.//// if (cond1) { branch1 }// <cond1>,<branch1>//// if (cond1) { branch1 } else if (cond2) { branch2 } else { branch3 }// <cond1>,<branch1>,<cond2>,<branch2>,<branch3>torrent::Objectapply_if(rpc::target_type target, const torrent::Object& raw_args, int flags) { auto& args = raw_args.as_list(); auto itr = args.begin(); if (args.empty()) throw torrent::input_error("Empty argument list to " + std::string((flags & 0x1) ? "branch" : "if") + "."); { torrent::Object tmp; const torrent::Object* conditional; if (flags & 0x1 && itr->is_string()) conditional = &(tmp = rpc::parse_command(target, itr->as_string().c_str(), itr->as_string().c_str() + itr->as_string().size()).first); else if (flags & 0x1 && itr->is_dict_key())Lines 1–500 of 958. Load more
#include "config.h" #include <sys/types.h> #include <cerrno> #include <ctime> #include <limits> #include <regex> #include <torrent/utils/log.h> #include "core/manager.h" #include "core/view_manager.h" #include "display/canvas.h" #include "ui/root.h" #include "ui/download_list.h" #include "display/color_map.h" #include "rpc/parse.h" #include "globals.h" #include "control.h" #include "command_helpers.h" typedef void (core::ViewManager::*view_event_slot)(const std::string&, const torrent::Object&); torrent::Object apply_view_filter_on(const torrent::Object::list_type& args) { if (args.size() < 1) throw torrent::input_error("Too few arguments."); const std::string& name = args.front().as_string(); if (name.empty()) throw torrent::input_error("First argument must be a string."); core::ViewManager::filter_args filterArgs; for (torrent::Object::list_const_iterator itr = ++args.begin(), last = args.end(); itr != last; itr++) filterArgs.push_back(itr->as_string()); control->view_manager()->set_filter_on(name, filterArgs); return torrent::Object(); } torrent::Object apply_view_event(view_event_slot viewFilterSlot, const torrent::Object::list_type& args) { if (args.size() != 2) throw torrent::input_error("Wrong argument count."); (control->view_manager()->*viewFilterSlot)(args.front().as_string(), args.back()); return torrent::Object(); } torrent::Object apply_view_sort(const torrent::Object::list_type& args) { if (args.size() <= 0 || args.size() > 2) throw torrent::input_error("Wrong argument count."); const std::string& name = args.front().as_string(); if (name.empty()) throw torrent::input_error("First argument must be a string."); int32_t value = 0; if (args.size() == 2) value = rpc::convert_to_value(args.back()); control->view_manager()->sort(name, value); return torrent::Object(); } torrent::Object apply_view_list() { torrent::Object rawResult = torrent::Object::create_list(); torrent::Object::list_type& result = rawResult.as_list(); for (auto itr : *control->view_manager()) result.push_back(itr->name()); return rawResult; } torrent::Object apply_view_set(const torrent::Object::list_type& args) { if (args.size() != 2) throw torrent::input_error("Wrong argument count."); core::ViewManager::iterator itr = control->view_manager()->find(args.back().as_string()); if (itr == control->view_manager()->end()) throw torrent::input_error("Could not find view \"" + args.back().as_string() + "\"."); // if (args.front().as_string() == "main") // control->ui()->download_list()->set_view(*itr); // else throw torrent::input_error("No such target."); } torrent::Object apply_print([[maybe_unused]] rpc::target_type target, const torrent::Object& rawArgs) { char buffer[1024]; rpc::print_object(buffer, buffer + 1024, &rawArgs, 0); control->core()->push_log(buffer); return torrent::Object(); } torrent::Object apply_cat([[maybe_unused]] rpc::target_type target, const torrent::Object& rawArgs) { std::string result; rpc::print_object_std(&result, &rawArgs, 0); return result; } torrent::Object apply_value([[maybe_unused]] rpc::target_type target, const torrent::Object::list_type& args) { if (args.size() < 1) throw torrent::input_error("'value' takes at least a number argument!"); if (args.size() > 2) throw torrent::input_error("'value' takes at most two arguments!"); torrent::Object::value_type val = 0; if (args.front().is_value()) { val = args.front().as_value(); } else { int64_t base = args.size() > 1 ? args.back().is_value() ? args.back().as_value() : strtoll(args.back().as_string().c_str(), NULL, 10) : 10; if (base != 0 && (base < 2 || base > 36)) throw torrent::input_error("'value' base must be 0 or between 2 and 36!"); char* endptr = 0; errno = 0; val = strtoll(args.front().as_string().c_str(), &endptr, base); if (errno == ERANGE) throw torrent::input_error("Number out of range: " + args.front().as_string()); while (*endptr == ' ' || *endptr == '\n') ++endptr; if (*endptr) throw torrent::input_error("Junk at end of number: " + args.front().as_string()); } return val; } torrent::Object apply_try(rpc::target_type target, const torrent::Object& args) { try { return rpc::call_object(args, target); } catch (torrent::input_error& e) { lt_log_print(torrent::LOG_RPC_EVENTS, "try command caught input_error: %s", e.what()); } return torrent::Object(); } // Move these boolean operators to a new file. inline bool as_boolean(const torrent::Object& rawArgs) { switch (rawArgs.type()) { case torrent::Object::TYPE_VALUE: return rawArgs.as_value(); case torrent::Object::TYPE_STRING: return !rawArgs.as_string().empty(); // We need to properly handle argument lists that are single-object // at a higher level. case torrent::Object::TYPE_LIST: return !rawArgs.as_list().empty() && as_boolean(rawArgs.as_list().front()); // case torrent::Object::TYPE_MAP: return !rawArgs.as_map().empty(); default: return false; } } torrent::Object apply_not(rpc::target_type target, const torrent::Object& rawArgs) { bool result; if (rawArgs.is_dict_key()) result = as_boolean(rpc::commands.call_command(rawArgs.as_dict_key().c_str(), rawArgs.as_dict_obj(), target)); // TODO: Thus we should clean this up... else if (rawArgs.is_list() && !rawArgs.as_list().empty()) return apply_not(target, rawArgs.as_list().front()); else result = as_boolean(rawArgs); return (int64_t)!result; } torrent::Object apply_false([[maybe_unused]] rpc::target_type target, [[maybe_unused]] const torrent::Object& rawArgs) { return (int64_t)0; } torrent::Object apply_and(rpc::target_type target, const torrent::Object& rawArgs) { if (rawArgs.type() != torrent::Object::TYPE_LIST) return as_boolean(rawArgs); for (const auto& itr : rawArgs.as_list()) if (itr.is_dict_key()) { if (!as_boolean(rpc::commands.call_command(itr.as_dict_key().c_str(), itr.as_dict_obj(), target))) return (int64_t)false; } else if (itr.is_value()) { if (!itr.as_value()) return (int64_t)false; } else { // TODO: Switch to new versions that only accept the new command syntax. if (!as_boolean(rpc::parse_command_single(target, itr.as_string()))) return (int64_t)false; } return (int64_t)true; } torrent::Object apply_or(rpc::target_type target, const torrent::Object& rawArgs) { if (rawArgs.type() != torrent::Object::TYPE_LIST) return as_boolean(rawArgs); for (const auto& itr : rawArgs.as_list()) if (itr.is_dict_key()) { if (as_boolean(rpc::commands.call_command(itr.as_dict_key().c_str(), itr.as_dict_obj(), target))) return (int64_t)true; } else if (itr.is_value()) { if (itr.as_value()) return (int64_t)true; } else { if (as_boolean(rpc::parse_command_single(target, itr.as_string()))) return (int64_t)true; } return (int64_t)false; } torrent::Object apply_cmp(rpc::target_type target, const torrent::Object::list_type& args) { // We only need to check if empty() since if size() == 1 it calls // the same command for both, or if size() == 2 then each side of // the comparison has different commands. if (args.empty()) throw torrent::input_error("Wrong argument count."); // This really should be converted to using args flagged as // commands, so that we can compare commands and statics values. torrent::Object result1; torrent::Object result2; rpc::target_type target1 = rpc::is_target_pair(target) ? rpc::get_target_left(target) : target; rpc::target_type target2 = rpc::is_target_pair(target) ? rpc::get_target_right(target) : target; if (args.front().is_dict_key()) result1 = rpc::commands.call_command(args.front().as_dict_key().c_str(), args.front().as_dict_obj(), target1); else result1 = rpc::parse_command_single(target1, args.front().as_string()); if (args.back().is_dict_key()) result2 = rpc::commands.call_command(args.back().as_dict_key().c_str(), args.back().as_dict_obj(), target2); else result2 = rpc::parse_command_single(target2, args.back().as_string()); if (result1.type() != result2.type()) throw torrent::input_error("Type mismatch."); switch (result1.type()) { case torrent::Object::TYPE_VALUE: return result1.as_value() - result2.as_value(); case torrent::Object::TYPE_STRING: return result1.as_string().compare(result2.as_string()); default: return torrent::Object(); } } torrent::Object apply_less(rpc::target_type target, const torrent::Object::list_type& args) { torrent::Object result = apply_cmp(target, args); return result.is_value() ? result.as_value() < 0 : (int64_t)false; } torrent::Object apply_greater(rpc::target_type target, const torrent::Object::list_type& args) { torrent::Object result = apply_cmp(target, args); return result.is_value() ? result.as_value() > 0 : (int64_t)false; } torrent::Object apply_equal(rpc::target_type target, const torrent::Object::list_type& args) { torrent::Object result = apply_cmp(target, args); return result.is_value() ? result.as_value() == 0 : (int64_t)false; } torrent::Object apply_compare(rpc::target_type target, const torrent::Object::list_type& args) { if (!rpc::is_target_pair(target)) throw torrent::input_error("Can only compare a target pair."); if (args.size() < 2) throw torrent::input_error("Need at least order and one field."); torrent::Object::list_const_iterator itr = args.begin(); std::string order = (itr++)->as_string(); const char* current = order.c_str(); torrent::Object result1; torrent::Object result2; for (torrent::Object::list_const_iterator last = args.end(); itr != last; itr++) { std::string field = itr->as_string(); result1 = rpc::parse_command_single(rpc::get_target_left(target), field); result2 = rpc::parse_command_single(rpc::get_target_right(target), field); if (result1.type() != result2.type()) throw torrent::input_error(std::string("Type mismatch in compare of ") + field); bool descending = *current == 'd' || *current == 'D' || *current == '-'; if (*current) { if (!descending && !(*current == 'a' || *current == 'A' || *current == '+')) throw torrent::input_error(std::string("Bad order '") + *current + "' in " + order); ++current; } switch (result1.type()) { case torrent::Object::TYPE_VALUE: if (result1.as_value() != result2.as_value()) return (int64_t) (descending ^ (result1.as_value() < result2.as_value())); break; case torrent::Object::TYPE_STRING: if (result1.as_string() != result2.as_string()) return (int64_t) (descending ^ (result1.as_string() < result2.as_string())); break; default: break; // treat unknown types as equal } } // if all else is equal, ensure stable sort order based on memory location return (int64_t) (target.second < target.third); } // Regexp based 'match' function. // arg1: the text to match. // arg2: the regexp pattern. // eg: match{d.name=,.*linux.*iso} torrent::Object apply_match(rpc::target_type target, const torrent::Object::list_type& args) { if (args.size() != 2) throw torrent::input_error("Wrong argument count for 'match': 2 arguments needed."); // This really should be converted to using args flagged as // commands, so that we can compare commands and statics values. torrent::Object result1; torrent::Object result2; rpc::target_type target1 = rpc::is_target_pair(target) ? rpc::get_target_left(target) : target; rpc::target_type target2 = rpc::is_target_pair(target) ? rpc::get_target_right(target) : target; if (args.front().is_dict_key()) result1 = rpc::commands.call_command(args.front().as_dict_key().c_str(), args.front().as_dict_obj(), target1); else result1 = rpc::parse_command_single(target1, args.front().as_string()); if (args.back().is_dict_key()) result2 = rpc::commands.call_command(args.back().as_dict_key().c_str(), args.back().as_dict_obj(), target2); else result2 = args.back().as_string(); if (result1.type() != result2.type()) throw torrent::input_error("Type mismatch for 'match' arguments."); std::string text = result1.as_string(); std::string pattern = result2.as_string(); std::transform(text.begin(), text.end(), text.begin(), ::tolower); std::transform(pattern.begin(), pattern.end(), pattern.begin(), ::tolower); bool isAMatch = false; try { std::regex re(pattern); isAMatch = std::regex_match(text, re); } catch (const std::regex_error& exc) { control->core()->push_log_std("regex_error: " + std::string(exc.what())); } return isAMatch ? (int64_t)true : (int64_t)false; } torrent::Object apply_to_time(const torrent::Object& rawArgs, int flags) { std::tm *u; time_t t = (uint64_t)rawArgs.as_value(); if (flags & 0x1) u = std::localtime(&t); else u = std::gmtime(&t); if (u == NULL) return torrent::Object(); char buffer[11]; if (flags & 0x2) snprintf(buffer, 11, "%02u/%02u/%04u", u->tm_mday, (u->tm_mon + 1), (1900 + u->tm_year)); else snprintf(buffer, 9, "%2d:%02d:%02d", u->tm_hour, u->tm_min, u->tm_sec); return std::string(buffer); } torrent::Object apply_to_elapsed_time(const torrent::Object& rawArgs) { auto cached_seconds = torrent::this_thread::cached_seconds().count(); auto value = rawArgs.as_value(); uint64_t arg = value >= 0 && value <= cached_seconds ? cached_seconds - value : 0; char buffer[48]; snprintf(buffer, 48, "%2d:%02d:%02d", (int)(arg / 3600), (int)((arg / 60) % 60), (int)(arg % 60)); return std::string(buffer); } torrent::Object apply_to_kb(const torrent::Object& rawArgs) { char buffer[32]; snprintf(buffer, 32, "%5.1f", (double)rawArgs.as_value() / (1 << 10)); return std::string(buffer); } torrent::Object apply_to_mb(const torrent::Object& rawArgs) { char buffer[32]; snprintf(buffer, 32, "%8.1f", (double)rawArgs.as_value() / (1 << 20)); return std::string(buffer); } torrent::Object apply_to_xb(const torrent::Object& rawArgs) { char buffer[48]; int64_t arg = rawArgs.as_value(); if (arg < (int64_t(1000) << 10)) snprintf(buffer, 48, "%5.1f KB", (double)arg / (int64_t(1) << 10)); else if (arg < (int64_t(1000) << 20)) snprintf(buffer, 48, "%5.1f MB", (double)arg / (int64_t(1) << 20)); else if (arg < (int64_t(1000) << 30)) snprintf(buffer, 48, "%5.1f GB", (double)arg / (int64_t(1) << 30)); else snprintf(buffer, 48, "%5.1f TB", (double)arg / (int64_t(1) << 40)); return std::string(buffer); } torrent::Object apply_to_throttle(const torrent::Object& rawArgs) { int64_t arg = rawArgs.as_value(); if (arg < 0) return "---"; else if (arg == 0) return "off"; char buffer[32]; snprintf(buffer, 32, "%3d", (int)(arg / (1 << 10))); return std::string(buffer); } // A series of if/else statements. Every even arguments are // conditionals and odd arguments are branches to be executed, except // the last one which is always a branch. // // if (cond1) { branch1 } // <cond1>,<branch1> // // if (cond1) { branch1 } else if (cond2) { branch2 } else { branch3 } // <cond1>,<branch1>,<cond2>,<branch2>,<branch3> torrent::Object apply_if(rpc::target_type target, const torrent::Object& raw_args, int flags) { auto& args = raw_args.as_list(); auto itr = args.begin(); if (args.empty()) throw torrent::input_error("Empty argument list to " + std::string((flags & 0x1) ? "branch" : "if") + "."); { torrent::Object tmp; const torrent::Object* conditional; if (flags & 0x1 && itr->is_string()) conditional = &(tmp = rpc::parse_command(target, itr->as_string().c_str(), itr->as_string().c_str() + itr->as_string().size()).first); else if (flags & 0x1 && itr->is_dict_key())