extern int handle_help_command(t_connection * c, char const * text) { unsigned int i, j; char cmd[MAX_COMMAND_LEN]; if (hfd == NULL) { /* an error ocured opening readonly the help file, helpfile_unload was called, or helpfile_init hasn't been called */ message_send_text(c, message_type_error, c, "Oops ! There is a problem with the help file. Please contact the administrator of the server."); return 0; } for (i = 0; text[i] != ' ' && text[i] != '\0'; i++); /* skip command */ for (; text[i] == ' '; i++); if (text[i] == '/') /* skip / in front of command (if present) */ i++; for (j = 0; text[i] != ' ' && text[i] != '\0'; i++) /* get comm */ if (j < sizeof(cmd)-1) cmd[j++] = text[i]; cmd[j] = '\0'; /* just read the whole file and dump only the commands */ if (cmd[0] == '\0') { list_commands(c); return 0; } describe_command(c, cmd); return 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; }