From: michael Date: Thu, 25 Jun 2015 14:36:47 +0000 (+0000) Subject: - Change userinfo_create() to not always allocate memory for each user message; impro... X-Git-Tag: 1.1.0beta1~17 X-Git-Url: http://git.serene-ircd.net/?a=commitdiff_plain;h=0886c0e4d9ec932d152a5b173e14854e1802b6c3;p=hopm.git - Change userinfo_create() to not always allocate memory for each user message; improve checking for malformed sender prefixes - Remove now unused userinfo_free() git-svn-id: svn://svn.ircd-hybrid.org/svnroot/hopm/trunk@6198 82007160-df01-0410-b94d-b575c5fd34c7 --- diff --git a/src/irc.c b/src/irc.c index 2cf51bd..d35a14c 100644 --- a/src/irc.c +++ b/src/irc.c @@ -106,7 +106,7 @@ get_channel(const char *channel) * the source (parv[0]) is a server. */ static void -m_perform(char *parv[], unsigned int parc, const char *msg, const struct UserInfo *notused) +m_perform(char *parv[], unsigned int parc, const char *msg, const char *source_p) { node_t *node; @@ -155,7 +155,7 @@ m_perform(char *parv[], unsigned int parc, const char *msg, const struct UserInf * the source (parv[0]) is a server. */ static void -m_ping(char *parv[], unsigned int parc, const char *msg, const struct UserInfo *source_p) +m_ping(char *parv[], unsigned int parc, const char *msg, const char *source_p) { if (parc < 3) return; @@ -177,7 +177,7 @@ m_ping(char *parv[], unsigned int parc, const char *msg, const struct UserInfo * * the source (parv[0]) is a server. */ static void -m_invite(char *parv[], unsigned int parc, const char *msg, const struct UserInfo *source_p) +m_invite(char *parv[], unsigned int parc, const char *msg, const char *source_p) { const struct ChannelConf *channel = NULL; @@ -202,11 +202,11 @@ m_invite(char *parv[], unsigned int parc, const char *msg, const struct UserInfo * the source (parv[0]) is a server. */ static void -m_ctcp(char *parv[], unsigned int parc, const char *msg, const struct UserInfo *source_p) +m_ctcp(char *parv[], unsigned int parc, const char *msg, const char *source_p) { if (strncasecmp(parv[3], "\001VERSION\001", 9) == 0) irc_send("NOTICE %s :\001VERSION Hybrid Open Proxy Monitor %s(%s)\001", - source_p->irc_nick, VERSION, SERIALNUM); + source_p, VERSION, SERIALNUM); } /* m_privmsg @@ -220,7 +220,7 @@ m_ctcp(char *parv[], unsigned int parc, const char *msg, const struct UserInfo * * the source (parv[0]) is a server. */ static void -m_privmsg(char *parv[], unsigned int parc, const char *msg, const struct UserInfo *source_p) +m_privmsg(char *parv[], unsigned int parc, const char *msg, const char *source_p) { const struct ChannelConf *channel = NULL; size_t nick_len; @@ -273,7 +273,7 @@ m_privmsg(char *parv[], unsigned int parc, const char *msg, const struct UserInf * the source (parv[0]) is a server. */ static void -m_notice(char *parv[], unsigned int parc, const char *msg, const struct UserInfo *source_p) +m_notice(char *parv[], unsigned int parc, const char *msg, const char *source_p) { static regex_t *preg = NULL; regmatch_t pmatch[5]; @@ -359,7 +359,7 @@ m_notice(char *parv[], unsigned int parc, const char *msg, const struct UserInfo * the source (parv[0]) is a server. */ static void -m_userhost(char *parv[], unsigned int parc, const char *msg, const struct UserInfo *source_p) +m_userhost(char *parv[], unsigned int parc, const char *msg, const char *source_p) { if (parc < 4) return; @@ -376,7 +376,7 @@ m_userhost(char *parv[], unsigned int parc, const char *msg, const struct UserIn * parv[4] = error text */ static void -m_cannot_join(char *parv[], unsigned int parc, const char *msg, const struct UserInfo *source_p) +m_cannot_join(char *parv[], unsigned int parc, const char *msg, const char *source_p) { const struct ChannelConf *channel = NULL; @@ -402,7 +402,7 @@ m_cannot_join(char *parv[], unsigned int parc, const char *msg, const struct Use * parv[4] = error text */ static void -m_kill(char *parv[], unsigned int parc, const char *msg, const struct UserInfo *source_p) +m_kill(char *parv[], unsigned int parc, const char *msg, const char *source_p) { /* Restart hopm to rehash */ main_restart(); @@ -419,66 +419,36 @@ m_kill(char *parv[], unsigned int parc, const char *msg, const struct UserInfo * * Return: * pointer to new UserInfo struct, or NULL if parsing failed */ -static struct UserInfo * +static const char * userinfo_create(const char *source) { - struct UserInfo *ret; - char tmp[MSGLENMAX]; - char *nick = tmp; - char *username = NULL; - char *hostname = NULL; + static char name[MSGLENMAX]; + unsigned int has_user = 0; + unsigned int has_host = 0; - strlcpy(tmp, source, sizeof(tmp)); + strlcpy(name, source, sizeof(name)); - for (char *p = tmp; *p; ++p) + for (char *p = name; *p; ++p) { if (*p == '!') { *p = '\0'; - username = p + 1; + ++has_user; continue; } - if (*p == '@') + if (*p == '@' && has_user) { - *p = '\0'; - hostname = p + 1; + ++has_host; continue; } } - if (username == NULL || hostname == NULL) - return NULL; - - ret = xcalloc(sizeof(*ret)); - ret->irc_nick = xstrdup(nick); - ret->irc_username = xstrdup(username); - ret->irc_hostname = xstrdup(hostname); - - return ret; + if (has_user == 1 && has_host == 1) + return name; + return NULL; }; -/* userinfo_free - * - * Free a UserInfo struct created with userinfo_create. - * - * Parameters: - * source: struct to free - * - * Return: None - */ -static void -userinfo_free(struct UserInfo *source_p) -{ - if (source_p == NULL) - return; - - xfree(source_p->irc_nick); - xfree(source_p->irc_username); - xfree(source_p->irc_hostname); - xfree(source_p); -} - /* irc_init * * Resolve IRC host and perform other initialization. @@ -732,11 +702,7 @@ irc_parse(void) { if (strcasecmp(cmd->command, parv[1]) == 0) { - /* Generate a UserInfo struct from the source */ - struct UserInfo *source_p = userinfo_create(parv[0]); - - cmd->handler(parv, parc, msg, source_p); - userinfo_free(source_p); + cmd->handler(parv, parc, msg, userinfo_create(parv[0])); break; } } diff --git a/src/irc.h b/src/irc.h index eba28c4..2ed1bb4 100644 --- a/src/irc.h +++ b/src/irc.h @@ -26,17 +26,10 @@ #define MSGLENMAX 512 + 1 -struct UserInfo -{ - char *irc_nick; - char *irc_username; - char *irc_hostname; -}; - struct CommandHash { const char *command; - void (*handler)(char *[], unsigned int, const char *, const struct UserInfo *); + void (*handler)(char *[], unsigned int, const char *, const char *); }; extern void irc_send(const char *, ...); diff --git a/src/opercmd.c b/src/opercmd.c index ed9e9c3..4fef20d 100644 --- a/src/opercmd.c +++ b/src/opercmd.c @@ -115,7 +115,7 @@ cmd_protocols(char *param, const struct ChannelConf *target) * Pointer to new Command */ static struct Command * -command_create(const struct OperCommandHash *tab, char *param, char *irc_nick, +command_create(const struct OperCommandHash *tab, char *param, const char *irc_nick, const struct ChannelConf *target) { struct Command *command = xcalloc(sizeof(*command)); @@ -165,8 +165,7 @@ command_free(struct Command *command) * */ void -command_parse(char *command, const struct ChannelConf *target, - const struct UserInfo *source_p) +command_parse(char *command, const struct ChannelConf *target, const char *source_p) { char *param; /* Parsed parameters */ static const struct OperCommandHash COMMAND_TABLE[] = @@ -181,7 +180,7 @@ command_parse(char *command, const struct ChannelConf *target, if (OPT_DEBUG) log_printf("COMMAND -> Parsing command (%s) from %s [%s]", command, - source_p->irc_nick, target->name); + source_p, target->name); /* Only allow COMMANDMAX commands in the queue */ if (LIST_SIZE(&COMMANDS) >= COMMANDMAX) @@ -223,14 +222,14 @@ command_parse(char *command, const struct ChannelConf *target, if (strcasecmp(command, tab->command) == 0) { /* Queue this command */ - struct Command *cmd = command_create(tab, param, source_p->irc_nick, target); + struct Command *cmd = command_create(tab, param, source_p, target); list_add(&COMMANDS, node_create(cmd)); break; } } - irc_send("USERHOST %s", source_p->irc_nick); + irc_send("USERHOST %s", source_p); } /* command_timer diff --git a/src/opercmd.h b/src/opercmd.h index f8d4a90..622b3a8 100644 --- a/src/opercmd.h +++ b/src/opercmd.h @@ -56,5 +56,5 @@ struct Command extern void command_init(void); extern void command_userhost(const char *); extern void command_timer(void); -extern void command_parse(char *, const struct ChannelConf *, const struct UserInfo *); +extern void command_parse(char *, const struct ChannelConf *, const char *); #endif