]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_svshold.cpp
Allow linefeeds in this module in one value
[user/henk/code/inspircd.git] / src / modules / m_svshold.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 <algorithm>
15 #include "users.h"
16 #include "channels.h"
17 #include "modules.h"
18 #include "configreader.h"
19 #include "inspircd.h"
20
21 /* $ModDesc: Implements SVSHOLD. Like Q:Lines, but can only be added/removed by Services. */
22
23 /** Holds a SVSHold item
24  */
25 class SVSHold : public classbase
26 {
27 public:
28         std::string nickname;
29         std::string set_by;
30         time_t set_on;
31         long length;
32         std::string reason;
33
34         SVSHold()
35         {
36         }
37
38         SVSHold(const std::string &nn, const std::string &sb, const time_t so, const long ln, const std::string &rs) : nickname(nn), set_by(sb), set_on(so), length(ln), reason(rs)
39         {
40         }
41 };
42
43
44 bool SVSHoldComp(const SVSHold &ban1, const SVSHold &ban2);
45
46 typedef std::vector<SVSHold> SVSHoldlist;
47
48 /* SVSHolds is declared here, as our type is right above. Don't try move it. */
49 SVSHoldlist SVSHolds;
50
51 /** Handle /SVSHold
52  */
53 class cmd_svshold : public command_t
54 {
55  public:
56         cmd_svshold(InspIRCd* Me) : command_t(Me, "SVSHOLD", 'o', 1)
57         {
58                 this->source = "m_svshold.so";
59                 this->
60                 syntax = "<nickname> [<duration> :<reason>]";
61         }
62
63         CmdResult Handle(const char** parameters, int pcnt, userrec *user)
64         {
65                 /* syntax: svshold nickname time :reason goes here */
66                 /* 'time' is a human-readable timestring, like 2d3h2s. */
67
68                 if (pcnt == 1)
69                 {
70                         /* form: svshold nickname removes a hold. */
71                         for (SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
72                         {
73                                 if (irc::string(parameters[0]) == irc::string(iter->nickname.c_str()))
74                                 {
75                                         unsigned long remaining = (iter->set_on + iter->length) - ServerInstance->Time();
76                                         user->WriteServ( "386 %s %s :Removed SVSHOLD with %lu seconds left before expiry (%s)", user->nick, iter->nickname.c_str(), remaining, iter->reason.c_str());
77                                         SVSHolds.erase(iter);
78                                         break;
79                                 }
80                         }
81                 }
82                 else if (pcnt >= 2)
83                 {
84                         /* full form to add a SVSHold */
85                         if (ServerInstance->IsNick(parameters[0]))
86                         {
87                                 // parameters[0] = w00t
88                                 // parameters[1] = 1h3m2s
89                                 // parameters[2] = Registered nickname
90                                 long length = ServerInstance->Duration(parameters[1]);
91                                 std::string reason = (pcnt > 2) ? parameters[2] : "No reason supplied";
92                                 
93                                 SVSHolds.push_back(SVSHold(parameters[0], user->nick, ServerInstance->Time(), length, reason));
94                                         
95                                 std::sort(SVSHolds.begin(), SVSHolds.end(), SVSHoldComp);
96                                 
97                                 if(length > 0)
98                                 {
99                                         user->WriteServ( "385 %s %s :Added %lu second SVSHOLD (%s)", user->nick, parameters[0], length, reason.c_str());
100                                         ServerInstance->WriteOpers("*** %s added %lu second SVSHOLD on %s (%s)", user->nick, length, parameters[0], reason.c_str());
101                                 }
102                                 else
103                                 {
104                                         user->WriteServ( "385 %s %s :Added permanent SVSHOLD on %s (%s)", user->nick, parameters[0], reason.c_str());
105                                         ServerInstance->WriteOpers("*** %s added permanent SVSHOLD on %s (%s)", user->nick, parameters[0], reason.c_str());
106                                 }
107                         }
108                         else
109                         {
110                                 /* as this is primarily a Services command, do not provide an error */
111                                 return CMD_FAILURE;
112                         }
113                 }
114
115                 return CMD_SUCCESS;
116         }
117 };
118
119 bool SVSHoldComp(const SVSHold &ban1, const SVSHold &ban2)
120 {
121         return ((ban1.set_on + ban1.length) < (ban2.set_on + ban2.length));
122 }
123
124 class ModuleSVSHold : public Module
125 {
126         cmd_svshold *mycommand;
127         
128
129  public:
130         ModuleSVSHold(InspIRCd* Me) : Module::Module(Me)
131         {
132                 mycommand = new cmd_svshold(Me);
133                 ServerInstance->AddCommand(mycommand);
134         }
135
136         void Implements(char* List)
137         {
138                 List[I_OnUserPreNick] = List[I_OnSyncOtherMetaData] = List[I_OnDecodeMetaData] = List[I_OnStats] = 1;
139         }
140         
141         virtual int OnStats(char symbol, userrec* user, string_list &results)
142         {
143                 ExpireBans();
144         
145                 if(symbol == 'S')
146                 {
147                         for(SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
148                         {
149                                 unsigned long remaining = (iter->set_on + iter->length) - ServerInstance->Time();
150                                 results.push_back(std::string(ServerInstance->Config->ServerName)+" 210 "+user->nick+" "+iter->nickname.c_str()+" "+iter->set_by+" "+ConvToStr(iter->set_on)+" "+ConvToStr(iter->length)+" "+ConvToStr(remaining)+" :"+iter->reason);
151                         }
152                 }
153                 
154                 return 0;
155         }
156
157         virtual int OnUserPreNick(userrec *user, const std::string &newnick)
158         {
159                 ExpireBans();
160         
161                 /* check SVSHolds in here, and apply as necessary. */
162                 for(SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
163                 {
164                         if (irc::string(iter->nickname.c_str()) == irc::string(newnick.c_str()))
165                         {
166                                 // nope, boned.
167                                 user->WriteServ( "432 %s %s :Reserved nickname: %s", user->nick, newnick.c_str(), iter->reason.c_str());
168                                 return 1;
169                         }
170                 }
171                 return 0;
172         }
173         
174         virtual void OnSyncOtherMetaData(Module* proto, void* opaque)
175         {
176                 for(SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
177                 {
178                         proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "SVSHold", EncodeSVSHold(*iter));
179                 }
180         }
181         
182         virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
183         {
184                 if((target_type == TYPE_OTHER) && (extname == "SVSHold"))
185                 {
186                         SVSHolds.push_back(DecodeSVSHold(extdata));
187                         std::sort(SVSHolds.begin(), SVSHolds.end(), SVSHoldComp);
188                 }
189         }
190
191         virtual ~ModuleSVSHold()
192         {
193         }
194         
195         virtual Version GetVersion()
196         {
197                 return Version(1,1,0,1,VF_VENDOR|VF_COMMON,API_VERSION);
198         }
199
200         std::string EncodeSVSHold(const SVSHold &ban)
201         {
202                 std::ostringstream stream;      
203                 stream << ban.nickname << " " << ban.set_by << " " << ban.set_on << " " << ban.length << " " << ban.reason;
204                 return stream.str();    
205         }
206
207         SVSHold DecodeSVSHold(const std::string &data)
208         {
209                 SVSHold res;
210                 std::istringstream stream(data);
211                 stream >> res.nickname;
212                 stream >> res.set_by;
213                 stream >> res.set_on;
214                 stream >> res.length;
215                 res.reason = stream.str();
216         
217                 return res;
218         }
219
220         void ExpireBans()
221         {
222                 bool go_again = true;
223
224                 while (go_again)
225                 {
226                         go_again = false;
227         
228                         for (SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
229                         {
230                                 /* 0 == permanent, don't mess with them! -- w00t */
231                                 if (iter->length != 0)
232                                 {
233                                         if (iter->set_on + iter->length <= ServerInstance->Time())
234                                         {
235                                                 ServerInstance->Log(DEBUG, "m_svshold.so: hold on %s expired, removing...", iter->nickname.c_str());
236                                                 ServerInstance->WriteOpers("*** %li second SVSHOLD on %s (%s) set %u seconds ago expired", iter->length, iter->nickname.c_str(), iter->reason.c_str(), ServerInstance->Time() - iter->set_on);
237                                                 SVSHolds.erase(iter);
238                                                 go_again = true;
239                                         }
240                                 }
241         
242                                 if (go_again == true)
243                                         break;
244                         }
245                 }
246         }
247 };
248
249 class ModuleSVSHoldFactory : public ModuleFactory
250 {
251  public:
252         ModuleSVSHoldFactory()
253         {
254         }
255         
256         ~ModuleSVSHoldFactory()
257         {
258         }
259         
260         virtual Module * CreateModule(InspIRCd* Me)
261         {
262                 return new ModuleSVSHold(Me);
263         }
264         
265 };
266
267
268 extern "C" void * init_module( void )
269 {
270         return new ModuleSVSHoldFactory;
271 }