]> jfr.im git - solanum.git/blame - extensions/chm_regmsg.c
check bans and quiets for cmode -n/nonmember PRIVMSG
[solanum.git] / extensions / chm_regmsg.c
CommitLineData
0ba1da59
EM
1/*
2 * Solanum: a slightly advanced ircd
3 * chm_regmsg: require identification to chat (+R mode).
4 *
5 * Copyright (c) 2020 Eric Mertens <emertens@gmail.com>
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice is present in all copies.
10 *
11 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
12 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
13 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
14 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
15 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
19 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
20 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21 * POSSIBILITY OF SUCH DAMAGE.
22 */
23
24#include "stdinc.h"
25#include "modules.h"
26#include "hook.h"
27#include "client.h"
28#include "ircd.h"
29#include "logger.h"
30#include "send.h"
31#include "s_conf.h"
32#include "s_user.h"
33#include "s_serv.h"
34#include "numeric.h"
35#include "chmode.h"
36#include "inline/stringops.h"
37
38static const char chm_regmsg_desc[] =
0ea108fc 39 "Adds channel mode +R, which blocks messages from unregistered users";
0ba1da59
EM
40
41static unsigned int mode_regmsg;
42
8d67f060
SB
43static void hook_privmsg_channel(void *);
44static void hook_can_send(void *);
0ba1da59
EM
45
46mapi_hfn_list_av1 chm_regmsg_hfnlist[] = {
8d67f060
SB
47 { "privmsg_channel", hook_privmsg_channel },
48 { "can_send", hook_can_send },
0ba1da59
EM
49 { NULL, NULL }
50};
51
8d67f060
SB
52static bool
53chm_regmsg_test(struct Client *source_p, struct Channel *chptr)
54{
55 struct membership *msptr;
56
57 /* mode is unset, accept */
58 if (!(chptr->mode.mode & mode_regmsg))
59 return true;
60
61 /* user is identified, accept */
62 if (!EmptyString(source_p->user->suser))
63 return true;
64
65 /* voice and op override identification requirement, accept */
66 msptr = find_channel_membership(chptr, source_p);
67 if (is_chanop_voiced(msptr))
68 return true;
69
70 return false;
71}
72
0ba1da59 73static void
8d67f060 74hook_privmsg_channel(void *data_)
0ba1da59
EM
75{
76 hook_data_privmsg_channel *data = data_;
8d67f060
SB
77
78 /* Only apply this hook if the channel isn't +z - if it is +z, then the can_send
79 * hook should be used to enable +z to do its job, as an error numeric won't be sent in that case */
80 if (data->chptr->mode.mode & MODE_OPMODERATE)
81 return;
0ba1da59
EM
82
83 /* message is already blocked, defer */
84 if (data->approved)
85 return;
86
8d67f060 87 if (chm_regmsg_test(data->source_p, data->chptr))
981a94c3
EM
88 return;
89
8d67f060
SB
90 sendto_one_numeric(data->source_p, ERR_MSGNEEDREGGEDNICK, form_str(ERR_MSGNEEDREGGEDNICK), data->chptr->chname);
91 data->approved = ERR_MSGNEEDREGGEDNICK;
92}
93
94static void hook_can_send(void *data_)
95{
96 hook_data_channel_approval *data = data_;
97
98 /* Only apply this hook if the channel is +z - if it isn't +z, then the privmsg_channel
99 * hook should be used to enable a custom error numeric to be sent */
100 if (!(data->chptr->mode.mode & MODE_OPMODERATE))
0ba1da59
EM
101 return;
102
8d67f060
SB
103 /* message is already blocked, defer */
104 if (data->approved == CAN_SEND_NO)
0ba1da59
EM
105 return;
106
8d67f060
SB
107 if (chm_regmsg_test(data->client, data->chptr))
108 return;
109
110 data->approved = CAN_SEND_NO;
0ba1da59
EM
111}
112
113static int
114_modinit(void)
115{
116 mode_regmsg = cflag_add('R', chm_simple);
117 if (mode_regmsg == 0) {
118 ierror("chm_regmsg: unable to allocate cmode slot for +R, unloading module");
119 return -1;
120 }
121
122 return 0;
123}
124
125static void
126_moddeinit(void)
127{
128 cflag_orphan('R');
129}
130
131DECLARE_MODULE_AV2(chm_regmsg, _modinit, _moddeinit, NULL, NULL, chm_regmsg_hfnlist, NULL, NULL, chm_regmsg_desc);