Aligned the hub_user and plugin_user data structures so that they can be mixed without a conversion.

The hub_user struct starts with the exact same data and size,
but contain more information which is purely internal to the hub.

Plugins thus have only access to the plugin_user struct part of it.
A simple cast from hub_user to plugin_user is legal.
This commit is contained in:
Jan Vidar Krey 2010-11-12 18:39:12 +01:00
parent a761d4eec5
commit 963564dc31
3 changed files with 35 additions and 45 deletions

View File

@ -378,7 +378,7 @@ static int check_user_agent(struct hub_info* hub, struct hub_user* user, struct
ua = adc_msg_unescape(ua_encoded); ua = adc_msg_unescape(ua_encoded);
if (ua) if (ua)
{ {
memcpy(user->user_agent, ua, MIN(strlen(ua), MAX_UA_LEN)); memcpy(user->id.user_agent, ua, MIN(strlen(ua), MAX_UA_LEN));
hub_free(ua); hub_free(ua);
} }
} }

View File

@ -74,14 +74,18 @@
#define PLUGIN_INVOKE_3(HUB, FUNCNAME, ARG1, ARG2, ARG3) INVOKE(HUB, FUNCNAME, { plugin->funcs.FUNCNAME(plugin, ARG1, ARG2, ARG3); }) #define PLUGIN_INVOKE_3(HUB, FUNCNAME, ARG1, ARG2, ARG3) INVOKE(HUB, FUNCNAME, { plugin->funcs.FUNCNAME(plugin, ARG1, ARG2, ARG3); })
static void convert_user_type(struct plugin_user* puser, struct hub_user* user) static struct plugin_user* convert_user_type(struct hub_user* user)
{ {
struct plugin_user* puser = (struct plugin_user*) user;
return puser;
#if 0
puser->sid = user->id.sid; puser->sid = user->id.sid;
puser->nick = user->id.nick; puser->nick = user->id.nick;
puser->cid = user->id.cid; puser->cid = user->id.cid;
puser->user_agent = user->user_agent; puser->user_agent = user->user_agent;
puser->addr = user->id.addr; puser->addr = user->id.addr;
puser->credentials = user->credentials; puser->credentials = user->credentials;
#endif
} }
plugin_st plugin_check_ip_early(struct hub_info* hub, struct ip_addr_encap* addr) plugin_st plugin_check_ip_early(struct hub_info* hub, struct ip_addr_encap* addr)
@ -106,85 +110,71 @@ void plugin_log_connection_denied(struct hub_info* hub, struct ip_addr_encap* ip
void plugin_log_user_login_success(struct hub_info* hub, struct hub_user* who) void plugin_log_user_login_success(struct hub_info* hub, struct hub_user* who)
{ {
struct plugin_user user; struct plugin_user* user = convert_user_type(who);
convert_user_type(&user, who); PLUGIN_INVOKE_1(hub, on_user_login, user);
PLUGIN_INVOKE_1(hub, on_user_login, &user);
} }
void plugin_log_user_login_error(struct hub_info* hub, struct hub_user* who, const char* reason) void plugin_log_user_login_error(struct hub_info* hub, struct hub_user* who, const char* reason)
{ {
struct plugin_user user; struct plugin_user* user = convert_user_type(who);
convert_user_type(&user, who); PLUGIN_INVOKE_2(hub, on_user_login_error, user, reason);
PLUGIN_INVOKE_2(hub, on_user_login_error, &user, reason);
} }
void plugin_log_user_logout(struct hub_info* hub, struct hub_user* who, const char* reason) void plugin_log_user_logout(struct hub_info* hub, struct hub_user* who, const char* reason)
{ {
struct plugin_user user; struct plugin_user* user = convert_user_type(who);
convert_user_type(&user, who); PLUGIN_INVOKE_2(hub, on_user_logout, user, reason);
PLUGIN_INVOKE_2(hub, on_user_logout, &user, reason);
} }
void plugin_log_user_nick_change(struct hub_info* hub, struct hub_user* who, const char* new_nick) void plugin_log_user_nick_change(struct hub_info* hub, struct hub_user* who, const char* new_nick)
{ {
struct plugin_user user; struct plugin_user* user = convert_user_type(who);
convert_user_type(&user, who); PLUGIN_INVOKE_2(hub, on_user_nick_change, user, new_nick);
PLUGIN_INVOKE_2(hub, on_user_nick_change, &user, new_nick);
} }
void plugin_log_user_update_error(struct hub_info* hub, struct hub_user* who, const char* reason) void plugin_log_user_update_error(struct hub_info* hub, struct hub_user* who, const char* reason)
{ {
struct plugin_user user; struct plugin_user* user = convert_user_type(who);
convert_user_type(&user, who); PLUGIN_INVOKE_2(hub, on_user_update_error, user, reason);
PLUGIN_INVOKE_2(hub, on_user_update_error, &user, reason);
} }
void plugin_log_chat_message(struct hub_info* hub, struct hub_user* who, const char* message, int flags) void plugin_log_chat_message(struct hub_info* hub, struct hub_user* who, const char* message, int flags)
{ {
struct plugin_user user; struct plugin_user* user = convert_user_type(who);
convert_user_type(&user, who); PLUGIN_INVOKE_3(hub, on_user_chat_message, user, message, flags);
PLUGIN_INVOKE_3(hub, on_user_chat_message, &user, message, flags);
} }
plugin_st plugin_handle_chat_message(struct hub_info* hub, struct hub_user* from, const char* message, int flags) plugin_st plugin_handle_chat_message(struct hub_info* hub, struct hub_user* from, const char* message, int flags)
{ {
struct plugin_user user; struct plugin_user* user = convert_user_type(from);
convert_user_type(&user, from); PLUGIN_INVOKE_STATUS_2(hub, on_chat_msg, user, message);
PLUGIN_INVOKE_STATUS_2(hub, on_chat_msg, &user, message);
} }
plugin_st plugin_handle_private_message(struct hub_info* hub, struct hub_user* from, struct hub_user* to, 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)
{ {
struct plugin_user user1; struct plugin_user* user1 = convert_user_type(from);
struct plugin_user user2; struct plugin_user* user2 = convert_user_type(to);
convert_user_type(&user1, from); PLUGIN_INVOKE_STATUS_3(hub, on_private_msg, user1, user2, message);
convert_user_type(&user2, to);
PLUGIN_INVOKE_STATUS_3(hub, on_private_msg, &user1, &user2, message);
} }
plugin_st plugin_handle_search(struct hub_info* hub, struct hub_user* from, const char* data) plugin_st plugin_handle_search(struct hub_info* hub, struct hub_user* from, const char* data)
{ {
struct plugin_user user; struct plugin_user* user = convert_user_type(from);
convert_user_type(&user, from); PLUGIN_INVOKE_STATUS_2(hub, on_search, user, data);
PLUGIN_INVOKE_STATUS_2(hub, on_search, &user, data);
} }
plugin_st plugin_handle_connect(struct hub_info* hub, struct hub_user* from, struct hub_user* to) plugin_st plugin_handle_connect(struct hub_info* hub, struct hub_user* from, struct hub_user* to)
{ {
struct plugin_user user1; struct plugin_user* user1 = convert_user_type(from);
struct plugin_user user2; struct plugin_user* user2 = convert_user_type(to);
convert_user_type(&user1, from); PLUGIN_INVOKE_STATUS_2(hub, on_p2p_connect, user1, user2);
convert_user_type(&user2, to);
PLUGIN_INVOKE_STATUS_2(hub, on_p2p_connect, &user1, &user2);
} }
plugin_st plugin_handle_revconnect(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)
{ {
struct plugin_user user1; struct plugin_user* user1 = convert_user_type(from);
struct plugin_user user2; struct plugin_user* user2 = convert_user_type(to);
convert_user_type(&user1, from); PLUGIN_INVOKE_STATUS_2(hub, on_p2p_revconnect, user1, user2);
convert_user_type(&user2, to);
PLUGIN_INVOKE_STATUS_2(hub, on_p2p_revconnect, &user1, &user2);
} }
plugin_st plugin_auth_get_user(struct hub_info* hub, const char* nickname, struct auth_info* info) plugin_st plugin_auth_get_user(struct hub_info* hub, const char* nickname, struct auth_info* info)

View File

@ -82,8 +82,9 @@ extern const char* user_get_quit_reason_string(enum user_quit_reason);
struct hub_user_info struct hub_user_info
{ {
sid_t sid; /** session ID */ sid_t sid; /** session ID */
char cid[MAX_CID_LEN+1]; /** global client ID */
char nick[MAX_NICK_LEN+1]; /** User's nick name */ char nick[MAX_NICK_LEN+1]; /** User's nick name */
char cid[MAX_CID_LEN+1]; /** global client ID */
char user_agent[MAX_UA_LEN+1];/** User agent string */
struct ip_addr_encap addr; /** User's IP address */ struct ip_addr_encap addr; /** User's IP address */
}; };
@ -105,11 +106,10 @@ struct hub_user_limits
struct hub_user struct hub_user
{ {
enum user_state state; /** see enum user_state */
enum auth_credentials credentials; /** see enum user_credentials */
struct hub_user_info id; /** Contains nick name and CID */ 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 */
uint32_t flags; /** see enum user_features */ uint32_t flags; /** see enum user_features */
char user_agent[MAX_UA_LEN+1];/** User agent string */
struct linked_list* feature_cast; /** Features supported by feature cast */ struct linked_list* feature_cast; /** Features supported by feature cast */
struct adc_message* info; /** ADC 'INF' message (broadcasted to everyone joining the hub) */ struct adc_message* info; /** ADC 'INF' message (broadcasted to everyone joining the hub) */
struct hub_info* hub; /** The hub instance this user belong to */ struct hub_info* hub; /** The hub instance this user belong to */