]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_helpop.cpp
Remove global namespacing, makes modules compile FASTAH. Also massive update on heade...
[user/henk/code/inspircd.git] / src / modules / m_helpop.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "users.h"
15 #include "channels.h"
16 #include "modules.h"
17 #include "inspircd.h"
18
19 // Global Vars
20 static ConfigReader *helpop;
21
22 /*bool do_helpop(const char**, int, userrec*);
23 void sendtohelpop(userrec*, int, const char**);*/
24
25 /* $ModDesc: /helpop Command, Works like Unreal helpop */
26
27 /** Handles user mode +h
28  */
29 class Helpop : public ModeHandler
30 {
31  public:
32         Helpop(InspIRCd* Instance) : ModeHandler(Instance, 'h', 0, 0, false, MODETYPE_USER, true) { }
33
34         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
35         {
36                 if (adding)
37                 {
38                         if (!dest->IsModeSet('h'))
39                         {
40                                 dest->SetMode('h',true);
41                                 return MODEACTION_ALLOW;
42                         }
43                 }
44                 else
45                 {
46                         if (dest->IsModeSet('h'))
47                         {
48                                 dest->SetMode('h',false);
49                                 return MODEACTION_ALLOW;
50                         }
51                 }
52
53                 return MODEACTION_DENY;
54         }
55 };
56
57 /** Handles /HELPOP
58  */
59 class cmd_helpop : public command_t
60 {
61  public:
62          cmd_helpop (InspIRCd* Instance) : command_t(Instance, "HELPOP", 0, 1)
63          {
64                  this->source = "m_helpop.so";
65                  syntax = "[?|!]<any-text>";
66          }
67
68         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
69         {
70                 char a[MAXBUF];
71                 std::string output = " ";
72
73                 if (!helpop)
74                         return CMD_FAILURE;
75
76                 if (pcnt < 1)
77                 {
78                         do_helpop(NULL,pcnt,user);
79                         return CMD_SUCCESS;
80                 }
81
82                 if (*parameters[0] == '!')
83                 {
84                         // Force send to all +h users
85                         sendtohelpop(user, pcnt, parameters);
86                 }
87                 else if (*parameters[0] == '?')
88                 {
89                         // Force to the helpop system with no forward if not found.
90                         if (do_helpop(parameters, pcnt, user) == false)
91                         {
92                                 // Not handled by the Database, Tell the user, and bail.
93                                 for (int i = 1; output != ""; i++)
94                                 {
95                                         snprintf(a,MAXBUF,"line%d",i);
96                                         output = helpop->ReadValue("helpop_nohelp", a, 0);
97         
98                                         if(output != "")
99                                         {
100                                                 user->WriteServ("290 "+std::string(user->nick)+" :"+output);
101                                         }
102                                 }
103                         }
104                 }
105                 else
106                 {
107                         // Check with the helpop database, if not found send to +h
108                         if (do_helpop(parameters, pcnt, user) == false)
109                         {
110                                 // Not handled by the Database, Tell the user, and forward.
111                                 for (int i = 1; output != ""; i++)
112                                 {
113                                         snprintf(a,MAXBUF,"line%d",i);
114                                         /* "nohelpo" for opers "nohelp" for users */
115                                         output = helpop->ReadValue("helpop_nohelpo", a, 0);
116                                         if (output != "")
117                                         {
118                                                 user->WriteServ("290 "+std::string(user->nick)+" :"+output);
119                                         }
120                                 }
121                                 // Forward.
122                                 sendtohelpop(user, pcnt, parameters);
123                         }
124                 }
125
126                 return CMD_SUCCESS;
127         }
128
129
130         bool do_helpop(const char** parameters, int pcnt, userrec *src)
131         {
132                 char search[MAXBUF];
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                         strcpy(search,"helpop_start");
140                 }
141                 else
142                 {
143                         if (*parameters[0] == '?')
144                                 parameters[0]++;
145                         strlcpy(search, "helpop_", MAXBUF);
146                         strlcat(search, parameters[0], MAXBUF);
147                 }
148
149                 for (char* n = search; *n; n++)
150                         *n = tolower(*n);
151
152                 for (int i = 1; output != ""; i++)
153                 {
154                         snprintf(a,MAXBUF,"line%d",i);
155                         output = helpop->ReadValue(search, a, 0);
156                         if (output != "")
157                         {
158                                 src->WriteServ("290 "+std::string(src->nick)+" :"+output);
159                                 nlines++;
160                         }
161                 }
162                 return (nlines>0);
163         }
164
165         void sendtohelpop(userrec *src, int pcnt, const char **params)
166         {
167                 const char* first = params[0];
168                 if (*first == '!')
169                 {
170                         first++;
171                 }
172
173                 std::string line = "*** HELPOPS - From "+std::string(src->nick)+": "+std::string(first)+" ";
174                 for (int i = 1; i < pcnt; i++)
175                 {
176                         line = line + std::string(params[i]) + " ";
177                 }
178                 ServerInstance->WriteMode("oh",WM_AND,line.c_str());
179         }
180 };
181
182 /** Thrown by m_helpop
183  */
184 class HelpopException : public ModuleException
185 {
186  private:
187         std::string err;
188  public:
189         HelpopException(std::string message) : err(message) { }
190         ~HelpopException() throw() { };
191         virtual const char* GetReason() { return err.c_str(); }
192 };
193
194 class ModuleHelpop : public Module
195 {
196         private:
197                 std::string  h_file;
198                 cmd_helpop* mycommand;
199                 Helpop* ho;
200
201         public:
202                 ModuleHelpop(InspIRCd* Me)
203                         : Module::Module(Me)
204                 {
205                         ReadConfig();
206                         ho = new Helpop(ServerInstance);
207                         ServerInstance->AddMode(ho, 'h');
208                         mycommand = new cmd_helpop(ServerInstance);
209                         ServerInstance->AddCommand(mycommand);
210                 }
211
212                 virtual void ReadConfig()
213                 {
214                         helpop = new ConfigReader(ServerInstance);
215                         if ((helpop->ReadValue("helpop_nohelp",  "line1", 0) == "") ||
216                                 (helpop->ReadValue("helpop_nohelpo", "line1", 0) == "") ||
217                                 (helpop->ReadValue("helpop_start",   "line1", 0) == ""))
218                         {
219                                 HelpopException e("m_helpop: Helpop file is missing important entries. Please check the example conf.");
220                                 throw(e);
221                         }
222                 }
223
224                 void Implements(char* List)
225                 {
226                         List[I_OnRehash] = List[I_OnWhois] = 1;
227                 }
228
229                 virtual void OnRehash(const std::string &parameter)
230                 {
231                         if (helpop)
232                                 DELETE(helpop);
233                         ReadConfig();
234                 }
235
236                 virtual void OnWhois(userrec* src, userrec* dst)
237                 {
238                         if (dst->IsModeSet('h'))
239                         {
240                                 ServerInstance->SendWhoisLine(src, dst, 310, std::string(src->nick)+" "+std::string(dst->nick)+" :is available for help.");
241                         }
242                 }
243
244                 virtual ~ModuleHelpop()
245                 {
246                         ServerInstance->Modes->DelMode(ho);
247                         DELETE(helpop);
248                         DELETE(ho);
249                 }
250         
251                 virtual Version GetVersion()
252                 {
253                         return Version(1,1,0,1,VF_COMMON|VF_VENDOR,API_VERSION);
254                 }
255 };
256
257 class ModuleHelpopFactory : public ModuleFactory
258 {
259  public:
260         ModuleHelpopFactory()
261         {
262         }
263         
264         ~ModuleHelpopFactory()
265         {
266         }
267         
268         virtual Module * CreateModule(InspIRCd* Me)
269         {
270                 return new ModuleHelpop(Me);
271         }
272         
273 };
274
275 extern "C" void * init_module( void )
276 {
277         return new ModuleHelpopFactory;
278 }