Cleanup code and allow for plugins to provide an error message.

This commit is contained in:
Jan Vidar Krey 2010-07-18 19:57:07 +02:00
parent d41d649353
commit bdcf35b63d
5 changed files with 83 additions and 39 deletions

View File

@ -32,29 +32,35 @@
} \
}
#define PLUGIN_INVOKE_STATUS(HUB, FUNCNAME, ARGS) \
plugin_st status = st_default; \
PLUGIN_INVOKE(HUB, FUNCNAME, { \
status = plugin->funcs.FUNCNAME ARGS ; \
if (status != st_default) \
break; \
}); \
return status
static void convert_user_type(struct plugin_user* puser, struct hub_user* user)
{
puser->sid = user->id.sid;
puser->nick = user->id.nick;
puser->cid = user->id.cid;
puser->addr = user->id.addr;
puser->credentials = user->credentials;
}
plugin_st plugin_check_ip_early(struct hub_info* hub, struct ip_addr_encap* addr)
{
plugin_st status = st_default;
PLUGIN_INVOKE(hub, login_check_ip_early, {
status = plugin->funcs.login_check_ip_early(addr);
if (status != st_default)
break;
});
return status;
PLUGIN_INVOKE_STATUS(hub, login_check_ip_early, (addr));
}
plugin_st plugin_check_ip_late(struct hub_info* hub, struct ip_addr_encap* addr)
{
plugin_st status = st_default;
PLUGIN_INVOKE(hub, login_check_ip_late, {
status = plugin->funcs.login_check_ip_late(addr);
if (status != st_default)
break;
});
return status;
PLUGIN_INVOKE_STATUS(hub, login_check_ip_late, (addr));
}
void plugin_log_connection_accepted(struct hub_info* hub, struct ip_addr_encap* ipaddr)
{
const char* addr = ip_convert_to_string(ipaddr);
@ -80,12 +86,29 @@ void plugin_log_user_logout(struct hub_info* hub, struct hub_user* user)
{
}
static void convert_user_to_plugin_user(struct plugin_user* puser, struct hub_user* user)
plugin_st plugin_handle_chat_message(struct hub_info* hub, struct hub_user* from, const char* message, int flags)
{
puser->sid = user->id.sid;
puser->nick = user->id.nick;
puser->cid = user->id.cid;
puser->addr = user->id.addr;
puser->credentials = user->credentials;
return st_default;
}
plugin_st plugin_handle_private_message(struct hub_info* hub, struct hub_user* from, struct hub_user* to, const char* message, int flags)
{
return st_default;
}
plugin_st plugin_handle_search(struct hub_info* hub, struct hub_user* user, const char* data)
{
return st_default;
}
plugin_st plugin_handle_connect(struct hub_info* hub, struct hub_user* from, struct hub_user* to)
{
return st_default;
}
plugin_st plugin_handle_revconnect(struct hub_info* hub, struct hub_user* from, struct hub_user* to)
{
return st_default;
}

View File

@ -40,7 +40,16 @@ plugin_st plugin_check_ip_late(struct hub_info* hub, struct ip_addr_encap* addr)
plugin_st plugin_check_nickname_valid(struct hub_info* hub, const char* nick);
plugin_st plugin_check_nickname_reserved(struct hub_info* hub, const char* nick);
/* Handle chat messages */
plugin_st plugin_handle_chat_message(struct hub_info* hub, struct hub_user* from, const char* message, int flags);
plugin_st plugin_handle_private_message(struct hub_info* hub, struct hub_user* from, struct hub_user* to, const char* message, int flags);
/* Handle searches */
plugin_st plugin_handle_search(struct hub_info* hub, struct hub_user* user, const char* data);
/* Handle p2p connections */
plugin_st plugin_handle_connect(struct hub_info* hub, struct hub_user* from, struct hub_user* to);
plugin_st plugin_handle_revconnect(struct hub_info* hub, struct hub_user* from, struct hub_user* to);
#endif

View File

@ -123,18 +123,18 @@ void plugin_unload(struct uhub_plugin_handle* plugin)
static int plugin_parse_line(char* line, int line_count, void* ptr_data)
{
struct uhub_plugins* handle = (struct uhub_plugins*) ptr_data;
struct linked_list* tokens = cfg_tokenize(line);
struct cfg_tokens* tokens = cfg_tokenize(line);
char *directive, *soname, *params;
if (list_size(tokens) == 0)
if (cfg_token_count(tokens) == 0)
return 0;
if (list_size(tokens) < 2)
if (cfg_token_count(tokens) < 2)
return -1;
directive = list_get_first(tokens);
soname = list_get_next(tokens);
params = list_get_next(tokens);
directive = cfg_token_get_first(tokens);
soname = cfg_token_get_next(tokens);
params = cfg_token_get_next(tokens);
if (strcmp(directive, "plugin") == 0 && soname && *soname)
{
@ -151,6 +151,7 @@ static int plugin_parse_line(char* line, int line_count, void* ptr_data)
}
}
cfg_tokens_free(tokens);
return -1;
}

View File

@ -34,8 +34,6 @@
#define MAX_PASS_LEN 64
#endif
struct ip_addr_encap;
struct plugin_user
{
unsigned int sid;
@ -118,6 +116,7 @@ struct uhub_plugin_handle
const char* version; /* plugin version */
const char* description; /* plugin description */
void* ptr; /* Plugin specific data */
const char* error_msg; /* Error message for registration error. */
size_t plugin_api_version; /* Plugin API version */
size_t plugin_funcs_size; /* Size of the plugin funcs */
struct plugin_funcs funcs;

View File

@ -28,15 +28,18 @@ void insert_user(struct linked_list* users, const char* nick, const char* pass,
static int parse_line(char* line, int line_count, void* ptr_data)
{
struct linked_list* users = (struct linked_list*) ptr_data;
struct linked_list* tokens = cfg_tokenize(line);
struct cfg_tokens* tokens = cfg_tokenize(line);
enum auth_credentials cred;
if (list_size(tokens) != 3)
if (cfg_token_count(tokens) == 0)
return 0;
char* credential = (char*) list_get_first(tokens);
char* username = (char*) list_get_next(tokens);
char* password = (char*) list_get_next(tokens);
if (cfg_token_count(tokens) < 2)
return -1;
char* credential = cfg_token_get_first(tokens);
char* username = cfg_token_get_next(tokens);
char* password = cfg_token_get_next(tokens);
if (strcmp(credential, "admin")) cred = auth_cred_admin;
else if (strcmp(credential, "super")) cred = auth_cred_super;
@ -51,7 +54,7 @@ static int parse_line(char* line, int line_count, void* ptr_data)
}
static struct acl_list* load_acl(const char* filename)
static struct acl_list* load_acl(const char* filename, struct uhub_plugin_handle* handle)
{
struct acl_list* list = (struct acl_list*) hub_malloc(sizeof(struct acl_list));
struct linked_list* users = list_create();
@ -92,25 +95,34 @@ static int get_user(const char* nickname, struct auth_info* info)
static plugin_st register_user(struct auth_info* user)
{
/* Read only mode - so rejected */
return st_deny;
}
static plugin_st update_user(struct auth_info* user)
{
/* Read only mode - so rejected */
return st_deny;
}
static plugin_st delete_user(struct auth_info* user)
{
/* Read only mode - so rejected */
return st_deny;
}
static void set_error_message(struct uhub_plugin_handle* plugin, const char* msg)
{
// if (plugin->error_msg)
// hub_free(plugin->error_msg);
plugin->error_msg = msg;
}
int plugin_register(struct uhub_plugin_handle* plugin, const char* config)
{
plugin->name = "File authentication plugin";
plugin->version = "0.1";
plugin->description = "Simple authentication plugin that authenticates users based on a file.";
plugin->description = "Authenticated users based on a read-only text file.";
plugin->plugin_api_version = PLUGIN_API_VERSION;
plugin->plugin_funcs_size = sizeof(struct plugin_funcs);
memset(&plugin->funcs, 0, sizeof(struct plugin_funcs));
@ -121,13 +133,13 @@ int plugin_register(struct uhub_plugin_handle* plugin, const char* config)
plugin->funcs.auth_update_user = update_user;
plugin->funcs.auth_delete_user = delete_user;
plugin->ptr = load_acl(config);
plugin->ptr = load_acl(config, plugin);
return 0;
}
int plugin_unregister(struct uhub_plugin_handle* plugin)
{
set_error_message(plugin, 0);
unload_acl(plugin->ptr);
return 0;
}