]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_helpop.cpp
Reasonably sized fix - when adding modes in modules, be sure to check the return...
[user/henk/code/inspircd.git] / src / modules / m_helpop.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "users.h"
15 #include "channels.h"
16 #include "modules.h"
17 #include "inspircd.h"
18
19 /* $ModDesc: /helpop Command, Works like Unreal helpop */
20 static std::map<irc::string, std::string> helpop_map;
21
22
23 /** Handles user mode +h
24  */
25 class Helpop : public ModeHandler
26 {
27  public:
28         Helpop(InspIRCd* Instance) : ModeHandler(Instance, 'h', 0, 0, false, MODETYPE_USER, true) { }
29
30         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
31         {
32                 if (adding)
33                 {
34                         if (!dest->IsModeSet('h'))
35                         {
36                                 dest->SetMode('h',true);
37                                 return MODEACTION_ALLOW;
38                         }
39                 }
40                 else
41                 {
42                         if (dest->IsModeSet('h'))
43                         {
44                                 dest->SetMode('h',false);
45                                 return MODEACTION_ALLOW;
46                         }
47                 }
48
49                 return MODEACTION_DENY;
50         }
51 };
52
53 /** Handles /HELPOP
54  */
55 class cmd_helpop : public command_t
56 {
57  public:
58          cmd_helpop (InspIRCd* Instance) : command_t(Instance, "HELPOP", 0, 1)
59          {
60                  this->source = "m_helpop.so";
61                  syntax = "<any-text>";
62          }
63
64         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
65         {               
66                 irc::string parameter = parameters[0];
67
68                 if (parameter == "index")
69                 {
70                         /* iterate over all helpop items */
71                         user->WriteServ("NOTICE %s :HELPOP topic index", user->nick);
72                         for (std::map<irc::string, std::string>::iterator iter = helpop_map.begin(); iter != helpop_map.end(); iter++)
73                         {
74                                 user->WriteServ("NOTICE %s :    %s", user->nick, iter->first.c_str());                          
75                         }
76                         user->WriteServ("NOTICE %s :*** End of HELPOP topic index", user->nick);
77                 }
78                 else
79                 {
80                         user->WriteServ("NOTICE %s :*** HELPOP for %s", user->nick, parameters[0]);
81
82                         std::map<irc::string, std::string>::iterator iter = helpop_map.find(parameter);
83
84                         if (iter == helpop_map.end())
85                         {
86                                 iter = helpop_map.find("nohelp");
87                         }
88
89                         std::string value = iter->second;
90                         irc::sepstream stream(value, '\n');
91                         std::string token = "*";
92
93                         while ((token = stream.GetToken()) != "")
94                         {
95                                 user->WriteServ("NOTICE %s :%s", user->nick, token.c_str());
96                         }
97
98                         user->WriteServ("NOTICE %s :*** End of HELPOP", user->nick);
99                 }
100
101                 /* We dont want these going out over the network, return CMD_FAILURE
102                  * to make sure the protocol module thinks theyre not worth sending.
103                  */
104                 return CMD_FAILURE;
105         }
106 };
107
108 class ModuleHelpop : public Module
109 {
110         private:
111                 std::string  h_file;
112                 cmd_helpop* mycommand;
113                 Helpop* ho;
114
115         public:
116                 ModuleHelpop(InspIRCd* Me)
117                         : Module::Module(Me)
118                 {
119                         ReadConfig();
120                         ho = new Helpop(ServerInstance);
121                         if (!ServerInstance->AddMode(ho, 'h'))
122                                 throw ModuleException("Could not add new modes!");
123                         mycommand = new cmd_helpop(ServerInstance);
124                         ServerInstance->AddCommand(mycommand);
125                 }
126
127                 virtual void ReadConfig()
128                 {
129                         ConfigReader *MyConf = new ConfigReader(ServerInstance);
130
131                         helpop_map.clear();
132
133                         for (int i = 0; i < MyConf->Enumerate("helpop"); i++)
134                         {
135                                 irc::string key = assign(MyConf->ReadValue("helpop", "key", i));
136                                 std::string value = MyConf->ReadValue("helpop", "value", i, true); /* Linefeeds allowed! */
137
138                                 if (key == "index")
139                                 {
140                                         throw ModuleException("m_helpop: The key 'index' is reserved for internal purposes. Please remove it.");
141                                 }
142
143                                 helpop_map[key] = value;
144                         }
145
146                         if (helpop_map.find("start") == helpop_map.end())
147                         {
148                                 // error!
149                                 throw ModuleException("m_helpop: Helpop file is missing important entries. Please check the example conf.");
150                         }
151                         else if (helpop_map.find("nohelp") == helpop_map.end())
152                         {
153                                 // error!
154                                 throw ModuleException("m_helpop: Helpop file is missing important entries. Please check the example conf.");
155                         }
156
157                 }
158
159                 void Implements(char* List)
160                 {
161                         List[I_OnRehash] = List[I_OnWhois] = 1;
162                 }
163
164                 virtual void OnRehash(const std::string &parameter)
165                 {
166                         ReadConfig();
167                 }
168
169                 virtual void OnWhois(userrec* src, userrec* 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                         DELETE(ho);
181                 }
182         
183                 virtual Version GetVersion()
184                 {
185                         return Version(1,1,0,1,VF_COMMON|VF_VENDOR,API_VERSION);
186                 }
187 };
188
189 class ModuleHelpopFactory : public ModuleFactory
190 {
191  public:
192         ModuleHelpopFactory()
193         {
194         }
195         
196         ~ModuleHelpopFactory()
197         {
198         }
199         
200         virtual Module * CreateModule(InspIRCd* Me)
201         {
202                 return new ModuleHelpop(Me);
203         }
204         
205 };
206
207 extern "C" void * init_module( void )
208 {
209         return new ModuleHelpopFactory;
210 }