int plugin_register(struct plugin_handle* plugin, const char* config) { struct chat_history_data* data; PLUGIN_INITIALIZE(plugin, "SQLite chat history plugin", "1.0", "Provide a global chat history log."); plugin->funcs.on_user_chat_message = history_add; plugin->funcs.on_user_login = user_login; data = parse_config(config, plugin); if (!data) return -1; plugin->ptr = data; create_tables(plugin); data->command_history_handle = (struct plugin_command_handle*) hub_malloc(sizeof(struct plugin_command_handle)); PLUGIN_COMMAND_INITIALIZE(data->command_history_handle, plugin, "history", "?N", auth_cred_guest, &command_history, "Show chat message history."); plugin->hub.command_add(plugin, data->command_history_handle); data->command_historycleanup_handle = (struct plugin_command_handle*) hub_malloc(sizeof(struct plugin_command_handle)); PLUGIN_COMMAND_INITIALIZE(data->command_historycleanup_handle, plugin, "historycleanup", "", auth_cred_admin, &command_historycleanup, "Clean chat message history."); plugin->hub.command_add(plugin, data->command_historycleanup_handle); return 0; }
int plugin_register(struct plugin_handle* plugin, const char* config) { PLUGIN_INITIALIZE(plugin, "SQLite logging plugin", "0.5", "Logs users entering and leaving the hub to SQLite database."); struct log_data* ldata; plugin->funcs.on_user_login = log_user_login; plugin->funcs.on_user_login_error = log_user_login_error; plugin->funcs.on_user_logout = log_user_logout; plugin->funcs.on_user_nick_change = log_change_nick; ldata = parse_config(config, plugin); if (!ldata) return -1; ldata->command_userlog_handle = (struct plugin_command_handle*) hub_malloc(sizeof(struct plugin_command_handle)); PLUGIN_COMMAND_INITIALIZE(ldata->command_userlog_handle, plugin, "userlog", "?N?mm", auth_cred_operator, &command_userlog, "[<lines> [<column> <search pattern>]]", "Search in userlog for a value."); plugin->hub.command_add(plugin, ldata->command_userlog_handle); ldata->command_userlogcleanup_handle = (struct plugin_command_handle*) hub_malloc(sizeof(struct plugin_command_handle)); PLUGIN_COMMAND_INITIALIZE(ldata->command_userlogcleanup_handle, plugin, "userlogcleanup", "N", auth_cred_admin, &command_userlogcleanup, "<days>", "Delete log entries."); plugin->hub.command_add(plugin, ldata->command_userlogcleanup_handle); plugin->ptr = ldata; create_tables(plugin); return 0; }
extern struct cbuffer* cbuf_create(size_t capacity) { struct cbuffer* buf = hub_malloc(sizeof(struct cbuffer)); buf->capacity = capacity; buf->size = 0; buf->flags = 0; buf->buf = hub_malloc(capacity + 1); buf->buf[0] = '\0'; return buf; }
int plugin_register(struct plugin_handle* plugin, const char* config) { struct extras_data* extrasdata; PLUGIN_INITIALIZE(plugin, "Extras plugin", "0.1", "Plugin for extra features like hub news, releases, hublist."); extrasdata = parse_config(config, plugin); if (!extrasdata) return -1; extrasdata->command_hubadd_handle = (struct plugin_command_handle*) hub_malloc(sizeof(struct plugin_command_handle)); PLUGIN_COMMAND_INITIALIZE(extrasdata->command_hubadd_handle, plugin, "hubadd", "m+m", auth_cred_admin, &command_hubadd, "<address> <hub name>", "Add hub to hublist."); plugin->hub.command_add(plugin, extrasdata->command_hubadd_handle); extrasdata->command_hubdel_handle = (struct plugin_command_handle*) hub_malloc(sizeof(struct plugin_command_handle)); PLUGIN_COMMAND_INITIALIZE(extrasdata->command_hubdel_handle, plugin, "hubdel", "N", auth_cred_admin, &command_hubdel, "<hub id>", "Delete hub from hublist."); plugin->hub.command_add(plugin, extrasdata->command_hubdel_handle); extrasdata->command_hublist_handle = (struct plugin_command_handle*) hub_malloc(sizeof(struct plugin_command_handle)); PLUGIN_COMMAND_INITIALIZE(extrasdata->command_hublist_handle, plugin, "hublist", "", auth_cred_user, &command_hublist, "", "List hubs in hublist."); plugin->hub.command_add(plugin, extrasdata->command_hublist_handle); extrasdata->command_newsadd_handle = (struct plugin_command_handle*) hub_malloc(sizeof(struct plugin_command_handle)); PLUGIN_COMMAND_INITIALIZE(extrasdata->command_newsadd_handle, plugin, "newsadd", "+m", auth_cred_admin, &command_newsadd, "", "Add news item."); plugin->hub.command_add(plugin, extrasdata->command_newsadd_handle); extrasdata->command_newsdel_handle = (struct plugin_command_handle*) hub_malloc(sizeof(struct plugin_command_handle)); PLUGIN_COMMAND_INITIALIZE(extrasdata->command_newsdel_handle, plugin, "newsdel", "N", auth_cred_admin, &command_newsdel, "<news id>", "Delete news item."); plugin->hub.command_add(plugin, extrasdata->command_newsdel_handle); extrasdata->command_news_handle = (struct plugin_command_handle*) hub_malloc(sizeof(struct plugin_command_handle)); PLUGIN_COMMAND_INITIALIZE(extrasdata->command_news_handle, plugin, "news", "", auth_cred_user, &command_news, "", "Show hubnews."); plugin->hub.command_add(plugin, extrasdata->command_news_handle); extrasdata->command_releaseadd_handle = (struct plugin_command_handle*) hub_malloc(sizeof(struct plugin_command_handle)); PLUGIN_COMMAND_INITIALIZE(extrasdata->command_releaseadd_handle, plugin, "releaseadd", "m+m", auth_cred_admin, &command_releaseadd, "<tth> <title>", "Add release."); plugin->hub.command_add(plugin, extrasdata->command_releaseadd_handle); extrasdata->command_releasedel_handle = (struct plugin_command_handle*) hub_malloc(sizeof(struct plugin_command_handle)); PLUGIN_COMMAND_INITIALIZE(extrasdata->command_releasedel_handle, plugin, "releasedel", "N", auth_cred_admin, &command_releasedel, "<release id>", "Delete release."); plugin->hub.command_add(plugin, extrasdata->command_releasedel_handle); extrasdata->command_releases_handle = (struct plugin_command_handle*) hub_malloc(sizeof(struct plugin_command_handle)); PLUGIN_COMMAND_INITIALIZE(extrasdata->command_releases_handle, plugin, "releases", "", auth_cred_user, &command_releases, "", "Show releases."); plugin->hub.command_add(plugin, extrasdata->command_releases_handle); plugin->ptr = extrasdata; create_tables(plugin); return 0; }
static void command_register(struct plugin_handle* plugin) { struct example_plugin_data* data = (struct example_plugin_data*) hub_malloc(sizeof(struct example_plugin_data)); data->example = hub_malloc_zero(sizeof(struct plugin_command_handle)); PLUGIN_COMMAND_INITIALIZE(data->example, (void*) data, "example", "", auth_cred_guest, example_command_handler, "This is an example command that is added dynamically by loading the mod_example plug-in."); plugin->hub.command_add(plugin, data->example); plugin->ptr = data; }
void* hub_malloc_zero(size_t size) { void* data = hub_malloc(size); if (data) { memset(data, 0, size); } return data; }
struct cbuffer* cbuf_create_const(const char* buffer) { struct cbuffer* buf = hub_malloc(sizeof(struct cbuffer)); buf->capacity = 0; buf->size = strlen(buffer); buf->flags = CBUF_FLAG_CONST_BUFFER; buf->buf = (char*) buffer; return buf; }
struct auth_info* acl_get_access_info(struct hub_info* hub, const char* name) { struct auth_info* info = 0; info = (struct auth_info*) hub_malloc(sizeof(struct auth_info)); if (plugin_auth_get_user(hub, name, info) != st_allow) { hub_free(info); return NULL; } return info; }
struct rb_tree* rb_tree_create(rb_tree_compare compare, rb_tree_alloc a, rb_tree_free f) { struct rb_tree* tree = a ? a(sizeof(struct rb_tree)) : hub_malloc(sizeof(struct rb_tree)); tree->compare = compare; tree->alloc = a ? a : hub_malloc; tree->free = f ? f : hub_free; tree->root = NULL; tree->elements = 0; tree->iterator.node = NULL; tree->iterator.stack = list_create(); return tree; }
int plugin_register(struct plugin_handle* plugin, const char* config) { struct patterns_data* pdata; PLUGIN_INITIALIZE(plugin, "Forbidden patterns plugin", "0.4", "Searches for forbidden patterns in chat messages and user info."); plugin->funcs.on_chat_msg = check_mainchat; plugin->funcs.on_private_msg = check_pm; plugin->funcs.on_check_user_late = check_user_info; pdata = parse_config(config, plugin); if (!pdata) return -1; if(!create_patterns_table(plugin, pdata)) return -1; if(!create_pattern_exceptions_table(plugin, pdata)) return -1; pdata->command_patternadd_handle = (struct plugin_command_handle*) hub_malloc(sizeof(struct plugin_command_handle)); PLUGIN_COMMAND_INITIALIZE(pdata->command_patternadd_handle, plugin, "patternadd", "mCC+m", auth_cred_admin, &command_patternadd, "<type> <min credentials> <max credentials> <pattern>", "Add forbidden pattern."); plugin->hub.command_add(plugin, pdata->command_patternadd_handle); pdata->command_patterndel_handle = (struct plugin_command_handle*) hub_malloc(sizeof(struct plugin_command_handle)); PLUGIN_COMMAND_INITIALIZE(pdata->command_patterndel_handle, plugin, "patterndel", "N", auth_cred_admin, &command_patterndel, "<pattern id>", "Delete forbidden pattern."); plugin->hub.command_add(plugin, pdata->command_patterndel_handle); pdata->command_patternlist_handle = (struct plugin_command_handle*) hub_malloc(sizeof(struct plugin_command_handle)); PLUGIN_COMMAND_INITIALIZE(pdata->command_patternlist_handle, plugin, "patternlist", "", auth_cred_super, &command_patternlist, "", "List forbidden patterns."); plugin->hub.command_add(plugin, pdata->command_patternlist_handle); pdata->command_patterntest_handle = (struct plugin_command_handle*) hub_malloc(sizeof(struct plugin_command_handle)); PLUGIN_COMMAND_INITIALIZE(pdata->command_patterntest_handle, plugin, "patterntest", "N+m", auth_cred_super, &command_patterntest, "<pattern id> <test string>", "Test a pattern (must be added first)."); plugin->hub.command_add(plugin, pdata->command_patterntest_handle); pdata->command_patternexadd_handle = (struct plugin_command_handle*) hub_malloc(sizeof(struct plugin_command_handle)); PLUGIN_COMMAND_INITIALIZE(pdata->command_patternexadd_handle, plugin, "patternexadd", "NCC+m", auth_cred_admin, &command_patternexadd, "<pattern id> <min credentials> <max credentials> <exception pattern>", "Add exception to a forbidden pattern."); plugin->hub.command_add(plugin, pdata->command_patternexadd_handle); pdata->command_patternexdel_handle = (struct plugin_command_handle*) hub_malloc(sizeof(struct plugin_command_handle)); PLUGIN_COMMAND_INITIALIZE(pdata->command_patternexdel_handle, plugin, "patternexdel", "N", auth_cred_admin, &command_patternexdel, "<exception id>", "Delete pattern exception."); plugin->hub.command_add(plugin, pdata->command_patternexdel_handle); pdata->command_patternexlist_handle = (struct plugin_command_handle*) hub_malloc(sizeof(struct plugin_command_handle)); PLUGIN_COMMAND_INITIALIZE(pdata->command_patternexlist_handle, plugin, "patternexlist", "", auth_cred_super, &command_patternexlist, "", "List pattern exceptions."); plugin->hub.command_add(plugin, pdata->command_patternexlist_handle); plugin->ptr = pdata; return 0; }
struct command_base* command_initialize(struct hub_info* hub) { struct command_base* cbase = (struct command_base*) hub_malloc(sizeof(struct command_base)); uhub_assert(cbase != NULL); // uhub_assert(hub != NULL); cbase->hub = hub; cbase->handlers = (struct linked_list*) list_create(); cbase->prefix_length_max = 0; uhub_assert(cbase->handlers != NULL); commands_builtin_add(cbase); return cbase; }
/** * Add a chat message to history. */ static void history_add(struct plugin_handle* plugin, struct plugin_user* from, const char* message, int flags) { size_t loglen = strlen(message) + strlen(from->nick) + 13; struct chat_history_data* data = (struct chat_history_data*) plugin->ptr; char* log = hub_malloc(loglen + 1); snprintf(log, loglen, "%s <%s> %s\n", get_timestamp(time(NULL)), from->nick, message); log[loglen] = '\0'; list_append(data->chat_history, log); while (list_size(data->chat_history) > data->history_max) { char* msg = list_get_first(data->chat_history); list_remove(data->chat_history, msg); hub_free(msg); } }
void hub_chat_history_add(struct hub_info* hub, struct hub_user* user, struct adc_message* cmd) { char* msg_esc = adc_msg_get_argument(cmd, 0); char* message = adc_msg_unescape(msg_esc); char* log = hub_malloc(strlen(message) + strlen(user->id.nick) + 14); sprintf(log, "%s <%s> %s\n", get_timestamp(time(NULL)), user->id.nick, message); list_append(hub->chat_history, log); while (list_size(hub->chat_history) > (size_t) hub->config->max_chat_history) { char* msg = list_get_first(hub->chat_history); list_remove(hub->chat_history, msg); hub_free(msg); } hub_free(message); hub_free(msg_esc); }
struct uhub_notify_handle* net_notify_create(net_notify_callback cb, void* ptr) { LOG_TRACE("net_notify_create()"); struct uhub_notify_handle* handle = (struct uhub_notify_handle*) hub_malloc(sizeof(struct uhub_notify_handle)); handle->callback = cb; handle->ptr = ptr; #ifndef WIN32 int ret = pipe(handle->pipe_fd); if (ret == -1) { LOG_ERROR("Unable to setup notification pipes."); hub_free(handle); return 0; } handle->con = net_con_create(); net_con_initialize(handle->con, handle->pipe_fd[0], notify_callback, handle, NET_EVENT_READ); #endif return handle; }
static int get_messages_callback(void* ptr, int argc, char **argv, char **colName) { struct linked_list* messages = (struct linked_list*) ptr; struct chat_history_line* line = hub_malloc(sizeof(struct chat_history_line)); int i = 0; memset(line, 0, sizeof(struct chat_history_line)); for (; i < argc; i++) { if (strcmp(colName[i], "from_nick") == 0) strncpy(line->from, argv[i], MAX_NICK_LEN); else if (strcmp(colName[i], "message") == 0) strncpy(line->message, argv[i], MAX_HISTORY_SIZE); else if (strcmp(colName[i], "time") == 0) strncpy(line->time, argv[i], 20); } list_append(messages, line); return 0; }
int plugin_register(struct plugin_handle* plugin, const char* config) { struct topic_plugin_data* data = (struct topic_plugin_data*) hub_malloc(sizeof(struct topic_plugin_data)); data->topic = (struct plugin_command_handle*) hub_malloc_zero(sizeof(struct plugin_command_handle)); data->resettopic = (struct plugin_command_handle*) hub_malloc_zero(sizeof(struct plugin_command_handle)); data->showtopic = (struct plugin_command_handle*) hub_malloc_zero(sizeof(struct plugin_command_handle)); PLUGIN_INITIALIZE(plugin, "Topic plugin", "1.0", "Add commands for changing the hub topic (description)"); PLUGIN_COMMAND_INITIALIZE(data->topic, (void*) data, "topic", "+m", auth_cred_operator, command_topic_handler, "Set new topic"); PLUGIN_COMMAND_INITIALIZE(data->resettopic, (void*) data, "resettopic", "", auth_cred_operator, command_resettopic_handler, "Set topic to default"); PLUGIN_COMMAND_INITIALIZE(data->showtopic, (void*) data, "showtopic", "", auth_cred_guest, command_showtopic_handler, "Shows the current topic"); plugin->hub.command_add(plugin, data->topic); plugin->hub.command_add(plugin, data->resettopic); plugin->hub.command_add(plugin, data->showtopic); plugin->ptr = data; return 0; }
struct sid_pool* sid_pool_create(sid_t max) { struct sid_pool* pool = hub_malloc(sizeof(struct sid_pool)); if (!pool) return 0; pool->min = 1; pool->max = max + 1; pool->count = 0; pool->map = hub_malloc_zero(sizeof(struct hub_user*) * pool->max); if (!pool->map) { hub_free(pool); return 0; } pool->map[0] = (struct hub_user*) pool; /* hack to reserve the first sid. */ #ifdef DEBUG_SID LOG_DUMP("SID_POOL: max=%d", (int) pool->max); #endif return pool; }
void event_queue_post(struct event_queue* queue, struct event_data* message) { struct linked_list* q = (!queue->locked) ? queue->q1 : queue->q2; struct event_data* data; data = (struct event_data*) hub_malloc(sizeof(struct event_data)); if (data) { data->id = message->id; data->ptr = message->ptr; data->flags = message->flags; #ifdef EQ_DEBUG eq_debug("POST", data); #endif list_append(q, data); } else { LOG_ERROR("event_queue_post: OUT OF MEMORY"); } }
struct auth_info* acl_get_access_info(struct hub_info* hub, const char* name) { struct auth_info* info = 0; #ifdef PLUGIN_SUPPORT info = (struct auth_info*) hub_malloc(sizeof(struct auth_info)); if (plugin_auth_get_user(hub, name, info) != st_allow) { hub_free(info); return NULL; } return info; #else info = (struct auth_info*) list_get_first(hub->acl->users); while (info) { if (strcasecmp((char*)info->nickname, name) == 0) { return info; } info = (struct auth_info*) list_get_next(hub->acl->users); } return NULL; #endif }
struct hub_info* hub_start_service(struct hub_config* config) { struct hub_info* hub = 0; int ipv6_supported; hub = hub_malloc_zero(sizeof(struct hub_info)); if (!hub) { LOG_FATAL("Unable to allocate memory for hub"); return 0; } hub->tm_started = time(0); ipv6_supported = net_is_ipv6_supported(); if (ipv6_supported) LOG_DEBUG("IPv6 supported."); else LOG_DEBUG("IPv6 not supported."); hub->server = start_listening_socket(config->server_bind_addr, config->server_port, config->server_listen_backlog, hub); if (!hub->server) { hub_free(hub); LOG_FATAL("Unable to start hub service"); return 0; } LOG_INFO("Starting " PRODUCT "/" VERSION ", listening on %s:%d...", net_get_local_address(hub->server->sd), config->server_port); #ifdef SSL_SUPPORT if (!load_ssl_certificates(hub, config)) { hub_free(hub); return 0; } #endif hub->config = config; hub->users = NULL; hub->muxes = list_create(); hub->users = uman_init(); if (!hub->users) { net_con_close(hub->server); hub_free(hub); return 0; } if (event_queue_initialize(&hub->queue, hub_event_dispatcher, (void*) hub) == -1) { net_con_close(hub->server); uman_shutdown(hub->users); hub_free(hub); return 0; } hub->recvbuf = hub_malloc(MAX_RECV_BUF); hub->sendbuf = hub_malloc(MAX_SEND_BUF); if (!hub->recvbuf || !hub->sendbuf) { net_con_close(hub->server); hub_free(hub->recvbuf); hub_free(hub->sendbuf); uman_shutdown(hub->users); hub_free(hub); return 0; } hub->logout_info = (struct linked_list*) list_create(); server_alt_port_start(hub, config); hub->status = hub_status_running; g_hub = hub; if (net_backend_get_timeout_queue()) { hub->stats.timeout = hub_malloc_zero(sizeof(struct timeout_evt)); timeout_evt_initialize(hub->stats.timeout, hub_timer_statistics, hub); timeout_queue_insert(net_backend_get_timeout_queue(), hub->stats.timeout, TIMEOUT_STATS); } // Start the hub command sub-system hub->commands = command_initialize(hub); return hub; }