]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_helpop.cpp
Tidyup
[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         ~HelpopException() throw() { };
196         virtual const char* GetReason() { return err.c_str(); }
197 };
198
199 class ModuleHelpop : public Module
200 {
201         private:
202                 std::string  h_file;
203                 cmd_helpop* mycommand;
204                 Helpop* ho;
205
206         public:
207                 ModuleHelpop(InspIRCd* Me)
208                         : Module::Module(Me)
209                 {
210                         ReadConfig();
211                         ho = new Helpop(ServerInstance);
212                         ServerInstance->AddMode(ho, 'h');
213                         mycommand = new cmd_helpop(ServerInstance);
214                         ServerInstance->AddCommand(mycommand);
215                 }
216
217                 virtual void ReadConfig()
218                 {
219                         helpop = new ConfigReader(ServerInstance);
220                         if ((helpop->ReadValue("helpop_nohelp",  "line1", 0) == "") ||
221                                 (helpop->ReadValue("helpop_nohelpo", "line1", 0) == "") ||
222                                 (helpop->ReadValue("helpop_start",   "line1", 0) == ""))
223                         {
224                                 HelpopException e("m_helpop: Helpop file is missing important entries. Please check the example conf.");
225                                 throw(e);
226                         }
227                 }
228
229                 void Implements(char* List)
230                 {
231                         List[I_OnRehash] = List[I_OnWhois] = 1;
232                 }
233
234                 virtual void OnRehash(const std::string &parameter)
235                 {
236                         if (helpop)
237                                 DELETE(helpop);
238                         ReadConfig();
239                 }
240
241                 virtual void OnWhois(userrec* src, userrec* dst)
242                 {
243                         if (dst->IsModeSet('h'))
244                         {
245                                 ServerInstance->SendWhoisLine(src, dst, 310, std::string(src->nick)+" "+std::string(dst->nick)+" :is available for help.");
246                         }
247                 }
248
249                 virtual ~ModuleHelpop()
250                 {
251                         ServerInstance->Modes->DelMode(ho);
252                         DELETE(helpop);
253                         DELETE(ho);
254                 }
255         
256                 virtual Version GetVersion()
257                 {
258                         return Version(1,1,0,1,VF_COMMON|VF_VENDOR,API_VERSION);
259                 }
260 };
261
262 class ModuleHelpopFactory : public ModuleFactory
263 {
264  public:
265         ModuleHelpopFactory()
266         {
267         }
268         
269         ~ModuleHelpopFactory()
270         {
271         }
272         
273         virtual Module * CreateModule(InspIRCd* Me)
274         {
275                 return new ModuleHelpop(Me);
276         }
277         
278 };
279
280 extern "C" void * init_module( void )
281 {
282         return new ModuleHelpopFactory;
283 }