]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_shun.cpp
Fix some of the include guard names (requested by SaberUK)
[user/henk/code/inspircd.git] / src / modules / m_shun.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 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: 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(time_t s_time, long d, std::string src, std::string re, std::string shunmask)
25                 : XLine(s_time, d, src, re, "SHUN")
26         {
27                 this->matchtext = shunmask;
28         }
29
30         ~Shun()
31         {
32         }
33
34         bool Matches(User *u)
35         {
36                 // E: overrides shun
37                 if (u->exempt)
38                         return false;
39
40                 if (InspIRCd::Match(u->GetFullHost(), matchtext) || InspIRCd::Match(u->GetFullRealHost(), matchtext) || InspIRCd::Match(u->nick+"!"+u->ident+"@"+u->GetIPString(), matchtext))
41                         return true;
42
43                 return false;
44         }
45
46         bool Matches(const std::string &s)
47         {
48                 if (matchtext == s)
49                         return true;
50                 return false;
51         }
52
53         void Apply(User *u)
54         {
55         }
56
57
58         void DisplayExpiry()
59         {
60                 ServerInstance->SNO->WriteToSnoMask('x',"Removing expired shun %s (set by %s %ld seconds ago)",
61                         this->matchtext.c_str(), this->source.c_str(), (long int)(ServerInstance->Time() - this->set_time));
62         }
63
64         const char* Displayable()
65         {
66                 return matchtext.c_str();
67         }
68 };
69
70 /** An XLineFactory specialized to generate shun pointers
71  */
72 class ShunFactory : public XLineFactory
73 {
74  public:
75         ShunFactory() : XLineFactory("SHUN") { }
76
77         /** Generate a shun
78         */
79         XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
80         {
81                 return new Shun(set_time, duration, source, reason, xline_specific_mask);
82         }
83 };
84
85 //typedef std::vector<Shun> shunlist;
86
87 class CommandShun : public Command
88 {
89  public:
90         CommandShun(Module* Creator) : Command(Creator, "SHUN", 1, 3)
91         {
92                 flags_needed = 'o'; 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                 std::string target = parameters[0];
101                 
102                 User *find = ServerInstance->FindNick(target.c_str());
103                 if (find)
104                         target = std::string("*!*@") + find->GetIPString();
105
106                 if (parameters.size() == 1)
107                 {
108                         if (ServerInstance->XLines->DelLine(target.c_str(), "SHUN", user))
109                         {
110                                 ServerInstance->SNO->WriteToSnoMask('x',"%s removed SHUN on %s",user->nick.c_str(),target.c_str());
111                         }
112                         else
113                         {
114                                 user->WriteServ("NOTICE %s :*** Shun %s not found in list, try /stats H.",user->nick.c_str(),target.c_str());
115                         }
116
117                         return CMD_SUCCESS;
118                 }
119                 else if (parameters.size() >= 2)
120                 {
121                         // Adding - XXX todo make this respect <insane> tag perhaps..
122                         long duration;
123                         std::string expr;
124                         if (parameters.size() > 2)
125                         {
126                                 duration = ServerInstance->Duration(parameters[1]);
127                                 expr = parameters[2];
128                         }
129                         else
130                         {
131                                 duration = 0;
132                                 expr = parameters[1];
133                         }
134                         Shun *r = NULL;
135
136                         try
137                         {
138                                 r = new Shun(ServerInstance->Time(), duration, user->nick.c_str(), expr.c_str(), target.c_str());
139                         }
140                         catch (...)
141                         {
142                                 ; // Do nothing. If we get here, the regex was fucked up, and they already got told it fucked up.
143                         }
144
145                         if (r)
146                         {
147                                 if (ServerInstance->XLines->AddLine(r, user))
148                                 {
149                                         if (!duration)
150                                         {
151                                                 ServerInstance->SNO->WriteToSnoMask('x',"%s added permanent SHUN for %s: %s",
152                                                         user->nick.c_str(), target.c_str(), expr.c_str());
153                                         }
154                                         else
155                                         {
156                                                 time_t c_requires_crap = duration + ServerInstance->Time();
157                                                 ServerInstance->SNO->WriteToSnoMask('x', "%s added timed SHUN for %s to expire on %s: %s",
158                                                         user->nick.c_str(), target.c_str(), ServerInstance->TimeString(c_requires_crap).c_str(), expr.c_str());
159                                         }
160
161                                         ServerInstance->XLines->ApplyLines();
162                                 }
163                                 else
164                                 {
165                                         delete r;
166                                         user->WriteServ("NOTICE %s :*** Shun for %s already exists", user->nick.c_str(), expr.c_str());
167                                 }
168                         }
169                 }
170
171                 return CMD_FAILURE;
172         }
173
174         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
175         {
176                 return ROUTE_BROADCAST;
177         }
178 };
179
180 class ModuleShun : public Module
181 {
182         CommandShun cmd;
183         ShunFactory f;
184         std::set<std::string> ShunEnabledCommands;
185         bool NotifyOfShun;
186         bool affectopers;
187
188  public:
189         ModuleShun() : cmd(this)
190         {
191                 ServerInstance->XLines->RegisterFactory(&f);
192                 ServerInstance->AddCommand(&cmd);
193
194                 Implementation eventlist[] = { I_OnStats, I_OnPreCommand, I_OnUserConnect, I_OnRehash };
195                 ServerInstance->Modules->Attach(eventlist, this, 4);
196                 OnRehash(NULL);
197         }
198
199         virtual ~ModuleShun()
200         {
201                 ServerInstance->XLines->DelAll("SHUN");
202                 ServerInstance->XLines->UnregisterFactory(&f);
203         }
204
205         void Prioritize()
206         {
207                 Module* alias = ServerInstance->Modules->Find("m_alias.so");
208                 ServerInstance->Modules->SetPriority(this, I_OnPreCommand, PRIORITY_BEFORE, &alias);
209         }
210
211         virtual ModResult OnStats(char symbol, User* user, string_list& out)
212         {
213                 if (symbol != 'H')
214                         return MOD_RES_PASSTHRU;
215
216                 ServerInstance->XLines->InvokeStats("SHUN", 223, user, out);
217                 return MOD_RES_DENY;
218         }
219
220         virtual void OnRehash(User* user)
221         {
222                 ConfigReader MyConf;
223                 std::string cmds = MyConf.ReadValue("shun", "enabledcommands", 0);
224
225                 if (cmds.empty())
226                         cmds = "PING PONG QUIT";
227
228                 ShunEnabledCommands.clear();
229                 NotifyOfShun = true;
230                 affectopers = false;
231
232                 std::stringstream dcmds(cmds);
233                 std::string thiscmd;
234
235                 while (dcmds >> thiscmd)
236                 {
237                         ShunEnabledCommands.insert(thiscmd);
238                 }
239
240                 NotifyOfShun = MyConf.ReadFlag("shun", "notifyuser", "yes", 0);
241                 affectopers = MyConf.ReadFlag("shun", "affectopers", "no", 0);
242         }
243
244         virtual void OnUserConnect(LocalUser* user)
245         {
246                 if (!IS_LOCAL(user))
247                         return;
248
249                 // Apply lines on user connect
250                 XLine *rl = ServerInstance->XLines->MatchesLine("SHUN", user);
251
252                 if (rl)
253                 {
254                         // Bang. :P
255                         rl->Apply(user);
256                 }
257         }
258
259         virtual ModResult OnPreCommand(std::string &command, std::vector<std::string>& parameters, LocalUser* user, bool validated, const std::string &original_line)
260         {
261                 if (validated)
262                         return MOD_RES_PASSTHRU;
263
264                 if (!ServerInstance->XLines->MatchesLine("SHUN", user))
265                 {
266                         /* Not shunned, don't touch. */
267                         return MOD_RES_PASSTHRU;
268                 }
269
270                 if (!affectopers && IS_OPER(user))
271                 {
272                         /* Don't do anything if the user is an operator and affectopers isn't set */
273                         return MOD_RES_PASSTHRU;
274                 }
275
276                 std::set<std::string>::iterator i = ShunEnabledCommands.find(command);
277
278                 if (i == ShunEnabledCommands.end())
279                 {
280                         if (NotifyOfShun)
281                                 user->WriteServ("NOTICE %s :*** Command %s not processed, as you have been blocked from issuing commands (SHUN)", user->nick.c_str(), command.c_str());
282                         return MOD_RES_DENY;
283                 }
284
285                 if (command == "QUIT")
286                 {
287                         /* Allow QUIT but dont show any quit message */
288                         parameters.clear();
289                 }
290                 else if (command == "PART")
291                 {
292                         /* same for PART */
293                         parameters[1] = "";
294                 }
295
296                 /* if we're here, allow the command. */
297                 return MOD_RES_PASSTHRU;
298         }
299
300         virtual Version GetVersion()
301         {
302                 return Version("Provides the /shun command, which stops a user executing all commands except PING and PONG.",VF_VENDOR|VF_COMMON);
303         }
304 };
305
306 MODULE_INIT(ModuleShun)
307