]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_svshold.cpp
4a3858085d50df605a7dc4243dd34d4a553f6a95
[user/henk/code/inspircd.git] / src / modules / m_svshold.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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: Implements SVSHOLD. Like Q:Lines, but can only be added/removed by Services. */
17
18 /** Holds a SVSHold item
19  */
20 class SVSHold : public classbase
21 {
22 public:
23         std::string nickname;
24         std::string set_by;
25         time_t set_on;
26         long length;
27         std::string reason;
28
29         SVSHold()
30         {
31         }
32
33         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)
34         {
35         }
36 };
37
38
39 bool SVSHoldComp(const SVSHold* ban1, const SVSHold* ban2);
40
41 typedef std::vector<SVSHold*> SVSHoldlist;
42 typedef std::map<irc::string, SVSHold*> SVSHoldMap;
43
44 /* SVSHolds is declared here, as our type is right above. Don't try move it. */
45 SVSHoldlist SVSHolds;
46 SVSHoldMap HoldMap;
47
48 /** Handle /SVSHold
49  */
50 class CommandSvshold : public Command
51 {
52  public:
53         CommandSvshold(InspIRCd* Me) : Command(Me, "SVSHOLD", "o", 1)
54         {
55                 this->source = "m_svshold.so";
56                 this->syntax = "<nickname> [<duration> :<reason>]";
57                 TRANSLATE4(TR_NICK, TR_TEXT, TR_TEXT, TR_END);
58         }
59
60         CmdResult Handle(const std::vector<std::string> &parameters, User *user)
61         {
62                 /* syntax: svshold nickname time :reason goes here */
63                 /* 'time' is a human-readable timestring, like 2d3h2s. */
64
65                 if (!ServerInstance->ULine(user->server))
66                 {
67                         /* don't allow SVSHOLD from non-ulined clients */
68                         return CMD_FAILURE;
69                 }
70
71                 if (parameters.size() == 1)
72                 {
73                         SVSHoldMap::iterator n = HoldMap.find(parameters[0].c_str());
74                         if (n != HoldMap.end())
75                         {
76                                 /* form: svshold nickname removes a hold. */
77                                 for (SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
78                                 {
79                                         if (parameters[0] == assign((*iter)->nickname))
80                                         {
81                                                 unsigned long remaining = 0;
82                                                 if ((*iter)->length)
83                                                 {
84                                                         remaining = ((*iter)->set_on + (*iter)->length) - ServerInstance->Time();
85                                                         user->WriteServ( "386 %s %s :Removed SVSHOLD with %lu seconds left before expiry (%s)", user->nick.c_str(), (*iter)->nickname.c_str(), (unsigned long)remaining, (*iter)->reason.c_str());
86                                                 }
87                                                 else
88                                                 {
89                                                         user->WriteServ( "386 %s %s :Removed permanent SVSHOLD (%s)", user->nick.c_str(), (*iter)->nickname.c_str(), (*iter)->reason.c_str());
90                                                 }
91                                                 SVSHolds.erase(iter);
92                                                 break;
93                                         }
94                                 }
95
96                                 HoldMap.erase(n);
97                                 delete n->second;
98                         }
99                 }
100                 else if (parameters.size() >= 2)
101                 {
102                         /* full form to add a SVSHold */
103
104                         /* NOTE: We check nicks up to 512 in length here, as a hax to allow
105                          * remote nicks that are longer than our configuration to be held
106                          */
107                         if (ServerInstance->IsNick(parameters[0].c_str(), 512))
108                         {
109                                 // parameters[0] = w00t
110                                 // parameters[1] = 1h3m2s
111                                 // parameters[2] = Registered nickname
112
113                                 /* Already exists? */
114                                 if (HoldMap.find(parameters[0].c_str()) != HoldMap.end())
115                                 {
116                                         user->WriteServ( "385 %s %s :SVSHOLD already exists", user->nick.c_str(), parameters[0].c_str());
117                                         return CMD_FAILURE;
118                                 }
119
120                                 unsigned long length = ServerInstance->Duration(parameters[1]);
121                                 std::string reason = (parameters.size() > 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].c_str()] = 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.c_str(), parameters[0].c_str(), length, reason.c_str());
132                                 }
133                                 else
134                                 {
135                                         user->WriteServ( "385 %s %s :Added permanent SVSHOLD on %s (%s)", user->nick.c_str(), parameters[0].c_str(), parameters[0].c_str(), reason.c_str());
136                                 }
137                         }
138                         else
139                         {
140                                 /* as this is primarily a Services command, do not provide an error */
141                                 return CMD_FAILURE;
142                         }
143                 }
144
145                 return CMD_SUCCESS;
146         }
147 };
148
149 bool SVSHoldComp(const SVSHold* ban1, const SVSHold* ban2)
150 {
151         return ((ban1->set_on + ban1->length) < (ban2->set_on + ban2->length));
152 }
153
154 class ModuleSVSHold : public Module
155 {
156         CommandSvshold *mycommand;
157
158
159  public:
160         ModuleSVSHold(InspIRCd* Me) : Module(Me)
161         {
162                 mycommand = new CommandSvshold(Me);
163                 ServerInstance->AddCommand(mycommand);
164                 Implementation eventlist[] = { I_OnUserPreNick, I_OnSyncOtherMetaData, I_OnDecodeMetaData, I_OnStats };
165                 ServerInstance->Modules->Attach(eventlist, this, 4);
166         }
167
168
169         virtual int OnStats(char symbol, User* user, string_list &results)
170         {
171                 ExpireBans();
172
173                 if(symbol == 'S')
174                 {
175                         for(SVSHoldlist::iterator iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
176                         {
177                                 unsigned long remaining = ((*iter)->set_on + (*iter)->length) - ServerInstance->Time();
178                                 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);
179                         }
180                 }
181
182                 return 0;
183         }
184
185         virtual int OnUserPreNick(User *user, const std::string &newnick)
186         {
187                 ExpireBans();
188
189                 /* check SVSHolds in here, and apply as necessary. */
190                 SVSHoldMap::iterator n = HoldMap.find(assign(newnick));
191                 if (n != HoldMap.end())
192                 {
193                         user->WriteServ( "432 %s %s :Reserved nickname: %s", user->nick.c_str(), newnick.c_str(), n->second->reason.c_str());
194                         return 1;
195                 }
196                 return 0;
197         }
198
199         virtual void OnSyncOtherMetaData(Module* proto, void* opaque, bool displayable)
200         {
201                 for(SVSHoldMap::iterator iter = HoldMap.begin(); iter != HoldMap.end(); iter++)
202                 {
203                         proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "SVSHold", EncodeSVSHold(iter->second));
204                 }
205         }
206
207         virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
208         {
209                 if((target_type == TYPE_OTHER) && (extname == "SVSHold"))
210                 {
211                         SVSHold* S = DecodeSVSHold(extdata); /* NOTE: Allocates a new SVSHold* */
212                         if (HoldMap.find(assign(S->nickname)) == HoldMap.end())
213                         {
214                                 SVSHolds.push_back(S);
215                                 HoldMap[assign(S->nickname)] = S;
216                                 std::sort(SVSHolds.begin(), SVSHolds.end(), SVSHoldComp);
217                         }
218                         else
219                         {
220                                 delete S;
221                         }
222                 }
223         }
224
225         virtual ~ModuleSVSHold()
226         {
227         }
228
229         virtual Version GetVersion()
230         {
231                 return Version(1, 2, 0, 1, VF_COMMON | VF_VENDOR, API_VERSION);
232         }
233
234         std::string EncodeSVSHold(const SVSHold* ban)
235         {
236                 std::ostringstream stream;
237                 stream << ban->nickname << " " << ban->set_by << " " << ban->set_on << " " << ban->length << " :" << ban->reason;
238                 return stream.str();
239         }
240
241         SVSHold* DecodeSVSHold(const std::string &data)
242         {
243                 SVSHold* res = new SVSHold();
244                 int set_on;
245                 irc::tokenstream tokens(data);
246                 tokens.GetToken(res->nickname);
247                 tokens.GetToken(res->set_by);
248                 tokens.GetToken(set_on);
249                 res->set_on = set_on;
250                 tokens.GetToken(res->length);
251                 tokens.GetToken(res->reason);
252                 return res;
253         }
254
255         void ExpireBans()
256         {
257                 SVSHoldlist::iterator iter,safeiter;
258                 for (iter = SVSHolds.begin(); iter != SVSHolds.end(); iter++)
259                 {
260                         /* 0 == permanent, don't mess with them! -- w00t */
261                         if ((*iter)->length != 0)
262                         {
263                                 if ((*iter)->set_on + (*iter)->length <= ServerInstance->Time())
264                                 {
265                                         ServerInstance->Logs->Log("m_svshold",DEBUG, "m_svshold.so: hold on %s expired, removing...", (*iter)->nickname.c_str());
266                                         HoldMap.erase(assign((*iter)->nickname));
267                                         delete *iter;
268                                         safeiter = iter;
269                                         --iter;
270                                         SVSHolds.erase(safeiter);
271                                 }
272                         }
273                 }
274         }
275 };
276
277 MODULE_INIT(ModuleSVSHold)