]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_helpop.cpp
Another big commit, just to please all my fans out there.. cmd_* -> Command*. Muahaha.
[user/henk/code/inspircd.git] / src / modules / m_helpop.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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 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)
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 char** parameters, int pcnt, User *user)
62         {
63                 irc::string parameter("start");
64                 if (pcnt > 0)
65                         parameter = parameters[0];
66
67                 if (parameter == "index")
68                 {
69                         /* iterate over all helpop items */
70                         user->WriteServ("NOTICE %s :HELPOP topic index", user->nick);
71                         for (std::map<irc::string, std::string>::iterator iter = helpop_map.begin(); iter != helpop_map.end(); iter++)
72                         {
73                                 user->WriteServ("NOTICE %s :    %s", user->nick, iter->first.c_str());                          
74                         }
75                         user->WriteServ("NOTICE %s :*** End of HELPOP topic index", user->nick);
76                 }
77                 else
78                 {
79                         user->WriteServ("NOTICE %s :*** HELPOP for %s", user->nick, parameter.c_str());
80
81                         std::map<irc::string, std::string>::iterator iter = helpop_map.find(parameter);
82
83                         if (iter == helpop_map.end())
84                         {
85                                 iter = helpop_map.find("nohelp");
86                         }
87
88                         std::string value = iter->second;
89                         irc::sepstream stream(value, '\n');
90                         std::string token = "*";
91
92                         while (stream.GetToken(token))
93                                 user->WriteServ("NOTICE %s :%s", user->nick, token.c_str());
94
95                         user->WriteServ("NOTICE %s :*** End of HELPOP", user->nick);
96                 }
97
98                 /* We dont want these going out over the network, return CMD_FAILURE
99                  * to make sure the protocol module thinks theyre not worth sending.
100                  */
101                 return CMD_FAILURE;
102         }
103 };
104
105 class ModuleHelpop : public Module
106 {
107         private:
108                 std::string  h_file;
109                 CommandHelpop* mycommand;
110                 Helpop* ho;
111
112         public:
113                 ModuleHelpop(InspIRCd* Me)
114                         : Module(Me)
115                 {
116                         ReadConfig();
117                         ho = new Helpop(ServerInstance);
118                         if (!ServerInstance->AddMode(ho, 'h'))
119                                 throw ModuleException("Could not add new modes!");
120                         mycommand = new CommandHelpop(ServerInstance);
121                         ServerInstance->AddCommand(mycommand);
122                 }
123
124                 virtual void ReadConfig()
125                 {
126                         ConfigReader *MyConf = new ConfigReader(ServerInstance);
127
128                         helpop_map.clear();
129
130                         for (int i = 0; i < MyConf->Enumerate("helpop"); i++)
131                         {
132                                 irc::string key = assign(MyConf->ReadValue("helpop", "key", i));
133                                 std::string value = MyConf->ReadValue("helpop", "value", i, true); /* Linefeeds allowed! */
134
135                                 if (key == "index")
136                                 {
137                                         throw ModuleException("m_helpop: The key 'index' is reserved for internal purposes. Please remove it.");
138                                 }
139
140                                 helpop_map[key] = value;
141                         }
142
143                         if (helpop_map.find("start") == helpop_map.end())
144                         {
145                                 // error!
146                                 throw ModuleException("m_helpop: Helpop file is missing important entries. Please check the example conf.");
147                         }
148                         else if (helpop_map.find("nohelp") == helpop_map.end())
149                         {
150                                 // error!
151                                 throw ModuleException("m_helpop: Helpop file is missing important entries. Please check the example conf.");
152                         }
153
154                 }
155
156                 void Implements(char* List)
157                 {
158                         List[I_OnRehash] = List[I_OnWhois] = 1;
159                 }
160
161                 virtual void OnRehash(User* user, const std::string &parameter)
162                 {
163                         ReadConfig();
164                 }
165
166                 virtual void OnWhois(User* src, User* dst)
167                 {
168                         if (dst->IsModeSet('h'))
169                         {
170                                 ServerInstance->SendWhoisLine(src, dst, 310, std::string(src->nick)+" "+std::string(dst->nick)+" :is available for help.");
171                         }
172                 }
173
174                 virtual ~ModuleHelpop()
175                 {
176                         ServerInstance->Modes->DelMode(ho);
177                         DELETE(ho);
178                 }
179         
180                 virtual Version GetVersion()
181                 {
182                         return Version(1,1,0,1,VF_COMMON|VF_VENDOR,API_VERSION);
183                 }
184 };
185
186 MODULE_INIT(ModuleHelpop)