0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-06-14 16:08:19 +02: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 Client;
struct MsgBuf;
struct alias_entry;
extern void parse(struct Client *, char *, char *);
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 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;
#endif /* INCLUDED_parse_h_h */

View file

@ -304,8 +304,8 @@ struct admin_info
struct alias_entry
{
char *name;
char *target;
std::string name;
std::string target;
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 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_reason = NULL;
@ -1815,10 +1815,10 @@ conf_set_service_name(void *data)
static int
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)
yy_alias->name = rb_strdup(conf_cur_block_name);
yy_alias->name = conf_cur_block_name;
yy_alias->flags = 0;
@ -1831,25 +1831,19 @@ conf_end_alias(struct TopConf *tc)
if (yy_alias == NULL)
return -1;
if (yy_alias->name == NULL)
if (yy_alias->name.size() == 0)
{
conf_report_error("Ignoring alias -- must have a name.");
rb_free(yy_alias);
return -1;
}
if (yy_alias->target == NULL)
if (yy_alias->target.size() == 0)
{
conf_report_error("Ignoring alias -- must have a target.");
rb_free(yy_alias);
return -1;
}
rb_dictionary_add(alias_dict, yy_alias->name, yy_alias);
alias_dict[yy_alias->name] = std::move(yy_alias);
return 0;
}
@ -1860,7 +1854,7 @@ conf_set_alias_name(void *data)
if (data == NULL || yy_alias == NULL) /* this shouldn't ever happen */
return;
yy_alias->name = rb_strdup((const char *)data);
yy_alias->name = (const char *) data;
}
static void
@ -1869,7 +1863,7 @@ conf_set_alias_target(void *data)
if (data == NULL || yy_alias == NULL) /* this shouldn't ever happen */
return;
yy_alias->target = rb_strdup((const char *)data);
yy_alias->target = (const char *) data;
}
static void

View file

@ -41,7 +41,7 @@
#include <ircd/packet.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;
static void cancel_clients(struct Client *, struct Client *);

View file

@ -810,9 +810,6 @@ set_default_conf(void)
ConfigFileEntry.nicklen = NICKLEN;
ConfigFileEntry.certfp_method = RB_SSL_CERTFP_METH_CERT_SHA1;
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);
}
/*
* 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
*
@ -1524,11 +1508,7 @@ clear_out_old_conf(void)
}
/* remove any aliases... -- nenolod */
if (alias_dict != NULL)
{
rb_dictionary_destroy(alias_dict, free_alias_cb, NULL);
alias_dict = NULL;
}
alias_dict.clear();
del_blacklist_all();

View file

@ -53,19 +53,16 @@ static const struct MessageEntry alias_msgtab[] =
static inline void
create_aliases(void)
{
rb_dictionary_iter iter;
s_assert(rb_dlink_list_length(&alias_messages) == 0);
void *elem;
RB_DICTIONARY_FOREACH(elem, &iter, alias_dict)
for (auto it = alias_dict.begin(); it != alias_dict.end(); it++)
{
const auto alias(reinterpret_cast<alias_entry *>(elem));
struct Message *message = (Message *)rb_malloc(sizeof(*message) + strlen(alias->name) + 1);
std::shared_ptr<alias_entry> alias = it->second;
struct Message *message = (Message *)rb_malloc(sizeof(*message) + alias->name.size() + 1);
char *cmd = (char*)message + sizeof(*message);
/* 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;
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)
{
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;
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)
flood_endgrace(client_p);
p = strchr(aptr->target, '@');
p = strchr(aptr->target.c_str(), '@');
if(p != NULL)
{
/* user@server */
@ -140,14 +137,14 @@ m_alias(struct MsgBuf *msgbuf, struct Client *client_p, struct Client *source_p,
else
{
/* 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))
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;
}
@ -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",
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);
}