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"
48 um_callerid_modinit(void)
50 user_modes
['g'] = find_umode_slot();
53 ierror("um_callerid: unable to allocate usermode slot for +g; unloading module.");
57 user_modes
['G'] = find_umode_slot();
62 ierror("um_callerid: unable to allocate usermode slot for +G; unloading module.");
68 add_isupport("CALLERID", isupport_umode
, "g");
74 um_callerid_moddeinit(void)
80 delete_isupport("CALLERID");
83 #define IsSetStrictCallerID(c) ((c->umodes & user_modes['g']) == user_modes['g'])
84 #define IsSetRelaxedCallerID(c) ((c->umodes & user_modes['G']) == user_modes['G'])
85 #define IsSetAnyCallerID(c) (IsSetStrictCallerID(c) || IsSetRelaxedCallerID(c))
87 static const char um_callerid_desc
[] =
88 "Provides usermodes +g and +G which restrict messages from unauthorized users.";
91 has_common_channel(struct Client
*source_p
, struct Client
*target_p
)
93 rb_dlink_node
*ps
, *pt
;
94 struct membership
*ms
, *mt
;
95 struct Channel
*chptr
;
97 ITER_COMM_CHANNELS(ps
, pt
, source_p
->user
->channel
.head
, target_p
->user
->channel
.head
, ms
, mt
, chptr
)
99 if (ms
!= NULL
&& mt
!= NULL
)
107 allow_message(struct Client
*source_p
, struct Client
*target_p
)
109 if (!MyClient(target_p
))
112 if (!IsSetAnyCallerID(target_p
))
115 if (IsSetRelaxedCallerID(target_p
) && has_common_channel(source_p
, target_p
) && !IsSetStrictCallerID(target_p
))
118 if (IsServer(source_p
))
121 /* XXX: controversial? allow opers to send through +g */
122 if (IsOperGeneral(source_p
))
125 if (accept_message(source_p
, target_p
))
132 send_callerid_notice(enum message_type msgtype
, struct Client
*source_p
, struct Client
*target_p
)
134 if (!MyClient(target_p
))
137 if (msgtype
== MESSAGE_TYPE_NOTICE
)
140 sendto_one_numeric(source_p
, ERR_TARGUMODEG
, form_str(ERR_TARGUMODEG
),
141 target_p
->name
, IsSetStrictCallerID(target_p
) ? "+g" : "+G");
143 if ((target_p
->localClient
->last_caller_id_time
+ ConfigFileEntry
.caller_id_wait
) < rb_current_time())
145 sendto_one_numeric(source_p
, RPL_TARGNOTIFY
, form_str(RPL_TARGNOTIFY
),
148 sendto_one(target_p
, form_str(RPL_UMODEGMSG
),
149 me
.name
, target_p
->name
, source_p
->name
,
150 source_p
->username
, source_p
->host
, IsSetStrictCallerID(target_p
) ? "+g" : "+G");
152 target_p
->localClient
->last_caller_id_time
= rb_current_time();
157 add_callerid_accept_for_source(enum message_type msgtype
, struct Client
*source_p
, struct Client
*target_p
)
159 /* only do this on source_p's server */
160 if (!MyClient(source_p
))
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
169 if(msgtype
!= MESSAGE_TYPE_NOTICE
&&
170 IsSetAnyCallerID(source_p
) &&
171 !accept_message(target_p
, source_p
) &&
172 !IsOperGeneral(target_p
))
174 if(rb_dlink_list_length(&source_p
->localClient
->allow_list
) <
175 (unsigned long)ConfigFileEntry
.max_accept
)
177 rb_dlinkAddAlloc(target_p
, &source_p
->localClient
->allow_list
);
178 rb_dlinkAddAlloc(source_p
, &target_p
->on_allow_list
);
182 sendto_one_numeric(source_p
, ERR_OWNMODE
,
183 form_str(ERR_OWNMODE
),
184 target_p
->name
, IsSetStrictCallerID(target_p
) ? "+g" : "+G");
193 h_hdl_invite(void *vdata
)
195 hook_data_channel_approval
*data
= vdata
;
196 struct Client
*source_p
= data
->client
;
197 struct Client
*target_p
= data
->target
;
198 static char errorbuf
[BUFSIZE
];
203 if (!add_callerid_accept_for_source(MESSAGE_TYPE_PRIVMSG
, source_p
, target_p
))
205 data
->approved
= ERR_TARGUMODEG
;
209 if (allow_message(source_p
, target_p
))
212 snprintf(errorbuf
, sizeof errorbuf
, form_str(ERR_TARGUMODEG
),
213 target_p
->name
, IsSetStrictCallerID(target_p
) ? "+g" : "+G");
215 data
->approved
= ERR_TARGUMODEG
;
216 data
->error
= errorbuf
;
220 h_hdl_privmsg_user(void *vdata
)
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
;
230 if (!add_callerid_accept_for_source(msgtype
, source_p
, target_p
))
232 data
->approved
= ERR_TARGUMODEG
;
236 if (allow_message(source_p
, target_p
))
239 send_callerid_notice(msgtype
, source_p
, target_p
);
241 data
->approved
= ERR_TARGUMODEG
;
244 static mapi_hfn_list_av1 um_callerid_hfnlist
[] = {
245 { "invite", h_hdl_invite
},
246 { "privmsg_user", h_hdl_privmsg_user
},
250 DECLARE_MODULE_AV2(um_callerid
, um_callerid_modinit
, um_callerid_moddeinit
,
251 NULL
, NULL
, um_callerid_hfnlist
, NULL
, NULL
, um_callerid_desc
);