]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_helpop.cpp
Allow the maximum length of a chanfilter message to be configured.
[user/henk/code/inspircd.git] / src / modules / m_helpop.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2005-2009 Robin Burchell <robin+git@viroteck.net>
6  *   Copyright (C) 2004-2006, 2008 Craig Edwards <craigedwards@brainbox.cc>
7  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
8  *   Copyright (C) 2004-2005 Craig McLure <craig@chatspike.net>
9  *
10  * This file is part of InspIRCd.  InspIRCd is free software: you can
11  * redistribute it and/or modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation, version 2.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23
24 #include "inspircd.h"
25
26 enum
27 {
28         // From UnrealIRCd.
29         RPL_WHOISHELPOP = 310
30 };
31
32 typedef std::map<std::string, std::string, irc::insensitive_swo> HelpopMap;
33 static HelpopMap helpop_map;
34
35 /** Handles user mode +h
36  */
37 class Helpop : public SimpleUserModeHandler
38 {
39  public:
40         Helpop(Module* Creator) : SimpleUserModeHandler(Creator, "helpop", 'h')
41         {
42                 oper = true;
43         }
44 };
45
46 /** Handles /HELPOP
47  */
48 class CommandHelpop : public Command
49 {
50         const std::string startkey;
51  public:
52         CommandHelpop(Module* Creator)
53                 : Command(Creator, "HELPOP", 0)
54                 , startkey("start")
55         {
56                 syntax = "<any-text>";
57         }
58
59         CmdResult Handle(const std::vector<std::string>& parameters, User* user) CXX11_OVERRIDE
60         {
61                 const std::string& parameter = (!parameters.empty() ? parameters[0] : startkey);
62
63                 if (parameter == "index")
64                 {
65                         /* iterate over all helpop items */
66                         user->WriteNumeric(290, "HELPOP topic index");
67                         for (HelpopMap::const_iterator iter = helpop_map.begin(); iter != helpop_map.end(); iter++)
68                                 user->WriteNumeric(292, InspIRCd::Format("  %s", iter->first.c_str()));
69                         user->WriteNumeric(292, "*** End of HELPOP topic index");
70                 }
71                 else
72                 {
73                         user->WriteNumeric(290, InspIRCd::Format("*** HELPOP for %s", parameter.c_str()));
74                         user->WriteNumeric(292, " -");
75
76                         HelpopMap::const_iterator iter = helpop_map.find(parameter);
77
78                         if (iter == helpop_map.end())
79                         {
80                                 iter = helpop_map.find("nohelp");
81                         }
82
83                         const std::string& value = iter->second;
84                         irc::sepstream stream(value, '\n');
85                         std::string token = "*";
86
87                         while (stream.GetToken(token))
88                         {
89                                 // Writing a blank line will not work with some clients
90                                 if (token.empty())
91                                         user->WriteNumeric(292, ' ');
92                                 else
93                                         user->WriteNumeric(292, token);
94                         }
95
96                         user->WriteNumeric(292, " -");
97                         user->WriteNumeric(292, "*** End of HELPOP");
98                 }
99                 return CMD_SUCCESS;
100         }
101 };
102
103 class ModuleHelpop : public Module, public Whois::EventListener
104 {
105                 CommandHelpop cmd;
106                 Helpop ho;
107
108         public:
109                 ModuleHelpop()
110                         : Whois::EventListener(this)
111                         , cmd(this)
112                         , ho(this)
113                 {
114                 }
115
116                 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
117                 {
118                         HelpopMap help;
119
120                         ConfigTagList tags = ServerInstance->Config->ConfTags("helpop");
121                         for(ConfigIter i = tags.first; i != tags.second; ++i)
122                         {
123                                 ConfigTag* tag = i->second;
124                                 std::string key = tag->getString("key");
125                                 std::string value;
126                                 tag->readString("value", value, true); /* Linefeeds allowed */
127
128                                 if (key == "index")
129                                 {
130                                         throw ModuleException("m_helpop: The key 'index' is reserved for internal purposes. Please remove it.");
131                                 }
132
133                                 help[key] = value;
134                         }
135
136                         if (help.find("start") == help.end())
137                         {
138                                 // error!
139                                 throw ModuleException("m_helpop: Helpop file is missing important entry 'start'. Please check the example conf.");
140                         }
141                         else if (help.find("nohelp") == help.end())
142                         {
143                                 // error!
144                                 throw ModuleException("m_helpop: Helpop file is missing important entry 'nohelp'. Please check the example conf.");
145                         }
146
147                         helpop_map.swap(help);
148                 }
149
150                 void OnWhois(Whois::Context& whois) CXX11_OVERRIDE
151                 {
152                         if (whois.GetTarget()->IsModeSet(ho))
153                         {
154                                 whois.SendLine(RPL_WHOISHELPOP, "is available for help.");
155                         }
156                 }
157
158                 Version GetVersion() CXX11_OVERRIDE
159                 {
160                         return Version("Provides the /HELPOP command for useful information", VF_VENDOR);
161                 }
162 };
163
164 MODULE_INIT(ModuleHelpop)