]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_helpop.cpp
All this works now (hopefully)
[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(char**, int, userrec*);
29 void sendtohelpop(userrec*, int, 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 (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][0] == '!')
84                 {
85                         // Force send to all +h users
86                         sendtohelpop(user, pcnt, parameters);
87                 }
88                 else if (parameters[0][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(char **parameters, int pcnt, userrec *src)
131 {
132         char *search;
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                 search = "start";
140         }
141         else
142         {
143                 search = parameters[0];
144         }
145
146         if (search[0] == '?')
147         {
148                 search++;
149         }
150
151         strlower(search);
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                         Srv->SendTo(NULL,src,"290 "+std::string(src->nick)+" :"+output);
160                         nlines++;
161                 }
162         }
163         return (nlines>0);
164 }
165
166
167
168 void sendtohelpop(userrec *src, int pcnt, char **params)
169 {
170         char* first = params[0];
171         if (*first == '!')
172         {
173                 first++;
174         }
175
176         std::string line = "*** HELPOPS - From "+std::string(src->nick)+": "+std::string(first)+" ";
177         for (int i = 1; i < pcnt; i++)
178         {
179                 line = line + std::string(params[i]) + " ";
180         }
181         Srv->SendToModeMask("oh",WM_AND,line);
182 }
183
184 class HelpopException : public ModuleException
185 {
186  private:
187         std::string err;
188  public:
189         HelpopException(std::string message) : err(message) { }
190         virtual const char* GetReason() { return err.c_str(); }
191 };
192
193 class ModuleHelpop : public Module
194 {
195         private:
196                 ConfigReader *conf;
197                 std::string  h_file;
198                 cmd_helpop* mycommand;
199                 Helpop* ho;
200
201         public:
202                 ModuleHelpop(Server* Me)
203                         : Module::Module(Me)
204                 {
205                         Srv  = Me;
206
207                         ReadConfig();
208                         ho = new Helpop();
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                                 Srv->SendTo(NULL,src,"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(Server* Me)
283         {
284                 return new ModuleHelpop(Me);
285         }
286         
287 };
288
289 extern "C" void * init_module( void )
290 {
291         return new ModuleHelpopFactory;
292 }