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