예제 #1
0
void filtercontainer::handle_action(const std::string& action, const std::vector<std::string>& params) {
	/*
	 * filtercontainer does nothing but to save (filter name, filter expression) tuples.
	 * These tuples are used for enabling the user to predefine filter expressions and
	 * then select them from a list by their name.
	 */
	if (action == "define-filter") {
		if (params.size() < 2)
			throw confighandlerexception(AHS_TOO_FEW_PARAMS);
		matcher m;
		if (!m.parse(params[1]))
			throw confighandlerexception(utils::strprintf(_("couldn't parse filter expression `%s': %s"), params[1].c_str(), m.get_parse_error().c_str()));
		filters.push_back(filter_name_expr_pair(params[0],params[1]));
	} else
		throw confighandlerexception(AHS_INVALID_COMMAND);
}
예제 #2
0
void rss_ignores::handle_action(const std::string& action, const std::vector<std::string>& params) {
	if (action == "ignore-article") {
		if (params.size() < 2)
			throw confighandlerexception(AHS_TOO_FEW_PARAMS);
		std::string ignore_rssurl = params[0];
		std::string ignore_expr = params[1];
		matcher m;
		if (!m.parse(ignore_expr))
			throw confighandlerexception(utils::strprintf(_("couldn't parse filter expression `%s': %s"), ignore_expr.c_str(), m.get_parse_error().c_str()));
		ignores.push_back(feedurl_expr_pair(ignore_rssurl, new matcher(ignore_expr)));
	} else if (action == "always-download") {
		for (std::vector<std::string>::const_iterator it=params.begin();it!=params.end();++it) {
			ignores_lastmodified.push_back(*it);
		}
	} else if (action == "reset-unread-on-update") {
		for (std::vector<std::string>::const_iterator it=params.begin();it!=params.end();++it) {
			resetflag.push_back(*it);
		}
	} else
		throw confighandlerexception(AHS_INVALID_COMMAND);
}
예제 #3
0
void configcontainer::handle_action(const std::string& action, const std::vector<std::string>& params) {
	std::string resolved_action = lookup_alias(action);

	configdata& cfgdata = config_data[resolved_action];

	// configdata_t::INVALID indicates that the action didn't exist, and that the returned object was created ad-hoc.
	if (cfgdata.type == configdata_t::INVALID) {
		LOG(level::WARN, "configcontainer::handler_action: unknown action %s", action);
		throw confighandlerexception(action_handler_status::INVALID_COMMAND);
	}

	LOG(level::DEBUG, "configcontainer::handle_action: action = %s, type = %u", action, cfgdata.type);

	if (params.size() < 1) {
		throw confighandlerexception(action_handler_status::TOO_FEW_PARAMS);
	}

	switch (cfgdata.type) {
	case configdata_t::BOOL:
		if (!is_bool(params[0]))
			throw confighandlerexception(strprintf::fmt(_("expected boolean value, found `%s' instead"), params[0]));
		cfgdata.value = params[0];
		break;

	case configdata_t::INT:
		if (!is_int(params[0]))
			throw confighandlerexception(strprintf::fmt(_("expected integer value, found `%s' instead"), params[0]));
		cfgdata.value = params[0];
		break;

	case configdata_t::ENUM:
		if (cfgdata.enum_values.find(params[0]) == cfgdata.enum_values.end())
			throw confighandlerexception(strprintf::fmt(_("invalid configuration value `%s'"), params[0]));
	// fall-through
	case configdata_t::STR:
	case configdata_t::PATH:
		if (cfgdata.multi_option)
			cfgdata.value = utils::join(params, " ");
		else
			cfgdata.value = params[0];
		break;

	default:
		// should not happen
		throw confighandlerexception(action_handler_status::INVALID_COMMAND);
	}
}
예제 #4
0
void colormanager::handle_action(const std::string& action, const std::vector<std::string>& params) {
	LOG(LOG_DEBUG, "colormanager::handle_action(%s,...) was called",action.c_str());
	if (action == "color") {
		if (params.size() < 3) {
			throw confighandlerexception(AHS_TOO_FEW_PARAMS);
		}

		/*
		 * the command syntax is:
		 * color <element> <fgcolor> <bgcolor> [<attribute> ...]
		 */
		std::string element = params[0];
		std::string fgcolor = params[1];
		std::string bgcolor = params[2];

		if (!utils::is_valid_color(fgcolor))
			throw confighandlerexception(utils::strprintf(_("`%s' is not a valid color"), fgcolor.c_str()));
		if (!utils::is_valid_color(bgcolor))
			throw confighandlerexception(utils::strprintf(_("`%s' is not a valid color"), bgcolor.c_str()));

		std::vector<std::string> attribs;
		for (unsigned int i=3; i<params.size(); ++i) {
			if (!utils::is_valid_attribute(params[i]))
				throw confighandlerexception(utils::strprintf(_("`%s' is not a valid attribute"), params[i].c_str()));
			attribs.push_back(params[i]);
		}

		/* we only allow certain elements to be configured, also to indicate the user possible mis-spellings */
		if (element == "listnormal" || element == "listfocus" || element == "listnormal_unread" || element =="listfocus_unread"
		        || element == "info" || element == "background" || element == "article") {
			fg_colors[element] = fgcolor;
			bg_colors[element] = bgcolor;
			attributes[element] = attribs;
			colors_loaded_ = true;
		} else
			throw confighandlerexception(utils::strprintf(_("`%s' is not a valid configuration element"), element.c_str()));

	} else
		throw confighandlerexception(AHS_INVALID_COMMAND);
}
예제 #5
0
void keymap::handle_action(const std::string& action, const std::vector<std::string>& params) {
	/*
	 * The keymap acts as config_action_handler so that all the key-related configuration is immediately
	 * handed to it.
	 */
	LOG(level::DEBUG,"keymap::handle_action(%s, ...) called",action);
	if (action == "bind-key") {
		if (params.size() < 2)
			throw confighandlerexception(action_handler_status::TOO_FEW_PARAMS);
		std::string context = "all";
		if (params.size() >= 3)
			context = params[2];
		if (!is_valid_context(context))
			throw confighandlerexception(strprintf::fmt(_("`%s' is not a valid context"), context));
		operation op = get_opcode(params[1]);
		if (op > OP_SK_MIN && op < OP_SK_MAX)
			unset_key(getkey(op, context), context);
		set_key(op, params[0], context);
	} else if (action == "unbind-key") {
		if (params.size() < 1)
			throw confighandlerexception(action_handler_status::TOO_FEW_PARAMS);
		std::string context = "all";
		if (params.size() >= 2)
			context = params[1];
		unset_key(params[0], context);
	} else if (action == "macro") {
		if (params.size() < 1)
			throw confighandlerexception(action_handler_status::TOO_FEW_PARAMS);
		auto it = params.begin();
		std::string macrokey = *it;
		std::vector<macrocmd> cmds;
		macrocmd tmpcmd;
		tmpcmd.op = OP_NIL;
		bool first = true;
		++it;

		while (it != params.end()) {
			if (first && *it != ";") {
				tmpcmd.op = get_opcode(*it);
				LOG(level::DEBUG, "keymap::handle_action: new operation `%s' (op = %u)", it, tmpcmd.op);
				if (tmpcmd.op == OP_NIL)
					throw confighandlerexception(strprintf::fmt(_("`%s' is not a valid key command"), *it));
				first = false;
			} else {
				if (*it == ";") {
					if (tmpcmd.op != OP_NIL)
						cmds.push_back(tmpcmd);
					tmpcmd.op = OP_NIL;
					tmpcmd.args.clear();
					first = true;
				} else {
					LOG(level::DEBUG, "keymap::handle_action: new parameter `%s' (op = %u)", it);
					tmpcmd.args.push_back(*it);
				}
			}
			++it;
		}
		if (tmpcmd.op != OP_NIL)
			cmds.push_back(tmpcmd);

		macros_[macrokey] = cmds;
	} else
		throw confighandlerexception(action_handler_status::INVALID_PARAMS);
}
예제 #6
0
void regexmanager::handle_action(const std::string& action, const std::vector<std::string>& params) {
	if (action == "highlight") {
		if (params.size() < 3)
			throw confighandlerexception(AHS_TOO_FEW_PARAMS);

		std::string location = params[0];
		if (location != "all" && location != "article" && location != "articlelist" && location != "feedlist")
			throw confighandlerexception(utils::strprintf(_("`%s' is an invalid dialog type"), location.c_str()));

		regex_t * rx = new regex_t;
		int err;
		if ((err = regcomp(rx, params[1].c_str(), REG_EXTENDED | REG_ICASE)) != 0) {
			char buf[1024];
			regerror(err, rx, buf, sizeof(buf));
			delete rx;
			throw confighandlerexception(utils::strprintf(_("`%s' is not a valid regular expression: %s"), params[1].c_str(), buf));
		}
		std::string colorstr;
		if (params[2] != "default") {
			colorstr.append("fg=");
			if (!utils::is_valid_color(params[2]))
				throw confighandlerexception(utils::strprintf(_("`%s' is not a valid color"), params[2].c_str()));
			colorstr.append(params[2]);
		}
		if (params.size() > 3) {
			if (params[3] != "default") {
				if (colorstr.length() > 0)
					colorstr.append(",");
				colorstr.append("bg=");
				if (!utils::is_valid_color(params[3]))
					throw confighandlerexception(utils::strprintf(_("`%s' is not a valid color"), params[3].c_str()));
				colorstr.append(params[3]);
			}
			for (unsigned int i=4; i<params.size(); ++i) {
				if (params[i] != "default") {
					if (colorstr.length() > 0)
						colorstr.append(",");
					colorstr.append("attr=");
					if (!utils::is_valid_attribute(params[i]))
						throw confighandlerexception(utils::strprintf(_("`%s' is not a valid attribute"), params[i].c_str()));
					colorstr.append(params[i]);
				}
			}
		}
		if (location != "all") {
			LOG(LOG_DEBUG, "regexmanager::handle_action: adding rx = %s colorstr = %s to location %s",
			    params[1].c_str(), colorstr.c_str(), location.c_str());
			locations[location].first.push_back(rx);
			locations[location].second.push_back(colorstr);
		} else {
			delete rx;
			for (auto& location : locations) {
				LOG(LOG_DEBUG, "regexmanager::handle_action: adding rx = %s colorstr = %s to location %s",
				    params[1].c_str(), colorstr.c_str(), location.first.c_str());
				rx = new regex_t;
				// we need to create a new one for each push_back, otherwise we'd have double frees.
				regcomp(rx, params[1].c_str(), REG_EXTENDED | REG_ICASE);
				location.second.first.push_back(rx);
				location.second.second.push_back(colorstr);
			}
		}
		std::string line = "highlight";
		for (const auto& param : params) {
			line.append(" ");
			line.append(utils::quote(param));
		}
		cheat_store_for_dump_config.push_back(line);
	} else if (action == "highlight-article") {
		if (params.size() < 3)
			throw confighandlerexception(AHS_TOO_FEW_PARAMS);

		std::string expr = params[0];
		std::string fgcolor = params[1];
		std::string bgcolor = params[2];

		std::string colorstr;
		if (fgcolor != "default") {
			colorstr.append("fg=");
			if (!utils::is_valid_color(fgcolor))
				throw confighandlerexception(utils::strprintf(_("`%s' is not a valid color"), fgcolor.c_str()));
			colorstr.append(fgcolor);
		}
		if (bgcolor != "default") {
			if (colorstr.length() > 0)
				colorstr.append(",");
			colorstr.append("bg=");
			if (!utils::is_valid_color(bgcolor))
				throw confighandlerexception(utils::strprintf(_("`%s' is not a valid color"), bgcolor.c_str()));
			colorstr.append(bgcolor);
		}

		for (unsigned int i=3; i<params.size(); i++) {
			if (params[i] != "default") {
				if (colorstr.length() > 0)
					colorstr.append(",");
				colorstr.append("attr=");
				if (!utils::is_valid_attribute(params[i]))
					throw confighandlerexception(utils::strprintf(_("`%s' is not a valid attribute"), params[i].c_str()));
				colorstr.append(params[i]);
			}
		}

		std::shared_ptr<matcher> m(new matcher());
		if (!m->parse(params[0])) {
			throw confighandlerexception(utils::strprintf(_("couldn't parse filter expression `%s': %s"), params[0].c_str(), m->get_parse_error().c_str()));
		}

		int pos = locations["articlelist"].first.size();

		locations["articlelist"].first.push_back(NULL);
		locations["articlelist"].second.push_back(colorstr);

		matchers.push_back(std::pair<std::shared_ptr<matcher>, int>(m, pos));

	} else
		throw confighandlerexception(AHS_INVALID_COMMAND);
}