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