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