]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_helpop.cpp
Note to self, cast time_t to long int for printf... thanks Ankit for pointing this...
[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://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("290 %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("292 %s :  %s", user->nick.c_str(), iter->first.c_str());
52                         }
53                         user->WriteServ("292 %s :*** End of HELPOP topic index", user->nick.c_str());
54                 }
55                 else
56                 {
57                         user->WriteServ("290 %s :*** HELPOP for %s", user->nick.c_str(), parameter.c_str());
58                         user->WriteServ("292 %s : -", user->nick.c_str());
59
60                         std::map<irc::string, std::string>::iterator iter = helpop_map.find(parameter);
61
62                         if (iter == helpop_map.end())
63                         {
64                                 iter = helpop_map.find("nohelp");
65                         }
66
67                         std::string value = iter->second;
68                         irc::sepstream stream(value, '\n');
69                         std::string token = "*";
70
71                         while (stream.GetToken(token))
72                         {
73                                 // Writing a blank line will not work with some clients
74                                 if (token.empty())
75                                         user->WriteServ("292 %s : ", user->nick.c_str());
76                                 else
77                                         user->WriteServ("292 %s :%s", user->nick.c_str(), token.c_str());
78                         }
79
80                         user->WriteServ("292 %s : -", user->nick.c_str());
81                         user->WriteServ("292 %s :*** End of HELPOP", user->nick.c_str());
82                 }
83
84                 /* We dont want these going out over the network, return CMD_FAILURE
85                  * to make sure the protocol module thinks theyre not worth sending.
86                  */
87                 return CMD_FAILURE;
88         }
89 };
90
91 class ModuleHelpop : public Module
92 {
93         private:
94                 std::string  h_file;
95                 CommandHelpop* mycommand;
96                 Helpop* ho;
97
98         public:
99                 ModuleHelpop(InspIRCd* Me)
100                         : Module(Me)
101                 {
102                         ReadConfig();
103                         ho = new Helpop(ServerInstance);
104                         if (!ServerInstance->Modes->AddMode(ho))
105                                 throw ModuleException("Could not add new modes!");
106                         mycommand = new CommandHelpop(ServerInstance);
107                         ServerInstance->AddCommand(mycommand);
108                 Implementation eventlist[] = { I_OnRehash, I_OnWhois };
109                 ServerInstance->Modules->Attach(eventlist, this, 2);
110                 }
111
112                 virtual void ReadConfig()
113                 {
114                         ConfigReader MyConf(ServerInstance);
115
116                         helpop_map.clear();
117
118                         for (int i = 0; i < MyConf.Enumerate("helpop"); i++)
119                         {
120                                 irc::string key = assign(MyConf.ReadValue("helpop", "key", i));
121                                 std::string value = MyConf.ReadValue("helpop", "value", i, true); /* Linefeeds allowed! */
122
123                                 if (key == "index")
124                                 {
125                                         throw ModuleException("m_helpop: The key 'index' is reserved for internal purposes. Please remove it.");
126                                 }
127
128                                 helpop_map[key] = value;
129                         }
130
131                         if (helpop_map.find("start") == helpop_map.end())
132                         {
133                                 // error!
134                                 throw ModuleException("m_helpop: Helpop file is missing important entries. Please check the example conf.");
135                         }
136                         else if (helpop_map.find("nohelp") == helpop_map.end())
137                         {
138                                 // error!
139                                 throw ModuleException("m_helpop: Helpop file is missing important entries. Please check the example conf.");
140                         }
141
142                 }
143
144
145                 virtual void OnRehash(User* user, const std::string &parameter)
146                 {
147                         ReadConfig();
148                 }
149
150                 virtual void OnWhois(User* src, User* dst)
151                 {
152                         if (dst->IsModeSet('h'))
153                         {
154                                 ServerInstance->SendWhoisLine(src, dst, 310, std::string(src->nick)+" "+std::string(dst->nick)+" :is available for help.");
155                         }
156                 }
157
158                 virtual ~ModuleHelpop()
159                 {
160                         ServerInstance->Modes->DelMode(ho);
161                         delete ho;
162                 }
163
164                 virtual Version GetVersion()
165                 {
166                         return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
167                 }
168 };
169
170 MODULE_INIT(ModuleHelpop)