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