]> jfr.im git - solanum.git/blob - modules/um_callerid.c
/accept list should track nick changes when you share channels (#96)
[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 user_modes['g'] = 0;
61
62 ierror("um_callerid: unable to allocate usermode slot for +G; unloading module.");
63 return -1;
64 }
65
66 construct_umodebuf();
67
68 add_isupport("CALLERID", isupport_umode, "g");
69
70 return 0;
71 }
72
73 static void
74 um_callerid_moddeinit(void)
75 {
76 user_modes['g'] = 0;
77 user_modes['G'] = 0;
78 construct_umodebuf();
79
80 delete_isupport("CALLERID");
81 }
82
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))
86
87 static const char um_callerid_desc[] =
88 "Provides usermodes +g and +G which restrict messages from unauthorized users.";
89
90 static bool
91 allow_message(struct Client *source_p, struct Client *target_p)
92 {
93 if (!MyClient(target_p))
94 return true;
95
96 if (!IsSetAnyCallerID(target_p))
97 return true;
98
99 if (IsSetRelaxedCallerID(target_p) && has_common_channel(source_p, target_p) && !IsSetStrictCallerID(target_p))
100 return true;
101
102 if (IsServer(source_p))
103 return true;
104
105 /* XXX: controversial? allow opers to send through +g */
106 if (IsOperGeneral(source_p))
107 return true;
108
109 if (accept_message(source_p, target_p))
110 return true;
111
112 return false;
113 }
114
115 static void
116 send_callerid_notice(enum message_type msgtype, struct Client *source_p, struct Client *target_p)
117 {
118 if (!MyClient(target_p))
119 return;
120
121 if (msgtype == MESSAGE_TYPE_NOTICE)
122 return;
123
124 sendto_one_numeric(source_p, ERR_TARGUMODEG, form_str(ERR_TARGUMODEG),
125 target_p->name, IsSetStrictCallerID(target_p) ? "+g" : "+G");
126
127 if ((target_p->localClient->last_caller_id_time + ConfigFileEntry.caller_id_wait) < rb_current_time())
128 {
129 sendto_one_numeric(source_p, RPL_TARGNOTIFY, form_str(RPL_TARGNOTIFY),
130 target_p->name);
131
132 sendto_one(target_p, form_str(RPL_UMODEGMSG),
133 me.name, target_p->name, source_p->name,
134 source_p->username, source_p->host, IsSetStrictCallerID(target_p) ? "+g" : "+G");
135
136 target_p->localClient->last_caller_id_time = rb_current_time();
137 }
138 }
139
140 static bool
141 add_callerid_accept_for_source(enum message_type msgtype, struct Client *source_p, struct Client *target_p)
142 {
143 /* only do this on source_p's server */
144 if (!MyClient(source_p))
145 return true;
146
147 /*
148 * XXX: Controversial? Allow target users to send replies
149 * through a +g. Rationale is that people can presently use +g
150 * as a way to taunt users, e.g. harass them and hide behind +g
151 * as a way of griefing. --nenolod
152 */
153 if(msgtype != MESSAGE_TYPE_NOTICE &&
154 IsSetAnyCallerID(source_p) &&
155 !accept_message(target_p, source_p) &&
156 !IsOperGeneral(target_p))
157 {
158 if(rb_dlink_list_length(&source_p->localClient->allow_list) <
159 (unsigned long)ConfigFileEntry.max_accept)
160 {
161 rb_dlinkAddAlloc(target_p, &source_p->localClient->allow_list);
162 rb_dlinkAddAlloc(source_p, &target_p->on_allow_list);
163 }
164 else
165 {
166 sendto_one_numeric(source_p, ERR_OWNMODE,
167 form_str(ERR_OWNMODE),
168 target_p->name, IsSetStrictCallerID(target_p) ? "+g" : "+G");
169 return false;
170 }
171 }
172
173 return true;
174 }
175
176 static void
177 h_hdl_invite(void *vdata)
178 {
179 hook_data_channel_approval *data = vdata;
180 struct Client *source_p = data->client;
181 struct Client *target_p = data->target;
182 static char errorbuf[BUFSIZE];
183
184 if (data->approved)
185 return;
186
187 if (!add_callerid_accept_for_source(MESSAGE_TYPE_PRIVMSG, source_p, target_p))
188 {
189 data->approved = ERR_TARGUMODEG;
190 return;
191 }
192
193 if (allow_message(source_p, target_p))
194 return;
195
196 snprintf(errorbuf, sizeof errorbuf, form_str(ERR_TARGUMODEG),
197 target_p->name, IsSetStrictCallerID(target_p) ? "+g" : "+G");
198
199 data->approved = ERR_TARGUMODEG;
200 data->error = errorbuf;
201 }
202
203 static void
204 h_hdl_privmsg_user(void *vdata)
205 {
206 hook_data_privmsg_user *data = vdata;
207 enum message_type msgtype = data->msgtype;
208 struct Client *source_p = data->source_p;
209 struct Client *target_p = data->target_p;
210
211 if (data->approved)
212 return;
213
214 if (!add_callerid_accept_for_source(msgtype, source_p, target_p))
215 {
216 data->approved = ERR_TARGUMODEG;
217 return;
218 }
219
220 if (allow_message(source_p, target_p))
221 return;
222
223 send_callerid_notice(msgtype, source_p, target_p);
224
225 data->approved = ERR_TARGUMODEG;
226 }
227
228 static mapi_hfn_list_av1 um_callerid_hfnlist[] = {
229 { "invite", h_hdl_invite },
230 { "privmsg_user", h_hdl_privmsg_user },
231 { NULL, NULL }
232 };
233
234 DECLARE_MODULE_AV2(um_callerid, um_callerid_modinit, um_callerid_moddeinit,
235 NULL, NULL, um_callerid_hfnlist, NULL, NULL, um_callerid_desc);