2016-04-06 14:27:50 +02:00
|
|
|
/* modules/m_alias.c - main module for aliases
|
|
|
|
* Copyright (c) 2016 Elizabeth Myers <elizabeth@interlinked.me>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice is present in all copies.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2016-08-13 05:05:54 +02:00
|
|
|
using namespace ircd;
|
|
|
|
|
2016-04-06 14:27:50 +02:00
|
|
|
static const char alias_desc[] = "Provides the system for services aliases";
|
|
|
|
|
|
|
|
static int _modinit(void);
|
|
|
|
static void _moddeinit(void);
|
|
|
|
static int reload_aliases(hook_data *);
|
2016-08-23 02:37:07 +02:00
|
|
|
static void m_alias(struct MsgBuf *, client::client &, client::client &, int, const char **);
|
2016-04-06 14:27:50 +02:00
|
|
|
|
|
|
|
mapi_hfn_list_av1 alias_hfnlist[] = {
|
|
|
|
{ "rehash", (hookfn)reload_aliases },
|
|
|
|
{ NULL, NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
DECLARE_MODULE_AV2(alias, _modinit, _moddeinit, NULL, NULL, alias_hfnlist, NULL, NULL, alias_desc);
|
|
|
|
|
|
|
|
static rb_dlink_list alias_messages;
|
|
|
|
static const struct MessageEntry alias_msgtab[] =
|
|
|
|
{{m_alias, 2}, {m_alias, 2}, mg_ignore, mg_ignore, mg_ignore, {m_alias, 2}};
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
create_aliases(void)
|
|
|
|
{
|
2016-04-06 18:43:19 +02:00
|
|
|
s_assert(rb_dlink_list_length(&alias_messages) == 0);
|
2016-04-06 14:27:50 +02:00
|
|
|
|
2016-07-31 08:57:04 +02:00
|
|
|
for (auto it = alias_dict.begin(); it != alias_dict.end(); it++)
|
2016-04-06 14:27:50 +02:00
|
|
|
{
|
2016-07-31 08:57:04 +02:00
|
|
|
std::shared_ptr<alias_entry> alias = it->second;
|
|
|
|
struct Message *message = (Message *)rb_malloc(sizeof(*message) + alias->name.size() + 1);
|
2016-07-13 07:17:21 +02:00
|
|
|
char *cmd = (char*)message + sizeof(*message);
|
2016-04-06 14:27:50 +02:00
|
|
|
|
2016-04-24 01:09:12 +02:00
|
|
|
/* copy the alias name as it will be freed early on a rehash */
|
2016-07-31 08:57:04 +02:00
|
|
|
strcpy(cmd, alias->name.c_str());
|
2016-04-24 01:09:12 +02:00
|
|
|
message->cmd = cmd;
|
2016-04-06 14:27:50 +02:00
|
|
|
memcpy(message->handlers, alias_msgtab, sizeof(alias_msgtab));
|
|
|
|
|
|
|
|
mod_add_cmd(message);
|
|
|
|
rb_dlinkAddAlloc(message, &alias_messages);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
destroy_aliases(void)
|
|
|
|
{
|
|
|
|
rb_dlink_node *ptr, *nptr;
|
|
|
|
|
|
|
|
RB_DLINK_FOREACH_SAFE(ptr, nptr, alias_messages.head)
|
|
|
|
{
|
|
|
|
mod_del_cmd((struct Message *)ptr->data);
|
|
|
|
rb_free(ptr->data);
|
|
|
|
rb_dlinkDestroy(ptr, &alias_messages);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_modinit(void)
|
|
|
|
{
|
|
|
|
create_aliases();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_moddeinit(void)
|
|
|
|
{
|
|
|
|
destroy_aliases();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-04-06 14:33:36 +02:00
|
|
|
reload_aliases(hook_data *data)
|
2016-04-06 14:27:50 +02:00
|
|
|
{
|
|
|
|
destroy_aliases(); /* Clear old aliases */
|
|
|
|
create_aliases();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The below was mostly taken from the old do_alias */
|
|
|
|
static void
|
2016-08-23 02:37:07 +02:00
|
|
|
m_alias(struct MsgBuf *msgbuf, client::client &client, client::client &source, int parc, const char **parv)
|
2016-04-06 14:27:50 +02:00
|
|
|
{
|
2016-08-22 03:57:43 +02:00
|
|
|
client::client *target_p;
|
2016-07-31 08:57:04 +02:00
|
|
|
std::shared_ptr<alias_entry> aptr = alias_dict[msgbuf->cmd];
|
2016-07-31 10:24:12 +02:00
|
|
|
char *str;
|
2016-04-06 14:27:50 +02:00
|
|
|
|
|
|
|
if(aptr == NULL)
|
|
|
|
{
|
2016-04-06 14:33:36 +02:00
|
|
|
/* This shouldn't happen... */
|
2016-08-23 04:33:36 +02:00
|
|
|
if(is_person(client))
|
2016-08-23 02:37:07 +02:00
|
|
|
sendto_one(&client, form_str(ERR_UNKNOWNCOMMAND),
|
|
|
|
me.name, client.name, msgbuf->cmd);
|
2016-04-06 14:27:50 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-23 04:33:36 +02:00
|
|
|
if(!is_flood_done(client) && client.localClient->receiveM > 20)
|
2016-08-23 02:37:07 +02:00
|
|
|
flood_endgrace(&client);
|
2016-04-06 14:27:50 +02:00
|
|
|
|
2016-07-31 10:24:12 +02:00
|
|
|
auto pos = aptr->target.find('@');
|
|
|
|
if(pos != std::string::npos)
|
2016-04-06 14:27:50 +02:00
|
|
|
{
|
|
|
|
/* user@server */
|
2016-07-31 10:24:12 +02:00
|
|
|
target_p = find_server(NULL, aptr->target.substr(pos + 1).c_str());
|
2016-08-23 04:33:36 +02:00
|
|
|
if(target_p != NULL && is_me(*target_p))
|
2016-04-06 14:27:50 +02:00
|
|
|
target_p = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* nick, must be +S */
|
2016-08-22 03:57:43 +02:00
|
|
|
target_p = client::find_named_person(aptr->target.c_str());
|
2016-08-24 00:25:09 +02:00
|
|
|
if(target_p != NULL && !is(*target_p, umode::SERVICE))
|
2016-04-06 14:27:50 +02:00
|
|
|
target_p = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(target_p == NULL)
|
|
|
|
{
|
2016-08-23 02:37:07 +02:00
|
|
|
sendto_one_numeric(&client, ERR_SERVICESDOWN, form_str(ERR_SERVICESDOWN), aptr->target.c_str());
|
2016-04-06 14:27:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-12 20:44:45 +02:00
|
|
|
str = reconstruct_parv(parc - 1, &parv[1]);
|
2016-05-01 10:43:55 +02:00
|
|
|
if(EmptyString(str))
|
2016-04-06 14:27:50 +02:00
|
|
|
{
|
2016-08-23 02:37:07 +02:00
|
|
|
sendto_one(&client, form_str(ERR_NOTEXTTOSEND), me.name, target_p->name);
|
2016-04-06 14:27:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sendto_one(target_p, ":%s PRIVMSG %s :%s",
|
2016-08-23 02:37:07 +02:00
|
|
|
get_id(&client, target_p),
|
2016-07-31 10:24:12 +02:00
|
|
|
pos != std::string::npos ? aptr->target.c_str() : get_id(target_p, target_p),
|
2016-05-01 10:43:55 +02:00
|
|
|
str);
|
2016-04-06 14:27:50 +02:00
|
|
|
}
|