Ejemplo n.º 1
0
		/* Get account attribute value */
		extern int __account_get_attr(lua_State* L)
		{
			const char *username, *attrkey;
			int attrtype;
			std::string attrvalue;
			std::map<std::string, std::string> o_account;

			try
			{
				lua::stack st(L);
				// get args
				st.at(1, username);
				st.at(2, attrkey);
				st.at(3, attrtype);

				if (t_account *account = accountlist_find_account(username))
				{
					switch ((t_attr_type)attrtype)
					{
					case attr_type_str:
						attrvalue = account_get_strattr(account, attrkey);
						break;
					case attr_type_num:
						attrvalue = std_to_string(account_get_numattr(account, attrkey));
						break;
					case attr_type_bool:
						attrvalue = account_get_boolattr(account, attrkey) == 0 ? "false" : "true";
						break;
					case attr_type_raw:
						attrvalue = account_get_rawattr(account, attrkey);
						break;
					}
				}

				st.push(attrvalue);
			}
			catch (const std::exception& e)
			{
				eventlog(eventlog_level_error, __FUNCTION__, e.what());
			}
			catch (...)
			{
				eventlog(eventlog_level_error, __FUNCTION__, "lua exception\n");
			}

			return 1;
		}
Ejemplo n.º 2
0
		// handle command
		// /log read user startline
		// /log find user substr startline
		extern int handle_log_command(t_connection * c, char const *text)
		{
			const char *subcommand, *username;
			long startline = 0;
			std::map<long, char*> lines;

			// split command args
			std::vector<std::string> args = split_command(text, 4);
			if (args[1].empty() || args[2].empty() 
				|| (args[1].at(0) != 'r' && args[1].at(0) != 'f')) // check start symbols for subcommand
			{
				describe_command(c, args[0].c_str());
				return -1;
			}
			subcommand = args[1].c_str(); // sub command
			username = args[2].c_str(); // username

			if (!accountlist_find_account(username))
			{
				message_send_text(c, message_type_error, c, localize(c, "Invalid user."));
				return -1;
			}

			std::string title = localize(c, "{}'s log output", username);
			// read
			if (subcommand[0] == 'r')
			{
				if (!args[3].empty())
					startline = atoi(args[3].c_str());

				lines = userlog_read(username, startline);
			}
			// find
			else if (subcommand[0] == 'f')
			{
				if (args[3].empty())
				{
					describe_command(c, args[0].c_str());
					return -1;
				}
				const char * search = args[3].c_str();
				title += localize(c, " by occurrence \"{}\"", search);

				if (!args[4].empty())
					startline = atoi(args[4].c_str());

				lines = userlog_find_text(username, search, startline);
			}

			title += ":";
			message_send_text(c, message_type_info, c, title);

			int linelen = 0;
			int paddedlen = 0;
			std::string linenum;

			// send each log line to user
			for (std::map<long, char*>::reverse_iterator it = lines.rbegin(); it != lines.rend(); ++it)
			{
				int linelen = floor(log10(static_cast<double>(abs(it->first)))) + 1; // get length of integer (line number)
				if (linelen > paddedlen)
					paddedlen = linelen;

				linenum = std_to_string(it->first);
				// pad left to max line length
				linenum.insert(linenum.begin(), paddedlen - linenum.size(), '0');

				message_send_text(c, message_type_info, c, linenum + ": " + std::string(it->second));
			}

			return 0;
		}