-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
m_solvemsg.cpp
178 lines (150 loc) · 4.79 KB
/
m_solvemsg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* InspIRCd -- Internet Relay Chat Daemon
*
* Copyright (C) 2015-2016 Sadie Powell <sadie@witchery.services>
*
* This file is part of InspIRCd. InspIRCd is free software: you can
* redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/// $ModAuthor: Sadie Powell
/// $ModAuthorMail: sadie@witchery.services
/// $ModConfig: <solvemsg chanmsg="no" usermsg="yes">
/// $ModDepends: core 3
/// $ModDesc: Requires users to solve a basic maths problem before messaging others.
#include "inspircd.h"
#include "modules/account.h"
struct Problem CXX11_FINAL
{
int first;
int second;
time_t nextwarning;
};
class CommandSolve CXX11_FINAL
: public SplitCommand
{
private:
SimpleExtItem<Problem>& ext;
public:
CommandSolve(Module* Creator, SimpleExtItem<Problem>& Ext)
: SplitCommand(Creator, "SOLVE", 1, 1)
, ext(Ext)
{
}
CmdResult HandleLocal(LocalUser* user, const Params& parameters) CXX11_OVERRIDE
{
Problem* problem = ext.get(user);
if (!problem)
{
user->WriteNotice("** You have already solved your problem!");
return CMD_FAILURE;
}
int result = ConvToNum<int>(parameters[0]);
if (result != (problem->first + problem->second))
{
user->WriteNotice(InspIRCd::Format("*** %s is not the correct answer.", parameters[0].c_str()));
user->CommandFloodPenalty += 10000;
return CMD_FAILURE;
}
ext.unset(user);
user->WriteNotice(InspIRCd::Format("*** %s is the correct answer!", parameters[0].c_str()));
return CMD_SUCCESS;
}
};
class ModuleSolveMessage CXX11_FINAL
: public Module
{
private:
SimpleExtItem<Problem> ext;
CommandSolve cmd;
bool chanmsg;
bool usermsg;
bool exemptregistered;
time_t warntime;
public:
ModuleSolveMessage()
: ext("solve-message", ExtensionItem::EXT_USER, this)
, cmd(this, ext)
{
}
void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
{
ConfigTag* tag = ServerInstance->Config->ConfValue("solvemsg");
chanmsg = tag->getBool("chanmsg", false);
usermsg = tag->getBool("usermsg", true);
exemptregistered = tag->getBool("exemptregistered", true);
warntime = tag->getDuration("warntime", 60, 1);
}
void OnUserPostInit(LocalUser* user) CXX11_OVERRIDE
{
Problem problem;
problem.first = ServerInstance->GenRandomInt(9);
problem.second = ServerInstance->GenRandomInt(9);
problem.nextwarning = 0;
ext.set(user, problem);
}
ModResult OnUserPreMessage(User* user, const MessageTarget& msgtarget, MessageDetails& details) CXX11_OVERRIDE
{
LocalUser* source = IS_LOCAL(user);
if (!source)
return MOD_RES_PASSTHRU;
if (!source->MyClass->config->getBool("usesolvemsg", true))
return MOD_RES_PASSTHRU; // Exempt by connect class.
std::string ctcpname;
if (details.IsCTCP(ctcpname) && !irc::equals(ctcpname, "ACTION"))
return MOD_RES_PASSTHRU; // Exempt non-ACTION CTCPs.
if (exemptregistered)
{
const AccountExtItem* accextitem = GetAccountExtItem();
const std::string* account = accextitem ? accextitem->get(user) : NULL;
if (account)
return MOD_RES_PASSTHRU; // Exempt logged in users.
}
switch (msgtarget.type)
{
case MessageTarget::TYPE_USER:
{
if (!usermsg)
return MOD_RES_PASSTHRU; // Not enabled.
User* target = msgtarget.Get<User>();
if (target->server->IsULine())
return MOD_RES_PASSTHRU; // Allow messaging ulines.
break;
}
case MessageTarget::TYPE_CHANNEL:
{
if (!chanmsg)
return MOD_RES_PASSTHRU; // Not enabled.
Channel* target = msgtarget.Get<Channel>();
if (target->GetPrefixValue(user) >= VOICE_VALUE)
return MOD_RES_PASSTHRU; // Exempt users with a status rank.
break;
}
case MessageTarget::TYPE_SERVER:
return MOD_RES_PASSTHRU; // Only opers can do this.
}
Problem* problem = ext.get(user);
if (!problem)
return MOD_RES_PASSTHRU;
if (problem->nextwarning > ServerInstance->Time())
return MOD_RES_DENY;
user->WriteNotice("*** Before you can send messages you must solve the following problem:");
user->WriteNotice(InspIRCd::Format("*** What is %d + %d?", problem->first, problem->second));
user->WriteNotice("*** You can enter your answer using /QUOTE SOLVE <answer>");
problem->nextwarning = ServerInstance->Time() + warntime;
return MOD_RES_DENY;
}
Version GetVersion() CXX11_OVERRIDE
{
return Version("Requires users to solve a basic maths problem before messaging others.");
}
};
MODULE_INIT(ModuleSolveMessage)