]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_helpop.cpp
mass tidyup, change A LOT of stuff to const char** which was char** (such as paramete...
[user/henk/code/inspircd.git] / src / modules / m_helpop.cpp
1 /*   +------------------------------------+
2  *   | Inspire Internet Relay Chat Daemon |
3  *   +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *   E-mail:
7  *<brain@chatspike.net>
8  *        <Craig@chatspike.net>
9  * 
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include "users.h"
20 #include "channels.h"
21 #include "modules.h"
22 #include "helperfuncs.h"
23
24 // Global Vars
25 static ConfigReader *helpop;
26 static Server *Srv;
27
28 bool do_helpop(const char**, int, userrec*);
29 void sendtohelpop(userrec*, int, const char**);
30
31 /* $ModDesc: /helpop Command, Works like Unreal helpop */
32
33 class Helpop : public ModeHandler
34 {
35  public:
36         Helpop() : ModeHandler('h', 0, 0, false, MODETYPE_USER, true) { }
37
38         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
39         {
40                 if (adding)
41                 {
42                         if (!dest->IsModeSet('h'))
43                         {
44                                 dest->SetMode('h',true);
45                                 return MODEACTION_ALLOW;
46                         }
47                 }
48                 else
49                 {
50                         if (dest->IsModeSet('h'))
51                         {
52                                 dest->SetMode('h',false);
53                                 return MODEACTION_ALLOW;
54                         }
55                 }
56
57                 return MODEACTION_DENY;
58         }
59 };
60
61 class cmd_helpop : public command_t
62 {
63  public:
64          cmd_helpop () : command_t("HELPOP",0,1)
65          {
66                  this->source = "m_helpop.so";
67          }
68
69         void Handle (const char** parameters, int pcnt, userrec *user)
70         {
71                 char a[MAXBUF];
72                 std::string output = " ";
73
74                 if (!helpop)
75                         return;
76
77                 if (pcnt < 1)
78                 {
79                         do_helpop(NULL,pcnt,user);
80                         return;
81                 }
82
83                 if (*parameters[0] == '!')
84                 {
85                         // Force send to all +h users
86                         sendtohelpop(user, pcnt, parameters);
87                 }
88                 else if (*parameters[0] == '?')
89                 {
90                         // Force to the helpop system with no forward if not found.
91                         if (do_helpop(parameters, pcnt, user) == false)
92                         {
93                                 // Not handled by the Database, Tell the user, and bail.
94                                 for (int i = 1; output != ""; i++)
95                                 {
96                                         snprintf(a,MAXBUF,"line%d",i);
97                                         output = helpop->ReadValue("nohelp", std::string(a), 0);
98         
99                                         if(output != "")
100                                         {
101                                                 Srv->SendTo(NULL,user,"290 "+std::string(user->nick)+" :"+output);
102                                         }
103                                 }
104                         }
105                 }
106                 else
107                 {
108                         // Check with the helpop database, if not found send to +h
109                         if (do_helpop(parameters, pcnt, user) == false)
110                         {
111                                 // Not handled by the Database, Tell the user, and forward.
112                                 for (int i = 1; output != ""; i++)
113                                 {
114                                         snprintf(a,MAXBUF,"line%d",i);
115                                         /* "nohelpo" for opers "nohelp" for users */
116                                         output = helpop->ReadValue("nohelpo", std::string(a), 0);
117                                         if (output != "")
118                                         {
119                                                 Srv->SendTo(NULL,user,"290 "+std::string(user->nick)+" :"+output);
120                                         }
121                                 }
122                                 // Forward.
123                                 sendtohelpop(user, pcnt, parameters);
124                         }
125                 }
126         }
127 };
128
129
130 bool do_helpop(const char** parameters, int pcnt, userrec *src)
131 {
132         char search[MAXBUF];
133         std::string output = " "; // a fix bought to you by brain :p
134         char a[MAXBUF];
135         int nlines = 0;
136
137         if (!pcnt)
138         {
139                 strcpy(search,"start");
140         }
141         else
142         {
143                 if (*parameters[0] == '?')
144                         parameters[0]++;
145                 strlcpy(search,parameters[0],MAXBUF);
146         }
147
148         strlower(search);
149
150         for (int i = 1; output != ""; i++)
151         {
152                 snprintf(a,MAXBUF,"line%d",i);
153                 output = helpop->ReadValue(search, a, 0);
154                 if (output != "")
155                 {
156                         Srv->SendTo(NULL,src,"290 "+std::string(src->nick)+" :"+output);
157                         nlines++;
158                 }
159         }
160         return (nlines>0);
161 }
162
163
164
165 void sendtohelpop(userrec *src, int pcnt, const char **params)
166 {
167         const char* first = params[0];
168         if (*first == '!')
169         {
170                 first++;
171         }
172
173         std::string line = "*** HELPOPS - From "+std::string(src->nick)+": "+std::string(first)+" ";
174         for (int i = 1; i < pcnt; i++)
175         {
176                 line = line + std::string(params[i]) + " ";
177         }
178         Srv->SendToModeMask("oh",WM_AND,line);
179 }
180
181 class HelpopException : public ModuleException
182 {
183  private:
184         std::string err;
185  public:
186         HelpopException(std::string message) : err(message) { }
187         virtual const char* GetReason() { return err.c_str(); }
188 };
189
190 class ModuleHelpop : public Module
191 {
192         private:
193                 ConfigReader *conf;
194                 std::string  h_file;
195                 cmd_helpop* mycommand;
196                 Helpop* ho;
197
198         public:
199                 ModuleHelpop(Server* Me)
200                         : Module::Module(Me)
201                 {
202                         Srv  = Me;
203
204                         ReadConfig();
205                         ho = new Helpop();
206                         Srv->AddMode(ho, 'h');
207                         mycommand = new cmd_helpop();
208                         Srv->AddCommand(mycommand);
209                 }
210
211                 virtual void ReadConfig()
212                 {
213                         conf = new ConfigReader;
214                         h_file = conf->ReadValue("helpop", "file", 0);
215
216                         if (h_file == "")
217                         {
218                                 helpop = NULL;
219                                 HelpopException e("Missing helpop file");
220                                 throw(e);
221                         }
222
223                         helpop = new ConfigReader(h_file);
224                         if ((helpop->ReadValue("nohelp",  "line1", 0) == "") ||
225                                 (helpop->ReadValue("nohelpo", "line1", 0) == "") ||
226                                 (helpop->ReadValue("start",   "line1", 0) == ""))
227                         {
228                                 HelpopException e("m_helpop: Helpop file is missing important entries. Please check the example conf.");
229                                 throw(e);
230                         }
231                 }
232
233                 void Implements(char* List)
234                 {
235                         List[I_OnRehash] = List[I_OnWhois] = 1;
236                 }
237
238                 virtual void OnRehash(const std::string &parameter)
239                 {
240                         DELETE(conf);
241                         if (helpop)
242                                 DELETE(helpop);
243
244                         ReadConfig();
245                 }
246
247                 virtual void OnWhois(userrec* src, userrec* dst)
248                 {
249                         if (dst->IsModeSet('h'))
250                         {
251                                 Srv->SendTo(NULL,src,"310 "+std::string(src->nick)+" "+std::string(dst->nick)+" :is available for help.");
252                         }
253                 }
254
255                 virtual ~ModuleHelpop()
256                 {
257                         DELETE(conf);
258                         DELETE(helpop);
259                         DELETE(ho);
260                 }
261         
262                 virtual Version GetVersion()
263                 {
264                         return Version(1,0,0,1,VF_STATIC|VF_VENDOR);
265                 }
266 };
267
268 class ModuleHelpopFactory : public ModuleFactory
269 {
270  public:
271         ModuleHelpopFactory()
272         {
273         }
274         
275         ~ModuleHelpopFactory()
276         {
277         }
278         
279         virtual Module * CreateModule(Server* Me)
280         {
281                 return new ModuleHelpop(Me);
282         }
283         
284 };
285
286 extern "C" void * init_module( void )
287 {
288         return new ModuleHelpopFactory;
289 }