0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-17 07:20:55 +01:00

alias: convert to RAII/std::string/etc, no more rb_dictionary use (ref #202)

This commit is contained in:
William Pitcock 2016-07-31 01:57:04 -05:00
parent 1c77b054a8
commit 10ff2d192c
6 changed files with 23 additions and 51 deletions

View file

@ -32,6 +32,7 @@
struct Message; struct Message;
struct Client; struct Client;
struct MsgBuf; struct MsgBuf;
struct alias_entry;
extern void parse(struct Client *, char *, char *); extern void parse(struct Client *, char *, char *);
extern void handle_encap(struct MsgBuf *, struct Client *, struct Client *, extern void handle_encap(struct MsgBuf *, struct Client *, struct Client *,
@ -40,7 +41,7 @@ extern void mod_add_cmd(struct Message *msg);
extern void mod_del_cmd(struct Message *msg); extern void mod_del_cmd(struct Message *msg);
extern char *reconstruct_parv(int parc, const char *parv[]); extern char *reconstruct_parv(int parc, const char *parv[]);
extern rb_dictionary *alias_dict; extern std::map<std::string, std::shared_ptr<alias_entry>, case_insensitive_less> alias_dict;
extern std::map<std::string, Message *, case_insensitive_less> cmd_dict; extern std::map<std::string, Message *, case_insensitive_less> cmd_dict;
#endif /* INCLUDED_parse_h_h */ #endif /* INCLUDED_parse_h_h */

View file

@ -304,8 +304,8 @@ struct admin_info
struct alias_entry struct alias_entry
{ {
char *name; std::string name;
char *target; std::string target;
int flags; /* reserved for later use */ int flags; /* reserved for later use */
}; };

View file

@ -52,7 +52,7 @@ static rb_dlink_list yy_shared_list;
static rb_dlink_list yy_cluster_list; static rb_dlink_list yy_cluster_list;
static struct oper_conf *yy_oper = NULL; static struct oper_conf *yy_oper = NULL;
static struct alias_entry *yy_alias = NULL; static std::shared_ptr<alias_entry> yy_alias;
static char *yy_blacklist_host = NULL; static char *yy_blacklist_host = NULL;
static char *yy_blacklist_reason = NULL; static char *yy_blacklist_reason = NULL;
@ -1815,10 +1815,10 @@ conf_set_service_name(void *data)
static int static int
conf_begin_alias(struct TopConf *tc) conf_begin_alias(struct TopConf *tc)
{ {
yy_alias = (alias_entry *)rb_malloc(sizeof(struct alias_entry)); yy_alias = std::make_shared<alias_entry>();
if (conf_cur_block_name != NULL) if (conf_cur_block_name != NULL)
yy_alias->name = rb_strdup(conf_cur_block_name); yy_alias->name = conf_cur_block_name;
yy_alias->flags = 0; yy_alias->flags = 0;
@ -1831,25 +1831,19 @@ conf_end_alias(struct TopConf *tc)
if (yy_alias == NULL) if (yy_alias == NULL)
return -1; return -1;
if (yy_alias->name == NULL) if (yy_alias->name.size() == 0)
{ {
conf_report_error("Ignoring alias -- must have a name."); conf_report_error("Ignoring alias -- must have a name.");
rb_free(yy_alias);
return -1; return -1;
} }
if (yy_alias->target == NULL) if (yy_alias->target.size() == 0)
{ {
conf_report_error("Ignoring alias -- must have a target."); conf_report_error("Ignoring alias -- must have a target.");
rb_free(yy_alias);
return -1; return -1;
} }
rb_dictionary_add(alias_dict, yy_alias->name, yy_alias); alias_dict[yy_alias->name] = std::move(yy_alias);
return 0; return 0;
} }
@ -1860,7 +1854,7 @@ conf_set_alias_name(void *data)
if (data == NULL || yy_alias == NULL) /* this shouldn't ever happen */ if (data == NULL || yy_alias == NULL) /* this shouldn't ever happen */
return; return;
yy_alias->name = rb_strdup((const char *)data); yy_alias->name = (const char *) data;
} }
static void static void
@ -1869,7 +1863,7 @@ conf_set_alias_target(void *data)
if (data == NULL || yy_alias == NULL) /* this shouldn't ever happen */ if (data == NULL || yy_alias == NULL) /* this shouldn't ever happen */
return; return;
yy_alias->target = rb_strdup((const char *)data); yy_alias->target = (const char *) data;
} }
static void static void

View file

@ -41,7 +41,7 @@
#include <ircd/packet.h> #include <ircd/packet.h>
#include <ircd/s_assert.h> #include <ircd/s_assert.h>
rb_dictionary *alias_dict = NULL; std::map<std::string, std::shared_ptr<alias_entry>, case_insensitive_less> alias_dict;
std::map<std::string, Message *, case_insensitive_less> cmd_dict; std::map<std::string, Message *, case_insensitive_less> cmd_dict;
static void cancel_clients(struct Client *, struct Client *); static void cancel_clients(struct Client *, struct Client *);

View file

@ -810,9 +810,6 @@ set_default_conf(void)
ConfigFileEntry.nicklen = NICKLEN; ConfigFileEntry.nicklen = NICKLEN;
ConfigFileEntry.certfp_method = RB_SSL_CERTFP_METH_CERT_SHA1; ConfigFileEntry.certfp_method = RB_SSL_CERTFP_METH_CERT_SHA1;
ConfigFileEntry.hide_opers_in_whois = 0; ConfigFileEntry.hide_opers_in_whois = 0;
if (!alias_dict)
alias_dict = rb_dictionary_create("alias", reinterpret_cast<int (*)(const void *, const void *)>(rb_strcasecmp));
} }
/* /*
@ -1416,19 +1413,6 @@ read_conf_files(bool cold)
fclose(conf_fbfile_in); fclose(conf_fbfile_in);
} }
/*
* free an alias{} entry.
*/
static void
free_alias_cb(rb_dictionary_element *ptr, void *unused)
{
struct alias_entry *aptr = (alias_entry *)ptr->data;
rb_free(aptr->name);
rb_free(aptr->target);
rb_free(aptr);
}
/* /*
* clear_out_old_conf * clear_out_old_conf
* *
@ -1524,11 +1508,7 @@ clear_out_old_conf(void)
} }
/* remove any aliases... -- nenolod */ /* remove any aliases... -- nenolod */
if (alias_dict != NULL) alias_dict.clear();
{
rb_dictionary_destroy(alias_dict, free_alias_cb, NULL);
alias_dict = NULL;
}
del_blacklist_all(); del_blacklist_all();

View file

@ -53,19 +53,16 @@ static const struct MessageEntry alias_msgtab[] =
static inline void static inline void
create_aliases(void) create_aliases(void)
{ {
rb_dictionary_iter iter;
s_assert(rb_dlink_list_length(&alias_messages) == 0); s_assert(rb_dlink_list_length(&alias_messages) == 0);
void *elem; for (auto it = alias_dict.begin(); it != alias_dict.end(); it++)
RB_DICTIONARY_FOREACH(elem, &iter, alias_dict)
{ {
const auto alias(reinterpret_cast<alias_entry *>(elem)); std::shared_ptr<alias_entry> alias = it->second;
struct Message *message = (Message *)rb_malloc(sizeof(*message) + strlen(alias->name) + 1); struct Message *message = (Message *)rb_malloc(sizeof(*message) + alias->name.size() + 1);
char *cmd = (char*)message + sizeof(*message); char *cmd = (char*)message + sizeof(*message);
/* copy the alias name as it will be freed early on a rehash */ /* copy the alias name as it will be freed early on a rehash */
strcpy(cmd, alias->name); strcpy(cmd, alias->name.c_str());
message->cmd = cmd; message->cmd = cmd;
memcpy(message->handlers, alias_msgtab, sizeof(alias_msgtab)); memcpy(message->handlers, alias_msgtab, sizeof(alias_msgtab));
@ -113,7 +110,7 @@ static void
m_alias(struct MsgBuf *msgbuf, struct Client *client_p, struct Client *source_p, int parc, const char **parv) m_alias(struct MsgBuf *msgbuf, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
{ {
struct Client *target_p; struct Client *target_p;
struct alias_entry *aptr = (alias_entry *)rb_dictionary_retrieve(alias_dict, msgbuf->cmd); std::shared_ptr<alias_entry> aptr = alias_dict[msgbuf->cmd];
char *p, *str; char *p, *str;
if(aptr == NULL) if(aptr == NULL)
@ -129,7 +126,7 @@ m_alias(struct MsgBuf *msgbuf, struct Client *client_p, struct Client *source_p,
if(!IsFloodDone(client_p) && client_p->localClient->receiveM > 20) if(!IsFloodDone(client_p) && client_p->localClient->receiveM > 20)
flood_endgrace(client_p); flood_endgrace(client_p);
p = strchr(aptr->target, '@'); p = strchr(aptr->target.c_str(), '@');
if(p != NULL) if(p != NULL)
{ {
/* user@server */ /* user@server */
@ -140,14 +137,14 @@ m_alias(struct MsgBuf *msgbuf, struct Client *client_p, struct Client *source_p,
else else
{ {
/* nick, must be +S */ /* nick, must be +S */
target_p = find_named_person(aptr->target); target_p = find_named_person(aptr->target.c_str());
if(target_p != NULL && !IsService(target_p)) if(target_p != NULL && !IsService(target_p))
target_p = NULL; target_p = NULL;
} }
if(target_p == NULL) if(target_p == NULL)
{ {
sendto_one_numeric(client_p, ERR_SERVICESDOWN, form_str(ERR_SERVICESDOWN), aptr->target); sendto_one_numeric(client_p, ERR_SERVICESDOWN, form_str(ERR_SERVICESDOWN), aptr->target.c_str());
return; return;
} }
@ -160,6 +157,6 @@ m_alias(struct MsgBuf *msgbuf, struct Client *client_p, struct Client *source_p,
sendto_one(target_p, ":%s PRIVMSG %s :%s", sendto_one(target_p, ":%s PRIVMSG %s :%s",
get_id(client_p, target_p), get_id(client_p, target_p),
p != NULL ? aptr->target : get_id(target_p, target_p), p != NULL ? aptr->target.c_str() : get_id(target_p, target_p),
str); str);
} }