]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_helpop.cpp
4d4263d280142e0a2646f747d2c3cbdd17b4062a
[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
28 extern InspIRCd* ServerInstance;
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
133 bool do_helpop(const char** parameters, int pcnt, userrec *src)
134 {
135         char search[MAXBUF];
136         std::string output = " "; // a fix bought to you by brain :p
137         char a[MAXBUF];
138         int nlines = 0;
139
140         if (!pcnt)
141         {
142                 strcpy(search,"start");
143         }
144         else
145         {
146                 if (*parameters[0] == '?')
147                         parameters[0]++;
148                 strlcpy(search,parameters[0],MAXBUF);
149         }
150
151         for (char* n = search; *n; n++)
152                 *n = tolower(*n);
153
154         for (int i = 1; output != ""; i++)
155         {
156                 snprintf(a,MAXBUF,"line%d",i);
157                 output = helpop->ReadValue(search, a, 0);
158                 if (output != "")
159                 {
160                         src->WriteServ("290 "+std::string(src->nick)+" :"+output);
161                         nlines++;
162                 }
163         }
164         return (nlines>0);
165 }
166
167
168
169 void sendtohelpop(userrec *src, int pcnt, const char **params)
170 {
171         const char* first = params[0];
172         if (*first == '!')
173         {
174                 first++;
175         }
176
177         std::string line = "*** HELPOPS - From "+std::string(src->nick)+": "+std::string(first)+" ";
178         for (int i = 1; i < pcnt; i++)
179         {
180                 line = line + std::string(params[i]) + " ";
181         }
182         ServerInstance->WriteMode("oh",WM_AND,line.c_str());
183 }
184
185 class HelpopException : public ModuleException
186 {
187  private:
188         std::string err;
189  public:
190         HelpopException(std::string message) : err(message) { }
191         virtual const char* GetReason() { return err.c_str(); }
192 };
193
194 class ModuleHelpop : public Module
195 {
196         private:
197                 ConfigReader *conf;
198                 std::string  h_file;
199                 cmd_helpop* mycommand;
200                 Helpop* ho;
201
202         public:
203                 ModuleHelpop(InspIRCd* Me)
204                         : Module::Module(Me)
205                 {
206                         ReadConfig();
207                         ho = new Helpop(ServerInstance);
208                         ServerInstance->AddMode(ho, 'h');
209                         mycommand = new cmd_helpop(ServerInstance);
210                         ServerInstance->AddCommand(mycommand);
211                 }
212
213                 virtual void ReadConfig()
214                 {
215                         conf = new ConfigReader;
216                         h_file = conf->ReadValue("helpop", "file", 0);
217
218                         if (h_file == "")
219                         {
220                                 helpop = NULL;
221                                 HelpopException e("Missing helpop file");
222                                 throw(e);
223                         }
224
225                         helpop = new ConfigReader(h_file);
226                         if ((helpop->ReadValue("nohelp",  "line1", 0) == "") ||
227                                 (helpop->ReadValue("nohelpo", "line1", 0) == "") ||
228                                 (helpop->ReadValue("start",   "line1", 0) == ""))
229                         {
230                                 HelpopException e("m_helpop: Helpop file is missing important entries. Please check the example conf.");
231                                 throw(e);
232                         }
233                 }
234
235                 void Implements(char* List)
236                 {
237                         List[I_OnRehash] = List[I_OnWhois] = 1;
238                 }
239
240                 virtual void OnRehash(const std::string &parameter)
241                 {
242                         DELETE(conf);
243                         if (helpop)
244                                 DELETE(helpop);
245
246                         ReadConfig();
247                 }
248
249                 virtual void OnWhois(userrec* src, userrec* dst)
250                 {
251                         if (dst->IsModeSet('h'))
252                         {
253                                 src->WriteServ("310 "+std::string(src->nick)+" "+std::string(dst->nick)+" :is available for help.");
254                         }
255                 }
256
257                 virtual ~ModuleHelpop()
258                 {
259                         DELETE(conf);
260                         DELETE(helpop);
261                         DELETE(ho);
262                 }
263         
264                 virtual Version GetVersion()
265                 {
266                         return Version(1,0,0,1,VF_STATIC|VF_VENDOR);
267                 }
268 };
269
270 class ModuleHelpopFactory : public ModuleFactory
271 {
272  public:
273         ModuleHelpopFactory()
274         {
275         }
276         
277         ~ModuleHelpopFactory()
278         {
279         }
280         
281         virtual Module * CreateModule(InspIRCd* Me)
282         {
283                 return new ModuleHelpop(Me);
284         }
285         
286 };
287
288 extern "C" void * init_module( void )
289 {
290         return new ModuleHelpopFactory;
291 }