]> jfr.im git - solanum.git/blame - modules/um_regonlymsg.c
regonlymsg: catch find_umode_slot() failure
[solanum.git] / modules / um_regonlymsg.c
CommitLineData
968dee68
AC
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"
2bbfce68 43#include "logger.h"
968dee68
AC
44
45static int
46um_regonlymsg_modinit(void)
47{
48 user_modes['R'] = find_umode_slot();
49 construct_umodebuf();
50
2bbfce68
AC
51 if (!user_modes['R'])
52 {
53 ierror("um_regonlymsg: unable to allocate usermode slot for +R, unloading module");
54 return -1;
55 }
56
968dee68
AC
57 return 0;
58}
59
60static void
61um_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
69static const char um_regonlymsg_desc[] =
70 "Provides usermode +R which restricts messages from unregistered users.";
71
72static bool
73allow_message(struct Client *source_p, struct Client *target_p)
74{
4436a7ca
AC
75 if (!MyClient(target_p))
76 return true;
77
968dee68 78 if (!IsSetRegOnlyMsg(target_p))
dc5d1d01 79 return true;
968dee68
AC
80
81 if (IsServer(source_p))
dc5d1d01 82 return true;
968dee68
AC
83
84 /* XXX: controversial? allow opers to send through +R */
85 if (IsOper(source_p))
dc5d1d01 86 return true;
968dee68
AC
87
88 if (accept_message(source_p, target_p))
dc5d1d01 89 return true;
968dee68
AC
90
91 if (source_p->user->suser[0])
dc5d1d01 92 return true;
968dee68 93
dc5d1d01 94 return false;
968dee68
AC
95}
96
97static void
daaf127d 98h_hdl_invite(void *vdata)
968dee68
AC
99{
100 hook_data_channel_approval *data = vdata;
101 struct Client *source_p = data->client;
102 struct Client *target_p = data->target;
103
90e99760
AC
104 if (data->approved)
105 return;
106
968dee68
AC
107 if (allow_message(source_p, target_p))
108 return;
109
110 sendto_one_numeric(source_p, ERR_NONONREG, form_str(ERR_NONONREG),
111 target_p->name);
112
113 data->approved = ERR_NONONREG;
114}
115
116static void
117h_hdl_privmsg_user(void *vdata)
118{
119 hook_data_privmsg_user *data = vdata;
120 struct Client *source_p = data->source_p;
121 struct Client *target_p = data->target_p;
122
90e99760
AC
123 if (data->approved)
124 return;
125
968dee68
AC
126 if (allow_message(source_p, target_p))
127 return;
128
129 if (data->msgtype == MESSAGE_TYPE_NOTICE)
130 return;
131
132 sendto_one_numeric(source_p, ERR_NONONREG, form_str(ERR_NONONREG),
133 target_p->name);
134
135 data->approved = ERR_NONONREG;
136}
137
138static mapi_hfn_list_av1 um_regonlymsg_hfnlist[] = {
daaf127d 139 { "invite", h_hdl_invite },
968dee68
AC
140 { "privmsg_user", h_hdl_privmsg_user },
141 { NULL, NULL }
142};
143
144DECLARE_MODULE_AV2(um_regonlymsg, um_regonlymsg_modinit, um_regonlymsg_moddeinit,
145 NULL, NULL, um_regonlymsg_hfnlist, NULL, NULL, um_regonlymsg_desc);