]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_helpop.cpp
Inherit m_filter.cpp and m_filter_pcre.cpp from a base set of classes, so that change...
[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("helpop_nohelp", 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("helpop_nohelpo", 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,"helpop_start");
145                 }
146                 else
147                 {
148                         if (*parameters[0] == '?')
149                                 parameters[0]++;
150                         strlcpy(search, "helpop_", MAXBUF);
151                         strlcat(search, parameters[0], MAXBUF);
152                 }
153
154                 for (char* n = search; *n; n++)
155                         *n = tolower(*n);
156
157                 for (int i = 1; output != ""; i++)
158                 {
159                         snprintf(a,MAXBUF,"line%d",i);
160                         output = helpop->ReadValue(search, a, 0);
161                         if (output != "")
162                         {
163                                 src->WriteServ("290 "+std::string(src->nick)+" :"+output);
164                                 nlines++;
165                         }
166                 }
167                 return (nlines>0);
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
187 /** Thrown by m_helpop
188  */
189 class HelpopException : public ModuleException
190 {
191  private:
192         std::string err;
193  public:
194         HelpopException(std::string message) : err(message) { }
195         virtual const char* GetReason() { return err.c_str(); }
196 };
197
198 class ModuleHelpop : public Module
199 {
200         private:
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                         helpop = new ConfigReader(ServerInstance);
219                         if ((helpop->ReadValue("helpop_nohelp",  "line1", 0) == "") ||
220                                 (helpop->ReadValue("helpop_nohelpo", "line1", 0) == "") ||
221                                 (helpop->ReadValue("helpop_start",   "line1", 0) == ""))
222                         {
223                                 HelpopException e("m_helpop: Helpop file is missing important entries. Please check the example conf.");
224                                 throw(e);
225                         }
226                 }
227
228                 void Implements(char* List)
229                 {
230                         List[I_OnRehash] = List[I_OnWhois] = 1;
231                 }
232
233                 virtual void OnRehash(const std::string &parameter)
234                 {
235                         if (helpop)
236                                 DELETE(helpop);
237                         ReadConfig();
238                 }
239
240                 virtual void OnWhois(userrec* src, userrec* dst)
241                 {
242                         if (dst->IsModeSet('h'))
243                         {
244                                 ServerInstance->SendWhoisLine(src, dst, 310, std::string(src->nick)+" "+std::string(dst->nick)+" :is available for help.");
245                         }
246                 }
247
248                 virtual ~ModuleHelpop()
249                 {
250                         ServerInstance->Modes->DelMode(ho);
251                         DELETE(helpop);
252                         DELETE(ho);
253                 }
254         
255                 virtual Version GetVersion()
256                 {
257                         return Version(1,1,0,1,VF_COMMON|VF_VENDOR,API_VERSION);
258                 }
259 };
260
261 class ModuleHelpopFactory : public ModuleFactory
262 {
263  public:
264         ModuleHelpopFactory()
265         {
266         }
267         
268         ~ModuleHelpopFactory()
269         {
270         }
271         
272         virtual Module * CreateModule(InspIRCd* Me)
273         {
274                 return new ModuleHelpop(Me);
275         }
276         
277 };
278
279 extern "C" void * init_module( void )
280 {
281         return new ModuleHelpopFactory;
282 }