]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_helpop.cpp
WHEEEEE!!!!!
[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(const char**, int, userrec*);
29 void sendtohelpop(userrec*, int, const 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                  syntax = "[?|!]<any-text>";
68          }
69
70         void Handle (const char** parameters, int pcnt, userrec *user)
71         {
72                 char a[MAXBUF];
73                 std::string output = " ";
74
75                 if (!helpop)
76                         return;
77
78                 if (pcnt < 1)
79                 {
80                         do_helpop(NULL,pcnt,user);
81                         return;
82                 }
83
84                 if (*parameters[0] == '!')
85                 {
86                         // Force send to all +h users
87                         sendtohelpop(user, pcnt, parameters);
88                 }
89                 else if (*parameters[0] == '?')
90                 {
91                         // Force to the helpop system with no forward if not found.
92                         if (do_helpop(parameters, pcnt, user) == false)
93                         {
94                                 // Not handled by the Database, Tell the user, and bail.
95                                 for (int i = 1; output != ""; i++)
96                                 {
97                                         snprintf(a,MAXBUF,"line%d",i);
98                                         output = helpop->ReadValue("nohelp", std::string(a), 0);
99         
100                                         if(output != "")
101                                         {
102                                                 user->WriteServ("290 "+std::string(user->nick)+" :"+output);
103                                         }
104                                 }
105                         }
106                 }
107                 else
108                 {
109                         // Check with the helpop database, if not found send to +h
110                         if (do_helpop(parameters, pcnt, user) == false)
111                         {
112                                 // Not handled by the Database, Tell the user, and forward.
113                                 for (int i = 1; output != ""; i++)
114                                 {
115                                         snprintf(a,MAXBUF,"line%d",i);
116                                         /* "nohelpo" for opers "nohelp" for users */
117                                         output = helpop->ReadValue("nohelpo", std::string(a), 0);
118                                         if (output != "")
119                                         {
120                                                 user->WriteServ("290 "+std::string(user->nick)+" :"+output);
121                                         }
122                                 }
123                                 // Forward.
124                                 sendtohelpop(user, pcnt, parameters);
125                         }
126                 }
127         }
128 };
129
130
131 bool do_helpop(const char** parameters, int pcnt, userrec *src)
132 {
133         char search[MAXBUF];
134         std::string output = " "; // a fix bought to you by brain :p
135         char a[MAXBUF];
136         int nlines = 0;
137
138         if (!pcnt)
139         {
140                 strcpy(search,"start");
141         }
142         else
143         {
144                 if (*parameters[0] == '?')
145                         parameters[0]++;
146                 strlcpy(search,parameters[0],MAXBUF);
147         }
148
149         strlower(search);
150
151         for (int i = 1; output != ""; i++)
152         {
153                 snprintf(a,MAXBUF,"line%d",i);
154                 output = helpop->ReadValue(search, a, 0);
155                 if (output != "")
156                 {
157                         src->WriteServ("290 "+std::string(src->nick)+" :"+output);
158                         nlines++;
159                 }
160         }
161         return (nlines>0);
162 }
163
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         Srv->SendToModeMask("oh",WM_AND,line);
180 }
181
182 class HelpopException : public ModuleException
183 {
184  private:
185         std::string err;
186  public:
187         HelpopException(std::string message) : err(message) { }
188         virtual const char* GetReason() { return err.c_str(); }
189 };
190
191 class ModuleHelpop : public Module
192 {
193         private:
194                 ConfigReader *conf;
195                 std::string  h_file;
196                 cmd_helpop* mycommand;
197                 Helpop* ho;
198
199         public:
200                 ModuleHelpop(Server* Me)
201                         : Module::Module(Me)
202                 {
203                         Srv  = Me;
204
205                         ReadConfig();
206                         ho = new Helpop();
207                         Srv->AddMode(ho, 'h');
208                         mycommand = new cmd_helpop();
209                         Srv->AddCommand(mycommand);
210                 }
211
212                 virtual void ReadConfig()
213                 {
214                         conf = new ConfigReader;
215                         h_file = conf->ReadValue("helpop", "file", 0);
216
217                         if (h_file == "")
218                         {
219                                 helpop = NULL;
220                                 HelpopException e("Missing helpop file");
221                                 throw(e);
222                         }
223
224                         helpop = new ConfigReader(h_file);
225                         if ((helpop->ReadValue("nohelp",  "line1", 0) == "") ||
226                                 (helpop->ReadValue("nohelpo", "line1", 0) == "") ||
227                                 (helpop->ReadValue("start",   "line1", 0) == ""))
228                         {
229                                 HelpopException e("m_helpop: Helpop file is missing important entries. Please check the example conf.");
230                                 throw(e);
231                         }
232                 }
233
234                 void Implements(char* List)
235                 {
236                         List[I_OnRehash] = List[I_OnWhois] = 1;
237                 }
238
239                 virtual void OnRehash(const std::string &parameter)
240                 {
241                         DELETE(conf);
242                         if (helpop)
243                                 DELETE(helpop);
244
245                         ReadConfig();
246                 }
247
248                 virtual void OnWhois(userrec* src, userrec* dst)
249                 {
250                         if (dst->IsModeSet('h'))
251                         {
252                                 src->WriteServ("310 "+std::string(src->nick)+" "+std::string(dst->nick)+" :is available for help.");
253                         }
254                 }
255
256                 virtual ~ModuleHelpop()
257                 {
258                         DELETE(conf);
259                         DELETE(helpop);
260                         DELETE(ho);
261                 }
262         
263                 virtual Version GetVersion()
264                 {
265                         return Version(1,0,0,1,VF_STATIC|VF_VENDOR);
266                 }
267 };
268
269 class ModuleHelpopFactory : public ModuleFactory
270 {
271  public:
272         ModuleHelpopFactory()
273         {
274         }
275         
276         ~ModuleHelpopFactory()
277         {
278         }
279         
280         virtual Module * CreateModule(Server* Me)
281         {
282                 return new ModuleHelpop(Me);
283         }
284         
285 };
286
287 extern "C" void * init_module( void )
288 {
289         return new ModuleHelpopFactory;
290 }