]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_helpop.cpp
Merge insp20
[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
98 {
99                 CommandHelpop cmd;
100                 Helpop ho;
101
102         public:
103                 ModuleHelpop()
104                         : cmd(this), ho(this)
105                 {
106                 }
107
108                 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
109                 {
110                         HelpopMap help;
111
112                         ConfigTagList tags = ServerInstance->Config->ConfTags("helpop");
113                         for(ConfigIter i = tags.first; i != tags.second; ++i)
114                         {
115                                 ConfigTag* tag = i->second;
116                                 std::string key = tag->getString("key");
117                                 std::string value;
118                                 tag->readString("value", value, true); /* Linefeeds allowed */
119
120                                 if (key == "index")
121                                 {
122                                         throw ModuleException("m_helpop: The key 'index' is reserved for internal purposes. Please remove it.");
123                                 }
124
125                                 help[key] = value;
126                         }
127
128                         if (help.find("start") == help.end())
129                         {
130                                 // error!
131                                 throw ModuleException("m_helpop: Helpop file is missing important entry 'start'. Please check the example conf.");
132                         }
133                         else if (help.find("nohelp") == help.end())
134                         {
135                                 // error!
136                                 throw ModuleException("m_helpop: Helpop file is missing important entry 'nohelp'. Please check the example conf.");
137                         }
138
139                         helpop_map.swap(help);
140                 }
141
142                 void OnWhois(User* src, User* dst) CXX11_OVERRIDE
143                 {
144                         if (dst->IsModeSet(ho))
145                         {
146                                 ServerInstance->SendWhoisLine(src, dst, 310, dst->nick+" :is available for help.");
147                         }
148                 }
149
150                 Version GetVersion() CXX11_OVERRIDE
151                 {
152                         return Version("Provides the /HELPOP command for useful information", VF_VENDOR);
153                 }
154 };
155
156 MODULE_INIT(ModuleHelpop)