]> jfr.im git - solanum.git/blame - modules/m_alias.c
ircd: check_server: don't allow a connection if that would exceed the class limit
[solanum.git] / modules / m_alias.c
CommitLineData
b663a807
EM
1/* modules/m_alias.c - main module for aliases
2 * Copyright (c) 2016 Elizabeth Myers <elizabeth@interlinked.me>
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice is present in all copies.
7 *
8 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
9 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
11 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
12 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
13 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
14 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
15 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
16 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
17 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
18 * POSSIBILITY OF SUCH DAMAGE.
19 */
20
21#include "stdinc.h"
22#include "client.h"
23#include "parse.h"
24#include "msg.h"
25#include "modules.h"
26#include "s_conf.h"
27#include "s_serv.h"
28#include "hash.h"
29#include "ircd.h"
30#include "match.h"
31#include "numeric.h"
32#include "send.h"
33#include "packet.h"
34
35static const char alias_desc[] = "Provides the system for services aliases";
36
37static int _modinit(void);
38static void _moddeinit(void);
39static int reload_aliases(hook_data *);
40static void m_alias(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
41
42mapi_hfn_list_av1 alias_hfnlist[] = {
43 { "rehash", (hookfn)reload_aliases },
44 { NULL, NULL },
45};
46
47DECLARE_MODULE_AV2(alias, _modinit, _moddeinit, NULL, NULL, alias_hfnlist, NULL, NULL, alias_desc);
48
49static rb_dlink_list alias_messages;
50static const struct MessageEntry alias_msgtab[] =
51 {{m_alias, 2}, {m_alias, 2}, mg_ignore, mg_ignore, mg_ignore, {m_alias, 2}};
52
53static inline void
54create_aliases(void)
55{
56 rb_dictionary_iter iter;
57 struct alias_entry *alias;
58
9620c6d6 59 s_assert(rb_dlink_list_length(&alias_messages) == 0);
b663a807
EM
60
61 RB_DICTIONARY_FOREACH(alias, &iter, alias_dict)
62 {
00039dcd 63 struct Message *message = rb_malloc(sizeof(*message) + strlen(alias->name) + 1);
d8f0b5d7 64 char *cmd = (char*)message + sizeof(*message);
b663a807 65
00039dcd
SA
66 /* copy the alias name as it will be freed early on a rehash */
67 strcpy(cmd, alias->name);
68 message->cmd = cmd;
b663a807
EM
69 memcpy(message->handlers, alias_msgtab, sizeof(alias_msgtab));
70
71 mod_add_cmd(message);
72 rb_dlinkAddAlloc(message, &alias_messages);
73 }
74}
75
76static inline void
77destroy_aliases(void)
78{
79 rb_dlink_node *ptr, *nptr;
80
81 RB_DLINK_FOREACH_SAFE(ptr, nptr, alias_messages.head)
82 {
83 mod_del_cmd((struct Message *)ptr->data);
84 rb_free(ptr->data);
85 rb_dlinkDestroy(ptr, &alias_messages);
86 }
87}
88
89static int
90_modinit(void)
91{
92 create_aliases();
93 return 0;
94}
95
96static void
97_moddeinit(void)
98{
99 destroy_aliases();
100}
101
102static int
dd598516 103reload_aliases(hook_data *data)
b663a807
EM
104{
105 destroy_aliases(); /* Clear old aliases */
106 create_aliases();
107 return 0;
108}
109
110/* The below was mostly taken from the old do_alias */
111static void
112m_alias(struct MsgBuf *msgbuf, struct Client *client_p, struct Client *source_p, int parc, const char **parv)
113{
114 struct Client *target_p;
115 struct alias_entry *aptr = rb_dictionary_retrieve(alias_dict, msgbuf->cmd);
f4d828ef 116 char *p, *str;
b663a807
EM
117
118 if(aptr == NULL)
119 {
dd598516
EM
120 /* This shouldn't happen... */
121 if(IsPerson(client_p))
122 sendto_one(client_p, form_str(ERR_UNKNOWNCOMMAND),
123 me.name, client_p->name, msgbuf->cmd);
b663a807
EM
124
125 return;
126 }
b663a807 127
dd598516
EM
128 if(!IsFloodDone(client_p) && client_p->localClient->receiveM > 20)
129 flood_endgrace(client_p);
b663a807
EM
130
131 p = strchr(aptr->target, '@');
132 if(p != NULL)
133 {
134 /* user@server */
135 target_p = find_server(NULL, p + 1);
136 if(target_p != NULL && IsMe(target_p))
137 target_p = NULL;
138 }
139 else
140 {
141 /* nick, must be +S */
142 target_p = find_named_person(aptr->target);
143 if(target_p != NULL && !IsService(target_p))
144 target_p = NULL;
145 }
146
147 if(target_p == NULL)
148 {
dd598516 149 sendto_one_numeric(client_p, ERR_SERVICESDOWN, form_str(ERR_SERVICESDOWN), aptr->target);
b663a807
EM
150 return;
151 }
152
4ad9738d 153 str = reconstruct_parv(parc - 1, &parv[1]);
f4d828ef 154 if(EmptyString(str))
b663a807 155 {
dd598516 156 sendto_one(client_p, form_str(ERR_NOTEXTTOSEND), me.name, target_p->name);
b663a807
EM
157 return;
158 }
159
b663a807 160 sendto_one(target_p, ":%s PRIVMSG %s :%s",
dd598516 161 get_id(client_p, target_p),
b663a807 162 p != NULL ? aptr->target : get_id(target_p, target_p),
f4d828ef 163 str);
b663a807 164}