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