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