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