From 0f366fe06deb2173cffd29ae43938f3f7b258854 Mon Sep 17 00:00:00 2001 From: pk910 Date: Fri, 9 Sep 2011 02:27:14 +0200 Subject: [PATCH] added possibilities to change table content after adding it & added cmd_bans --- bot_NeonServ.c | 10 ++++++-- cmd_neonserv_bans.c | 57 ++++++++++++++++++++++++++++++++++++++++++++ cmd_neonserv_trace.c | 2 +- tools.c | 38 ++++++++++++++++++++++++++--- tools.h | 7 ++++-- 5 files changed, 106 insertions(+), 8 deletions(-) create mode 100644 cmd_neonserv_bans.c diff --git a/bot_NeonServ.c b/bot_NeonServ.c index 5920dc2..63887d1 100644 --- a/bot_NeonServ.c +++ b/bot_NeonServ.c @@ -37,6 +37,7 @@ static const struct default_language_entry msgtab[] = { {"NS_USER_PROTECTED", "Sorry, \002%s\002 is protected."}, {"NS_SERVICE_IMMUNE", "\002%s\002 may not be kicked, killed, banned, or deopped."}, {"NS_TABLE_NONE", " None"}, + {"NS_TABLE_COUNT", "Found \002%d\002 matches."}, {"NS_BAN_ALREADY_ADDED", "\002%s\002 is already banned in %s."}, {"NS_INVALID_ACCESS_RANGE", "Invalid access range; minimum (%d) must be lower than maximum (%d)."}, {"NS_CLVL_DONE", "%s now has access \002%d\002 in %s."}, @@ -120,8 +121,12 @@ static const struct default_language_entry msgtab[] = { {"NS_SET_TRIGGER_OWNER", "You must have access 500 in %s to change the channel trigger."}, {"NS_WIPEINFO_DONE", "Removed \002%s\002's infoline in \002%s\002."}, {"NS_TRACE_HEADER", "The following users were found:"}, - {"NS_TRACE_FOUND", "Found \002%d\002 matches."}, {"NS_ADDBAN_DONE", "\002%s\002 permantly added to the %s ban list. (matching %d users)"}, + {"NS_BANS_HEADER_MASK", "Mask"}, + {"NS_BANS_HEADER_SETBY", "Set By"}, + {"NS_BANS_HEADER_TRIGGERED", "Triggered"}, + {"NS_BANS_HEADER_EXPIRES", "Expires"}, + {"NS_BANS_HEADER_REASON", "Reason"}, {NULL, NULL} }; @@ -164,7 +169,7 @@ INCLUDE ALL CMD's HERE #include "cmd_neonserv_addban.c" //#include "cmd_neonserv_addtimeban.c" //#include "cmd_neonserv_delban.c" -//#include "cmd_neonserv_banlist.c" +#include "cmd_neonserv_bans.c" //#include "cmd_neonserv_open.c" //#include "cmd_neonserv_topic.c" //#include "cmd_neonserv_mode.c" @@ -304,6 +309,7 @@ void init_NeonServ() { register_command(BOTID, "ban", neonserv_cmd_ban, 1, CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH, "#channel_canban", 0); register_command(BOTID, "wipeinfo", neonserv_cmd_wipeinfo, 1, CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH, "#channel_wipeinfo", 0); register_command(BOTID, "addban", neonserv_cmd_addban, 1, CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH, "#channel_staticban", 0); + register_command(BOTID, "bans", neonserv_cmd_bans, 0, CMDFLAG_REQUIRE_CHAN | CMDFLAG_REGISTERED_CHAN | CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH, "1", 0); register_command(BOTID, "trace", neonserv_cmd_trace, 1, CMDFLAG_REQUIRE_AUTH | CMDFLAG_CHECK_AUTH, NULL, 400); diff --git a/cmd_neonserv_bans.c b/cmd_neonserv_bans.c new file mode 100644 index 0000000..29aeb6a --- /dev/null +++ b/cmd_neonserv_bans.c @@ -0,0 +1,57 @@ + +/* +* argv[0] (optional) mask +*/ +static CMD_BIND(neonserv_cmd_bans) { + MYSQL_RES *res; + MYSQL_ROW row; + //ban list + int i, with_expire = 0, cindex = 0; + char triggered_str[MAXLEN], expires_str[MAXLEN]; + struct Table *table; + printf_mysql_query("SELECT `ban_mask`, `user_user`, `ban_triggered`, `ban_timeout`, `ban_reason` FROM `bans` LEFT JOIN `users` ON `ban_owner` = `user_id` WHERE `ban_channel` = '%d'", chan->channel_id); + res = mysql_use(); + table = table_init(5, mysql_num_rows(res) + 1, 0); + char *content[5]; + //add a NULL row (we add values later) + content[0] = NULL; + content[1] = NULL; + content[2] = NULL; + content[3] = NULL; + content[4] = NULL; + table_add(table, content); + while ((row = mysql_fetch_row(res)) != NULL) { + if(argc > 0 && match(argv[0], row[0])) continue; + content[0] = row[0]; + content[1] = row[1]; + content[2] = (strcmp(row[2], "0") ? timeToStr(user, (time(0) - atoi(row[2])), 2, triggered_str) : get_language_string(user, "NS_USERS_SEEN_NEVER")); + if(strcmp(row[3], "0")) { + if(!with_expire) { + //we're using expire times now... + for(i = 0; i < cindex; i++) + table_change_field(table, i+1, 3, get_language_string(user, "NS_USERS_SEEN_NEVER")); + with_expire = 1; + } + content[3] = timeToStr(user, (time(0) - atoi(row[3])), 2, expires_str); + } else + content[3] = (with_expire ? get_language_string(user, "NS_USERS_SEEN_NEVER") : NULL); + content[4] = row[4]; + cindex++; + table_add(table, content); + } + //now we add the table header + content[0] = get_language_string(user, "NS_BANS_HEADER_MASK"); + content[1] = get_language_string(user, "NS_BANS_HEADER_SETBY"); + content[2] = get_language_string(user, "NS_BANS_HEADER_TRIGGERED"); + content[3] = (with_expire ? get_language_string(user, "NS_BANS_HEADER_EXPIRES") : NULL); + content[4] = get_language_string(user, "NS_BANS_HEADER_REASON"); + table_change(table, 0, content); + char **table_lines = table_end(table); + for(i = 0; i < table->entrys; i++) { + reply(getTextBot(), user, table_lines[i]); + } + if(!cindex) + reply(getTextBot(), user, "NS_TABLE_NONE"); + reply(getTextBot(), user, "NS_TABLE_COUNT", cindex); + table_free(table); +} diff --git a/cmd_neonserv_trace.c b/cmd_neonserv_trace.c index b1e1507..9f8da27 100644 --- a/cmd_neonserv_trace.c +++ b/cmd_neonserv_trace.c @@ -91,5 +91,5 @@ static CMD_BIND(neonserv_cmd_trace) { reply(getTextBot(), user, "%s!%s@%s %s", cuser->nick, cuser->ident, cuser->host, ((cuser->flags & USERFLAG_ISAUTHED) ? cuser->auth : "*")); } } - reply(getTextBot(), user, "NS_TRACE_FOUND", matches); + reply(getTextBot(), user, "NS_TABLE_COUNT", matches); } diff --git a/tools.c b/tools.c index a6b6f53..2f8427f 100644 --- a/tools.c +++ b/tools.c @@ -117,14 +117,44 @@ int table_add(struct Table *table, char **entry) { int col; if(table->entrys == table->length) return 0; for(col = 0; col < table->width; col++) { - table->contents[table->entrys][col] = ((table->flags & TABLE_FLAG_USE_POINTER) ? entry[col] : strdup(entry[col])); - if(strlen(entry[col]) > table->maxwidth[col]) + table->contents[table->entrys][col] = ((table->flags & TABLE_FLAG_USE_POINTER) || !entry[col] ? entry[col] : strdup(entry[col])); + if(table->contents[table->entrys][col]) + table->col_flags[col] |= TABLE_FLAG_COL_CONTENTS; + if(entry[col] && strlen(entry[col]) > table->maxwidth[col]) table->maxwidth[col] = strlen(entry[col]); } table->entrys++; return 1; } +int table_change(struct Table *table, int row, char **entry) { + int col; + if(row >= table->length) return 0; + for(col = 0; col < table->width; col++) { + if(table->contents[row][col] && !(table->flags & TABLE_FLAG_USE_POINTER)) + free(table->contents[row][col]); + table->contents[row][col] = ((table->flags & TABLE_FLAG_USE_POINTER) || !entry[col] ? entry[col] : strdup(entry[col])); + if(table->contents[row][col]) + table->col_flags[col] |= TABLE_FLAG_COL_CONTENTS; + if(entry[col] && strlen(entry[col]) > table->maxwidth[col]) + table->maxwidth[col] = strlen(entry[col]); + } + return 1; +} + +int table_change_field(struct Table *table, int row, int col, char *entry) { + if(row >= table->length) return 0; + if(col >= table->width) return 0; + if(table->contents[row][col] && !(table->flags & TABLE_FLAG_USE_POINTER)) + free(table->contents[row][col]); + table->contents[row][col] = (((table->flags & TABLE_FLAG_USE_POINTER) || !entry) ? entry : strdup(entry)); + if(table->contents[row][col]) + table->col_flags[col] |= TABLE_FLAG_COL_CONTENTS; + if(entry && strlen(entry) > table->maxwidth[col]) + table->maxwidth[col] = strlen(entry); + return 1; +} + int table_set_bold(struct Table *table, int collum, int bold) { if(bold) table->col_flags[collum] |= TABLE_FLAG_COL_BOLD; @@ -146,6 +176,7 @@ char **table_end(struct Table *table) { table->table_lines[row] = malloc(tablewidth * sizeof(*table->table_lines[row])); pos = 0; for(col = 0; col < table->width; col++) { + if(!(table->col_flags[col] & TABLE_FLAG_COL_CONTENTS)) continue; if(table->col_flags[col] & TABLE_FLAG_COL_BOLD) table->table_lines[row][pos++] = '\002'; for(i = 0; i < strlen(table->contents[row][col]); i++) { @@ -171,7 +202,8 @@ void table_free(struct Table *table) { for(row = 0; row < table->length; row++) { if(!(table->flags & TABLE_FLAG_USE_POINTER) && table->entrys > row) { for(col = 0; col < table->width; col++) { - free(table->contents[row][col]); + if(table->contents[row][col]) + free(table->contents[row][col]); } } free(table->contents[row]); diff --git a/tools.h b/tools.h index a311004..e8080e2 100644 --- a/tools.h +++ b/tools.h @@ -3,8 +3,9 @@ #include "main.h" -#define TABLE_FLAG_USE_POINTER 0x01 -#define TABLE_FLAG_COL_BOLD 0x02 +#define TABLE_FLAG_USE_POINTER 0x01 +#define TABLE_FLAG_COL_BOLD 0x02 +#define TABLE_FLAG_COL_CONTENTS 0x04 struct ClientSocket; struct UserNode; @@ -38,6 +39,8 @@ int match(const char *mask, const char *name); struct Table *table_init(int width, int length, int flags); int table_add(struct Table *table, char **entry); +int table_change(struct Table *table, int row, char **entry); +int table_change_field(struct Table *table, int row, int col, char *entry); int table_set_bold(struct Table *table, int collum, int bold); char **table_end(struct Table *table); void table_free(struct Table *table); -- 2.20.1