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