]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands/cmd_motd.cpp
Fixed using a function on every call for /motd, causing lag on large networks (reques...
[user/henk/code/inspircd.git] / src / commands / cmd_motd.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15
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
19  * may not.
20  */
21 class CommandMotd : public Command
22 {
23  public:
24         /** Constructor for motd.
25          */
26         CommandMotd ( Module* parent) : Command(parent,"MOTD",0,1) { ServerInstance->ProcessedMotdEscapes = false; syntax = "[<servername>]"; }
27         /** Handle command.
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.
32          */
33         CmdResult Handle(const std::vector<std::string>& parameters, User *user);
34         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
35         {
36                 if (parameters.size() > 0)
37                         return ROUTE_UNICAST(parameters[0]);
38                 return ROUTE_LOCALONLY;
39         }
40 };
41
42 inline std::string replace_all(const std::string &str, const std::string &orig, const std::string &repl)
43 {
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)
47         {
48                 new_str = new_str.substr(0, pos) + repl + new_str.substr(pos + orig_length);
49                 pos = new_str.find(orig, pos + repl_length);
50         }
51         return new_str;
52 }
53
54 /*
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
59  */
60 void ProcessColors(ConfigFileCache::iterator &file)
61 {
62         static struct special_chars
63         {
64                 std::string character;
65                 std::string replace;
66                 special_chars(const std::string &c, const std::string &r) : character(c), replace(r) { }
67         }
68
69         special[] = {
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
78                 special_chars("", "")
79         };
80
81         for(file_cache::iterator it = file->second.begin(); it != file->second.end(); it++)
82         {
83                 std::string ret = *it;
84                 for(int i = 0; special[i].character.empty() == false; ++i)
85                 {
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.
89
90                         ret = replace_all(ret, special[i].character, special[i].replace);
91                 }
92                 // Replace double slashes with a single slash before we return
93                 *it = replace_all(ret, "\\\\", "\\");
94         }
95 }
96
97 /** Handle /MOTD
98  */
99 CmdResult CommandMotd::Handle (const std::vector<std::string>& parameters, User *user)
100 {
101         if (parameters.size() > 0 && parameters[0] != ServerInstance->Config->ServerName)
102                 return CMD_SUCCESS;
103
104         ConfigTag* tag = NULL;
105         if (IS_LOCAL(user))
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())
110         {
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());
113                 return CMD_SUCCESS;
114         }
115
116         if(!ServerInstance->ProcessedMotdEscapes)
117         {
118                 ProcessColors(motd);
119                 ServerInstance->ProcessedMotdEscapes = true;
120         }
121
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());
124
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());
127
128         user->SendText(":%s %03d %s :End of message of the day.", ServerInstance->Config->ServerName.c_str(), RPL_ENDOFMOTD, user->nick.c_str());
129
130         return CMD_SUCCESS;
131 }
132
133 COMMAND_INIT(CommandMotd)