]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_helpop.cpp
a7f9e400710a1a7740293e9a9607277755c4c703
[user/henk/code/inspircd.git] / src / modules / m_helpop.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/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 SimpleUserModeHandler
23 {
24  public:
25         Helpop(InspIRCd* Instance) : SimpleUserModeHandler(Instance, 'h') { }
26 };
27
28 /** Handles /HELPOP
29  */
30 class CommandHelpop : public Command
31 {
32  public:
33         CommandHelpop (InspIRCd* Instance) : Command(Instance, "HELPOP", 0, 0)
34         {
35                 this->source = "m_helpop.so";
36                 syntax = "<any-text>";
37         }
38
39         CmdResult Handle (const std::vector<std::string> &parameters, User *user)
40         {
41                 irc::string parameter("start");
42                 if (parameters.size() > 0)
43                         parameter = parameters[0].c_str();
44
45                 if (parameter == "index")
46                 {
47                         /* iterate over all helpop items */
48                         user->WriteServ("NOTICE %s :HELPOP topic index", user->nick.c_str());
49                         for (std::map<irc::string, std::string>::iterator iter = helpop_map.begin(); iter != helpop_map.end(); iter++)
50                         {
51                                 user->WriteServ("NOTICE %s :    %s", user->nick.c_str(), iter->first.c_str());                          
52                         }
53                         user->WriteServ("NOTICE %s :*** End of HELPOP topic index", user->nick.c_str());
54                 }
55                 else
56                 {
57                         user->WriteServ("NOTICE %s :*** HELPOP for %s", user->nick.c_str(), parameter.c_str());
58
59                         std::map<irc::string, std::string>::iterator iter = helpop_map.find(parameter);
60
61                         if (iter == helpop_map.end())
62                         {
63                                 iter = helpop_map.find("nohelp");
64                         }
65
66                         std::string value = iter->second;
67                         irc::sepstream stream(value, '\n');
68                         std::string token = "*";
69
70                         while (stream.GetToken(token))
71                                 user->WriteServ("NOTICE %s :%s", user->nick.c_str(), token.c_str());
72
73                         user->WriteServ("NOTICE %s :*** End of HELPOP", user->nick.c_str());
74                 }
75
76                 /* We dont want these going out over the network, return CMD_FAILURE
77                  * to make sure the protocol module thinks theyre not worth sending.
78                  */
79                 return CMD_FAILURE;
80         }
81 };
82
83 class ModuleHelpop : public Module
84 {
85         private:
86                 std::string  h_file;
87                 CommandHelpop* mycommand;
88                 Helpop* ho;
89
90         public:
91                 ModuleHelpop(InspIRCd* Me)
92                         : Module(Me)
93                 {
94                         ReadConfig();
95                         ho = new Helpop(ServerInstance);
96                         if (!ServerInstance->Modes->AddMode(ho))
97                                 throw ModuleException("Could not add new modes!");
98                         mycommand = new CommandHelpop(ServerInstance);
99                         ServerInstance->AddCommand(mycommand);
100                 Implementation eventlist[] = { I_OnRehash, I_OnWhois };
101                 ServerInstance->Modules->Attach(eventlist, this, 2);
102                 }
103
104                 virtual void ReadConfig()
105                 {
106                         ConfigReader *MyConf = new ConfigReader(ServerInstance);
107
108                         helpop_map.clear();
109
110                         for (int i = 0; i < MyConf->Enumerate("helpop"); i++)
111                         {
112                                 irc::string key = assign(MyConf->ReadValue("helpop", "key", i));
113                                 std::string value = MyConf->ReadValue("helpop", "value", i, true); /* Linefeeds allowed! */
114
115                                 if (key == "index")
116                                 {
117                                         throw ModuleException("m_helpop: The key 'index' is reserved for internal purposes. Please remove it.");
118                                 }
119
120                                 helpop_map[key] = value;
121                         }
122
123                         if (helpop_map.find("start") == helpop_map.end())
124                         {
125                                 // error!
126                                 throw ModuleException("m_helpop: Helpop file is missing important entries. Please check the example conf.");
127                         }
128                         else if (helpop_map.find("nohelp") == helpop_map.end())
129                         {
130                                 // error!
131                                 throw ModuleException("m_helpop: Helpop file is missing important entries. Please check the example conf.");
132                         }
133
134                 }
135
136
137                 virtual void OnRehash(User* user, const std::string &parameter)
138                 {
139                         ReadConfig();
140                 }
141
142                 virtual void OnWhois(User* src, User* dst)
143                 {
144                         if (dst->IsModeSet('h'))
145                         {
146                                 ServerInstance->SendWhoisLine(src, dst, 310, std::string(src->nick)+" "+std::string(dst->nick)+" :is available for help.");
147                         }
148                 }
149
150                 virtual ~ModuleHelpop()
151                 {
152                         ServerInstance->Modes->DelMode(ho);
153                         delete ho;
154                 }
155         
156                 virtual Version GetVersion()
157                 {
158                         return Version(1,2,0,1,VF_COMMON|VF_VENDOR,API_VERSION);
159                 }
160 };
161
162 MODULE_INIT(ModuleHelpop)