]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands/cmd_motd.cpp
Rewrote the color codes parser to be one function, not two
[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 /*
43  * Replace all color codes from the special[] array to actual
44  * color code chars using C++ style escape sequences. You
45  * can append other chars to replace if you like (such as %U
46  * being underline). -- Justasic
47  */
48 void ProcessColors(ConfigFileCache::iterator &file)
49 {
50   static struct special_chars
51   {
52     std::string character;
53     std::string replace;
54     special_chars(const std::string &c, const std::string &r) : character(c), replace(r) { }
55   }
56   
57   special[] = {
58     special_chars("\\002", "\002"),  // Bold
59     special_chars("\\037", "\037"),  // underline
60     special_chars("\\003", "\003"),  // Color
61     special_chars("\\0017", "\017"), // Stop colors
62     special_chars("\\u", "\037"),    // Alias for underline
63     special_chars("\\b", "\002"),    // Alias for Bold
64     special_chars("\\x", "\017"),    // Alias for stop
65     special_chars("\\c", "\003"),    // Alias for color
66     special_chars("", "")
67   };
68   
69   for(file_cache::iterator it = file->second.begin(); it != file->second.end(); it++)
70   {
71     std::string ret = *it;
72     for(int i = 0; special[i].character.empty() == false; ++i)
73     {
74       std::string::size_type pos = ret.find(special[i].character);
75       if(pos != std::string::npos && ret[pos-1] == '\\' && ret[pos] == '\\')
76         continue; // Skip double slashes.
77         
78         // Replace all our characters in the array
79         while(pos != std::string::npos)
80         {
81           ret = ret.substr(0, pos) + special[i].replace + ret.substr(pos + special[i].character.size());
82           pos = ret.find(special[i].character, pos + special[i].replace.size());
83         }
84     }
85     
86     // Replace double slashes with a single slash before we return
87     std::string::size_type pos = ret.find("\\\\");
88     while(pos != std::string::npos)
89     {
90       ret = ret.substr(0, pos) + "\\" + ret.substr(pos + 2);
91       pos = ret.find("\\\\", pos + 1);
92     }
93     *it = 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)