]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_helpop.cpp
So much stuff changed in this one, i forgot most of it.
[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(Server* Me)
205                         : Module::Module(Me)
206                 {
207                         Srv  = Me;
208
209                         ReadConfig();
210                         ho = new Helpop(ServerInstance);
211                         Srv->AddMode(ho, 'h');
212                         mycommand = new cmd_helpop();
213                         Srv->AddCommand(mycommand);
214                 }
215
216                 virtual void ReadConfig()
217                 {
218                         conf = new ConfigReader;
219                         h_file = conf->ReadValue("helpop", "file", 0);
220
221                         if (h_file == "")
222                         {
223                                 helpop = NULL;
224                                 HelpopException e("Missing helpop file");
225                                 throw(e);
226                         }
227
228                         helpop = new ConfigReader(h_file);
229                         if ((helpop->ReadValue("nohelp",  "line1", 0) == "") ||
230                                 (helpop->ReadValue("nohelpo", "line1", 0) == "") ||
231                                 (helpop->ReadValue("start",   "line1", 0) == ""))
232                         {
233                                 HelpopException e("m_helpop: Helpop file is missing important entries. Please check the example conf.");
234                                 throw(e);
235                         }
236                 }
237
238                 void Implements(char* List)
239                 {
240                         List[I_OnRehash] = List[I_OnWhois] = 1;
241                 }
242
243                 virtual void OnRehash(const std::string &parameter)
244                 {
245                         DELETE(conf);
246                         if (helpop)
247                                 DELETE(helpop);
248
249                         ReadConfig();
250                 }
251
252                 virtual void OnWhois(userrec* src, userrec* dst)
253                 {
254                         if (dst->IsModeSet('h'))
255                         {
256                                 src->WriteServ("310 "+std::string(src->nick)+" "+std::string(dst->nick)+" :is available for help.");
257                         }
258                 }
259
260                 virtual ~ModuleHelpop()
261                 {
262                         DELETE(conf);
263                         DELETE(helpop);
264                         DELETE(ho);
265                 }
266         
267                 virtual Version GetVersion()
268                 {
269                         return Version(1,0,0,1,VF_STATIC|VF_VENDOR);
270                 }
271 };
272
273 class ModuleHelpopFactory : public ModuleFactory
274 {
275  public:
276         ModuleHelpopFactory()
277         {
278         }
279         
280         ~ModuleHelpopFactory()
281         {
282         }
283         
284         virtual Module * CreateModule(Server* Me)
285         {
286                 return new ModuleHelpop(Me);
287         }
288         
289 };
290
291 extern "C" void * init_module( void )
292 {
293         return new ModuleHelpopFactory;
294 }