]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_shun.cpp
43359c28a04c4bfce40bd3e969d65cbd57c6e75c
[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://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(InspIRCd* Instance, time_t s_time, long d, std::string src, std::string re, std::string shunmask)
25                 : XLine(Instance, 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(InspIRCd* Instance) : XLineFactory(Instance, "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(ServerInstance, 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(InspIRCd* Me, Module* Creator) : Command(Me, Creator, "SHUN", "o", 1, 3)
91         {
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                 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                                 // XXX todo implement stats
115                                 user->WriteServ("NOTICE %s :*** Shun %s not found in list, try /stats S.",user->nick.c_str(),target.c_str());
116                         }
117
118                         return CMD_SUCCESS;
119                 }
120                 else if (parameters.size() >= 2)
121                 {
122                         // Adding - XXX todo make this respect <insane> tag perhaps..
123                         long duration;
124                         std::string expr;
125                         if (parameters.size() > 2)
126                         {
127                                 duration = ServerInstance->Duration(parameters[1]);
128                                 expr = parameters[2];
129                         }
130                         else
131                         {
132                                 duration = 0;
133                                 expr = parameters[1];
134                         }
135                         Shun *r = NULL;
136
137                         try
138                         {
139                                 r = new Shun(ServerInstance, ServerInstance->Time(), duration, user->nick.c_str(), expr.c_str(), target.c_str());
140                         }
141                         catch (...)
142                         {
143                                 ; // Do nothing. If we get here, the regex was fucked up, and they already got told it fucked up.
144                         }
145
146                         if (r)
147                         {
148                                 if (ServerInstance->XLines->AddLine(r, user))
149                                 {
150                                         if (!duration)
151                                         {
152                                                 ServerInstance->SNO->WriteToSnoMask('x',"%s added permanent shun for %s: %s",
153                                                         user->nick.c_str(), target.c_str(), expr.c_str());
154                                         }
155                                         else
156                                         {
157                                                 time_t c_requires_crap = duration + ServerInstance->Time();
158                                                 ServerInstance->SNO->WriteToSnoMask('x', "%s added timed shun for %s, expires on %s: %s",
159                                                         user->nick.c_str(), target.c_str(), ServerInstance->TimeString(c_requires_crap).c_str(), expr.c_str());
160                                         }
161
162                                         ServerInstance->XLines->ApplyLines();
163                                 }
164                                 else
165                                 {
166                                         delete r;
167                                         user->WriteServ("NOTICE %s :*** Shun for %s already exists", user->nick.c_str(), expr.c_str());
168                                 }
169                         }
170                 }
171
172                 return CMD_FAILURE;
173         }
174 };
175
176 class ModuleShun : public Module
177 {
178         CommandShun cmd;
179         ShunFactory f;
180         std::set<std::string> ShunEnabledCommands;
181         bool NotifyOfShun;
182         bool affectopers;
183
184  public:
185         ModuleShun(InspIRCd* Me) : Module(Me), cmd(Me, this), f(Me)
186         {
187                 ServerInstance->XLines->RegisterFactory(&f);
188                 ServerInstance->AddCommand(&cmd);
189
190                 Implementation eventlist[] = { I_OnStats, I_OnPreCommand, I_OnUserConnect, I_OnRehash };
191                 ServerInstance->Modules->Attach(eventlist, this, 4);
192                 OnRehash(NULL);
193         }
194
195         virtual ~ModuleShun()
196         {
197                 ServerInstance->XLines->DelAll("SHUN");
198                 ServerInstance->XLines->UnregisterFactory(&f);
199         }
200
201         virtual ModResult OnStats(char symbol, User* user, string_list& out)
202         {
203                 if (symbol != 'S')
204                         return MOD_RES_PASSTHRU;
205
206                 ServerInstance->XLines->InvokeStats("SHUN", 223, user, out);
207                 return MOD_RES_DENY;
208         }
209
210         virtual void OnRehash(User* user)
211         {
212                 ConfigReader MyConf(ServerInstance);
213                 std::string cmds = MyConf.ReadValue("shun", "enabledcommands", 0);
214
215                 if (cmds.empty())
216                         cmds = "PING PONG QUIT";
217
218                 ShunEnabledCommands.clear();
219                 NotifyOfShun = true;
220                 affectopers = false;
221
222                 std::stringstream dcmds(cmds);
223                 std::string thiscmd;
224
225                 while (dcmds >> thiscmd)
226                 {
227                         ShunEnabledCommands.insert(thiscmd);
228                 }
229
230                 NotifyOfShun = MyConf.ReadFlag("shun", "notifyuser", "yes", 0);
231                 affectopers = MyConf.ReadFlag("shun", "affectopers", "no", 0);
232         }
233
234         virtual void OnUserConnect(User* user)
235         {
236                 if (!IS_LOCAL(user))
237                         return;
238
239                 // Apply lines on user connect
240                 XLine *rl = ServerInstance->XLines->MatchesLine("SHUN", user);
241
242                 if (rl)
243                 {
244                         // Bang. :P
245                         rl->Apply(user);
246                 }
247         }
248
249         virtual ModResult OnPreCommand(std::string &command, std::vector<std::string>& parameters, User* user, bool validated, const std::string &original_line)
250         {
251                 if (validated)
252                         return MOD_RES_PASSTHRU;
253
254                 if (!ServerInstance->XLines->MatchesLine("SHUN", user))
255                 {
256                         /* Not shunned, don't touch. */
257                         return MOD_RES_PASSTHRU;
258                 }
259
260                 if (!affectopers && IS_OPER(user))
261                 {
262                         /* Don't do anything if the user is an operator and affectopers isn't set */
263                         return MOD_RES_PASSTHRU;
264                 }
265
266                 std::set<std::string>::iterator i = ShunEnabledCommands.find(command);
267
268                 if (i == ShunEnabledCommands.end())
269                 {
270                         if (NotifyOfShun)
271                                 user->WriteServ("NOTICE %s :*** Command %s not processed, as you have been blocked from issuing commands (SHUN)", user->nick.c_str(), command.c_str());
272                         return MOD_RES_DENY;
273                 }
274
275                 if (command == "QUIT")
276                 {
277                         /* Allow QUIT but dont show any quit message */
278                         parameters.clear();
279                 }
280                 else if (command == "PART")
281                 {
282                         /* same for PART */
283                         parameters[1] = "";
284                 }
285
286                 /* if we're here, allow the command. */
287                 return MOD_RES_PASSTHRU;
288         }
289
290         virtual Version GetVersion()
291         {
292                 return Version("$Id$",VF_VENDOR|VF_COMMON,API_VERSION);
293         }
294 };
295
296 MODULE_INIT(ModuleShun)
297