From 70ba9d5831c5b7d6074cde9d5f918ca4ef7734ad Mon Sep 17 00:00:00 2001 From: Jan Vidar Krey Date: Wed, 13 Feb 2013 21:10:13 +0100 Subject: [PATCH] Create a bot user system. --- src/core/hub.c | 15 +++++++++++++++ src/core/plugincallback.c | 4 ++-- src/core/route.c | 14 +++++++++++++- src/core/user.c | 34 +++++++++++++++++++++++++++++++++- src/core/user.h | 12 +++++++++++- 5 files changed, 74 insertions(+), 5 deletions(-) diff --git a/src/core/hub.c b/src/core/hub.c index 37fc3e3..db99561 100644 --- a/src/core/hub.c +++ b/src/core/hub.c @@ -806,6 +806,19 @@ static void unload_ssl_certificates(struct hub_info* hub) } #endif /* SSL_SUPPORT */ +// #ifdef BOT_SUPPORT + +static void hub_bot_op_notify_handle(struct hub_user* bot, struct adc_message* msg) +{ + LOG_INFO("Invoked hub_bot_op_notify_handle()"); +} + +static void hub_bot_op_notify_create(struct hub_info* hub) +{ + struct hub_user* opcom = user_create_bot(hub, "Operations", "Hub operators", hub_bot_op_notify_handle); +} +// #endif + struct hub_info* hub_start_service(struct hub_config* config) { struct hub_info* hub = 0; @@ -889,6 +902,8 @@ struct hub_info* hub_start_service(struct hub_config* config) // Start the hub command sub-system hub->commands = command_initialize(hub); + + hub_bot_op_notify_create(hub); return hub; } diff --git a/src/core/plugincallback.c b/src/core/plugincallback.c index fb782fb..f5f73cf 100644 --- a/src/core/plugincallback.c +++ b/src/core/plugincallback.c @@ -104,7 +104,7 @@ static int cbfunc_command_add(struct plugin_handle* plugin, struct plugin_comman cmdh->internal_handle = command; list_append(data->commands, cmdh); command_add(plugin_get_hub(plugin)->commands, command, (void*) plugin); - printf("*** Add plugin command: %s (%p, %p)\n", command->prefix, command, cmdh); + LOG_DEBUG("*** Add plugin command: %s (%p, %p)\n", command->prefix, command, cmdh); return 0; } @@ -113,7 +113,7 @@ static int cbfunc_command_del(struct plugin_handle* plugin, struct plugin_comman struct plugin_callback_data* data = get_callback_data(plugin); struct command_handle* command = (struct command_handle*) cmdh->internal_handle; - printf("*** Del plugin command: %s (%p, %p)\n", command->prefix, command, cmdh); + LOG_DEBUG("*** Del plugin command: %s (%p, %p)\n", command->prefix, command, cmdh); list_remove(data->commands, cmdh); command_del(plugin_get_hub(plugin)->commands, command); hub_free(command); diff --git a/src/core/route.c b/src/core/route.c index 3fae178..0f407b4 100644 --- a/src/core/route.c +++ b/src/core/route.c @@ -104,7 +104,19 @@ int route_to_user(struct hub_info* hub, struct hub_user* user, struct adc_messag #endif if (!user->connection) - return 0; + { + switch (user->type) + { + case user_type_client: + return 0; // No connection - we're about to drop this user. + case user_type_bot: + { + bot_recv_msg handler = (bot_recv_msg) user->ptr; + handler(user, msg); + return 0; + } + } + } uhub_assert(msg->cache && *msg->cache); diff --git a/src/core/user.c b/src/core/user.c index 9f9ab7b..702478e 100644 --- a/src/core/user.c +++ b/src/core/user.c @@ -42,7 +42,6 @@ struct hub_user* user_create(struct hub_info* hub, struct net_connection* con, s LOG_TRACE("user_create(), hub=%p, con[sd=%d]", hub, net_con_get_sd(con)); user = (struct hub_user*) hub_malloc_zero(sizeof(struct hub_user)); - if (user == NULL) return NULL; /* OOM */ @@ -54,6 +53,7 @@ struct hub_user* user_create(struct hub_info* hub, struct net_connection* con, s memcpy(&user->id.addr, addr, sizeof(struct ip_addr_encap)); user_set_state(user, state_protocol); + user->type = user_type_client; flood_control_reset(&user->flood_chat); flood_control_reset(&user->flood_connect); @@ -65,6 +65,38 @@ struct hub_user* user_create(struct hub_info* hub, struct net_connection* con, s return user; } +struct hub_user* user_create_bot(struct hub_info* hub, const char* nick, const char* description, bot_recv_msg msg_handler) +{ + struct hub_user* user = NULL; + LOG_TRACE("user_create_bot(), hub=%p, con[sd=%d]", hub, net_con_get_sd(con)); + + user = (struct hub_user*) hub_malloc_zero(sizeof(struct hub_user)); + if (user == NULL) + return NULL; /* OOM */ + + strcpy(user->id.nick, nick); + uman_get_free_sid(hub->users, user); + + user_set_state(user, state_normal); + user->type = user_type_bot; + user->credentials = auth_cred_bot; + + // The message handler + user->ptr = (void*) msg_handler; + + user->info = adc_msg_construct(ADC_CMD_BINF, 15); + if (user->info) + { + adc_msg_add_argument(user->info, sid_to_string(user->id.sid)); + adc_msg_add_named_argument(user->info, ADC_INF_FLAG_CLIENT_TYPE, ADC_CLIENT_TYPE_BOT); + adc_msg_add_named_argument_string(user->info, ADC_INF_FLAG_USER_AGENT, PRODUCT_STRING); + adc_msg_add_named_argument_string(user->info, ADC_INF_FLAG_NICK, nick); + adc_msg_add_named_argument_string(user->info, ADC_INF_FLAG_DESCRIPTION, description); + } + + user->hub = hub; + return user; +} void user_destroy(struct hub_user* user) { diff --git a/src/core/user.h b/src/core/user.h index 0860659..f0bb0f8 100644 --- a/src/core/user.h +++ b/src/core/user.h @@ -105,21 +105,30 @@ struct hub_user_limits size_t hub_count_total; /** The number of hubs connected to in total */ }; +enum user_type +{ + user_type_client, /** A user connected normally as an ADC client */ + user_type_bot, /** Not really a user, but a bot inside the hub */ +}; + +typedef void (*bot_recv_msg)(struct hub_user*, struct adc_message* msg); + struct hub_user { struct hub_user_info id; /** Contains nick name and CID */ enum auth_credentials credentials; /** see enum user_credentials */ enum user_state state; /** see enum user_state */ + enum user_type type; uint32_t flags; /** see enum user_flags */ struct linked_list* feature_cast; /** Features supported by feature cast */ struct adc_message* info; /** ADC 'INF' message (broadcasted to everyone joining the hub) */ struct hub_info* hub; /** The hub instance this user belong to */ + void* ptr; struct ioq_recv* recv_queue; struct ioq_send* send_queue; struct net_connection* connection; /** Connection data */ struct hub_user_limits limits; /** Data used for limitation */ enum user_quit_reason quit_reason; /** Quit reason (see user_quit_reason) */ - struct flood_control flood_chat; struct flood_control flood_connect; struct flood_control flood_search; @@ -139,6 +148,7 @@ struct hub_user * @return User object or NULL if not enough memory is available. */ extern struct hub_user* user_create(struct hub_info* hub, struct net_connection* con, struct ip_addr_encap* addr); +extern struct hub_user* user_create_bot(struct hub_info* hub, const char* nick, const char* description, bot_recv_msg msg_handler); /** * Delete a user.