]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_helpop.cpp
Add Module* creator to Command and ModeHandler
[user/henk/code/inspircd.git] / src / modules / m_helpop.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15
16 /* $ModDesc: /helpop Command, Works like Unreal helpop */
17 static std::map<irc::string, std::string> helpop_map;
18
19
20 /** Handles user mode +h
21  */
22 class Helpop : public ModeHandler
23 {
24  public:
25         Helpop(InspIRCd* Instance, Module* Creator) : ModeHandler(Instance, Creator, 'h', 0, 0, false, MODETYPE_USER, true) { }
26
27         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding, bool)
28         {
29                 if (adding)
30                 {
31                         if (!dest->IsModeSet('h'))
32                         {
33                                 dest->SetMode('h',true);
34                                 return MODEACTION_ALLOW;
35                         }
36                 }
37                 else
38                 {
39                         if (dest->IsModeSet('h'))
40                         {
41                                 dest->SetMode('h',false);
42                                 return MODEACTION_ALLOW;
43                         }
44                 }
45
46                 return MODEACTION_DENY;
47         }
48 };
49
50 /** Handles /HELPOP
51  */
52 class CommandHelpop : public Command
53 {
54  public:
55         CommandHelpop (InspIRCd* Instance, Module* Creator) : Command(Instance, Creator, "HELPOP", 0, 0)
56         {
57                 syntax = "<any-text>";
58         }
59
60         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
61         {
62                 irc::string parameter("start");
63                 if (parameters.size() > 0)
64                         parameter = parameters[0].c_str();
65
66                 if (parameter == "index")
67                 {
68                         /* iterate over all helpop items */
69                         user->WriteServ("290 %s :HELPOP topic index", user->nick.c_str());
70                         for (std::map<irc::string, std::string>::iterator iter = helpop_map.begin(); iter != helpop_map.end(); iter++)
71                         {
72                                 user->WriteServ("292 %s :  %s", user->nick.c_str(), iter->first.c_str());
73                         }
74                         user->WriteServ("292 %s :*** End of HELPOP topic index", user->nick.c_str());
75                 }
76                 else
77                 {
78                         user->WriteServ("290 %s :*** HELPOP for %s", user->nick.c_str(), parameter.c_str());
79                         user->WriteServ("292 %s : -", user->nick.c_str());
80
81                         std::map<irc::string, std::string>::iterator iter = helpop_map.find(parameter);
82
83                         if (iter == helpop_map.end())
84                         {
85                                 iter = helpop_map.find("nohelp");
86                         }
87
88                         std::string value = iter->second;
89                         irc::sepstream stream(value, '\n');
90                         std::string token = "*";
91
92                         while (stream.GetToken(token))
93                         {
94                                 // Writing a blank line will not work with some clients
95                                 if (token.empty())
96                                         user->WriteServ("292 %s : ", user->nick.c_str());
97                                 else
98                                         user->WriteServ("292 %s :%s", user->nick.c_str(), token.c_str());
99                         }
100
101                         user->WriteServ("292 %s : -", user->nick.c_str());
102                         user->WriteServ("292 %s :*** End of HELPOP", user->nick.c_str());
103                 }
104
105                 /* We dont want these going out over the network, return CMD_FAILURE
106                  * to make sure the protocol module thinks theyre not worth sending.
107                  */
108                 return CMD_FAILURE;
109         }
110 };
111
112 class ModuleHelpop : public Module
113 {
114         private:
115                 std::string  h_file;
116                 CommandHelpop cmd;
117                 Helpop ho;
118
119         public:
120                 ModuleHelpop(InspIRCd* Me)
121                         : Module(Me), cmd(Me, this), ho(Me, this)
122                 {
123                         ReadConfig();
124                         if (!ServerInstance->Modes->AddMode(&ho))
125                                 throw ModuleException("Could not add new modes!");
126                         ServerInstance->AddCommand(&cmd);
127                         Implementation eventlist[] = { I_OnRehash, I_OnWhois };
128                         ServerInstance->Modules->Attach(eventlist, this, 2);
129                 }
130
131                 virtual void ReadConfig()
132                 {
133                         ConfigReader MyConf(ServerInstance);
134
135                         helpop_map.clear();
136
137                         for (int i = 0; i < MyConf.Enumerate("helpop"); i++)
138                         {
139                                 irc::string key = assign(MyConf.ReadValue("helpop", "key", i));
140                                 std::string value = MyConf.ReadValue("helpop", "value", i, true); /* Linefeeds allowed! */
141
142                                 if (key == "index")
143                                 {
144                                         throw ModuleException("m_helpop: The key 'index' is reserved for internal purposes. Please remove it.");
145                                 }
146
147                                 helpop_map[key] = value;
148                         }
149
150                         if (helpop_map.find("start") == helpop_map.end())
151                         {
152                                 // error!
153                                 throw ModuleException("m_helpop: Helpop file is missing important entries. Please check the example conf.");
154                         }
155                         else if (helpop_map.find("nohelp") == helpop_map.end())
156                         {
157                                 // error!
158                                 throw ModuleException("m_helpop: Helpop file is missing important entries. Please check the example conf.");
159                         }
160
161                 }
162
163
164                 virtual void OnRehash(User* user)
165                 {
166                         ReadConfig();
167                 }
168
169                 virtual void OnWhois(User* src, User* dst)
170                 {
171                         if (dst->IsModeSet('h'))
172                         {
173                                 ServerInstance->SendWhoisLine(src, dst, 310, std::string(src->nick)+" "+std::string(dst->nick)+" :is available for help.");
174                         }
175                 }
176
177                 virtual ~ModuleHelpop()
178                 {
179                         ServerInstance->Modes->DelMode(&ho);
180                 }
181
182                 virtual Version GetVersion()
183                 {
184                         return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
185                 }
186 };
187
188 MODULE_INIT(ModuleHelpop)