]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_shun.cpp
f4532ecd1b5befc0a3b0bb198779aa4090a1ef76
[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, 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                 // E: overrides shun
36                 if (u->exempt)
37                         return false;
38
39                 if (InspIRCd::Match(u->GetFullHost(), matchtext) || InspIRCd::Match(u->GetFullRealHost(), matchtext) || InspIRCd::Match(u->nick+"!"+u->ident+"@"+u->GetIPString(), matchtext))
40                         return true;
41
42                 return false;
43         }
44
45         bool Matches(const std::string &s)
46         {
47                 if (matchtext == s)
48                         return true;
49                 return false;
50         }
51
52         void Apply(User *u)
53         {
54         }
55
56
57         void DisplayExpiry()
58         {
59                 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));
60         }
61
62         const char* Displayable()
63         {
64                 return matchtext.c_str();
65         }
66 };
67
68 /** An XLineFactory specialized to generate shun pointers
69  */
70 class ShunFactory : public XLineFactory
71 {
72  public:
73         ShunFactory(InspIRCd* Instance) : XLineFactory(Instance, "SHUN") { }
74
75         /** Generate a shun
76         */
77         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
78         {
79                 return new Shun(ServerInstance, set_time, duration, source, reason, xline_specific_mask);
80         }
81 };
82
83 //typedef std::vector<Shun> shunlist;
84
85 class CommandShun : public Command
86 {
87  public:
88         CommandShun(InspIRCd* Me) : Command(Me, "SHUN", "o", 1, 3)
89         {
90                 this->source = "m_shun.so";
91                 this->syntax = "<nick!user@hostmask> [<shun-duration>] :<reason>";
92         }
93
94         CmdResult Handle(const std::vector<std::string>& parameters, User *user)
95         {
96                 /* syntax: SHUN nick!user@host time :reason goes here */
97                 /* 'time' is a human-readable timestring, like 2d3h2s. */
98
99                 std::string target = parameters[0];
100                 
101                 User *find = ServerInstance->FindNick(target.c_str());
102                 if (find)
103                         target = std::string("*!*@") + find->GetIPString();
104
105                 if (parameters.size() == 1)
106                 {
107                         if (ServerInstance->XLines->DelLine(target.c_str(), "SHUN", user))
108                         {
109                                 ServerInstance->SNO->WriteToSnoMask('x',"%s Removed shun on %s.",user->nick.c_str(),target.c_str());
110                         }
111                         else
112                         {
113                                 // XXX todo implement stats
114                                 user->WriteServ("NOTICE %s :*** Shun %s not found in list, try /stats S.",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, 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, expires 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
175 class ModuleShun : public Module
176 {
177         CommandShun cmd;
178         ShunFactory f;
179         std::set<std::string> ShunEnabledCommands;
180         bool NotifyOfShun;
181         bool affectopers;
182
183  public:
184         ModuleShun(InspIRCd* Me) : Module(Me), cmd(Me), f(Me)
185         {
186                 ServerInstance->XLines->RegisterFactory(&f);
187                 ServerInstance->AddCommand(&cmd);
188
189                 Implementation eventlist[] = { I_OnStats, I_OnPreCommand, I_OnUserConnect, I_OnRehash };
190                 ServerInstance->Modules->Attach(eventlist, this, 4);
191                 OnRehash(NULL);
192         }
193
194         virtual ~ModuleShun()
195         {
196                 ServerInstance->XLines->DelAll("SHUN");
197                 ServerInstance->XLines->UnregisterFactory(&f);
198         }
199
200         virtual int OnStats(char symbol, User* user, string_list& out)
201         {
202                 if (symbol != 'S')
203                         return 0;
204
205                 ServerInstance->XLines->InvokeStats("SHUN", 223, user, out);
206                 return 1;
207         }
208
209         virtual void OnRehash(User* user)
210         {
211                 ConfigReader MyConf(ServerInstance);
212                 std::string cmds = MyConf.ReadValue("shun", "enabledcommands", 0);
213
214                 if (cmds.empty())
215                         cmds = "PING PONG QUIT";
216
217                 ShunEnabledCommands.clear();
218                 NotifyOfShun = true;
219                 affectopers = false;
220
221                 std::stringstream dcmds(cmds);
222                 std::string thiscmd;
223
224                 while (dcmds >> thiscmd)
225                 {
226                         ShunEnabledCommands.insert(thiscmd);
227                 }
228
229                 NotifyOfShun = MyConf.ReadFlag("shun", "notifyuser", "yes", 0);
230                 affectopers = MyConf.ReadFlag("shun", "affectopers", "no", 0);
231         }
232
233         virtual void OnUserConnect(User* user)
234         {
235                 if (!IS_LOCAL(user))
236                         return;
237
238                 // Apply lines on user connect
239                 XLine *rl = ServerInstance->XLines->MatchesLine("SHUN", user);
240
241                 if (rl)
242                 {
243                         // Bang. :P
244                         rl->Apply(user);
245                 }
246         }
247
248         virtual int OnPreCommand(std::string &command, std::vector<std::string>& parameters, User* user, bool validated, const std::string &original_line)
249         {
250                 if (validated)
251                         return 0;
252
253                 if (!ServerInstance->XLines->MatchesLine("SHUN", user))
254                 {
255                         /* Not shunned, don't touch. */
256                         return 0;
257                 }
258
259                 if (!affectopers && IS_OPER(user))
260                 {
261                         /* Don't do anything if the user is an operator and affectopers isn't set */
262                         return 0;
263                 }
264
265                 std::set<std::string>::iterator i = ShunEnabledCommands.find(command);
266
267                 if (i == ShunEnabledCommands.end())
268                 {
269                         if (NotifyOfShun)
270                                 user->WriteServ("NOTICE %s :*** Command %s not processed, as you have been blocked from issuing commands (SHUN)", user->nick.c_str(), command.c_str());
271                         return 1;
272                 }
273
274                 if (command == "QUIT")
275                 {
276                         /* Allow QUIT but dont show any quit message */
277                         parameters.clear();
278                 }
279                 else if (command == "PART")
280                 {
281                         /* same for PART */
282                         parameters[1] = "";
283                 }
284
285                 /* if we're here, allow the command. */
286                 return 0;
287         }
288
289         virtual Version GetVersion()
290         {
291                 return Version("$Id$",VF_VENDOR|VF_COMMON,API_VERSION);
292         }
293 };
294
295 MODULE_INIT(ModuleShun)
296