From 125e3b21f271b3885bd6ce7c220975abf2ed0c87 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 18 Aug 2016 16:53:12 -0700 Subject: [PATCH] ircd: Improve/rename channel name check related. --- extensions/m_omode.cc | 2 +- include/ircd/channel.h | 30 +++++++++++++++++++++++++----- ircd/bandbi.cc | 2 +- ircd/channel.cc | 22 ---------------------- ircd/chmode.cc | 2 +- modules/core/m_ban.cc | 2 +- modules/core/m_join.cc | 6 +++--- modules/core/m_mode.cc | 8 ++++---- modules/m_invite.cc | 2 +- modules/m_list.cc | 4 ++-- modules/m_names.cc | 2 +- modules/m_resv.cc | 4 ++-- modules/m_testline.cc | 2 +- modules/m_who.cc | 2 +- 14 files changed, 44 insertions(+), 46 deletions(-) diff --git a/extensions/m_omode.cc b/extensions/m_omode.cc index 616046a17..2d3c94ce1 100644 --- a/extensions/m_omode.cc +++ b/extensions/m_omode.cc @@ -59,7 +59,7 @@ mo_omode(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source } /* Now, try to find the channel in question */ - if(!rfc1459::is_chan_prefix(parv[1][0]) || !chan::check_channel_name(parv[1])) + if(!rfc1459::is_chan_prefix(parv[1][0]) || !chan::valid_name(parv[1])) { sendto_one_numeric(source_p, ERR_BADCHANNAME, form_str(ERR_BADCHANNAME), parv[1]); diff --git a/include/ircd/channel.h b/include/ircd/channel.h index 05cf1aea8..92890756d 100644 --- a/include/ircd/channel.h +++ b/include/ircd/channel.h @@ -105,6 +105,11 @@ bool can_send_banned(const membership &); bool can_send_banned(const membership *const &); const char *find_status(const membership *const &msptr, const int &combine); +bool has_prefix(const char *const &name); +bool has_prefix(const std::string &name); +bool valid_name(const char *const &name); +bool valid_name(const std::string &name); + struct chan { std::string name; @@ -140,8 +145,6 @@ struct chan ~chan() noexcept; }; -bool is_name(const char *const &name); - bool is_secret(const chan &); bool is_secret(const chan *const &); bool is_hidden(const chan &); @@ -170,7 +173,6 @@ void remove_user_from_channel(membership *); void remove_user_from_channels(client *); void invalidate_bancache_user(client *); void free_channel_list(rb_dlink_list *); -bool check_channel_name(const char *name); void channel_member_names(chan *, client *, int show_eon); void del_invite(chan *, client *who); const char *channel_modes(chan *, client *who); @@ -263,9 +265,27 @@ can_show(const chan *const &c, const client *const &client) } inline bool -is_name(const char *const &name) +has_prefix(const char *const &name) { - return name && (*name == '#' || *name == '&'); + return name && rfc1459::is_chan_prefix(name[0]); +} + +inline bool +has_prefix(const std::string &name) +{ + return !name.empty() && rfc1459::is_chan_prefix(name[0]); +} + +inline bool +valid_name(const char *const &name) +{ + return name && name[0] && std::all_of(name, name + strlen(name), rfc1459::is_chan); +} + +inline bool +valid_name(const std::string &name) +{ + return !name.empty() && std::all_of(begin(name), end(name), rfc1459::is_chan); } inline bool diff --git a/ircd/bandbi.cc b/ircd/bandbi.cc index 1c402d697..f38b46073 100644 --- a/ircd/bandbi.cc +++ b/ircd/bandbi.cc @@ -182,7 +182,7 @@ bandb_handle_ban(char *parv[], int parc) break; case 'R': - if(chan::is_name(aconf->host)) + if(chan::has_prefix(aconf->host)) aconf->status = CONF_RESV_CHANNEL; else aconf->status = CONF_RESV_NICK; diff --git a/ircd/channel.cc b/ircd/channel.cc index 38c9a62d2..c6e02e8c8 100644 --- a/ircd/channel.cc +++ b/ircd/channel.cc @@ -345,28 +345,6 @@ chan::invalidate_bancache_user(client *client_p) } } -/* check_channel_name() - * - * input - channel name - * output - true if valid channel name, else false - * side effects - - */ -bool -chan::check_channel_name(const char *name) -{ - s_assert(name != NULL); - if(name == NULL) - return false; - - for (; *name; ++name) - { - if(!rfc1459::is_chan(*name)) - return false; - } - - return true; -} - /* free_channel_list() * * input - rb_dlink list to free diff --git a/ircd/chmode.cc b/ircd/chmode.cc index 3c0eeb561..afcbce394 100644 --- a/ircd/chmode.cc +++ b/ircd/chmode.cc @@ -511,7 +511,7 @@ check_forward(struct Client *source_p, chan::chan *chptr, chan::chan *targptr = NULL; chan::membership *msptr; - if(!chan::check_channel_name(forward) || + if(!chan::valid_name(forward) || (MyClient(source_p) && (strlen(forward) > LOC_CHANNELLEN || hash_find_resv(forward)))) { sendto_one_numeric(source_p, ERR_BADCHANNAME, form_str(ERR_BADCHANNAME), forward); diff --git a/modules/core/m_ban.cc b/modules/core/m_ban.cc index ce10a9d77..650f4f928 100644 --- a/modules/core/m_ban.cc +++ b/modules/core/m_ban.cc @@ -94,7 +94,7 @@ ms_ban(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p stype = "X-Line"; break; case 'R': - ntype = chan::is_name(parv[3]) ? CONF_RESV_CHANNEL : + ntype = chan::has_prefix(parv[3]) ? CONF_RESV_CHANNEL : CONF_RESV_NICK; stype = "RESV"; break; diff --git a/modules/core/m_join.cc b/modules/core/m_join.cc index 7c5943868..46b3b4caa 100644 --- a/modules/core/m_join.cc +++ b/modules/core/m_join.cc @@ -165,7 +165,7 @@ m_join(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p } /* check it begins with # or &, and local chans are disabled */ - else if(!chan::is_name(name) || + else if(!chan::has_prefix(name) || ( ConfigChannel.disable_local_channels && name[0] == '&')) { sendto_one_numeric(source_p, ERR_NOSUCHCHANNEL, @@ -404,7 +404,7 @@ ms_join(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_ if(parc < 4) return; - if(!chan::is_name(parv[2]) || !chan::check_channel_name(parv[2])) + if(!chan::has_prefix(parv[2]) || !chan::valid_name(parv[2])) return; /* joins for local channels cant happen. */ @@ -529,7 +529,7 @@ ms_sjoin(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source if(parc < 5) return; - if(!chan::is_name(parv[2]) || !chan::check_channel_name(parv[2])) + if(!chan::has_prefix(parv[2]) || !chan::valid_name(parv[2])) return; /* SJOIN's for local channels can't happen. */ diff --git a/modules/core/m_mode.cc b/modules/core/m_mode.cc index 1123f2a61..6c2e0d0d3 100644 --- a/modules/core/m_mode.cc +++ b/modules/core/m_mode.cc @@ -90,7 +90,7 @@ m_mode(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p return; } - if(!chan::check_channel_name(dest)) + if(!chan::valid_name(dest)) { sendto_one_numeric(source_p, ERR_BADCHANNAME, form_str(ERR_BADCHANNAME), parv[1]); return; @@ -157,7 +157,7 @@ ms_tmode(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source chan::membership *msptr; /* Now, try to find the channel in question */ - if(!rfc1459::is_chan_prefix(parv[2][0]) || !chan::check_channel_name(parv[2])) + if(!rfc1459::is_chan_prefix(parv[2][0]) || !chan::valid_name(parv[2])) { sendto_one_numeric(source_p, ERR_BADCHANNAME, form_str(ERR_BADCHANNAME), parv[2]); return; @@ -194,7 +194,7 @@ ms_mlock(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source chan::chan *chptr = NULL; /* Now, try to find the channel in question */ - if(!rfc1459::is_chan_prefix(parv[2][0]) || !chan::check_channel_name(parv[2])) + if(!rfc1459::is_chan_prefix(parv[2][0]) || !chan::valid_name(parv[2])) { sendto_one_numeric(source_p, ERR_BADCHANNAME, form_str(ERR_BADCHANNAME), parv[2]); return; @@ -271,7 +271,7 @@ ms_bmask(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source int mems; struct Client *fakesource_p; - if(!rfc1459::is_chan_prefix(parv[2][0]) || !chan::check_channel_name(parv[2])) + if(!rfc1459::is_chan_prefix(parv[2][0]) || !chan::valid_name(parv[2])) return; if((chptr = find_channel(parv[2])) == NULL) diff --git a/modules/m_invite.cc b/modules/m_invite.cc index 2e2f47165..6e378ece5 100644 --- a/modules/m_invite.cc +++ b/modules/m_invite.cc @@ -77,7 +77,7 @@ m_invite(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source return; } - if(chan::check_channel_name(parv[2]) == 0) + if(chan::valid_name(parv[2]) == 0) { sendto_one_numeric(source_p, ERR_BADCHANNAME, form_str(ERR_BADCHANNAME), diff --git a/modules/m_list.cc b/modules/m_list.cc index f89c2b61f..b91611d00 100644 --- a/modules/m_list.cc +++ b/modules/m_list.cc @@ -124,7 +124,7 @@ m_list(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p return; } - if (parc < 2 || chan::is_name(parv[1])) + if (parc < 2 || chan::has_prefix(parv[1])) { /* pace this due to the sheer traffic involved */ if (((last_used + ConfigFileEntry.pace_wait) > rb_current_time())) @@ -172,7 +172,7 @@ mo_list(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_ } /* Single channel. */ - if (args && chan::is_name(args)) + if (args && chan::has_prefix(args)) { safelist_channel_named(source_p, args, operspy); return; diff --git a/modules/m_names.cc b/modules/m_names.cc index 787fa3682..8e109cc61 100644 --- a/modules/m_names.cc +++ b/modules/m_names.cc @@ -60,7 +60,7 @@ m_names(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_ if((s = strchr(p, ','))) *s = '\0'; - if(!chan::check_channel_name(p)) + if(!chan::valid_name(p)) { sendto_one_numeric(source_p, ERR_BADCHANNAME, form_str(ERR_BADCHANNAME), diff --git a/modules/m_resv.cc b/modules/m_resv.cc index b23649985..1a08bc709 100644 --- a/modules/m_resv.cc +++ b/modules/m_resv.cc @@ -184,7 +184,7 @@ parse_resv(struct Client *source_p, const char *name, const char *reason, int te (temp_time > 0) ? SHARED_TRESV : SHARED_PRESV)) return; - if(chan::is_name(name)) + if(chan::has_prefix(name)) { if(hash_find_resv(name)) { @@ -490,7 +490,7 @@ remove_resv(struct Client *source_p, const char *name, int propagated) rb_dlink_node *ptr; time_t now; - if(chan::is_name(name)) + if(chan::has_prefix(name)) { if((aconf = hash_find_resv(name)) == NULL) { diff --git a/modules/m_testline.cc b/modules/m_testline.cc index 62075b6ba..68a215b61 100644 --- a/modules/m_testline.cc +++ b/modules/m_testline.cc @@ -68,7 +68,7 @@ mo_testline(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *sou mask = LOCAL_COPY(parv[1]); - if (chan::is_name(mask)) + if (chan::has_prefix(mask)) { resv_p = hash_find_resv(mask); if (resv_p != NULL) diff --git a/modules/m_who.cc b/modules/m_who.cc index 949a246c2..be9c38905 100644 --- a/modules/m_who.cc +++ b/modules/m_who.cc @@ -169,7 +169,7 @@ m_who(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, } /* '/who #some_channel' */ - if(chan::is_name(mask)) + if(chan::has_prefix(mask)) { /* List all users on a given channel */ chptr = find_channel(parv[1] + operspy);