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