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