]> jfr.im git - solanum.git/blame - modules/um_callerid.c
Update news
[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
ace0c865 83#define IsSetStrictCallerID(c) ((c->umodes & user_modes['g']) == user_modes['g'])
7b60cca0 84#define IsSetRelaxedCallerID(c) ((c->umodes & user_modes['G']) == user_modes['G'])
ace0c865 85#define IsSetAnyCallerID(c) (IsSetStrictCallerID(c) || IsSetRelaxedCallerID(c))
921b508b
AC
86
87static const char um_callerid_desc[] =
4d07382c 88 "Provides usermodes +g and +G which restrict messages from unauthorized users.";
921b508b 89
7b60cca0
AC
90static bool
91has_common_channel(struct Client *source_p, struct Client *target_p)
92{
25f673bf
EK
93 rb_dlink_node *ps, *pt;
94 struct membership *ms, *mt;
95 struct Channel *chptr;
7b60cca0 96
25f673bf 97 ITER_COMM_CHANNELS(ps, pt, source_p->user->channel.head, target_p->user->channel.head, ms, mt, chptr)
7b60cca0 98 {
25f673bf 99 if (ms != NULL && mt != NULL)
5d4cce6c 100 return true;
7b60cca0
AC
101 }
102
5d4cce6c 103 return false;
7b60cca0
AC
104}
105
921b508b
AC
106static bool
107allow_message(struct Client *source_p, struct Client *target_p)
108{
109 if (!MyClient(target_p))
110 return true;
111
ace0c865 112 if (!IsSetAnyCallerID(target_p))
921b508b
AC
113 return true;
114
ace0c865 115 if (IsSetRelaxedCallerID(target_p) && has_common_channel(source_p, target_p) && !IsSetStrictCallerID(target_p))
7b60cca0
AC
116 return true;
117
921b508b
AC
118 if (IsServer(source_p))
119 return true;
120
121 /* XXX: controversial? allow opers to send through +g */
d4f7eb4c 122 if (IsOperGeneral(source_p))
921b508b
AC
123 return true;
124
125 if (accept_message(source_p, target_p))
126 return true;
127
128 return false;
129}
130
131static void
132send_callerid_notice(enum message_type msgtype, struct Client *source_p, struct Client *target_p)
133{
134 if (!MyClient(target_p))
135 return;
136
137 if (msgtype == MESSAGE_TYPE_NOTICE)
138 return;
139
140 sendto_one_numeric(source_p, ERR_TARGUMODEG, form_str(ERR_TARGUMODEG),
ace0c865 141 target_p->name, IsSetStrictCallerID(target_p) ? "+g" : "+G");
921b508b
AC
142
143 if ((target_p->localClient->last_caller_id_time + ConfigFileEntry.caller_id_wait) < rb_current_time())
144 {
145 sendto_one_numeric(source_p, RPL_TARGNOTIFY, form_str(RPL_TARGNOTIFY),
146 target_p->name);
147
148 sendto_one(target_p, form_str(RPL_UMODEGMSG),
149 me.name, target_p->name, source_p->name,
ace0c865 150 source_p->username, source_p->host, IsSetStrictCallerID(target_p) ? "+g" : "+G");
921b508b
AC
151
152 target_p->localClient->last_caller_id_time = rb_current_time();
153 }
154}
155
156static bool
157add_callerid_accept_for_source(enum message_type msgtype, struct Client *source_p, struct Client *target_p)
158{
4abf8eb2
AC
159 /* only do this on source_p's server */
160 if (!MyClient(source_p))
4841ad61
AC
161 return true;
162
921b508b
AC
163 /*
164 * XXX: Controversial? Allow target users to send replies
165 * through a +g. Rationale is that people can presently use +g
166 * as a way to taunt users, e.g. harass them and hide behind +g
167 * as a way of griefing. --nenolod
168 */
169 if(msgtype != MESSAGE_TYPE_NOTICE &&
ace0c865 170 IsSetAnyCallerID(source_p) &&
4841ad61 171 !accept_message(target_p, source_p) &&
d4f7eb4c 172 !IsOperGeneral(target_p))
921b508b
AC
173 {
174 if(rb_dlink_list_length(&source_p->localClient->allow_list) <
175 (unsigned long)ConfigFileEntry.max_accept)
176 {
177 rb_dlinkAddAlloc(target_p, &source_p->localClient->allow_list);
178 rb_dlinkAddAlloc(source_p, &target_p->on_allow_list);
179 }
180 else
181 {
182 sendto_one_numeric(source_p, ERR_OWNMODE,
183 form_str(ERR_OWNMODE),
8ad8fd34 184 target_p->name, IsSetStrictCallerID(target_p) ? "+g" : "+G");
921b508b
AC
185 return false;
186 }
187 }
188
189 return true;
190}
191
192static void
db807634 193h_hdl_invite(void *vdata)
921b508b
AC
194{
195 hook_data_channel_approval *data = vdata;
196 struct Client *source_p = data->client;
197 struct Client *target_p = data->target;
c6edf654 198 static char errorbuf[BUFSIZE];
921b508b 199
6558648d
AC
200 if (data->approved)
201 return;
202
921b508b
AC
203 if (!add_callerid_accept_for_source(MESSAGE_TYPE_PRIVMSG, source_p, target_p))
204 {
205 data->approved = ERR_TARGUMODEG;
206 return;
207 }
208
209 if (allow_message(source_p, target_p))
210 return;
211
c6edf654 212 snprintf(errorbuf, sizeof errorbuf, form_str(ERR_TARGUMODEG),
c6e584dd 213 target_p->name, IsSetStrictCallerID(target_p) ? "+g" : "+G");
921b508b
AC
214
215 data->approved = ERR_TARGUMODEG;
c6edf654 216 data->error = errorbuf;
921b508b
AC
217}
218
219static void
220h_hdl_privmsg_user(void *vdata)
221{
222 hook_data_privmsg_user *data = vdata;
223 enum message_type msgtype = data->msgtype;
224 struct Client *source_p = data->source_p;
225 struct Client *target_p = data->target_p;
226
6558648d
AC
227 if (data->approved)
228 return;
229
921b508b
AC
230 if (!add_callerid_accept_for_source(msgtype, source_p, target_p))
231 {
232 data->approved = ERR_TARGUMODEG;
233 return;
234 }
235
236 if (allow_message(source_p, target_p))
237 return;
238
239 send_callerid_notice(msgtype, source_p, target_p);
240
241 data->approved = ERR_TARGUMODEG;
242}
243
244static mapi_hfn_list_av1 um_callerid_hfnlist[] = {
db807634 245 { "invite", h_hdl_invite },
921b508b
AC
246 { "privmsg_user", h_hdl_privmsg_user },
247 { NULL, NULL }
248};
249
250DECLARE_MODULE_AV2(um_callerid, um_callerid_modinit, um_callerid_moddeinit,
251 NULL, NULL, um_callerid_hfnlist, NULL, NULL, um_callerid_desc);