- void ReadConf()
- {
- ConfigReader Conf(ServerInstance);
- percent = Conf.ReadInteger("blockcaps", "percent", "100", 0, true);
- minlen = Conf.ReadInteger("blockcaps", "minlen", "1", 0, true);
- std::string hmap = Conf.ReadValue("blockcaps", "capsmap", 0);
- if (hmap.empty())
- hmap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- memset(&capsmap, 0, 255);
- for (std::string::iterator n = hmap.begin(); n != hmap.end(); n++)
- capsmap[(unsigned char)*n] = 1;
- if (percent < 1 || percent > 100)
- {
- ServerInstance->Log(DEFAULT, "<blockcaps:percent> out of range, setting to default of 100.");
- percent = 100;
- }
- if (minlen < 1 || minlen > MAXBUF-1)
- {
- ServerInstance->Log(DEFAULT, "<blockcaps:minlen> out of range, setting to default of 1.");
- minlen = 1;
+ // Count the characters to see how many upper case and
+ // ignored (non upper or lower) characters there are.
+ size_t upper = 0;
+ for (std::string::const_iterator iter = message.begin(); iter != message.end(); ++iter)
+ {
+ unsigned char chr = static_cast<unsigned char>(*iter);
+ if (uppercase.test(chr))
+ upper += 1;
+ else if (!lowercase.test(chr))
+ length -= 1;
+ }
+
+ // Calculate the percentage which is upper case. If the
+ // message was entirely symbols then it can't contain
+ // any upper case letters.
+ if (length > 0 && round((upper * 100) / length) >= percent)
+ {
+ const std::string msg = InspIRCd::Format("Your message cannot contain %d%% or more capital letters if it's longer than %d characters", percent, minlen);
+ user->WriteNumeric(Numerics::CannotSendTo(c, msg));
+ return MOD_RES_DENY;
+ }
+ }