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