]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_helpop.cpp
8f19bd72b739bfbae57e3363b91de7d83e942d2c
[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(Module* Creator) : ModeHandler(Creator, "helpop", 'h', PARAM_NONE, MODETYPE_USER)
26         {
27                 oper = true;
28         }
29
30         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
31         {
32                 if (adding)
33                 {
34                         if (!dest->IsModeSet('h'))
35                         {
36                                 dest->SetMode('h',true);
37                                 return MODEACTION_ALLOW;
38                         }
39                 }
40                 else
41                 {
42                         if (dest->IsModeSet('h'))
43                         {
44                                 dest->SetMode('h',false);
45                                 return MODEACTION_ALLOW;
46                         }
47                 }
48
49                 return MODEACTION_DENY;
50         }
51 };
52
53 /** Handles /HELPOP
54  */
55 class CommandHelpop : public Command
56 {
57  public:
58         CommandHelpop(Module* Creator) : Command(Creator, "HELPOP", 0)
59         {
60                 syntax = "<any-text>";
61         }
62
63         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
64         {
65                 irc::string parameter("start");
66                 if (parameters.size() > 0)
67                         parameter = parameters[0].c_str();
68
69                 if (parameter == "index")
70                 {
71                         /* iterate over all helpop items */
72                         user->WriteServ("290 %s :HELPOP topic index", user->nick.c_str());
73                         for (std::map<irc::string, std::string>::iterator iter = helpop_map.begin(); iter != helpop_map.end(); iter++)
74                         {
75                                 user->WriteServ("292 %s :  %s", user->nick.c_str(), iter->first.c_str());
76                         }
77                         user->WriteServ("292 %s :*** End of HELPOP topic index", user->nick.c_str());
78                 }
79                 else
80                 {
81                         user->WriteServ("290 %s :*** HELPOP for %s", user->nick.c_str(), parameter.c_str());
82                         user->WriteServ("292 %s : -", user->nick.c_str());
83
84                         std::map<irc::string, std::string>::iterator iter = helpop_map.find(parameter);
85
86                         if (iter == helpop_map.end())
87                         {
88                                 iter = helpop_map.find("nohelp");
89                         }
90
91                         std::string value = iter->second;
92                         irc::sepstream stream(value, '\n');
93                         std::string token = "*";
94
95                         while (stream.GetToken(token))
96                         {
97                                 // Writing a blank line will not work with some clients
98                                 if (token.empty())
99                                         user->WriteServ("292 %s : ", user->nick.c_str());
100                                 else
101                                         user->WriteServ("292 %s :%s", user->nick.c_str(), token.c_str());
102                         }
103
104                         user->WriteServ("292 %s : -", user->nick.c_str());
105                         user->WriteServ("292 %s :*** End of HELPOP", user->nick.c_str());
106                 }
107                 return CMD_SUCCESS;
108         }
109 };
110
111 class ModuleHelpop : public Module
112 {
113         private:
114                 std::string  h_file;
115                 CommandHelpop cmd;
116                 Helpop ho;
117
118         public:
119                 ModuleHelpop()
120                         : cmd(this), ho(this)
121                 {
122                         ReadConfig();
123                         if (!ServerInstance->Modes->AddMode(&ho))
124                                 throw ModuleException("Could not add new modes!");
125                         ServerInstance->AddCommand(&cmd);
126                         Implementation eventlist[] = { I_OnRehash, I_OnWhois };
127                         ServerInstance->Modules->Attach(eventlist, this, 2);
128                 }
129
130                 virtual void ReadConfig()
131                 {
132                         ConfigReader MyConf;
133
134                         helpop_map.clear();
135
136                         for (int i = 0;; i++)
137                         {
138                                 ConfigTag* tag = ServerInstance->Config->ConfValue("helpop", i);
139                                 if (!tag)
140                                         break;
141                                 irc::string key = assign(tag->getString("key"));
142                                 std::string value;
143                                 tag->readString("value", value, 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 entry 'start'. 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 entry 'nohelp'. Please check the example conf.");
162                         }
163
164                 }
165
166
167                 virtual void OnRehash(User* user)
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                 }
183
184                 virtual Version GetVersion()
185                 {
186                         return Version("/helpop Command, Works like Unreal helpop", VF_VENDOR | VF_COMMON);
187                 }
188 };
189
190 MODULE_INIT(ModuleHelpop)