2 * modules/um_callerid.c
3 * Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
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.
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.
41 #include "privilege.h"
42 #include "s_newconf.h"
44 #include "supported.h"
47 #define IsSetStrictCallerID(c) ((c->umodes & user_modes['g']) == user_modes['g'])
48 #define IsSetRelaxedCallerID(c) ((c->umodes & user_modes['G']) == user_modes['G'])
49 #define IsSetAnyCallerID(c) (IsSetStrictCallerID(c) || IsSetRelaxedCallerID(c))
50 #define IsSetTalkThroughCallerID(c) ((c->umodes & user_modes['M']) == user_modes['M'])
52 static const char um_callerid_desc
[] =
53 "Provides usermodes +g and +G which restrict messages from unauthorized users.";
56 struct CallerIDOverrideSession
{
59 struct Client
*client
;
63 static rb_dlink_list callerid_overriding_opers
= { NULL
, NULL
, 0 };
64 struct ev_entry
*expire_callerid_override_deadlines_ev
= NULL
;
67 update_session_deadline(struct Client
*source_p
)
69 struct CallerIDOverrideSession
*session_p
= NULL
;
72 RB_DLINK_FOREACH(n
, callerid_overriding_opers
.head
)
74 struct CallerIDOverrideSession
*s
= n
->data
;
76 if (s
->client
== source_p
)
83 if (session_p
!= NULL
)
85 rb_dlinkDelete(&session_p
->node
, &callerid_overriding_opers
);
89 session_p
= rb_malloc(sizeof(struct CallerIDOverrideSession
));
90 session_p
->client
= source_p
;
93 session_p
->deadline
= rb_current_time() + 1800;
95 rb_dlinkAddTail(session_p
, &session_p
->node
, &callerid_overriding_opers
);
99 expire_callerid_override_deadlines(void *unused
)
101 rb_dlink_node
*n
, *tn
;
103 RB_DLINK_FOREACH_SAFE(n
, tn
, callerid_overriding_opers
.head
)
105 struct CallerIDOverrideSession
*session_p
= n
->data
;
107 if (session_p
->deadline
>= rb_current_time())
113 const char *parv
[4] = {session_p
->client
->name
, session_p
->client
->name
, "-M", NULL
};
114 user_mode(session_p
->client
, session_p
->client
, 3, parv
);
120 allow_message(struct Client
*source_p
, struct Client
*target_p
)
122 if (!MyClient(target_p
))
125 if (!IsSetAnyCallerID(target_p
))
128 if (!IsPerson(source_p
))
131 if (IsSetRelaxedCallerID(target_p
) &&
132 !IsSetStrictCallerID(target_p
) &&
133 has_common_channel(source_p
, target_p
))
136 /* XXX: controversial? allow opers to send through +g */
137 if (IsSetTalkThroughCallerID(source_p
) || MayHavePrivilege(source_p
, "oper:always_message"))
140 if (accept_message(source_p
, target_p
))
147 send_callerid_notice(enum message_type msgtype
, struct Client
*source_p
, struct Client
*target_p
)
149 if (!MyClient(target_p
))
152 if (msgtype
== MESSAGE_TYPE_NOTICE
)
155 sendto_one_numeric(source_p
, ERR_TARGUMODEG
, form_str(ERR_TARGUMODEG
),
156 target_p
->name
, IsSetStrictCallerID(target_p
) ? "+g" : "+G");
158 if ((target_p
->localClient
->last_caller_id_time
+ ConfigFileEntry
.caller_id_wait
) < rb_current_time())
160 sendto_one_numeric(source_p
, RPL_TARGNOTIFY
, form_str(RPL_TARGNOTIFY
),
163 sendto_one(target_p
, form_str(RPL_UMODEGMSG
),
164 me
.name
, target_p
->name
, source_p
->name
,
165 source_p
->username
, source_p
->host
, IsSetStrictCallerID(target_p
) ? "+g" : "+G");
167 target_p
->localClient
->last_caller_id_time
= rb_current_time();
172 add_callerid_accept_for_source(enum message_type msgtype
, struct Client
*source_p
, struct Client
*target_p
)
174 /* only do this on source_p's server */
175 if (!MyClient(source_p
))
179 * XXX: Controversial? Allow target users to send replies
180 * through a +g. Rationale is that people can presently use +g
181 * as a way to taunt users, e.g. harass them and hide behind +g
182 * as a way of griefing. --nenolod
184 if(msgtype
!= MESSAGE_TYPE_NOTICE
&&
185 IsSetAnyCallerID(source_p
) &&
186 !accept_message(target_p
, source_p
) &&
187 !IsOperGeneral(target_p
))
189 if(rb_dlink_list_length(&source_p
->localClient
->allow_list
) <
190 (unsigned long)ConfigFileEntry
.max_accept
)
192 rb_dlinkAddAlloc(target_p
, &source_p
->localClient
->allow_list
);
193 rb_dlinkAddAlloc(source_p
, &target_p
->on_allow_list
);
197 sendto_one_numeric(source_p
, ERR_OWNMODE
,
198 form_str(ERR_OWNMODE
),
199 target_p
->name
, IsSetStrictCallerID(target_p
) ? "+g" : "+G");
208 h_hdl_invite(void *vdata
)
210 hook_data_channel_approval
*data
= vdata
;
211 struct Client
*source_p
= data
->client
;
212 struct Client
*target_p
= data
->target
;
213 static char errorbuf
[BUFSIZE
];
218 if (!add_callerid_accept_for_source(MESSAGE_TYPE_PRIVMSG
, source_p
, target_p
))
220 data
->approved
= ERR_TARGUMODEG
;
224 if (allow_message(source_p
, target_p
))
227 snprintf(errorbuf
, sizeof errorbuf
, form_str(ERR_TARGUMODEG
),
228 target_p
->name
, IsSetStrictCallerID(target_p
) ? "+g" : "+G");
230 data
->approved
= ERR_TARGUMODEG
;
231 data
->error
= errorbuf
;
235 h_hdl_privmsg_user(void *vdata
)
237 hook_data_privmsg_user
*data
= vdata
;
238 enum message_type msgtype
= data
->msgtype
;
239 struct Client
*source_p
= data
->source_p
;
240 struct Client
*target_p
= data
->target_p
;
245 if (!add_callerid_accept_for_source(msgtype
, source_p
, target_p
))
247 data
->approved
= ERR_TARGUMODEG
;
251 if (allow_message(source_p
, target_p
))
254 send_callerid_notice(msgtype
, source_p
, target_p
);
256 data
->approved
= ERR_TARGUMODEG
;
260 check_umode_change(void *vdata
)
262 hook_data_umode_changed
*data
= (hook_data_umode_changed
*)vdata
;
263 bool changed
= false;
264 struct Client
*source_p
= data
->client
;
266 if (!MyClient(source_p
))
269 if (data
->oldumodes
& UMODE_OPER
&& !IsOper(source_p
))
270 source_p
->umodes
&= ~user_modes
['M'];
272 changed
= ((data
->oldumodes
^ source_p
->umodes
) & user_modes
['M']);
274 if (changed
&& source_p
->umodes
& user_modes
['M'])
276 if (!HasPrivilege(source_p
, "oper:message"))
278 sendto_one_notice(source_p
, ":*** You need oper:message privilege for +M");
279 source_p
->umodes
&= ~user_modes
['M'];
283 update_session_deadline(source_p
);
287 // Unsetting +M; remove the timeout session
288 rb_dlink_node
*n
, *tn
;
290 RB_DLINK_FOREACH_SAFE(n
, tn
, callerid_overriding_opers
.head
)
292 struct CallerIDOverrideSession
*session_p
= n
->data
;
294 if (session_p
->client
!= source_p
)
297 rb_dlinkDelete(n
, &callerid_overriding_opers
);
303 static void check_priv_change(void *vdata
)
305 hook_data_priv_change
*data
= (hook_data_priv_change
*)vdata
;
306 struct Client
*source_p
= data
->client
;
307 const char *fakeparv
[4];
309 if (!MyClient(source_p
))
312 if (source_p
->umodes
& user_modes
['M'] && !HasPrivilege(source_p
, "oper:message"))
314 sendto_one_notice(source_p
, ":*** You need oper:message privilege for +M");
315 fakeparv
[0] = fakeparv
[1] = source_p
->name
;
318 user_mode(source_p
, source_p
, 3, fakeparv
);
323 handle_client_exit(void *vdata
)
325 hook_data_client_exit
*data
= (hook_data_client_exit
*) vdata
;
326 rb_dlink_node
*n
, *tn
;
327 struct Client
*source_p
= data
->target
;
329 RB_DLINK_FOREACH_SAFE(n
, tn
, callerid_overriding_opers
.head
)
331 struct CallerIDOverrideSession
*session_p
= n
->data
;
333 if (session_p
->client
!= source_p
)
336 rb_dlinkDelete(n
, &callerid_overriding_opers
);
341 static mapi_hfn_list_av1 um_callerid_hfnlist
[] = {
342 { "umode_changed", check_umode_change
},
343 { "priv_change", check_priv_change
},
344 { "invite", h_hdl_invite
},
345 { "privmsg_user", h_hdl_privmsg_user
},
346 { "client_exit", handle_client_exit
},
351 um_callerid_modinit(void)
355 user_modes
['g'] = find_umode_slot();
356 if (!user_modes
['g'])
358 ierror("um_callerid: unable to allocate usermode slot for +g; unloading module.");
362 user_modes
['G'] = find_umode_slot();
363 if (!user_modes
['G'])
367 ierror("um_callerid: unable to allocate usermode slot for +G; unloading module.");
371 user_modes
['M'] = find_umode_slot();
372 if (!user_modes
['M'])
377 ierror("um_callerid: unable to allocate usermode slot for +M; unloading module.");
381 construct_umodebuf();
383 add_isupport("CALLERID", isupport_umode
, "g");
385 RB_DLINK_FOREACH(ptr
, lclient_list
.head
)
387 struct Client
*client_p
= ptr
->data
;
388 if (IsPerson(client_p
) && (client_p
->umodes
& user_modes
['M']))
389 update_session_deadline(client_p
);
392 expire_callerid_override_deadlines_ev
= rb_event_add("expire_callerid_override_deadlines", expire_callerid_override_deadlines
, NULL
, 60);
398 um_callerid_moddeinit(void)
403 construct_umodebuf();
405 delete_isupport("CALLERID");
407 rb_event_delete(expire_callerid_override_deadlines_ev
);
410 DECLARE_MODULE_AV2(um_callerid
, um_callerid_modinit
, um_callerid_moddeinit
,
411 NULL
, NULL
, um_callerid_hfnlist
, NULL
, NULL
, um_callerid_desc
);