]> jfr.im git - solanum.git/blame - modules/um_callerid.c
callerid: actually the guard should be for source_p
[solanum.git] / modules / um_callerid.c
CommitLineData
921b508b
AC
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"
fdff78f8 45#include "logger.h"
921b508b
AC
46
47static int
48um_callerid_modinit(void)
49{
50 user_modes['g'] = find_umode_slot();
fdff78f8
AC
51 if (!user_modes['g'])
52 {
53 ierror("um_callerid: unable to allocate usermode slot for +g; unloading module.");
54 return -1;
55 }
56
7b60cca0 57 user_modes['G'] = find_umode_slot();
fdff78f8
AC
58 if (!user_modes['G'])
59 {
915bb561
AC
60 user_modes['g'] = 0;
61
fdff78f8
AC
62 ierror("um_callerid: unable to allocate usermode slot for +G; unloading module.");
63 return -1;
64 }
65
921b508b
AC
66 construct_umodebuf();
67
3ed61fba 68 add_isupport("CALLERID", isupport_umode, "g");
921b508b
AC
69
70 return 0;
71}
72
73static void
74um_callerid_moddeinit(void)
75{
76 user_modes['g'] = 0;
7b60cca0 77 user_modes['G'] = 0;
921b508b
AC
78 construct_umodebuf();
79
80 delete_isupport("CALLERID");
81}
82
83#define IsSetCallerID(c) ((c->umodes & user_modes['g']) == user_modes['g'])
7b60cca0 84#define IsSetRelaxedCallerID(c) ((c->umodes & user_modes['G']) == user_modes['G'])
921b508b
AC
85
86static const char um_callerid_desc[] =
4d07382c 87 "Provides usermodes +g and +G which restrict messages from unauthorized users.";
921b508b 88
7b60cca0
AC
89static bool
90has_common_channel(struct Client *source_p, struct Client *target_p)
91{
92 rb_dlink_node *ptr;
93
94 RB_DLINK_FOREACH(ptr, source_p->user->channel.head)
95 {
96 struct membership *msptr = ptr->data;
97 if (IsMember(target_p, msptr->chptr))
5d4cce6c 98 return true;
7b60cca0
AC
99 }
100
5d4cce6c 101 return false;
7b60cca0
AC
102}
103
921b508b
AC
104static bool
105allow_message(struct Client *source_p, struct Client *target_p)
106{
107 if (!MyClient(target_p))
108 return true;
109
110 if (!IsSetCallerID(target_p))
111 return true;
112
7b60cca0
AC
113 if (IsSetRelaxedCallerID(target_p) && has_common_channel(source_p, target_p))
114 return true;
115
921b508b
AC
116 if (IsServer(source_p))
117 return true;
118
119 /* XXX: controversial? allow opers to send through +g */
120 if (IsOper(source_p))
121 return true;
122
123 if (accept_message(source_p, target_p))
124 return true;
125
126 return false;
127}
128
129static void
130send_callerid_notice(enum message_type msgtype, struct Client *source_p, struct Client *target_p)
131{
132 if (!MyClient(target_p))
133 return;
134
135 if (msgtype == MESSAGE_TYPE_NOTICE)
136 return;
137
138 sendto_one_numeric(source_p, ERR_TARGUMODEG, form_str(ERR_TARGUMODEG),
139 target_p->name);
140
141 if ((target_p->localClient->last_caller_id_time + ConfigFileEntry.caller_id_wait) < rb_current_time())
142 {
143 sendto_one_numeric(source_p, RPL_TARGNOTIFY, form_str(RPL_TARGNOTIFY),
144 target_p->name);
145
146 sendto_one(target_p, form_str(RPL_UMODEGMSG),
147 me.name, target_p->name, source_p->name,
148 source_p->username, source_p->host);
149
150 target_p->localClient->last_caller_id_time = rb_current_time();
151 }
152}
153
154static bool
155add_callerid_accept_for_source(enum message_type msgtype, struct Client *source_p, struct Client *target_p)
156{
4abf8eb2
AC
157 /* only do this on source_p's server */
158 if (!MyClient(source_p))
4841ad61
AC
159 return true;
160
921b508b
AC
161 /*
162 * XXX: Controversial? Allow target users to send replies
163 * through a +g. Rationale is that people can presently use +g
164 * as a way to taunt users, e.g. harass them and hide behind +g
165 * as a way of griefing. --nenolod
166 */
167 if(msgtype != MESSAGE_TYPE_NOTICE &&
4841ad61
AC
168 IsSetCallerID(source_p) &&
169 !accept_message(target_p, source_p) &&
170 !IsOper(target_p))
921b508b
AC
171 {
172 if(rb_dlink_list_length(&source_p->localClient->allow_list) <
173 (unsigned long)ConfigFileEntry.max_accept)
174 {
175 rb_dlinkAddAlloc(target_p, &source_p->localClient->allow_list);
176 rb_dlinkAddAlloc(source_p, &target_p->on_allow_list);
177 }
178 else
179 {
180 sendto_one_numeric(source_p, ERR_OWNMODE,
181 form_str(ERR_OWNMODE),
182 target_p->name, "+g");
183 return false;
184 }
185 }
186
187 return true;
188}
189
190static void
db807634 191h_hdl_invite(void *vdata)
921b508b
AC
192{
193 hook_data_channel_approval *data = vdata;
194 struct Client *source_p = data->client;
195 struct Client *target_p = data->target;
c6edf654 196 static char errorbuf[BUFSIZE];
921b508b 197
6558648d
AC
198 if (data->approved)
199 return;
200
921b508b
AC
201 if (!add_callerid_accept_for_source(MESSAGE_TYPE_PRIVMSG, source_p, target_p))
202 {
203 data->approved = ERR_TARGUMODEG;
204 return;
205 }
206
207 if (allow_message(source_p, target_p))
208 return;
209
c6edf654
AC
210 snprintf(errorbuf, sizeof errorbuf, form_str(ERR_TARGUMODEG),
211 target_p->name);
921b508b
AC
212
213 data->approved = ERR_TARGUMODEG;
c6edf654 214 data->error = errorbuf;
921b508b
AC
215}
216
217static void
218h_hdl_privmsg_user(void *vdata)
219{
220 hook_data_privmsg_user *data = vdata;
221 enum message_type msgtype = data->msgtype;
222 struct Client *source_p = data->source_p;
223 struct Client *target_p = data->target_p;
224
6558648d
AC
225 if (data->approved)
226 return;
227
921b508b
AC
228 if (!add_callerid_accept_for_source(msgtype, source_p, target_p))
229 {
230 data->approved = ERR_TARGUMODEG;
231 return;
232 }
233
234 if (allow_message(source_p, target_p))
235 return;
236
237 send_callerid_notice(msgtype, source_p, target_p);
238
239 data->approved = ERR_TARGUMODEG;
240}
241
242static mapi_hfn_list_av1 um_callerid_hfnlist[] = {
db807634 243 { "invite", h_hdl_invite },
921b508b
AC
244 { "privmsg_user", h_hdl_privmsg_user },
245 { NULL, NULL }
246};
247
248DECLARE_MODULE_AV2(um_callerid, um_callerid_modinit, um_callerid_moddeinit,
249 NULL, NULL, um_callerid_hfnlist, NULL, NULL, um_callerid_desc);