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