]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_helpop.cpp
Just to mess with om's head, remove helperfuncs.h from everywhere
[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
23 #include "inspircd.h"
24
25 // Global Vars
26 static ConfigReader *helpop;
27
28
29
30 bool do_helpop(const char**, int, userrec*);
31 void sendtohelpop(userrec*, int, const char**);
32
33 /* $ModDesc: /helpop Command, Works like Unreal helpop */
34
35 class Helpop : public ModeHandler
36 {
37  public:
38         Helpop(InspIRCd* Instance) : ModeHandler(Instance, 'h', 0, 0, false, MODETYPE_USER, true) { }
39
40         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* 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 class cmd_helpop : public command_t
64 {
65  public:
66          cmd_helpop (InspIRCd* Instance) : command_t(Instance, "HELPOP", 0, 1)
67          {
68                  this->source = "m_helpop.so";
69                  syntax = "[?|!]<any-text>";
70          }
71
72         void Handle (const char** parameters, int pcnt, userrec *user)
73         {
74                 char a[MAXBUF];
75                 std::string output = " ";
76
77                 if (!helpop)
78                         return;
79
80                 if (pcnt < 1)
81                 {
82                         do_helpop(NULL,pcnt,user);
83                         return;
84                 }
85
86                 if (*parameters[0] == '!')
87                 {
88                         // Force send to all +h users
89                         sendtohelpop(user, pcnt, parameters);
90                 }
91                 else if (*parameters[0] == '?')
92                 {
93                         // Force to the helpop system with no forward if not found.
94                         if (do_helpop(parameters, pcnt, user) == false)
95                         {
96                                 // Not handled by the Database, Tell the user, and bail.
97                                 for (int i = 1; output != ""; i++)
98                                 {
99                                         snprintf(a,MAXBUF,"line%d",i);
100                                         output = helpop->ReadValue("nohelp", std::string(a), 0);
101         
102                                         if(output != "")
103                                         {
104                                                 user->WriteServ("290 "+std::string(user->nick)+" :"+output);
105                                         }
106                                 }
107                         }
108                 }
109                 else
110                 {
111                         // Check with the helpop database, if not found send to +h
112                         if (do_helpop(parameters, pcnt, user) == false)
113                         {
114                                 // Not handled by the Database, Tell the user, and forward.
115                                 for (int i = 1; output != ""; i++)
116                                 {
117                                         snprintf(a,MAXBUF,"line%d",i);
118                                         /* "nohelpo" for opers "nohelp" for users */
119                                         output = helpop->ReadValue("nohelpo", std::string(a), 0);
120                                         if (output != "")
121                                         {
122                                                 user->WriteServ("290 "+std::string(user->nick)+" :"+output);
123                                         }
124                                 }
125                                 // Forward.
126                                 sendtohelpop(user, pcnt, parameters);
127                         }
128                 }
129         }
130
131
132         bool do_helpop(const char** parameters, int pcnt, userrec *src)
133         {
134                 char search[MAXBUF];
135                 std::string output = " "; // a fix bought to you by brain :p
136                 char a[MAXBUF];
137                 int nlines = 0;
138
139                 if (!pcnt)
140                 {
141                         strcpy(search,"start");
142                 }
143                 else
144                 {
145                         if (*parameters[0] == '?')
146                                 parameters[0]++;
147                         strlcpy(search,parameters[0],MAXBUF);
148                 }
149
150                 for (char* n = search; *n; n++)
151                         *n = tolower(*n);
152
153                 for (int i = 1; output != ""; i++)
154                 {
155                         snprintf(a,MAXBUF,"line%d",i);
156                         output = helpop->ReadValue(search, a, 0);
157                         if (output != "")
158                         {
159                                 src->WriteServ("290 "+std::string(src->nick)+" :"+output);
160                                 nlines++;
161                         }
162                 }
163                 return (nlines>0);
164         }
165
166         void sendtohelpop(userrec *src, int pcnt, const char **params)
167         {
168                 const char* first = params[0];
169                 if (*first == '!')
170                 {
171                         first++;
172                 }
173
174                 std::string line = "*** HELPOPS - From "+std::string(src->nick)+": "+std::string(first)+" ";
175                 for (int i = 1; i < pcnt; i++)
176                 {
177                         line = line + std::string(params[i]) + " ";
178                 }
179                 ServerInstance->WriteMode("oh",WM_AND,line.c_str());
180         }
181 };
182
183 class HelpopException : public ModuleException
184 {
185  private:
186         std::string err;
187  public:
188         HelpopException(std::string message) : err(message) { }
189         virtual const char* GetReason() { return err.c_str(); }
190 };
191
192 class ModuleHelpop : public Module
193 {
194         private:
195                 ConfigReader *conf;
196                 std::string  h_file;
197                 cmd_helpop* mycommand;
198                 Helpop* ho;
199
200         public:
201                 ModuleHelpop(InspIRCd* Me)
202                         : Module::Module(Me)
203                 {
204                         ReadConfig();
205                         ho = new Helpop(ServerInstance);
206                         ServerInstance->AddMode(ho, 'h');
207                         mycommand = new cmd_helpop(ServerInstance);
208                         ServerInstance->AddCommand(mycommand);
209                 }
210
211                 virtual void ReadConfig()
212                 {
213                         conf = new ConfigReader(ServerInstance);
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(ServerInstance, 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                                 src->WriteServ("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(InspIRCd* Me)
280         {
281                 return new ModuleHelpop(Me);
282         }
283         
284 };
285
286 extern "C" void * init_module( void )
287 {
288         return new ModuleHelpopFactory;
289 }