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