Permalink
Browse files

Added color codes to /rules

  • Loading branch information...
Justasic committed May 16, 2012
1 parent 8fb1b91 commit f02c029012eabeacacd9b20edc5172d21ad7046a
Showing with 67 additions and 0 deletions.
  1. +4 −0 include/inspircd.h
  2. +1 −0 src/commands/cmd_rehash.cpp
  3. +62 −0 src/commands/cmd_rules.cpp
View
@@ -455,6 +455,10 @@ class CoreExport InspIRCd
*/
bool ProcessedMotdEscapes;
+ /** Holds whether the RULES has been parsed for color codes
+ */
+ bool ProcessedRulesEscapes;
+
/** Get the current time
* Because this only calls time() once every time around the mainloop,
* it is much faster than calling time() directly.
@@ -95,6 +95,7 @@ CmdResult CommandRehash::Handle (const std::vector<std::string>& parameters, Use
ServerInstance->DoGarbageCollect();
ServerInstance->ProcessedMotdEscapes = false; // Reprocess our motd file --Justasic
+ ServerInstance->ProcessedRulesEscapes = false; // Reprocess our rules file --Justasic
ServerInstance->PendingRehash = new ConfigReaderThread(user->uuid);
ServerInstance->Threads->Submit(ServerInstance->PendingRehash);
View
@@ -46,6 +46,61 @@ class CommandRules : public Command
}
};
+/*
+ * Replace all color codes from the special[] array to actual
+ * color code chars using C++ style escape sequences. You
+ * can append other chars to replace if you like (such as %U
+ * being underline). -- Justasic
+ */
+void ProcessColors(ConfigFileCache::iterator &file)
+{
+ static struct special_chars
+ {
+ std::string character;
+ std::string replace;
+ special_chars(const std::string &c, const std::string &r) : character(c), replace(r) { }
+ }
+
+ special[] = {
+ special_chars("\\002", "\002"), // Bold
+ special_chars("\\037", "\037"), // underline
+ special_chars("\\003", "\003"), // Color
+ special_chars("\\0017", "\017"), // Stop colors
+ special_chars("\\u", "\037"), // Alias for underline
+ special_chars("\\b", "\002"), // Alias for Bold
+ special_chars("\\x", "\017"), // Alias for stop
+ special_chars("\\c", "\003"), // Alias for color
+ special_chars("", "")
+ };
+
+ for(file_cache::iterator it = file->second.begin(); it != file->second.end(); it++)
+ {
+ std::string ret = *it;
+ for(int i = 0; special[i].character.empty() == false; ++i)
+ {
+ std::string::size_type pos = ret.find(special[i].character);
+ if(pos != std::string::npos && ret[pos-1] == '\\' && ret[pos] == '\\')
+ continue; // Skip double slashes.
+
+ // Replace all our characters in the array
+ while(pos != std::string::npos)
+ {
+ ret = ret.substr(0, pos) + special[i].replace + ret.substr(pos + special[i].character.size());
+ pos = ret.find(special[i].character, pos + special[i].replace.size());
+ }
+ }
+
+ // Replace double slashes with a single slash before we return
+ std::string::size_type pos = ret.find("\\\\");
+ while(pos != std::string::npos)
+ {
+ ret = ret.substr(0, pos) + "\\" + ret.substr(pos + 2);
+ pos = ret.find("\\\\", pos + 1);
+ }
+ *it = ret;
+ }
+}
+
CmdResult CommandRules::Handle (const std::vector<std::string>& parameters, User *user)
{
if (parameters.size() > 0 && parameters[0] != ServerInstance->Config->ServerName)
@@ -66,6 +121,13 @@ CmdResult CommandRules::Handle (const std::vector<std::string>& parameters, User
ServerInstance->Config->ServerName.c_str(), ERR_NORULES, user->nick.c_str());
return CMD_SUCCESS;
}
+
+ if(!ServerInstance->ProcessedRulesEscapes)
+ {
+ ProcessColors(rules);
+ ServerInstance->ProcessedRulesEscapes = true;
+ }
+
user->SendText(":%s %03d %s :%s server rules:", ServerInstance->Config->ServerName.c_str(),
RPL_RULESTART, user->nick.c_str(), ServerInstance->Config->ServerName.c_str());

0 comments on commit f02c029

Please sign in to comment.