]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_shun.cpp
fix little typo in syntax hint, type => action
[user/henk/code/inspircd.git] / src / modules / m_shun.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 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 "xline.h"
16
17 /* $ModDesc: Provides the /shun command, which stops a user executing all commands except PING and PONG. */
18
19 class Shun : public XLine
20 {
21 public:
22         std::string matchtext;
23
24         Shun(InspIRCd* Instance, time_t s_time, long d, const char* src, const char* re, const char *shunmask) : XLine(Instance, s_time, d, src, re, "SHUN")
25         {
26                 this->matchtext = shunmask;
27         }
28
29         ~Shun()
30         {
31         }
32
33         bool Matches(User *u)
34         {
35                 if (InspIRCd::Match(u->GetFullHost(), matchtext) || InspIRCd::Match(u->GetFullRealHost(), matchtext))
36                         return true;
37
38                 return false;
39         }
40
41         bool Matches(const std::string &s)
42         {
43                 if (matchtext == s)
44                         return true;
45                 return false;
46         }
47
48         void Apply(User *u)
49         {
50                 if (!u->GetExt("shunned"))
51                         u->Extend("shunned");
52         }
53
54
55         void DisplayExpiry()
56         {
57                 ServerInstance->SNO->WriteToSnoMask('x',"Removing expired shun %s (set by %s %ld seconds ago)", this->matchtext.c_str(), this->source, (long int)(ServerInstance->Time() - this->set_time));
58         }
59
60         const char* Displayable()
61         {
62                 return matchtext.c_str();
63         }
64 };
65
66 /** An XLineFactory specialized to generate shun pointers
67  */
68 class ShunFactory : public XLineFactory
69 {
70  public:
71         ShunFactory(InspIRCd* Instance) : XLineFactory(Instance, "SHUN") { }
72
73         /** Generate a shun
74         */
75         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
76         {
77                 return new Shun(ServerInstance, set_time, duration, source, reason, xline_specific_mask);
78         }
79 };
80
81 //typedef std::vector<Shun> shunlist;
82
83 class CommandShun : public Command
84 {
85  private:
86         InspIRCd *Srv;
87
88  public:
89         CommandShun(InspIRCd* Me) : Command(Me, "SHUN", "o", 1, 3), Srv(Me)
90         {
91                 this->source = "m_shun.so";
92                 this->syntax = "<nick!user@hostmask> [<shun-duration>] :<reason>";
93         }
94
95         CmdResult Handle(const std::vector<std::string>& parameters, User *user)
96         {
97                 /* syntax: SHUN nick!user@host time :reason goes here */
98                 /* 'time' is a human-readable timestring, like 2d3h2s. */
99
100                 if (parameters.size() == 1)
101                 {
102                         if (ServerInstance->XLines->DelLine(parameters[0].c_str(), "SHUN", user))
103                         {
104                                 ServerInstance->SNO->WriteToSnoMask('x',"%s Removed shun on %s.",user->nick.c_str(),parameters[0].c_str());
105                         }
106                         else
107                         {
108                                 // XXX todo implement stats
109                                 user->WriteServ("NOTICE %s :*** Shun %s not found in list, try /stats S.",user->nick.c_str(),parameters[0].c_str());
110                         }
111
112                         return CMD_SUCCESS;
113                 }
114                 else if (parameters.size() >= 2)
115                 {
116                         // Adding - XXX todo make this respect <insane> tag perhaps..
117                         long duration = ServerInstance->Duration(parameters[1]);
118                         Shun *r = NULL;
119
120                         try
121                         {
122                                 r = new Shun(ServerInstance, ServerInstance->Time(), duration, user->nick.c_str(), parameters[2].c_str(), parameters[0].c_str());
123                         }
124                         catch (...)
125                         {
126                                 ; // Do nothing. If we get here, the regex was fucked up, and they already got told it fucked up.
127                         }
128
129                         if (r)
130                         {
131                                 if (ServerInstance->XLines->AddLine(r, user))
132                                 {
133                                         if (!duration)
134                                         {
135                                                 ServerInstance->SNO->WriteToSnoMask('x',"%s added permanent shun for %s: %s", user->nick.c_str(), parameters[0].c_str(), parameters[2].c_str());
136                                         }
137                                         else
138                                         {
139                                                 time_t c_requires_crap = duration + ServerInstance->Time();
140                                                 ServerInstance->SNO->WriteToSnoMask('x', "%s added timed shun for %s, expires on %s: %s", user->nick.c_str(), parameters[0].c_str(), ServerInstance->TimeString(c_requires_crap).c_str(), parameters[2].c_str());
141                                         }
142
143                                         ServerInstance->XLines->ApplyLines();
144                                 }
145                                 else
146                                 {
147                                         delete r;
148                                         user->WriteServ("NOTICE %s :*** Shun for %s already exists", user->nick.c_str(), parameters[0].c_str());
149                                 }
150                         }
151                 }
152
153                 return CMD_FAILURE;
154         }
155 };
156
157 class ModuleShun : public Module
158 {
159         CommandShun* mycommand;
160         ShunFactory *f;
161         std::set<std::string> ShunEnabledCommands;
162         bool NotifyOfShun;
163
164  public:
165         ModuleShun(InspIRCd* Me) : Module(Me)
166         {
167                 f = new ShunFactory(ServerInstance);
168                 ServerInstance->XLines->RegisterFactory(f);
169
170                 mycommand = new CommandShun(ServerInstance);
171                 ServerInstance->AddCommand(mycommand);
172
173                 Implementation eventlist[] = { I_OnStats, I_OnPreCommand, I_OnUserConnect, I_OnRehash };
174                 ServerInstance->Modules->Attach(eventlist, this, 4);
175                 OnRehash(NULL, "");
176         }
177
178         virtual ~ModuleShun()
179         {
180                 ServerInstance->XLines->DelAll("SHUN");
181                 ServerInstance->XLines->UnregisterFactory(f);
182         }
183
184         virtual int OnStats(char symbol, User* user, string_list& out)
185         {
186                 if (symbol != 'S')
187                         return 0;
188
189                 ServerInstance->XLines->InvokeStats("SHUN", 223, user, out);
190                 return 1;
191         }
192
193         virtual void OnRehash(User* user, const std::string &parameter)
194         {
195                 ConfigReader MyConf(ServerInstance);
196                 std::string cmds = MyConf.ReadValue("shun", "enabledcommands", 0);
197
198                 if (cmds.empty())
199                         cmds = "PING PONG QUIT";
200
201                 ShunEnabledCommands.clear();
202                 NotifyOfShun = true;
203
204                 std::stringstream dcmds(cmds);
205                 std::string thiscmd;
206
207                 while (dcmds >> thiscmd)
208                 {
209                         ShunEnabledCommands.insert(thiscmd);
210                 }
211
212                 NotifyOfShun = MyConf.ReadFlag("shun", "notifyuser", "yes", 0);
213         }
214
215         virtual void OnUserConnect(User* user)
216         {
217                 if (!IS_LOCAL(user))
218                         return;
219
220                 // Apply lines on user connect
221                 XLine *rl = ServerInstance->XLines->MatchesLine("SHUN", user);
222
223                 if (rl)
224                 {
225                         // Bang. :P
226                         rl->Apply(user);
227                 }
228         }
229
230         virtual int OnPreCommand(std::string &command, std::vector<std::string>& parameters, User* user, bool validated, const std::string &original_line)
231         {
232                 if (validated || !user->GetExt("shunned"))
233                         return 0;
234
235                 if (!ServerInstance->XLines->MatchesLine("SHUN", user))
236                 {
237                         /* The shun previously set on this user has expired or been removed */
238                         user->Shrink("shunned");
239                         return 0;
240                 }
241
242                 std::set<std::string>::iterator i = ShunEnabledCommands.find(command);
243
244                 if (i == ShunEnabledCommands.end())
245                 {
246                         if (NotifyOfShun)
247                                 user->WriteServ("NOTICE %s :*** Command %s not processed, as you have been blocked from issuing commands (SHUN)", user->nick.c_str(), command.c_str());
248                         return 1;
249                 }
250
251                 if (command == "QUIT")
252                 {
253                         /* Allow QUIT but dont show any quit message */
254                         parameters.clear();
255                 }
256                 else if (command == "PART")
257                 {
258                         /* same for PART */
259                         parameters.clear();
260                 }
261
262                 return 1;
263         }
264
265         virtual Version GetVersion()
266         {
267                 return Version("$Id$",VF_VENDOR|VF_COMMON,API_VERSION);
268         }
269 };
270
271 MODULE_INIT(ModuleShun)
272