]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_helpop.cpp
Insert massive change here.
[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 "inspircd.h"
23
24 // Global Vars
25 static ConfigReader *helpop;
26
27 bool do_helpop(const char**, int, userrec*);
28 void sendtohelpop(userrec*, int, const char**);
29
30 /* $ModDesc: /helpop Command, Works like Unreal helpop */
31
32 /** Handles user mode +h
33  */
34 class Helpop : public ModeHandler
35 {
36  public:
37         Helpop(InspIRCd* Instance) : ModeHandler(Instance, 'h', 0, 0, false, MODETYPE_USER, true) { }
38
39         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
40         {
41                 if (adding)
42                 {
43                         if (!dest->IsModeSet('h'))
44                         {
45                                 dest->SetMode('h',true);
46                                 return MODEACTION_ALLOW;
47                         }
48                 }
49                 else
50                 {
51                         if (dest->IsModeSet('h'))
52                         {
53                                 dest->SetMode('h',false);
54                                 return MODEACTION_ALLOW;
55                         }
56                 }
57
58                 return MODEACTION_DENY;
59         }
60 };
61
62 /** Handles /HELPOP
63  */
64 class cmd_helpop : public command_t
65 {
66  public:
67          cmd_helpop (InspIRCd* Instance) : command_t(Instance, "HELPOP", 0, 1)
68          {
69                  this->source = "m_helpop.so";
70                  syntax = "[?|!]<any-text>";
71          }
72
73         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
74         {
75                 char a[MAXBUF];
76                 std::string output = " ";
77
78                 if (!helpop)
79                         return CMD_FAILURE;
80
81                 if (pcnt < 1)
82                 {
83                         do_helpop(NULL,pcnt,user);
84                         return CMD_SUCCESS;
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                 return CMD_SUCCESS;
132         }
133
134
135         bool do_helpop(const char** parameters, int pcnt, userrec *src)
136         {
137                 char search[MAXBUF];
138                 std::string output = " "; // a fix bought to you by brain :p
139                 char a[MAXBUF];
140                 int nlines = 0;
141
142                 if (!pcnt)
143                 {
144                         strcpy(search,"start");
145                 }
146                 else
147                 {
148                         if (*parameters[0] == '?')
149                                 parameters[0]++;
150                         strlcpy(search,parameters[0],MAXBUF);
151                 }
152
153                 for (char* n = search; *n; n++)
154                         *n = tolower(*n);
155
156                 for (int i = 1; output != ""; i++)
157                 {
158                         snprintf(a,MAXBUF,"line%d",i);
159                         output = helpop->ReadValue(search, a, 0);
160                         if (output != "")
161                         {
162                                 src->WriteServ("290 "+std::string(src->nick)+" :"+output);
163                                 nlines++;
164                         }
165                 }
166                 return (nlines>0);
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
186 /** Thrown by m_helpop
187  */
188 class HelpopException : public ModuleException
189 {
190  private:
191         std::string err;
192  public:
193         HelpopException(std::string message) : err(message) { }
194         virtual const char* GetReason() { return err.c_str(); }
195 };
196
197 class ModuleHelpop : public Module
198 {
199         private:
200                 ConfigReader *conf;
201                 std::string  h_file;
202                 cmd_helpop* mycommand;
203                 Helpop* ho;
204
205         public:
206                 ModuleHelpop(InspIRCd* Me)
207                         : Module::Module(Me)
208                 {
209                         ReadConfig();
210                         ho = new Helpop(ServerInstance);
211                         ServerInstance->AddMode(ho, 'h');
212                         mycommand = new cmd_helpop(ServerInstance);
213                         ServerInstance->AddCommand(mycommand);
214                 }
215
216                 virtual void ReadConfig()
217                 {
218                         conf = new ConfigReader(ServerInstance);
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(ServerInstance, 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                         ServerInstance->Modes->DelMode(ho);
263                         DELETE(conf);
264                         DELETE(helpop);
265                         DELETE(ho);
266                 }
267         
268                 virtual Version GetVersion()
269                 {
270                         return Version(1,1,0,1,VF_COMMON|VF_VENDOR,API_VERSION);
271                 }
272 };
273
274 class ModuleHelpopFactory : public ModuleFactory
275 {
276  public:
277         ModuleHelpopFactory()
278         {
279         }
280         
281         ~ModuleHelpopFactory()
282         {
283         }
284         
285         virtual Module * CreateModule(InspIRCd* Me)
286         {
287                 return new ModuleHelpop(Me);
288         }
289         
290 };
291
292 extern "C" void * init_module( void )
293 {
294         return new ModuleHelpopFactory;
295 }