]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_helpop.cpp
Update all wiki links to point to the new wiki. This was done automatically with...
[user/henk/code/inspircd.git] / src / modules / m_helpop.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15
16 /* $ModDesc: /helpop Command, Works like Unreal helpop */
17 static std::map<irc::string, std::string> helpop_map;
18
19
20 /** Handles user mode +h
21  */
22 class Helpop : public ModeHandler
23 {
24  public:
25         Helpop(InspIRCd* Instance) : ModeHandler(Instance, 'h', 0, 0, false, MODETYPE_USER, true) { }
26
27         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding, bool)
28         {
29                 if (adding)
30                 {
31                         if (!dest->IsModeSet('h'))
32                         {
33                                 dest->SetMode('h',true);
34                                 return MODEACTION_ALLOW;
35                         }
36                 }
37                 else
38                 {
39                         if (dest->IsModeSet('h'))
40                         {
41                                 dest->SetMode('h',false);
42                                 return MODEACTION_ALLOW;
43                         }
44                 }
45
46                 return MODEACTION_DENY;
47         }
48 };
49
50 /** Handles /HELPOP
51  */
52 class CommandHelpop : public Command
53 {
54  public:
55         CommandHelpop (InspIRCd* Instance) : Command(Instance, "HELPOP", 0, 0)
56         {
57                 this->source = "m_helpop.so";
58                 syntax = "<any-text>";
59         }
60
61         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
62         {
63                 irc::string parameter("start");
64                 if (parameters.size() > 0)
65                         parameter = parameters[0].c_str();
66
67                 if (parameter == "index")
68                 {
69                         /* iterate over all helpop items */
70                         user->WriteServ("290 %s :HELPOP topic index", user->nick.c_str());
71                         for (std::map<irc::string, std::string>::iterator iter = helpop_map.begin(); iter != helpop_map.end(); iter++)
72                         {
73                                 user->WriteServ("292 %s :  %s", user->nick.c_str(), iter->first.c_str());
74                         }
75                         user->WriteServ("292 %s :*** End of HELPOP topic index", user->nick.c_str());
76                 }
77                 else
78                 {
79                         user->WriteServ("290 %s :*** HELPOP for %s", user->nick.c_str(), parameter.c_str());
80                         user->WriteServ("292 %s : -", user->nick.c_str());
81
82                         std::map<irc::string, std::string>::iterator iter = helpop_map.find(parameter);
83
84                         if (iter == helpop_map.end())
85                         {
86                                 iter = helpop_map.find("nohelp");
87                         }
88
89                         std::string value = iter->second;
90                         irc::sepstream stream(value, '\n');
91                         std::string token = "*";
92
93                         while (stream.GetToken(token))
94                         {
95                                 // Writing a blank line will not work with some clients
96                                 if (token.empty())
97                                         user->WriteServ("292 %s : ", user->nick.c_str());
98                                 else
99                                         user->WriteServ("292 %s :%s", user->nick.c_str(), token.c_str());
100                         }
101
102                         user->WriteServ("292 %s : -", user->nick.c_str());
103                         user->WriteServ("292 %s :*** End of HELPOP", user->nick.c_str());
104                 }
105
106                 /* We dont want these going out over the network, return CMD_FAILURE
107                  * to make sure the protocol module thinks theyre not worth sending.
108                  */
109                 return CMD_FAILURE;
110         }
111 };
112
113 class ModuleHelpop : public Module
114 {
115         private:
116                 std::string  h_file;
117                 CommandHelpop* mycommand;
118                 Helpop* ho;
119
120         public:
121                 ModuleHelpop(InspIRCd* Me)
122                         : Module(Me)
123                 {
124                         ReadConfig();
125                         ho = new Helpop(ServerInstance);
126                         if (!ServerInstance->Modes->AddMode(ho))
127                                 throw ModuleException("Could not add new modes!");
128                         mycommand = new CommandHelpop(ServerInstance);
129                         ServerInstance->AddCommand(mycommand);
130                 Implementation eventlist[] = { I_OnRehash, I_OnWhois };
131                 ServerInstance->Modules->Attach(eventlist, this, 2);
132                 }
133
134                 virtual void ReadConfig()
135                 {
136                         ConfigReader MyConf(ServerInstance);
137
138                         helpop_map.clear();
139
140                         for (int i = 0; i < MyConf.Enumerate("helpop"); i++)
141                         {
142                                 irc::string key = assign(MyConf.ReadValue("helpop", "key", i));
143                                 std::string value = MyConf.ReadValue("helpop", "value", i, true); /* Linefeeds allowed! */
144
145                                 if (key == "index")
146                                 {
147                                         throw ModuleException("m_helpop: The key 'index' is reserved for internal purposes. Please remove it.");
148                                 }
149
150                                 helpop_map[key] = value;
151                         }
152
153                         if (helpop_map.find("start") == helpop_map.end())
154                         {
155                                 // error!
156                                 throw ModuleException("m_helpop: Helpop file is missing important entries. Please check the example conf.");
157                         }
158                         else if (helpop_map.find("nohelp") == helpop_map.end())
159                         {
160                                 // error!
161                                 throw ModuleException("m_helpop: Helpop file is missing important entries. Please check the example conf.");
162                         }
163
164                 }
165
166
167                 virtual void OnRehash(User* user, const std::string &parameter)
168                 {
169                         ReadConfig();
170                 }
171
172                 virtual void OnWhois(User* src, User* dst)
173                 {
174                         if (dst->IsModeSet('h'))
175                         {
176                                 ServerInstance->SendWhoisLine(src, dst, 310, std::string(src->nick)+" "+std::string(dst->nick)+" :is available for help.");
177                         }
178                 }
179
180                 virtual ~ModuleHelpop()
181                 {
182                         ServerInstance->Modes->DelMode(ho);
183                         delete ho;
184                 }
185
186                 virtual Version GetVersion()
187                 {
188                         return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
189                 }
190 };
191
192 MODULE_INIT(ModuleHelpop)