static int command_patternexlist(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd) { struct patterns_data* pdata = (struct patterns_data*) plugin->ptr; struct cbuffer* buf = cbuf_create(128); sqlite3_stmt *res; int error = 0; const char *tail; char *query = "SELECT * FROM pattern_exceptions;"; cbuf_append_format(buf, "*** %s:\n", cmd->prefix); error = sqlite3_prepare_v2(pdata->db, query, strlen(query), &res, &tail); while (sqlite3_step(res) == SQLITE_ROW) { cbuf_append_format(buf, "ID: %d Pattern ID: %d Exception pattern: \"%s\" Exempt credentials: %s-%s\n", sqlite3_column_int(res, 0), sqlite3_column_int(res, 2), (char*) sqlite3_column_text(res, 1), (char*) sqlite3_column_text(res, 3), (char*) sqlite3_column_text(res, 4)); } sqlite3_finalize(res); plugin->hub.send_message(plugin, user, cbuf_get(buf)); cbuf_destroy(buf); return 0; }
static int command_patternadd(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd) { struct patterns_data* pdata = (struct patterns_data*) plugin->ptr; struct cbuffer* buf = cbuf_create(128); struct plugin_command_arg_data* arg1 = (struct plugin_command_arg_data*) list_get_first(cmd->args); struct plugin_command_arg_data* arg2 = (struct plugin_command_arg_data*) list_get_next(cmd->args); struct plugin_command_arg_data* arg3 = (struct plugin_command_arg_data*) list_get_next(cmd->args); struct plugin_command_arg_data* arg4 = (struct plugin_command_arg_data*) list_get_next(cmd->args); char* t = arg1->data.string; enum pattern_types type; if (!pattern_string_to_type(t, &type)) { cbuf_append_format(buf, "*** %s: Wrong pattern type \"%s\". Available types are: MC, PM, NI, UA.", cmd->prefix, t); } else { enum auth_credentials mincred = arg2->data.credentials; enum auth_credentials maxcred = arg3->data.credentials; char* str = arg4->data.string; int rc = sql_execute(pdata, null_callback, NULL, "INSERT INTO patterns VALUES(NULL, '%s', %d, '%s', '%s');", sql_escape_string(str), type, auth_cred_to_string(mincred), auth_cred_to_string(maxcred)); if (rc > 0) cbuf_append_format(buf, "*** %s: Added pattern \"%s\" to %s group.", cmd->prefix, str, pattern_type_to_string(type)); else cbuf_append_format(buf, "*** %s: Unable to add pattern \"%s\".", cmd->prefix, str); } plugin->hub.send_message(plugin, user, cbuf_get(buf)); cbuf_destroy(buf); return 0; }
static int command_hublist(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd) { struct extras_data* extrasdata = (struct extras_data*) plugin->ptr; struct cbuffer* buf = cbuf_create(128); sqlite3_stmt *res; int error = 0; int rec_count = 0; const char *tail; char *query = "SELECT * FROM hubs;"; cbuf_append_format(buf, "*** %s:", cmd->prefix); error = sqlite3_prepare_v2(extrasdata->db, query, strlen(query), &res, &tail); while (sqlite3_step(res) == SQLITE_ROW) { cbuf_append_format(buf, "\nID: %d, Address: %s , Name: \"%s\"\n", sqlite3_column_int(res, 0), (char*) sqlite3_column_text(res, 1), (char*) sqlite3_column_text(res, 2)); rec_count++; } if (error != SQLITE_OK || rec_count < 1) { cbuf_append(buf, " No hubs found in hublist."); } sqlite3_finalize(res); plugin->hub.send_message(plugin, user, cbuf_get(buf)); cbuf_destroy(buf); return 0; }
static int command_releases(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd) { struct extras_data* extrasdata = (struct extras_data*) plugin->ptr; struct cbuffer* buf = cbuf_create(128); sqlite3_stmt *res; int error = 0; int rec_count = 0; const char *tail; char *query = "SELECT * FROM releases;"; cbuf_append_format(buf, "*** %s:", cmd->prefix); error = sqlite3_prepare_v2(extrasdata->db, query, strlen(query), &res, &tail); while (sqlite3_step(res) == SQLITE_ROW) { cbuf_append_format(buf, "\nID: %d\nTitle: %s\nMagnet link: magnet:?xt=urn:tree:tiger:%s\nPublished: %s", sqlite3_column_int(res, 0), (char*) sqlite3_column_text(res, 1), (char*) sqlite3_column_text(res, 2), (char*) sqlite3_column_text(res, 3)); rec_count++; } if (error != SQLITE_OK || rec_count < 1) { cbuf_append(buf, " No releases found."); } sqlite3_finalize(res); plugin->hub.send_message(plugin, user, cbuf_get(buf)); cbuf_destroy(buf); return 0; }
static int command_patternexadd(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd) { struct patterns_data* pdata = (struct patterns_data*) plugin->ptr; struct cbuffer* buf = cbuf_create(128); struct plugin_command_arg_data* arg1 = (struct plugin_command_arg_data*) list_get_first(cmd->args); struct plugin_command_arg_data* arg2 = (struct plugin_command_arg_data*) list_get_next(cmd->args); struct plugin_command_arg_data* arg3 = (struct plugin_command_arg_data*) list_get_next(cmd->args); struct plugin_command_arg_data* arg4 = (struct plugin_command_arg_data*) list_get_next(cmd->args); int pattern_id = arg1->data.integer; enum auth_credentials mincred = arg2->data.credentials; enum auth_credentials maxcred = arg3->data.credentials; char* str = arg4->data.string; int rc = sql_execute(pdata, null_callback, NULL, "PRAGMA foreign_keys=ON; INSERT INTO pattern_exceptions VALUES(NULL, '%s', %d, '%s', '%s');", sql_escape_string(str), pattern_id, auth_cred_to_string(mincred), auth_cred_to_string(maxcred)); if (rc > 0) cbuf_append_format(buf, "*** %s: Added pattern exception \"%s\" to pattern ID %d.", cmd->prefix, str, pattern_id); else cbuf_append_format(buf, "*** %s: Unable to add pattern exception \"%s\" to pattern ID %d.", cmd->prefix, str, pattern_id); plugin->hub.send_message(plugin, user, cbuf_get(buf)); cbuf_destroy(buf); return 0; }
void user_login(struct plugin_handle* plugin, struct plugin_user* user) { struct chat_history_data* data = (struct chat_history_data*) plugin->ptr; struct cbuffer* buf = NULL; struct linked_list* found = (struct linked_list*) list_create(); sql_execute(data, get_messages_callback, found, "SELECT from_nick,message, datetime(time, 'localtime') as time FROM chat_history ORDER BY time DESC LIMIT 0,%d;", (int) data->history_connect); if (data->history_connect > 0 && list_size(found) > 0) { buf = cbuf_create(MAX_HISTORY_SIZE); cbuf_append(buf, "Chat history:\n\n"); struct chat_history_line* history_line; history_line = (struct chat_history_line*) list_get_last(found); while (history_line) { cbuf_append_format(buf, "[%s] <%s> %s\n", history_line->time, history_line->from, history_line->message); list_remove(found, history_line); hub_free(history_line); history_line = (struct chat_history_line*) list_get_last(found); } plugin->hub.send_message(plugin, user, cbuf_get(buf)); cbuf_destroy(buf); } list_clear(found, &hub_free); list_destroy(found); }
static int command_resettopic_handler(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd) { struct cbuffer* buf = cbuf_create(128); plugin->hub.set_description(plugin, NULL); cbuf_append_format(buf, "*** %s: Topic reset.", cmd->prefix); plugin->hub.send_message(plugin, user, cbuf_get(buf)); cbuf_destroy(buf); return 0; }
/** * Send a status message back to the user who issued the !history command. */ static int command_status(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd, struct cbuffer* buf) { struct cbuffer* msg = cbuf_create(cbuf_size(buf) + strlen(cmd->prefix) + 8); cbuf_append_format(msg, "*** %s: %s", cmd->prefix, cbuf_get(buf)); plugin->hub.send_message(plugin, user, cbuf_get(msg)); cbuf_destroy(msg); cbuf_destroy(buf); return 0; }
static int command_showtopic_handler(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd) { struct cbuffer* buf = cbuf_create(128); char* topic = plugin->hub.get_description(plugin); cbuf_append_format(buf, "*** %s: Current topic is: \"%s\"", cmd->prefix, topic); plugin->hub.send_message(plugin, user, cbuf_get(buf)); cbuf_destroy(buf); hub_free(topic); return 0; }
/** * The callback function for handling the !history command. */ static int command_history(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd) { struct chat_history_data* data = (struct chat_history_data*) plugin->ptr; struct cbuffer* buf = cbuf_create(MAX_HISTORY_SIZE); struct linked_list* found = (struct linked_list*) list_create(); struct plugin_command_arg_data* arg = plugin->hub.command_arg_next(plugin, cmd, plugin_cmd_arg_type_integer); int maxlines; if (arg) maxlines = arg->data.integer; else maxlines = data->history_default; sql_execute(data, get_messages_callback, found, "SELECT from_nick,message, datetime(time, 'localtime') as time FROM chat_history ORDER BY time DESC LIMIT 0,%d;", maxlines); size_t linecount = list_size(found); if (linecount > 0) { cbuf_append_format(buf, "*** %s: Chat History:\n\n", cmd->prefix); struct chat_history_line* history_line; history_line = (struct chat_history_line*) list_get_last(found); while (history_line) { cbuf_append_format(buf, "[%s] <%s> %s\n", history_line->time, history_line->from, history_line->message); list_remove(found, history_line); hub_free(history_line); history_line = (struct chat_history_line*) list_get_last(found); } } else { cbuf_append_format(buf, "*** %s: No messages found.", cmd->prefix); } plugin->hub.send_message(plugin, user, cbuf_get(buf)); cbuf_destroy(buf); list_clear(found, &hub_free); list_destroy(found); return 0; }
static int command_patternexdel(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd) { struct patterns_data* pdata = (struct patterns_data*) plugin->ptr; struct cbuffer* buf = cbuf_create(128); struct plugin_command_arg_data* args = (struct plugin_command_arg_data*) list_get_first(cmd->args); int id = args->data.integer; int rc = sql_execute(pdata, null_callback, NULL, "DELETE FROM pattern_exceptions WHERE id=%d;", id); if (rc > 0) cbuf_append_format(buf, "*** %s: Deleted pattern exception with ID %d.", cmd->prefix, id); else cbuf_append_format(buf, "*** %s: Unable to delete pattern exception with id %d.", cmd->prefix, id); plugin->hub.send_message(plugin, user, cbuf_get(buf)); cbuf_destroy(buf); return 0; }
static int command_historycleanup(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd) { struct chat_history_data* data = (struct chat_history_data*) plugin->ptr; struct cbuffer* buf = cbuf_create(128); int rc = 0; rc = sql_execute(data, null_callback, NULL, "DELETE FROM chat_history;"); if (!rc) cbuf_append_format(buf, "*** %s: Unable to clean chat history table.", cmd->prefix); else cbuf_append_format(buf, "*** %s: Cleaned chat history table.", cmd->prefix); plugin->hub.send_message(plugin, user, cbuf_get(buf)); cbuf_destroy(buf); sql_execute(data, null_callback, NULL, "VACUUM;"); return 0; }
static int command_newsadd(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd) { struct extras_data* extrasdata = (struct extras_data*) plugin->ptr; struct cbuffer* buf = cbuf_create(128); struct plugin_command_arg_data* arg1 = (struct plugin_command_arg_data*) list_get_first(cmd->args); const char* news_text = sql_escape_string(arg1->data.string); int rc = sql_execute(extrasdata, null_callback, NULL, "INSERT INTO news (id, text) VALUES(NULL, '%s');", news_text); if (rc > 0) cbuf_append_format(buf, "*** %s: News updated.", cmd->prefix); else cbuf_append_format(buf, "*** %s: Unable to update news.", cmd->prefix); plugin->hub.send_message(plugin, user, cbuf_get(buf)); cbuf_destroy(buf); return 0; }
static int command_topic_handler(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd) { struct cbuffer* buf = cbuf_create(128); struct plugin_command_arg_data* arg = plugin->hub.command_arg_next(plugin, cmd, plugin_cmd_arg_type_string); char* topic = arg ? arg->data.string : ""; plugin->hub.set_description(plugin, topic); cbuf_append_format(buf, "*** %s: Topic set to \"%s\"", cmd->prefix, topic); plugin->hub.send_message(plugin, user, cbuf_get(buf)); cbuf_destroy(buf); return 0; }
static int command_patterntest(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd) { struct patterns_data* pdata = (struct patterns_data*) plugin->ptr; struct cbuffer* buf = cbuf_create(128); struct plugin_command_arg_data* arg1 = (struct plugin_command_arg_data*) list_get_first(cmd->args); struct plugin_command_arg_data* arg2 = (struct plugin_command_arg_data*) list_get_next(cmd->args); int id = arg1->data.integer; char* str = arg2->data.string; sqlite3_stmt *res; int error = 0; const char *tail; char query[80]; cbuf_append_format(buf, "*** %s: ", cmd->prefix); int n = sprintf(query, "SELECT regexp FROM patterns WHERE id=%d LIMIT 1;", id); error = sqlite3_prepare_v2(pdata->db, query, n, &res, &tail); if (sqlite3_step(res) == SQLITE_ROW) { if(pattern_match(str, (char*) sqlite3_column_text(res, 0))) cbuf_append_format(buf, "Tested string \"%s\" matches pattern \"%s\".", str, sqlite3_column_text(res, 0)); else cbuf_append_format(buf, "Tested string \"%s\" does not match pattern \"%s\".", str, sqlite3_column_text(res, 0)); } else { cbuf_append_format(buf, "Pattern ID \"%d\" not found.", id); } sqlite3_finalize(res); plugin->hub.send_message(plugin, user, cbuf_get(buf)); cbuf_destroy(buf); return 0; }
static int command_userlogcleanup(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd) { struct log_data* ldata = (struct log_data*) plugin->ptr; struct cbuffer* buf = cbuf_create(128); struct plugin_command_arg_data* arg = (struct plugin_command_arg_data*) list_get_first(cmd->args); int days = arg->data.integer; int rc = sql_execute(ldata, null_callback, NULL, "DELETE FROM userlog WHERE time < DATETIME('NOW', 'localtime', '-%d days');", days); if (rc > 0) cbuf_append_format(buf, "*** %s: Cleaned log entries older than %d days.", cmd->prefix, days); else cbuf_append_format(buf, "*** %s: Unable to clean log table.", cmd->prefix); plugin->hub.send_message(plugin, user, cbuf_get(buf)); cbuf_destroy(buf); sql_execute(ldata, null_callback, NULL, "VACUUM;"); return 0; }
static int command_releaseadd(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd) { struct extras_data* extrasdata = (struct extras_data*) plugin->ptr; struct cbuffer* buf = cbuf_create(128); struct plugin_command_arg_data* arg1 = (struct plugin_command_arg_data*) list_get_first(cmd->args); struct plugin_command_arg_data* arg2 = (struct plugin_command_arg_data*) list_get_next(cmd->args); char* tth = strdup(sql_escape_string(arg1->data.string)); char* title = strdup(sql_escape_string(arg2->data.string)); int rc = sql_execute(extrasdata, null_callback, NULL, "INSERT INTO releases (id, title, tth) VALUES(NULL, '%s', '%s');", title, tth); if (rc > 0) cbuf_append_format(buf, "*** %s: Added \"%s\" to releases.", cmd->prefix, title); else cbuf_append_format(buf, "*** %s: Unable to add \"%s\" to releases.", cmd->prefix, title); plugin->hub.send_message(plugin, user, cbuf_get(buf)); cbuf_destroy(buf); hub_free(tth); hub_free(title); return 0; }
/** * The callback function for handling the !history command. */ static int command_history(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd) { struct cbuffer* buf; struct chat_history_data* data = (struct chat_history_data*) plugin->ptr; struct plugin_command_arg_data* arg = plugin->hub.command_arg_next(plugin, cmd, plugin_cmd_arg_type_integer); int maxlines; if (!list_size(data->chat_history)) return command_status(plugin, user, cmd, cbuf_create_const("No messages.")); if (arg) maxlines = arg->data.integer; else maxlines = data->history_default; buf = cbuf_create(MAX_HISTORY_SIZE); cbuf_append_format(buf, "*** %s: Chat History:\n", cmd->prefix); get_messages(data, maxlines, buf); plugin->hub.send_message(plugin, user, cbuf_get(buf)); cbuf_destroy(buf); return 0; }
static int command_userlog(struct plugin_handle* plugin, struct plugin_user* user, struct plugin_command* cmd) { struct log_data* ldata = (struct log_data*) plugin->ptr; struct cbuffer* buf = cbuf_create(128); size_t argnum = list_size(cmd->args); struct plugin_command_arg_data* arg1 = NULL; struct plugin_command_arg_data* arg2 = NULL; struct plugin_command_arg_data* arg3 = NULL; if (argnum == 3) { arg1 = plugin->hub.command_arg_next(plugin, cmd, plugin_cmd_arg_type_integer); arg2 = plugin->hub.command_arg_next(plugin, cmd, plugin_cmd_arg_type_string); arg3 = plugin->hub.command_arg_next(plugin, cmd, plugin_cmd_arg_type_string); } else { if (argnum == 2) { cbuf_append_format(buf, "*** %s: Missing search pattern.", cmd->prefix); plugin->hub.send_message(plugin, user, cbuf_get(buf)); cbuf_destroy(buf); return 0; } if (argnum == 1) arg1 = plugin->hub.command_arg_next(plugin, cmd, plugin_cmd_arg_type_integer); } int lines = arg1 ? arg1->data.integer : 20; char* column = arg2 ? arg2->data.string : ""; char* search = arg3 ? arg3->data.string : ""; size_t column_len = strlen(column); size_t search_len = strlen(search); char query[1024]; sqlite3_stmt *res; int error = 0; const char *tail; size_t count = 0; if (lines > 200) lines = 200; if (search_len && column_len) { if(!check_column(column)) { cbuf_append_format(buf, "*** %s: Invalid column. Valid columns are: nick, cid, addr, credentials, useragent, message, all.", cmd->prefix); plugin->hub.send_message(plugin, user, cbuf_get(buf)); cbuf_destroy(buf); return 0; } if (strcmp(column, "all") == 0) { sprintf(query, "SELECT * FROM userlog WHERE nick LIKE '%%%s%%' OR cid LIKE '%%%s%%' OR credentials LIKE '%%%s%%' OR useragent LIKE '%%%s%%' OR addr LIKE '%%%s%%' OR message LIKE '%%%s%%' ORDER BY time DESC LIMIT %d;", search, search, search, search, search, search, lines); cbuf_append_format(buf, "*** %s: Search_ing for \"%s\" in all columns.", cmd->prefix, search); } else { sprintf(query, "SELECT * FROM userlog WHERE %s LIKE '%%%s%%' ORDER BY time DESC LIMIT %d;", column, search, lines); cbuf_append_format(buf, "*** %s: Searching for \"%s\" in column \"%s\".", cmd->prefix, search, column); } } else { sprintf(query, "SELECT * FROM userlog ORDER BY time DESC LIMIT %d;", lines); cbuf_append_format(buf, "*** %s: ", cmd->prefix); } error = sqlite3_prepare_v2(ldata->db, query, strlen(query), &res, &tail); while (sqlite3_step(res) == SQLITE_ROW) { cbuf_append_format(buf, "\n[%s] %s, %s [%s] [%s] \"%s\" - %s", (char*) sqlite3_column_text(res, 6), (char*) sqlite3_column_text(res, 1), (char*) sqlite3_column_text(res, 0), (char*) sqlite3_column_text(res, 3), (char*) sqlite3_column_text(res, 2), (char*) sqlite3_column_text(res, 4), (char*) sqlite3_column_text(res, 5)); count++; } if (error || count == 0) { if (search_len && column_len) cbuf_append(buf, "\n"); cbuf_append(buf, "No log entries found."); } else cbuf_append_format(buf, "\n\n%zd entr%s shown", count, count != 1 ? "ies" : "y"); sqlite3_finalize(res); plugin->hub.send_message(plugin, user, cbuf_get(buf)); cbuf_destroy(buf); return 0; }