]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_svshold.cpp
MetaData rework
[user/henk/code/inspircd.git] / src / modules / m_svshold.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/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 "xline.h"
16
17 /* $ModDesc: Implements SVSHOLD. Like Q:Lines, but can only be added/removed by Services. */
18
19 /** Holds a SVSHold item
20  */
21 class SVSHold : public XLine
22 {
23 public:
24         irc::string nickname;
25
26         SVSHold(InspIRCd* Instance, time_t s_time, long d, const char* src, const char* re, const char *nick) : XLine(Instance, s_time, d, src, re, "SVSHOLD")
27         {
28                 this->nickname = nick;
29         }
30
31         ~SVSHold()
32         {
33         }
34
35         bool Matches(User *u)
36         {
37                 if (u->nick == nickname)
38                         return true;
39                 return false;
40         }
41
42         bool Matches(const std::string &s)
43         {
44                 if (nickname == s)
45                         return true;
46                 return false;
47         }
48
49         void DisplayExpiry()
50         {
51                 ServerInstance->SNO->WriteToSnoMask('x',"Removing expired SVSHOLD %s (set by %s %ld seconds ago)", this->nickname.c_str(), this->source, (long int)(ServerInstance->Time() - this->set_time));
52         }
53
54         const char* Displayable()
55         {
56                 return nickname.c_str();
57         }
58 };
59
60 /** An XLineFactory specialized to generate SVSHOLD pointers
61  */
62 class SVSHoldFactory : public XLineFactory
63 {
64  public:
65         SVSHoldFactory(InspIRCd* Instance) : XLineFactory(Instance, "SVSHOLD") { }
66
67         /** Generate a shun
68         */
69         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
70         {
71                 return new SVSHold(ServerInstance, set_time, duration, source, reason, xline_specific_mask);
72         }
73 };
74
75 /** Handle /SVSHold
76  */
77 class CommandSvshold : public Command
78 {
79  public:
80         CommandSvshold(InspIRCd* Me) : Command(Me, "SVSHOLD", "o", 1)
81         {
82                 this->source = "m_svshold.so";
83                 this->syntax = "<nickname> [<duration> :<reason>]";
84                 TRANSLATE4(TR_NICK, TR_TEXT, TR_TEXT, TR_END);
85         }
86
87         CmdResult Handle(const std::vector<std::string> &parameters, User *user)
88         {
89                 /* syntax: svshold nickname time :reason goes here */
90                 /* 'time' is a human-readable timestring, like 2d3h2s. */
91
92                 if (!ServerInstance->ULine(user->server))
93                 {
94                         /* don't allow SVSHOLD from non-ulined clients */
95                         return CMD_FAILURE;
96                 }
97
98                 if (parameters.size() == 1)
99                 {
100                         if (ServerInstance->XLines->DelLine(parameters[0].c_str(), "SVSHOLD", user))
101                         {
102                                 ServerInstance->SNO->WriteToSnoMask('x',"%s Removed SVSHOLD on %s.",user->nick.c_str(),parameters[0].c_str());
103                         }
104                         else
105                         {
106                                 user->WriteServ("NOTICE %s :*** SVSHOLD %s not found in list, try /stats S.",user->nick.c_str(),parameters[0].c_str());
107                         }
108
109                         return CMD_SUCCESS;
110                 }
111                 else if (parameters.size() >= 2)
112                 {
113                         // Adding - XXX todo make this respect <insane> tag perhaps..
114                         long duration = ServerInstance->Duration(parameters[1]);
115                         SVSHold *r = NULL;
116
117                         try
118                         {
119                                 r = new SVSHold(ServerInstance, ServerInstance->Time(), duration, user->nick.c_str(), parameters[2].c_str(), parameters[0].c_str());
120                         }
121                         catch (...)
122                         {
123                                 ; // Do nothing.
124                         }
125
126                         if (r)
127                         {
128                                 if (ServerInstance->XLines->AddLine(r, user))
129                                 {
130                                         ServerInstance->XLines->ApplyLines();
131                                 }
132                                 else
133                                 {
134                                         delete r;
135                                 }
136                         }
137                 }
138
139                 return CMD_SUCCESS;
140         }
141 };
142
143 class ModuleSVSHold : public Module
144 {
145         CommandSvshold cmd;
146         SVSHoldFactory s;
147
148
149  public:
150         ModuleSVSHold(InspIRCd* Me) : Module(Me), cmd(Me), s(Me)
151         {
152                 ServerInstance->XLines->RegisterFactory(&s);
153                 ServerInstance->AddCommand(&cmd);
154                 Implementation eventlist[] = { I_OnUserPreNick, I_OnStats };
155                 ServerInstance->Modules->Attach(eventlist, this, 2);
156         }
157
158
159         virtual int OnStats(char symbol, User* user, string_list &out)
160         {
161                 if(symbol != 'S')
162                         return 0;
163
164                 ServerInstance->XLines->InvokeStats("SVSHOLD", 210, user, out);
165                 return 1;
166         }
167
168         virtual int OnUserPreNick(User *user, const std::string &newnick)
169         {
170                 XLine *rl = ServerInstance->XLines->MatchesLine("SVSHOLD", newnick);
171
172                 if (rl)
173                 {
174                         user->WriteServ( "432 %s %s :Services reserved nickname: %s", user->nick.c_str(), newnick.c_str(), rl->reason);
175                         return 1;
176                 }
177
178                 return 0;
179         }
180
181         virtual ~ModuleSVSHold()
182         {
183                 ServerInstance->XLines->DelAll("SVSHOLD");
184                 ServerInstance->XLines->UnregisterFactory(&s);
185         }
186
187         virtual Version GetVersion()
188         {
189                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
190         }
191 };
192
193 MODULE_INIT(ModuleSVSHold)