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