/** * Obtain 'num' messages from the chat history and append them to outbuf. * * @return the number of messages added to the buffer. */ static size_t get_messages(struct chat_history_data* data, size_t num, struct cbuffer* outbuf) { struct linked_list* messages = data->chat_history; char* message; int skiplines = 0; size_t lines = 0; size_t total = list_size(messages); if (total == 0) return 0; if (num <= 0 || num > total) num = total; if (num != total) skiplines = total - num; cbuf_append(outbuf, "\n"); message = (char*) list_get_first(messages); while (message) { if (--skiplines < 0) { cbuf_append(outbuf, message); lines++; } message = (char*) list_get_next(messages); } cbuf_append(outbuf, "\n"); return lines; }
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_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_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; }
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; // size_t messages = 0; if (data->history_connect > 0 && list_size(data->chat_history) > 0) { buf = cbuf_create(MAX_HISTORY_SIZE); cbuf_append(buf, "Chat history:\n"); get_messages(data, data->history_connect, buf); plugin->hub.send_message(plugin, user, cbuf_get(buf)); cbuf_destroy(buf); } }
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; }