]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_svshold.cpp
Use safe iter for deleting while iterating
[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 typedef std::map<irc::string, SVSHold*> SVSHoldMap;
48
49 /* SVSHolds is declared here, as our type is right above. Don't try move it. */
50 SVSHoldlist SVSHolds;
51 SVSHoldMap HoldMap;
52
53 /** Handle /SVSHold
54  */
55 class cmd_svshold : public command_t
56 {
57  public:
58         cmd_svshold(InspIRCd* Me) : command_t(Me, "SVSHOLD", 'o', 1)
59         {
60                 this->source = "m_svshold.so";
61                 this->syntax = "<nickname> [<duration> :<reason>]";
62         }
63
64         CmdResult Handle(const char** parameters, int pcnt, userrec *user)
65         {
66                 /* syntax: svshold nickname time :reason goes here */
67                 /* 'time' is a human-readable timestring, like 2d3h2s. */
68
69                 if (pcnt == 1)
70                 {
71                         SVSHoldMap::iterator n = HoldMap.find(parameters[0]);
72                         if (n != HoldMap.end())
73                         {
74                                 /* form: svshold nickname removes a hold. */
75                                 for (SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
76                                 {
77                                         if (parameters[0] == assign((*iter)->nickname))
78                                         {
79                                                 unsigned long remaining = ((*iter)->set_on + (*iter)->length) - ServerInstance->Time();
80                                                 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());
81                                                 SVSHolds.erase(iter);
82                                                 break;
83                                         }
84                                 }
85
86                                 HoldMap.erase(n);
87                                 delete n->second;
88                         }
89                 }
90                 else if (pcnt >= 2)
91                 {
92                         /* full form to add a SVSHold */
93                         if (ServerInstance->IsNick(parameters[0]))
94                         {
95                                 // parameters[0] = w00t
96                                 // parameters[1] = 1h3m2s
97                                 // parameters[2] = Registered nickname
98                                 
99                                 /* Already exists? */
100                                 if (HoldMap.find(parameters[0]) != HoldMap.end())
101                                 {
102                                         user->WriteServ( "385 %s %s :SVSHOLD already exists", user->nick, parameters[0]);
103                                         return CMD_FAILURE;
104                                 }
105
106                                 long length = ServerInstance->Duration(parameters[1]);
107                                 std::string reason = (pcnt > 2) ? parameters[2] : "No reason supplied";
108                                 
109                                 SVSHold* S = new SVSHold(parameters[0], user->nick, ServerInstance->Time(), length, reason);
110                                 SVSHolds.push_back(S);
111                                 HoldMap[parameters[0]] = S;
112
113                                 std::sort(SVSHolds.begin(), SVSHolds.end(), SVSHoldComp);
114
115                                 if(length > 0)
116                                 {
117                                         user->WriteServ( "385 %s %s :Added %lu second SVSHOLD (%s)", user->nick, parameters[0], length, reason.c_str());
118                                         ServerInstance->WriteOpers("*** %s added %lu second SVSHOLD on %s (%s)", user->nick, length, parameters[0], reason.c_str());
119                                 }
120                                 else
121                                 {
122                                         user->WriteServ( "385 %s %s :Added permanent SVSHOLD on %s (%s)", user->nick, parameters[0], reason.c_str());
123                                         ServerInstance->WriteOpers("*** %s added permanent SVSHOLD on %s (%s)", user->nick, parameters[0], reason.c_str());
124                                 }
125                         }
126                         else
127                         {
128                                 /* as this is primarily a Services command, do not provide an error */
129                                 return CMD_FAILURE;
130                         }
131                 }
132
133                 return CMD_SUCCESS;
134         }
135 };
136
137 bool SVSHoldComp(const SVSHold* ban1, const SVSHold* ban2)
138 {
139         return ((ban1->set_on + ban1->length) < (ban2->set_on + ban2->length));
140 }
141
142 class ModuleSVSHold : public Module
143 {
144         cmd_svshold *mycommand;
145         
146
147  public:
148         ModuleSVSHold(InspIRCd* Me) : Module::Module(Me)
149         {
150                 mycommand = new cmd_svshold(Me);
151                 ServerInstance->AddCommand(mycommand);
152         }
153
154         void Implements(char* List)
155         {
156                 List[I_OnUserPreNick] = List[I_OnSyncOtherMetaData] = List[I_OnDecodeMetaData] = List[I_OnStats] = 1;
157         }
158         
159         virtual int OnStats(char symbol, userrec* user, string_list &results)
160         {
161                 ExpireBans();
162         
163                 if(symbol == 'S')
164                 {
165                         for(SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
166                         {
167                                 unsigned long remaining = ((*iter)->set_on + (*iter)->length) - ServerInstance->Time();
168                                 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);
169                         }
170                 }
171                 
172                 return 0;
173         }
174
175         virtual int OnUserPreNick(userrec *user, const std::string &newnick)
176         {
177                 ExpireBans();
178         
179                 /* check SVSHolds in here, and apply as necessary. */
180                 SVSHoldMap::iterator n = HoldMap.find(assign(newnick));
181                 if (n != HoldMap.end())
182                 {
183                         user->WriteServ( "432 %s %s :Reserved nickname: %s", user->nick, newnick.c_str(), n->second->reason.c_str());
184                         return 1;
185                 }
186                 return 0;
187         }
188         
189         virtual void OnSyncOtherMetaData(Module* proto, void* opaque)
190         {
191                 for(SVSHoldMap::iterator iter = HoldMap.begin(); iter != HoldMap.end(); iter++)
192                 {
193                         proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "SVSHold", EncodeSVSHold(iter->second));
194                 }
195         }
196
197         virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
198         {
199                 if((target_type == TYPE_OTHER) && (extname == "SVSHold"))
200                 {
201                         SVSHold* S = DecodeSVSHold(extdata); /* NOTE: Allocates a new SVSHold* */
202                         if (HoldMap.find(assign(S->nickname)) == HoldMap.end())
203                         {
204                                 SVSHolds.push_back(S);
205                                 HoldMap[assign(S->nickname)] = S;
206                                 std::sort(SVSHolds.begin(), SVSHolds.end(), SVSHoldComp);
207                         }
208                         else
209                         {
210                                 delete S;
211                         }
212                 }
213         }
214
215         virtual ~ModuleSVSHold()
216         {
217         }
218         
219         virtual Version GetVersion()
220         {
221                 return Version(1,1,0,1,VF_VENDOR|VF_COMMON,API_VERSION);
222         }
223
224         std::string EncodeSVSHold(const SVSHold* ban)
225         {
226                 std::ostringstream stream;      
227                 stream << ban->nickname << " " << ban->set_by << " " << ban->set_on << " " << ban->length << " " << ban->reason;
228                 return stream.str();    
229         }
230
231         SVSHold* DecodeSVSHold(const std::string &data)
232         {
233                 SVSHold* res = new SVSHold();
234                 std::istringstream stream(data);
235                 stream >> res->nickname;
236                 stream >> res->set_by;
237                 stream >> res->set_on;
238                 stream >> res->length;
239                 res->reason = stream.str();
240                 return res;
241         }
242
243         void ExpireBans()
244         {
245                 SVSHoldlist::iterator iter,safeiter;
246                 for (iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
247                 {
248                         /* 0 == permanent, don't mess with them! -- w00t */
249                         if ((*iter)->length != 0)
250                         {
251                                 if ((*iter)->set_on + (*iter)->length <= ServerInstance->Time())
252                                 {
253                                         ServerInstance->Log(DEBUG, "m_svshold.so: hold on %s expired, removing...", (*iter)->nickname.c_str());
254                                         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);
255                                         HoldMap.erase(assign((*iter)->nickname));
256                                         delete *iter;
257                                         safeiter = iter;
258                                         --iter;
259                                         SVSHolds.erase(safeiter);
260                                 }
261                         }
262                 }
263         }
264 };
265
266 class ModuleSVSHoldFactory : public ModuleFactory
267 {
268  public:
269         ModuleSVSHoldFactory()
270         {
271         }
272         
273         ~ModuleSVSHoldFactory()
274         {
275         }
276         
277         virtual Module * CreateModule(InspIRCd* Me)
278         {
279                 return new ModuleSVSHold(Me);
280         }
281         
282 };
283
284
285 extern "C" void * init_module( void )
286 {
287         return new ModuleSVSHoldFactory;
288 }