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