]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_helpop.cpp
Merge pull request #85 from Robby-/insp20-typosnstuff
[user/henk/code/inspircd.git] / src / modules / m_helpop.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 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 static std::map<irc::string, std::string> helpop_map;
17
18 /** Handles user mode +h
19  */
20 class Helpop : public ModeHandler
21 {
22  public:
23         Helpop(Module* Creator) : ModeHandler(Creator, "helpop", 'h', PARAM_NONE, MODETYPE_USER)
24         {
25                 oper = true;
26         }
27
28         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
29         {
30                 if (adding)
31                 {
32                         if (!dest->IsModeSet('h'))
33                         {
34                                 dest->SetMode('h',true);
35                                 return MODEACTION_ALLOW;
36                         }
37                 }
38                 else
39                 {
40                         if (dest->IsModeSet('h'))
41                         {
42                                 dest->SetMode('h',false);
43                                 return MODEACTION_ALLOW;
44                         }
45                 }
46
47                 return MODEACTION_DENY;
48         }
49 };
50
51 /** Handles /HELPOP
52  */
53 class CommandHelpop : public Command
54 {
55  public:
56         CommandHelpop(Module* Creator) : Command(Creator, "HELPOP", 0)
57         {
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                 return CMD_SUCCESS;
106         }
107 };
108
109 class ModuleHelpop : public Module
110 {
111         private:
112                 std::string  h_file;
113                 CommandHelpop cmd;
114                 Helpop ho;
115
116         public:
117                 ModuleHelpop()
118                         : cmd(this), ho(this)
119                 {
120                 }
121
122                 void init()
123                 {
124                         ReadConfig();
125                         ServerInstance->Modules->AddService(ho);
126                         ServerInstance->Modules->AddService(cmd);
127                         Implementation eventlist[] = { I_OnRehash, I_OnWhois };
128                         ServerInstance->Modules->Attach(eventlist, this, 2);
129                 }
130
131                 void ReadConfig()
132                 {
133                         helpop_map.clear();
134
135                         ConfigTagList tags = ServerInstance->Config->ConfTags("helpop");
136                         for(ConfigIter i = tags.first; i != tags.second; ++i)
137                         {
138                                 ConfigTag* tag = i->second;
139                                 irc::string key = assign(tag->getString("key"));
140                                 std::string value;
141                                 tag->readString("value", value, true); /* Linefeeds allowed */
142
143                                 if (key == "index")
144                                 {
145                                         throw ModuleException("m_helpop: The key 'index' is reserved for internal purposes. Please remove it.");
146                                 }
147
148                                 helpop_map[key] = value;
149                         }
150
151                         if (helpop_map.find("start") == helpop_map.end())
152                         {
153                                 // error!
154                                 throw ModuleException("m_helpop: Helpop file is missing important entry 'start'. Please check the example conf.");
155                         }
156                         else if (helpop_map.find("nohelp") == helpop_map.end())
157                         {
158                                 // error!
159                                 throw ModuleException("m_helpop: Helpop file is missing important entry 'nohelp'. Please check the example conf.");
160                         }
161
162                 }
163
164                 void OnRehash(User* user)
165                 {
166                         ReadConfig();
167                 }
168
169                 void OnWhois(User* src, User* dst)
170                 {
171                         if (dst->IsModeSet('h'))
172                         {
173                                 ServerInstance->SendWhoisLine(src, dst, 310, std::string(src->nick)+" "+std::string(dst->nick)+" :is available for help.");
174                         }
175                 }
176
177                 Version GetVersion()
178                 {
179                         return Version("Provides the /HELPOP command, works like UnrealIRCd's helpop", VF_VENDOR);
180                 }
181 };
182
183 MODULE_INIT(ModuleHelpop)