]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_helpop.cpp
Add Numeric::Numeric
[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 typedef std::map<std::string, std::string, irc::insensitive_swo> HelpopMap;
27 static HelpopMap helpop_map;
28
29 /** Handles user mode +h
30  */
31 class Helpop : public SimpleUserModeHandler
32 {
33  public:
34         Helpop(Module* Creator) : SimpleUserModeHandler(Creator, "helpop", 'h')
35         {
36                 oper = true;
37         }
38 };
39
40 /** Handles /HELPOP
41  */
42 class CommandHelpop : public Command
43 {
44         const std::string startkey;
45  public:
46         CommandHelpop(Module* Creator)
47                 : Command(Creator, "HELPOP", 0)
48                 , startkey("start")
49         {
50                 syntax = "<any-text>";
51         }
52
53         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
54         {
55                 const std::string& parameter = (!parameters.empty() ? parameters[0] : startkey);
56
57                 if (parameter == "index")
58                 {
59                         /* iterate over all helpop items */
60                         user->WriteNumeric(290, ":HELPOP topic index");
61                         for (HelpopMap::const_iterator iter = helpop_map.begin(); iter != helpop_map.end(); iter++)
62                                 user->WriteNumeric(292, ":  %s", iter->first.c_str());
63                         user->WriteNumeric(292, ":*** End of HELPOP topic index");
64                 }
65                 else
66                 {
67                         user->WriteNumeric(290, ":*** HELPOP for %s", parameter.c_str());
68                         user->WriteNumeric(292, ": -");
69
70                         HelpopMap::const_iterator iter = helpop_map.find(parameter);
71
72                         if (iter == helpop_map.end())
73                         {
74                                 iter = helpop_map.find("nohelp");
75                         }
76
77                         const std::string& value = iter->second;
78                         irc::sepstream stream(value, '\n');
79                         std::string token = "*";
80
81                         while (stream.GetToken(token))
82                         {
83                                 // Writing a blank line will not work with some clients
84                                 if (token.empty())
85                                         user->WriteNumeric(292, ": ");
86                                 else
87                                         user->WriteNumeric(292, ":%s", token.c_str());
88                         }
89
90                         user->WriteNumeric(292, ": -");
91                         user->WriteNumeric(292, ":*** End of HELPOP");
92                 }
93                 return CMD_SUCCESS;
94         }
95 };
96
97 class ModuleHelpop : public Module, public Whois::EventListener
98 {
99                 CommandHelpop cmd;
100                 Helpop ho;
101
102         public:
103                 ModuleHelpop()
104                         : Whois::EventListener(this)
105                         , cmd(this)
106                         , ho(this)
107                 {
108                 }
109
110                 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
111                 {
112                         HelpopMap help;
113
114                         ConfigTagList tags = ServerInstance->Config->ConfTags("helpop");
115                         for(ConfigIter i = tags.first; i != tags.second; ++i)
116                         {
117                                 ConfigTag* tag = i->second;
118                                 std::string key = tag->getString("key");
119                                 std::string value;
120                                 tag->readString("value", value, true); /* Linefeeds allowed */
121
122                                 if (key == "index")
123                                 {
124                                         throw ModuleException("m_helpop: The key 'index' is reserved for internal purposes. Please remove it.");
125                                 }
126
127                                 help[key] = value;
128                         }
129
130                         if (help.find("start") == help.end())
131                         {
132                                 // error!
133                                 throw ModuleException("m_helpop: Helpop file is missing important entry 'start'. Please check the example conf.");
134                         }
135                         else if (help.find("nohelp") == help.end())
136                         {
137                                 // error!
138                                 throw ModuleException("m_helpop: Helpop file is missing important entry 'nohelp'. Please check the example conf.");
139                         }
140
141                         helpop_map.swap(help);
142                 }
143
144                 void OnWhois(Whois::Context& whois) CXX11_OVERRIDE
145                 {
146                         if (whois.GetTarget()->IsModeSet(ho))
147                         {
148                                 whois.SendLine(310, ":is available for help.");
149                         }
150                 }
151
152                 Version GetVersion() CXX11_OVERRIDE
153                 {
154                         return Version("Provides the /HELPOP command for useful information", VF_VENDOR);
155                 }
156 };
157
158 MODULE_INIT(ModuleHelpop)