]> jfr.im git - solanum.git/blob - modules/um_callerid.c
callerid: catch find_umode_slot() failures
[solanum.git] / modules / um_callerid.c
1 /*
2 * modules/um_callerid.c
3 * Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
21 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include "stdinc.h"
31 #include "modules.h"
32 #include "hook.h"
33 #include "client.h"
34 #include "ircd.h"
35 #include "send.h"
36 #include "hash.h"
37 #include "s_conf.h"
38 #include "s_user.h"
39 #include "s_serv.h"
40 #include "numeric.h"
41 #include "privilege.h"
42 #include "s_newconf.h"
43 #include "hook.h"
44 #include "supported.h"
45 #include "logger.h"
46
47 static int
48 um_callerid_modinit(void)
49 {
50 user_modes['g'] = find_umode_slot();
51 if (!user_modes['g'])
52 {
53 ierror("um_callerid: unable to allocate usermode slot for +g; unloading module.");
54 return -1;
55 }
56
57 user_modes['G'] = find_umode_slot();
58 if (!user_modes['G'])
59 {
60 ierror("um_callerid: unable to allocate usermode slot for +G; unloading module.");
61 return -1;
62 }
63
64 construct_umodebuf();
65
66 add_isupport("CALLERID", isupport_string, "g");
67
68 return 0;
69 }
70
71 static void
72 um_callerid_moddeinit(void)
73 {
74 user_modes['g'] = 0;
75 user_modes['G'] = 0;
76 construct_umodebuf();
77
78 delete_isupport("CALLERID");
79 }
80
81 #define IsSetCallerID(c) ((c->umodes & user_modes['g']) == user_modes['g'])
82 #define IsSetRelaxedCallerID(c) ((c->umodes & user_modes['G']) == user_modes['G'])
83
84 static const char um_callerid_desc[] =
85 "Provides usermode +g which restricts messages from unauthorized users.";
86
87 static bool
88 has_common_channel(struct Client *source_p, struct Client *target_p)
89 {
90 rb_dlink_node *ptr;
91
92 RB_DLINK_FOREACH(ptr, source_p->user->channel.head)
93 {
94 struct membership *msptr = ptr->data;
95 if (IsMember(target_p, msptr->chptr))
96 return msptr->chptr;
97 }
98
99 return NULL;
100 }
101
102 static bool
103 allow_message(struct Client *source_p, struct Client *target_p)
104 {
105 if (!MyClient(target_p))
106 return true;
107
108 if (!IsSetCallerID(target_p))
109 return true;
110
111 if (IsSetRelaxedCallerID(target_p) && has_common_channel(source_p, target_p))
112 return true;
113
114 if (IsServer(source_p))
115 return true;
116
117 /* XXX: controversial? allow opers to send through +g */
118 if (IsOper(source_p))
119 return true;
120
121 if (accept_message(source_p, target_p))
122 return true;
123
124 return false;
125 }
126
127 static void
128 send_callerid_notice(enum message_type msgtype, struct Client *source_p, struct Client *target_p)
129 {
130 if (!MyClient(target_p))
131 return;
132
133 if (msgtype == MESSAGE_TYPE_NOTICE)
134 return;
135
136 sendto_one_numeric(source_p, ERR_TARGUMODEG, form_str(ERR_TARGUMODEG),
137 target_p->name);
138
139 if ((target_p->localClient->last_caller_id_time + ConfigFileEntry.caller_id_wait) < rb_current_time())
140 {
141 sendto_one_numeric(source_p, RPL_TARGNOTIFY, form_str(RPL_TARGNOTIFY),
142 target_p->name);
143
144 sendto_one(target_p, form_str(RPL_UMODEGMSG),
145 me.name, target_p->name, source_p->name,
146 source_p->username, source_p->host);
147
148 target_p->localClient->last_caller_id_time = rb_current_time();
149 }
150 }
151
152 static bool
153 add_callerid_accept_for_source(enum message_type msgtype, struct Client *source_p, struct Client *target_p)
154 {
155 /*
156 * XXX: Controversial? Allow target users to send replies
157 * through a +g. Rationale is that people can presently use +g
158 * as a way to taunt users, e.g. harass them and hide behind +g
159 * as a way of griefing. --nenolod
160 */
161 if(msgtype != MESSAGE_TYPE_NOTICE &&
162 IsSetCallerID(source_p) &&
163 !accept_message(target_p, source_p) &&
164 !IsOper(target_p))
165 {
166 if(rb_dlink_list_length(&source_p->localClient->allow_list) <
167 (unsigned long)ConfigFileEntry.max_accept)
168 {
169 rb_dlinkAddAlloc(target_p, &source_p->localClient->allow_list);
170 rb_dlinkAddAlloc(source_p, &target_p->on_allow_list);
171 }
172 else
173 {
174 sendto_one_numeric(source_p, ERR_OWNMODE,
175 form_str(ERR_OWNMODE),
176 target_p->name, "+g");
177 return false;
178 }
179 }
180
181 return true;
182 }
183
184 static void
185 h_hdl_invite(void *vdata)
186 {
187 hook_data_channel_approval *data = vdata;
188 struct Client *source_p = data->client;
189 struct Client *target_p = data->target;
190
191 if (data->approved)
192 return;
193
194 if (!add_callerid_accept_for_source(MESSAGE_TYPE_PRIVMSG, source_p, target_p))
195 {
196 data->approved = ERR_TARGUMODEG;
197 return;
198 }
199
200 if (allow_message(source_p, target_p))
201 return;
202
203 send_callerid_notice(MESSAGE_TYPE_PRIVMSG, source_p, target_p);
204
205 data->approved = ERR_TARGUMODEG;
206 }
207
208 static void
209 h_hdl_privmsg_user(void *vdata)
210 {
211 hook_data_privmsg_user *data = vdata;
212 enum message_type msgtype = data->msgtype;
213 struct Client *source_p = data->source_p;
214 struct Client *target_p = data->target_p;
215
216 if (data->approved)
217 return;
218
219 if (!add_callerid_accept_for_source(msgtype, source_p, target_p))
220 {
221 data->approved = ERR_TARGUMODEG;
222 return;
223 }
224
225 if (allow_message(source_p, target_p))
226 return;
227
228 send_callerid_notice(msgtype, source_p, target_p);
229
230 data->approved = ERR_TARGUMODEG;
231 }
232
233 static mapi_hfn_list_av1 um_callerid_hfnlist[] = {
234 { "invite", h_hdl_invite },
235 { "privmsg_user", h_hdl_privmsg_user },
236 { NULL, NULL }
237 };
238
239 DECLARE_MODULE_AV2(um_callerid, um_callerid_modinit, um_callerid_moddeinit,
240 NULL, NULL, um_callerid_hfnlist, NULL, NULL, um_callerid_desc);