1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
16 /** Handle /MOTD. These command handlers can be reloaded by the core,
17 * and handle basic RFC1459 commands. Commands within modules work
18 * the same way, however, they can be fully unloaded, where these
21 class CommandMotd : public Command
24 /** Constructor for motd.
26 CommandMotd ( Module* parent) : Command(parent,"MOTD",0,1) { ServerInstance->ProcessedMotdEscapes = false; syntax = "[<servername>]"; }
28 * @param parameters The parameters to the comamnd
29 * @param pcnt The number of parameters passed to teh command
30 * @param user The user issuing the command
31 * @return A value from CmdResult to indicate command success or failure.
33 CmdResult Handle(const std::vector<std::string>& parameters, User *user);
34 RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
36 if (parameters.size() > 0)
37 return ROUTE_UNICAST(parameters[0]);
38 return ROUTE_LOCALONLY;
42 inline std::string replace_all(const std::string &str, const std::string &orig, const std::string &repl)
44 std::string new_str = str;
45 std::string::size_type pos = new_str.find(orig), orig_length = orig.length(), repl_length = repl.length();
46 while (pos != std::string::npos)
48 new_str = new_str.substr(0, pos) + repl + new_str.substr(pos + orig_length);
49 pos = new_str.find(orig, pos + repl_length);
55 * Replace all color codes from the special[] array to actual
56 * color code chars using C++ style escape sequences. You
57 * can append other chars to replace if you like (such as %U
58 * being underline). -- Justasic
60 void ProcessColors(ConfigFileCache::iterator &file)
62 static struct special_chars
64 std::string character;
66 special_chars(const std::string &c, const std::string &r) : character(c), replace(r) { }
70 special_chars("\\002", "\002"), // Bold
71 special_chars("\\037", "\037"), // underline
72 special_chars("\\003", "\003"), // Color
73 special_chars("\\0017", "\017"), // Stop colors
74 special_chars("\\u", "\037"), // Alias for underline
75 special_chars("\\b", "\002"), // Alias for Bold
76 special_chars("\\x", "\017"), // Alias for stop
77 special_chars("\\c", "\003"), // Alias for color
81 for(file_cache::iterator it = file->second.begin(); it != file->second.end(); it++)
83 std::string ret = *it;
84 for(int i = 0; special[i].character.empty() == false; ++i)
86 std::string::size_type pos = ret.find(special[i].character);
87 if(pos != std::string::npos && ret[pos-1] == '\\' && ret[pos] == '\\')
88 continue; // Skip double slashes.
90 ret = replace_all(ret, special[i].character, special[i].replace);
92 // Replace double slashes with a single slash before we return
93 *it = replace_all(ret, "\\\\", "\\");
99 CmdResult CommandMotd::Handle (const std::vector<std::string>& parameters, User *user)
101 if (parameters.size() > 0 && parameters[0] != ServerInstance->Config->ServerName)
104 ConfigTag* tag = NULL;
106 tag = user->GetClass()->config;
107 std::string motd_name = tag->getString("motd", "motd");
108 ConfigFileCache::iterator motd = ServerInstance->Config->Files.find(motd_name);
109 if (motd == ServerInstance->Config->Files.end())
111 user->SendText(":%s %03d %s :Message of the day file is missing.",
112 ServerInstance->Config->ServerName.c_str(), ERR_NOMOTD, user->nick.c_str());
116 if(!ServerInstance->ProcessedMotdEscapes)
119 ServerInstance->ProcessedMotdEscapes = true;
122 user->SendText(":%s %03d %s :%s message of the day", ServerInstance->Config->ServerName.c_str(),
123 RPL_MOTDSTART, user->nick.c_str(), ServerInstance->Config->ServerName.c_str());
125 for (file_cache::iterator i = motd->second.begin(); i != motd->second.end(); i++)
126 user->SendText(":%s %03d %s :- %s", ServerInstance->Config->ServerName.c_str(), RPL_MOTD, user->nick.c_str(), i->c_str());
128 user->SendText(":%s %03d %s :End of message of the day.", ServerInstance->Config->ServerName.c_str(), RPL_ENDOFMOTD, user->nick.c_str());
133 COMMAND_INIT(CommandMotd)