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