]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_helpop.cpp
a76cb87a8a1aba9413184254dae0c46bbb19319f
[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 void handle_helpop(char**, int, userrec*);
29 bool do_helpop(char**, int, userrec*);
30 void sendtohelpop(userrec*, int, char**);
31
32 /* $ModDesc: /helpop Command, Works like Unreal helpop */
33
34 class cmd_helpop : public command_t
35 {
36  public:
37          cmd_helpop () : command_t("HELPOP",0,0)
38          {
39                  this->source = "m_helpop.so";
40          }
41
42         void Handle (char **parameters, int pcnt, userrec *user)
43         {
44                 char a[MAXBUF];
45                 std::string output = " ";
46
47                 if (!helpop)
48                         return;
49
50                 if (pcnt < 1)
51                 {
52                         do_helpop(NULL,pcnt,user);
53                         return;
54                 }
55
56                 if (parameters[0][0] == '!')
57                 {
58                         // Force send to all +h users
59                         sendtohelpop(user, pcnt, parameters);
60                 }
61                 else if (parameters[0][0] == '?')
62                 {
63                         // Force to the helpop system with no forward if not found.
64                         if (do_helpop(parameters, pcnt, user) == false)
65                         {
66                                 // Not handled by the Database, Tell the user, and bail.
67                                 for (int i = 1; output != ""; i++)
68                                 {
69                                         snprintf(a,MAXBUF,"line%d",i);
70                                         output = helpop->ReadValue("nohelp", std::string(a), 0);
71         
72                                         if(output != "")
73                                         {
74                                                 Srv->SendTo(NULL,user,"290 "+std::string(user->nick)+" :"+output);
75                                         }
76                                 }
77                         }
78                 }
79                 else
80                 {
81                         // Check with the helpop database, if not found send to +h
82                         if (do_helpop(parameters, pcnt, user) == false)
83                         {
84                                 // Not handled by the Database, Tell the user, and forward.
85                                 for (int i = 1; output != ""; i++)
86                                 {
87                                         snprintf(a,MAXBUF,"line%d",i);
88                                         /* "nohelpo" for opers "nohelp" for users */
89                                         output = helpop->ReadValue("nohelpo", std::string(a), 0);
90                                         if (output != "")
91                                         {
92                                                 Srv->SendTo(NULL,user,"290 "+std::string(user->nick)+" :"+output);
93                                         }
94                                 }
95                                 // Forward.
96                                 sendtohelpop(user, pcnt, parameters);
97                         }
98                 }
99         }
100 };
101
102
103 bool do_helpop(char **parameters, int pcnt, userrec *src)
104 {
105         char *search;
106         std::string output = " "; // a fix bought to you by brain :p
107         char a[MAXBUF];
108         char lower[MAXBUF];
109         int nlines = 0;
110
111         if (!parameters)
112         {
113                 search = "start";
114         }
115         else
116         {
117                 search = parameters[0];
118         }
119
120         if (search[0] == '?')
121         {
122                 search++;
123         }
124
125         /* XXX - don't we have an strtolower()? if not, might pay to add one.. that works on char *, preferably.. */
126         strlcpy(lower, search, MAXBUF);
127         for (unsigned int t = 0; t < strlen(lower); t++)
128                 lower[t] = tolower(lower[t]);
129
130
131         for (int i = 1; output != ""; i++)
132         {
133                 snprintf(a,MAXBUF,"line%d",i);
134                 output = helpop->ReadValue(lower, a, 0);
135                 if (output != "")
136                 {
137                         Srv->SendTo(NULL,src,"290 "+std::string(src->nick)+" :"+output);
138                         nlines++;
139                 }
140         }
141         return (nlines>0);
142 }
143
144
145
146 void sendtohelpop(userrec *src, int pcnt, char **params)
147 {
148         char* first = params[0];
149         if (*first == '!')
150         {
151                 first++;
152         }
153
154         std::string line = "*** HELPOPS - From "+std::string(src->nick)+": "+std::string(first)+" ";
155         for (int i = 1; i < pcnt; i++)
156         {
157                 line = line + std::string(params[i]) + " ";
158         }
159         Srv->SendToModeMask("oh",WM_AND,line);
160 }
161
162 class HelpopException : public ModuleException
163 {
164  private:
165         std::string err;
166  public:
167         HelpopException(std::string message) : err(message) { }
168         virtual char* GetReason() { return (char*)err.c_str(); }
169 };
170
171 class ModuleHelpop : public Module
172 {
173         private:
174                 ConfigReader *conf;
175                 std::string  h_file;
176                 cmd_helpop* mycommand;
177
178         public:
179                 ModuleHelpop(Server* Me)
180                         : Module::Module(Me)
181                 {
182                         Srv  = Me;
183
184                         ReadConfig();
185                         if (!Srv->AddExtendedMode('h',MT_CLIENT,true,0,0))
186                         {
187                                 Srv->Log(DEFAULT,"Unable to claim the +h usermode.");
188                                 return;
189                         }
190
191                         mycommand = new cmd_helpop();
192                         Srv->AddCommand(mycommand);
193                 }
194
195                 virtual void ReadConfig()
196                 {
197                         conf = new ConfigReader;
198                         h_file = conf->ReadValue("helpop", "file", 0);
199
200                         if (h_file == "")
201                         {
202                                 helpop = NULL;
203                                 HelpopException e("Missing helpop file");
204                                 throw(e);
205                         }
206
207                         helpop = new ConfigReader(h_file);
208                         if ((helpop->ReadValue("nohelp",  "line1", 0) == "") ||
209                                 (helpop->ReadValue("nohelpo", "line1", 0) == "") ||
210                                 (helpop->ReadValue("start",   "line1", 0) == ""))
211                         {
212                                 HelpopException e("m_helpop: Helpop file is missing important entries. Please check the example conf.");
213                                 throw(e);
214                         }
215                 }
216
217                 void Implements(char* List)
218                 {
219                         List[I_OnRehash] = List[I_OnExtendedMode] = List[I_OnWhois] = 1;
220                 }
221
222                 virtual void OnRehash(std::string parameter)
223                 {
224                         delete conf;
225                         if (helpop)
226                                 delete helpop;
227
228                         ReadConfig();
229                 }
230
231                 virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
232                 {
233                         if ((modechar == 'h') && (type == MT_CLIENT))
234                         {
235                                 return 1;
236                         }
237                         return 0;
238                 }
239
240                 virtual void OnWhois(userrec* src, userrec* dst)
241                 {
242                         if (strchr(dst->modes,'h'))
243                         {
244                                 Srv->SendTo(NULL,src,"310 "+std::string(src->nick)+" "+std::string(dst->nick)+" :is available for help.");
245                         }
246                 }
247
248                 virtual ~ModuleHelpop()
249                 {
250                         delete conf;
251                         delete helpop;
252                 }
253         
254                 virtual Version GetVersion()
255                 {
256                         return Version(1,0,0,1,VF_STATIC|VF_VENDOR);
257                 }
258 };
259
260 class ModuleHelpopFactory : public ModuleFactory
261 {
262  public:
263         ModuleHelpopFactory()
264         {
265         }
266         
267         ~ModuleHelpopFactory()
268         {
269         }
270         
271         virtual Module * CreateModule(Server* Me)
272         {
273                 return new ModuleHelpop(Me);
274         }
275         
276 };
277
278 extern "C" void * init_module( void )
279 {
280         return new ModuleHelpopFactory;
281 }