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