Fix !log command, split it up into multiple messages, and allow for searches.

This commit is contained in:
Jan Vidar Krey 2009-07-28 02:02:19 +02:00
parent 80348044c3
commit f322fbb197
1 changed files with 43 additions and 14 deletions

View File

@ -410,39 +410,68 @@ static int command_history(struct hub_info* hub, struct hub_user* user, struct h
static int command_log(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd) static int command_log(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
{ {
char* buffer;
struct linked_list* messages = hub->logout_info; struct linked_list* messages = hub->logout_info;
struct hub_logout_info* log; struct hub_logout_info* log;
int ret = 0;
char tmp[1024]; char tmp[1024];
char* search = 0;
size_t search_len = 0;
size_t search_hits = 0;
if (!list_size(messages)) if (!list_size(messages))
{ {
return command_status(hub, user, cmd, "No entries logged."); return command_status(hub, user, cmd, "No entries logged.");
} }
buffer = hub_malloc(32 + (list_size(messages) * (sizeof(struct hub_logout_info) + 25))); search = list_get_first(cmd->args);
if (!buffer) if (search)
{ {
return command_status(hub, user, cmd, "Not enough memory."); search_len = strlen(search);
} }
buffer[0] = 0; if (search_len)
strcat(buffer, "Log entries"); {
strcat(buffer, "\n"); sprintf(tmp, "Logged entries: %d, searching for \"%s\"", (int) list_size(messages), search);
}
else
{
sprintf(tmp, "Logged entries: %d", (int) list_size(messages));
}
send_message(hub, user, tmp);
log = (struct hub_logout_info*) list_get_first(messages); log = (struct hub_logout_info*) list_get_first(messages);
while (log) while (log)
{ {
sprintf(tmp, "%s %s, %s (%s)\n", get_timestamp(log->time), log->cid, log->nick, ip_convert_to_string(&log->addr)); const char* address = ip_convert_to_string(&log->addr);
strcat(buffer, tmp); int show = 0;
if (search_len)
{
if (memmem(log->cid, MAX_CID_LEN, search, search_len) || memmem(log->nick, MAX_NICK_LEN, search, search_len) || memmem(address, strlen(address), search, search_len))
{
search_hits++;
show = 1;
}
}
else
{
show = 1;
}
if (show)
{
sprintf(tmp, "* %s %s, %s (%s)", get_timestamp(log->time), log->cid, log->nick, ip_convert_to_string(&log->addr));
send_message(hub, user, tmp);
}
log = (struct hub_logout_info*) list_get_next(messages); log = (struct hub_logout_info*) list_get_next(messages);
} }
strcat(buffer, "\n");
ret = command_status(hub, user, cmd, buffer); if (search_len)
hub_free(buffer); {
return ret; sprintf(tmp, "%d entries shown.", (int) search_hits);
send_message(hub, user, tmp);
}
return 0;
} }
#ifdef CRASH_DEBUG #ifdef CRASH_DEBUG