]> jfr.im git - solanum.git/blob - modules/um_regonlymsg.c
Add ACCOUNTEXTBAN ISUPPORT token
[solanum.git] / modules / um_regonlymsg.c
1 /*
2 * modules/um_regonlymsg.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 "logger.h"
44
45 static int
46 um_regonlymsg_modinit(void)
47 {
48 user_modes['R'] = find_umode_slot();
49 construct_umodebuf();
50
51 if (!user_modes['R'])
52 {
53 ierror("um_regonlymsg: unable to allocate usermode slot for +R, unloading module");
54 return -1;
55 }
56
57 return 0;
58 }
59
60 static void
61 um_regonlymsg_moddeinit(void)
62 {
63 user_modes['R'] = 0;
64 construct_umodebuf();
65 }
66
67 #define IsSetRegOnlyMsg(c) ((c->umodes & user_modes['R']) == user_modes['R'])
68
69 static const char um_regonlymsg_desc[] =
70 "Provides usermode +R which restricts messages from unregistered users.";
71
72 static bool
73 allow_message(struct Client *source_p, struct Client *target_p)
74 {
75 if (!MyClient(target_p))
76 return true;
77
78 if (!IsSetRegOnlyMsg(target_p))
79 return true;
80
81 if (!IsPerson(source_p))
82 return true;
83
84 /* XXX: controversial? allow opers to send through +R */
85 if (IsOper(source_p))
86 return true;
87
88 if (accept_message(source_p, target_p))
89 return true;
90
91 if (source_p->user->suser[0])
92 return true;
93
94 return false;
95 }
96
97 static bool
98 add_callerid_accept_for_source(enum message_type msgtype, struct Client *source_p, struct Client *target_p)
99 {
100 if (!MyClient(source_p))
101 return true;
102
103 if(msgtype != MESSAGE_TYPE_NOTICE &&
104 IsSetRegOnlyMsg(source_p) &&
105 !accept_message(target_p, source_p) &&
106 !IsOperGeneral(target_p))
107 {
108 if(rb_dlink_list_length(&source_p->localClient->allow_list) <
109 (unsigned long)ConfigFileEntry.max_accept)
110 {
111 rb_dlinkAddAlloc(target_p, &source_p->localClient->allow_list);
112 rb_dlinkAddAlloc(source_p, &target_p->on_allow_list);
113 }
114 else
115 {
116 sendto_one_numeric(source_p, ERR_OWNMODE,
117 form_str(ERR_OWNMODE),
118 target_p->name, "+R");
119 return false;
120 }
121 }
122
123 return true;
124 }
125
126 static void
127 h_hdl_invite(void *vdata)
128 {
129 hook_data_channel_approval *data = vdata;
130 struct Client *source_p = data->client;
131 struct Client *target_p = data->target;
132 static char errorbuf[BUFSIZE];
133
134 if (data->approved)
135 return;
136
137 if (!add_callerid_accept_for_source(MESSAGE_TYPE_PRIVMSG, source_p, target_p))
138 {
139 data->approved = ERR_NONONREG;
140 return;
141 }
142
143 if (allow_message(source_p, target_p))
144 return;
145
146 snprintf(errorbuf, sizeof errorbuf, form_str(ERR_NONONREG),
147 target_p->name);
148
149 data->approved = ERR_NONONREG;
150 data->error = errorbuf;
151 }
152
153 static void
154 h_hdl_privmsg_user(void *vdata)
155 {
156 hook_data_privmsg_user *data = vdata;
157 struct Client *source_p = data->source_p;
158 struct Client *target_p = data->target_p;
159
160 if (data->approved)
161 return;
162
163 if (!add_callerid_accept_for_source(data->msgtype, source_p, target_p))
164 {
165 data->approved = ERR_NONONREG;
166 return;
167 }
168
169 if (allow_message(source_p, target_p))
170 return;
171
172 data->approved = ERR_NONONREG;
173
174 if (data->msgtype == MESSAGE_TYPE_NOTICE)
175 return;
176
177 sendto_one_numeric(source_p, ERR_NONONREG, form_str(ERR_NONONREG),
178 target_p->name);
179 }
180
181 static mapi_hfn_list_av1 um_regonlymsg_hfnlist[] = {
182 { "invite", h_hdl_invite },
183 { "privmsg_user", h_hdl_privmsg_user },
184 { NULL, NULL }
185 };
186
187 DECLARE_MODULE_AV2(um_regonlymsg, um_regonlymsg_modinit, um_regonlymsg_moddeinit,
188 NULL, NULL, um_regonlymsg_hfnlist, NULL, NULL, um_regonlymsg_desc);